1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.xml.util;
17
18 import java.util.Enumeration;
19
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.stream.XMLStreamWriter;
22
23 import org.castor.core.util.Assert;
24 import org.exolab.castor.xml.NamespacesStack;
25 import org.xml.sax.Attributes;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.helpers.DefaultHandler;
28
29
30
31
32
33
34
35
36 public class StaxStreamHandler extends DefaultHandler {
37
38
39
40
41 private final XMLStreamWriter xmlStreamWriter;
42
43
44
45
46 private NamespacesStack namespacesStack = new NamespacesStack();
47
48
49
50
51 private boolean createNamespaceScope = true;
52
53
54
55
56
57
58
59
60 public StaxStreamHandler(XMLStreamWriter xmlStreamWriter) {
61 Assert.paramNotNull(xmlStreamWriter, "xmlStreamWriter");
62
63 this.xmlStreamWriter = xmlStreamWriter;
64 }
65
66 @Override
67 public void startDocument() throws SAXException {
68 try {
69 xmlStreamWriter.writeStartDocument();
70 } catch (XMLStreamException e) {
71 convertToSAXException("Error occurred when writing the document start.", e);
72 }
73 }
74
75 @Override
76 public void endDocument() throws SAXException {
77 try {
78 xmlStreamWriter.writeEndDocument();
79 xmlStreamWriter.flush();
80 } catch (XMLStreamException e) {
81 convertToSAXException("Error occurred when writing the document end.", e);
82 }
83 }
84
85 @Override
86 public void startPrefixMapping(String prefix, String uri) throws SAXException {
87 if (createNamespaceScope) {
88 namespacesStack.addNewNamespaceScope();
89 createNamespaceScope = false;
90 }
91
92 namespacesStack.addNamespace(prefix, uri);
93 }
94
95 @Override
96 public void startElement(String uri, String localName,
97 String qName, Attributes attributes) throws SAXException {
98 try {
99
100 xmlStreamWriter.writeStartElement(qName);
101
102
103 for (int i = 0; i < attributes.getLength(); i++) {
104 xmlStreamWriter.writeAttribute(attributes.getQName(i), attributes.getValue(i));
105 }
106
107
108 String defaultNamespace = namespacesStack.getDefaultNamespaceURI();
109 if(defaultNamespace != null && defaultNamespace.length() > 0) {
110 xmlStreamWriter.setDefaultNamespace(defaultNamespace);
111 }
112
113
114 Enumeration enumeration = namespacesStack.getLocalNamespacePrefixes();
115 String prefix;
116 while(enumeration.hasMoreElements()) {
117 prefix = (String)enumeration.nextElement();
118 xmlStreamWriter.writeNamespace(prefix,
119 namespacesStack.getNamespaceURI(prefix));
120 }
121 } catch (XMLStreamException e) {
122 convertToSAXException("Error occurred when writing the element start.", e);
123 }
124 }
125
126 @Override
127 public void endElement(String uri, String localName, String qName) throws SAXException {
128 try {
129
130 xmlStreamWriter.writeEndElement();
131 } catch (XMLStreamException e) {
132 convertToSAXException("Error occurred when writing the element end.", e);
133 }
134 }
135
136 @Override
137 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
138 try {
139
140 xmlStreamWriter.writeCharacters(ch, start, length);
141 } catch (XMLStreamException e) {
142 convertToSAXException("Error occurred when writing the white spaces.", e);
143 }
144 }
145
146 @Override
147 public void characters(char[] ch, int start, int length) throws SAXException {
148 try {
149
150 xmlStreamWriter.writeCharacters(ch, start, length);
151 } catch (XMLStreamException e) {
152 convertToSAXException("Error occurred when writing the characters.", e);
153 }
154 }
155
156 @Override
157 public void processingInstruction(String target, String data) throws SAXException {
158 try {
159
160 xmlStreamWriter.writeProcessingInstruction(target, data);
161 } catch (XMLStreamException e) {
162 convertToSAXException("Error occurred when writing the processing instruction.", e);
163 }
164 }
165
166
167
168
169
170
171
172
173
174
175 private void convertToSAXException(String msg, XMLStreamException e) throws SAXException {
176 throw new SAXException(msg, e);
177 }
178 }