1 /**
2 * Copyright (C) cedarsoft GmbH.
3 *
4 * Licensed under the GNU General Public License version 3 (the "License")
5 * with Classpath Exception; you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at
7 *
8 * http://www.cedarsoft.org/gpl3ce
9 * (GPL 3 with Classpath Exception)
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 only, as
13 * published by the Free Software Foundation. cedarsoft GmbH designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by cedarsoft GmbH in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 3 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 3 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
28 * or visit www.cedarsoft.com if you need additional information or
29 * have any questions.
30 */
31
32 package com.cedarsoft.test.io;
33
34 import com.cedarsoft.UnsupportedVersionException;
35 import com.cedarsoft.Version;
36 import com.cedarsoft.VersionRange;
37 import com.cedarsoft.serialization.stax.AbstractStaxMateSerializer;
38 import com.cedarsoft.test.Money;
39 import org.codehaus.staxmate.out.SMOutputElement;
40
41 import javax.xml.stream.XMLStreamException;
42 import javax.xml.stream.XMLStreamReader;
43 import java.io.IOException;
44
45 /**
46 * This is an extended version of money serializer.
47 * <p/>
48 * It represents the next step of the evolution of MoneySerializer.
49 * This is an example for a refactoring that might happen, after the serializer has been
50 * released (and shipped to thousands of customers creating millions of files).
51 * <p/>
52 * Therefore this serializer is able to still read the old format.
53 * Writing is only supported for the latest file.
54 */
55 public class MoneySerializer2 extends AbstractStaxMateSerializer<Money> {
56 public MoneySerializer2() {
57 //This serializer supports an old version, too
58 super( "money", "http://thecompany.com/test/money", new VersionRange( new Version( 1, 0, 0 ), new Version( 1, 0, 1 ) ) );
59 }
60
61 @Override
62 public void serialize( SMOutputElement serializeTo, Money object ) throws IOException, XMLStreamException {
63 serializeTo.addAttribute( "cents", String.valueOf( object.getCents() ) );
64 //We use an attribute - just because it is possible...
65 }
66
67
68 @Override
69 public Money deserialize( XMLStreamReader deserializeFrom, Version formatVersion ) throws IOException, XMLStreamException {
70 //This serializer supports reading of an old format. Therefore we have to switch based on the format version.
71 //This might be solved using the strategy pattern. But in most of the cases the format changes only in small portions.
72 //So it seems easier to add just one if/else.
73 //If a serializer evolves further a switch to the strategy pattern might be done. A simple map holding the strategies should do it.
74
75
76 //The common case - current version
77 if ( formatVersion.equals( new Version( 1, 0, 1 ) ) ) {
78 int cents = Integer.parseInt( deserializeFrom.getAttributeValue( null, "cents" ) );
79
80 //We have to close the tag! Every stax based serializer has to close its tag
81 //getText does this automatically for us. But when only using attributes, we have to close it manually.
82 closeTag( deserializeFrom );
83
84 return new Money( cents );
85
86 //The old format that does not use an attribute but text instead
87 } else if ( formatVersion.equals( new Version( 1, 0, 0 ) ) ) {
88 int cents = Integer.parseInt( getText( deserializeFrom ) );
89
90 //We don't have to close the tag. The getText method does that for us
91 return new Money( cents );
92
93 //Whoo - something went terribly wrong
94 } else {
95 throw new UnsupportedVersionException( formatVersion, getFormatVersionRange() );
96 }
97 }
98 }