3535import org .springframework .xml .transform .StaxResult ;
3636
3737/**
38+ * Non-caching payload in Axiom.
39+ *
3840 * @author Jim Cummings
3941 * @author Arjen Poutsma
4042 * @since 1.5.2
@@ -63,7 +65,9 @@ private class DelegatingStreamWriter implements XMLStreamWriter {
6365
6466 private QName name ;
6567
66- private String encoding ;
68+ private String encoding = "UTF-8" ;
69+
70+ private int elementDepth = 0 ;
6771
6872 private DelegatingStreamWriter () {
6973 try {
@@ -75,51 +79,65 @@ private DelegatingStreamWriter() {
7579 }
7680
7781 public void writeStartDocument () throws XMLStreamException {
78- this . encoding = "UTF-8" ;
82+ // ignored
7983 }
8084
8185 public void writeStartDocument (String version ) throws XMLStreamException {
82- this . encoding = "UTF-8" ;
86+ // ignored
8387 }
8488
8589 public void writeStartDocument (String encoding , String version ) throws XMLStreamException {
8690 this .encoding = encoding ;
8791 }
8892
8993 public void writeEndDocument () throws XMLStreamException {
90- delegate .writeEndDocument ();
91- delegate .flush ();
92- if (baos .size () > 0 ) {
93- byte [] buf = baos .toByteArray ();
94- OMDataSource dataSource = new ByteArrayDataSource (buf , encoding );
95- OMNamespace namespace = getAxiomFactory ().createOMNamespace (name .getNamespaceURI (), name .getPrefix ());
96- OMElement payloadElement =
97- getAxiomFactory ().createOMElement (dataSource , name .getLocalPart (), namespace );
98- getAxiomBody ().addChild (payloadElement );
99- }
94+ // ignored
10095 }
10196
10297 public void writeStartElement (String localName ) throws XMLStreamException {
10398 if (name == null ) {
10499 name = new QName (localName );
105100 }
101+ elementDepth ++;
106102 delegate .writeStartElement (localName );
107103 }
108104
109105 public void writeStartElement (String namespaceURI , String localName ) throws XMLStreamException {
110106 if (name == null ) {
111107 name = new QName (namespaceURI , localName );
112108 }
109+ elementDepth ++;
113110 delegate .writeStartElement (namespaceURI , localName );
114111 }
115112
116113 public void writeStartElement (String prefix , String localName , String namespaceURI ) throws XMLStreamException {
117114 if (name == null ) {
118115 name = new QName (namespaceURI , localName , prefix );
119116 }
117+ elementDepth ++;
120118 delegate .writeStartElement (prefix , localName , namespaceURI );
121119 }
122120
121+ public void writeEndElement () throws XMLStreamException {
122+ elementDepth --;
123+ delegate .writeEndElement ();
124+ if (elementDepth <= 0 ) {
125+ addPayload ();
126+ }
127+ }
128+
129+ private void addPayload () throws XMLStreamException {
130+ delegate .flush ();
131+ if (baos .size () > 0 ) {
132+ byte [] buf = baos .toByteArray ();
133+ OMDataSource dataSource = new ByteArrayDataSource (buf , encoding );
134+ OMNamespace namespace = getAxiomFactory ().createOMNamespace (name .getNamespaceURI (), name .getPrefix ());
135+ OMElement payloadElement =
136+ getAxiomFactory ().createOMElement (dataSource , name .getLocalPart (), namespace );
137+ getAxiomBody ().addChild (payloadElement );
138+ }
139+ }
140+
123141 public void writeEmptyElement (String localName ) throws XMLStreamException {
124142 if (name == null ) {
125143 name = new QName (localName );
@@ -212,10 +230,6 @@ public void writeDTD(String dtd) throws XMLStreamException {
212230 delegate .writeDTD (dtd );
213231 }
214232
215- public void writeEndElement () throws XMLStreamException {
216- delegate .writeEndElement ();
217- }
218-
219233 public void writeEntityRef (String name ) throws XMLStreamException {
220234 delegate .writeEntityRef (name );
221235 }
0 commit comments