1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
49
50
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
75
76
77
78
79
80
81
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
105
106
107
108
109
110
111
112
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 }