View Javadoc

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.serialization.stax;
33  
34  import com.cedarsoft.Version;
35  import com.cedarsoft.VersionRange;
36  import com.cedarsoft.serialization.AbstractXmlSerializer;
37  import org.codehaus.staxmate.out.SMNamespace;
38  import org.codehaus.staxmate.out.SMOutputDocument;
39  import org.codehaus.staxmate.out.SMOutputElement;
40  
41  import javax.annotation.Nonnull;
42  import javax.xml.stream.XMLStreamException;
43  import javax.xml.stream.XMLStreamReader;
44  import java.io.IOException;
45  import java.io.OutputStream;
46  
47  /**
48   * Abstract base class for stax mate based serializers
49   *
50   * @param <T> the type
51   */
52  public abstract class AbstractStaxMateSerializer<T> extends AbstractStaxBasedSerializer<T, SMOutputElement> {
53    protected AbstractStaxMateSerializer( @Nonnull String defaultElementName, @Nonnull String nameSpaceUriBase, @Nonnull VersionRange formatVersionRange ) {
54      super( defaultElementName, nameSpaceUriBase, formatVersionRange );
55    }
56  
57    @Override
58    public void serialize( @Nonnull T object, @Nonnull OutputStream out ) throws IOException {
59      try {
60        SMOutputDocument doc = StaxMateSupport.getSmOutputFactory().createOutputDocument( out );
61  
62        String nameSpaceUri = getNameSpace();
63        SMNamespace nameSpace = doc.getNamespace( nameSpaceUri );
64  
65        SMOutputElement root = doc.addElement( nameSpace, getDefaultElementName() );
66        serialize( root, object, getFormatVersion() );
67        doc.closeRoot();
68      } catch ( XMLStreamException e ) {
69        throw new IOException( e );
70      }
71    }
72  
73    /**
74     * Serializes the elements of a collection
75     *
76     * @param objects       the objects that are serialized
77     * @param type          the type
78     * @param elementName   the element name
79     * @param serializeTo   the object the elements are serialized to
80     * @param formatVersion the format version
81     * @throws IOException
82     */
83    protected <T> void serializeCollection( @Nonnull Iterable<? extends T> objects, @Nonnull Class<T> type, @Nonnull String elementName, @Nonnull SMOutputElement serializeTo, @Nonnull Version formatVersion ) throws XMLStreamException, IOException {
84      AbstractXmlSerializer<? super T, SMOutputElement, XMLStreamReader, XMLStreamException> serializer = getSerializer( type );
85      Version resolvedVersion = getDelegatesMappings().resolveVersion( type, formatVersion );
86  
87      for ( T object : objects ) {
88        SMOutputElement doorElement = serializeTo.addElement( serializeTo.getNamespace(), elementName );
89        serializer.serialize( doorElement, object, resolvedVersion );
90      }
91    }
92  
93    protected <T> void serializeCollection( @Nonnull Iterable<? extends T> objects, @Nonnull Class<T> type, @Nonnull SMOutputElement serializeTo, @Nonnull Version formatVersion ) throws XMLStreamException, IOException {
94      AbstractXmlSerializer<? super T, SMOutputElement, XMLStreamReader, XMLStreamException> serializer = getSerializer( type );
95      Version resolvedVersion = getDelegatesMappings().resolveVersion( type, formatVersion );
96  
97      for ( T object : objects ) {
98        SMOutputElement doorElement = serializeTo.addElement( serializeTo.getNamespace(), serializer.getDefaultElementName() );
99        serializer.serialize( doorElement, object, resolvedVersion );
100     }
101   }
102 
103   /**
104    * Serializes the elements of the collection to a own sub element
105    *
106    * @param objects               the objects that are serialized
107    * @param type                  the type
108    * @param collectionElementName the collection element name
109    * @param elementName           the element name
110    * @param serializeTo           the object the elements are serialized to
111    * @param formatVersion         the format version
112    * @throws IOException
113    */
114   protected <T> void serializeCollectionToElement( @Nonnull Iterable<? extends T> objects, @Nonnull Class<T> type, @Nonnull String collectionElementName, @Nonnull String elementName, @Nonnull SMOutputElement serializeTo, Version formatVersion ) throws XMLStreamException, IOException {
115     SMOutputElement collectionElement = serializeTo.addElement( serializeTo.getNamespace(), collectionElementName );
116     serializeCollection( objects, type, elementName, collectionElement, formatVersion );
117   }
118 
119   protected <T> void serializeCollectionToElement( @Nonnull Iterable<? extends T> objects, @Nonnull Class<T> type, @Nonnull String collectionElementName, @Nonnull SMOutputElement serializeTo, Version formatVersion ) throws XMLStreamException, IOException {
120     SMOutputElement collectionElement = serializeTo.addElement( serializeTo.getNamespace(), collectionElementName );
121     serializeCollection( objects, type, collectionElement, formatVersion );
122   }
123 
124   protected void serializeToElementWithCharacters( @Nonnull String elementName, @Nonnull String characters, @Nonnull SMOutputElement serializeTo ) throws XMLStreamException {
125     serializeTo.addElementWithCharacters( serializeTo.getNamespace(), elementName, characters );
126   }
127 }