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.Version;
35  import com.cedarsoft.version.VersionRange;
36  import com.cedarsoft.serialization.AbstractXmlSerializer;
37  
38  import javax.annotation.Nonnull;
39  import javax.xml.stream.XMLStreamException;
40  import javax.xml.stream.XMLStreamReader;
41  import javax.xml.stream.XMLStreamWriter;
42  import java.io.IOException;
43  import java.io.OutputStream;
44  
45  /**
46   * Abstract base class for serializer using stax.
47   *
48   * @param <T> the type
49   */
50  public abstract class AbstractStaxSerializer<T> extends AbstractStaxBasedSerializer<T, XMLStreamWriter> {
51    /**
52     * Creates a new serializer
53     *
54     * @param defaultElementName the default element name
55     * @param nameSpaceUriBase   the name space uri base
56     * @param formatVersionRange the format version range
57     */
58    protected AbstractStaxSerializer( @Nonnull String defaultElementName, @Nonnull String nameSpaceUriBase, @Nonnull VersionRange formatVersionRange ) {
59      super( defaultElementName, nameSpaceUriBase, formatVersionRange );
60    }
61  
62    @Override
63    public void serialize( @Nonnull T object, @Nonnull OutputStream out ) throws IOException {
64      try {
65        XMLStreamWriter writer = StaxSupport.getXmlOutputFactory().createXMLStreamWriter( out );
66  
67        //Sets the name space
68        String nameSpace = getNameSpace();
69        writer.setDefaultNamespace( nameSpace );
70  
71        writer.writeStartElement( getDefaultElementName() );
72        writer.writeDefaultNamespace( nameSpace );
73  
74        serialize( writer, object, getFormatVersion() );
75        writer.writeEndElement();
76  
77        writer.close();
78      } catch ( XMLStreamException e ) {
79        throw new IOException( e );
80      }
81    }
82  
83    /**
84     * Serializes the elements of a collection
85     *
86     * @param objects       the objects that are serialized
87     * @param type          the type
88     * @param elementName   the element name
89     * @param serializeTo   the object the elements are serialized to
90     * @param formatVersion the format version
91     * @throws XMLStreamException
92     * @throws IOException
93     */
94    protected <T> void serializeCollection( @Nonnull Iterable<? extends T> objects, @Nonnull Class<T> type, @Nonnull String elementName, @Nonnull XMLStreamWriter serializeTo, @Nonnull Version formatVersion ) throws XMLStreamException, IOException {
95      AbstractXmlSerializer<? super T, XMLStreamWriter, XMLStreamReader, XMLStreamException> serializer = getSerializer( type );
96      Version resolvedVersion = getDelegatesMappings().resolveVersion( type, formatVersion );
97  
98      for ( T object : objects ) {
99        serializeTo.writeStartElement( elementName );
100       serializer.serialize( serializeTo, object, resolvedVersion );
101       serializeTo.writeEndElement();
102     }
103   }
104 
105   protected <T> void serializeCollection( @Nonnull Iterable<? extends T> objects, @Nonnull Class<T> type, @Nonnull XMLStreamWriter serializeTo, @Nonnull Version formatVersion ) throws XMLStreamException, IOException {
106     AbstractXmlSerializer<? super T, XMLStreamWriter, XMLStreamReader, XMLStreamException> serializer = getSerializer( type );
107     Version resolvedVersion = getDelegatesMappings().resolveVersion( type, formatVersion );
108 
109     for ( T object : objects ) {
110       serializeTo.writeStartElement( serializer.getDefaultElementName() );
111       serializer.serialize( serializeTo, object, resolvedVersion );
112       serializeTo.writeEndElement();
113     }
114   }
115 }