|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.converters; |
|
23 |
| |
|
24 |
| import nu.xom.Attribute; |
|
25 |
| import nu.xom.Comment; |
|
26 |
| import nu.xom.DocType; |
|
27 |
| import nu.xom.Document; |
|
28 |
| import nu.xom.Element; |
|
29 |
| import nu.xom.Node; |
|
30 |
| import nu.xom.Nodes; |
|
31 |
| import nu.xom.ParentNode; |
|
32 |
| import nu.xom.ProcessingInstruction; |
|
33 |
| import nu.xom.Text; |
|
34 |
| |
|
35 |
| import org.xml.sax.ContentHandler; |
|
36 |
| import org.xml.sax.SAXException; |
|
37 |
| import org.xml.sax.ext.LexicalHandler; |
|
38 |
| import org.xml.sax.helpers.AttributesImpl; |
|
39 |
| import org.xml.sax.helpers.LocatorImpl; |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| public class SAXConverter { |
|
51 |
| |
|
52 |
| |
|
53 |
| private ContentHandler contentHandler; |
|
54 |
| private LexicalHandler lexicalHandler; |
|
55 |
| private LocatorImpl locator; |
|
56 |
| private boolean stripBaseAttributes = true; |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
1003
| public SAXConverter(ContentHandler handler) {
|
|
71 |
1003
| setContentHandler(handler);
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
1993
| public void setContentHandler(ContentHandler handler) {
|
|
87 |
| |
|
88 |
1993
| if (handler == null) {
|
|
89 |
1
| throw new NullPointerException(
|
|
90 |
| "ContentHandler must be non-null." |
|
91 |
| ); |
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
1992
| else if ("nu.xom.xslt.XSLTHandler".equals(handler.getClass().getName())) {
|
|
98 |
972
| this.stripBaseAttributes = false;
|
|
99 |
| } |
|
100 |
| else { |
|
101 |
1020
| this.contentHandler = handler;
|
|
102 |
| } |
|
103 |
| |
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
2
| public ContentHandler getContentHandler() {
|
|
115 |
2
| return this.contentHandler;
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
450
| public void setLexicalHandler(LexicalHandler handler) {
|
|
130 |
450
| this.lexicalHandler = handler;
|
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
2
| public LexicalHandler getLexicalHandler() {
|
|
144 |
2
| return this.lexicalHandler;
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
993
| public void convert(Document doc) throws SAXException {
|
|
165 |
| |
|
166 |
993
| locator = new LocatorImpl();
|
|
167 |
993
| locator.setSystemId(doc.getBaseURI());
|
|
168 |
993
| contentHandler.setDocumentLocator(locator);
|
|
169 |
993
| contentHandler.startDocument();
|
|
170 |
993
| for (int i = 0; i < doc.getChildCount(); i++) {
|
|
171 |
1500
| process(doc.getChild(i));
|
|
172 |
| } |
|
173 |
893
| contentHandler.endDocument();
|
|
174 |
| |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
586725
| private void process(Node node) throws SAXException {
|
|
179 |
| |
|
180 |
586725
| if (node instanceof Element) {
|
|
181 |
222512
| convertElement((Element) node);
|
|
182 |
| } |
|
183 |
364213
| else if (node instanceof Text) {
|
|
184 |
362169
| String data = node.getValue();
|
|
185 |
362169
| contentHandler.characters(
|
|
186 |
| data.toCharArray(), 0, data.length()); |
|
187 |
| } |
|
188 |
2044
| else if (node instanceof ProcessingInstruction) {
|
|
189 |
88
| ProcessingInstruction instruction
|
|
190 |
| = (ProcessingInstruction) node; |
|
191 |
| |
|
192 |
88
| contentHandler.processingInstruction(
|
|
193 |
| instruction.getTarget(), instruction.getValue()); |
|
194 |
| } |
|
195 |
1956
| else if (node instanceof Comment && lexicalHandler != null) {
|
|
196 |
744
| String data = node.getValue();
|
|
197 |
744
| lexicalHandler.comment(
|
|
198 |
| data.toCharArray(), 0, data.length()); |
|
199 |
| } |
|
200 |
1212
| else if (node instanceof DocType && lexicalHandler != null) {
|
|
201 |
97
| DocType type = (DocType) node;
|
|
202 |
97
| lexicalHandler.startDTD(type.getRootElementName(),
|
|
203 |
| type.getPublicID(), type.getSystemID()); |
|
204 |
97
| lexicalHandler.endDTD();
|
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
222825
| private boolean convertNamespace(Element element, String prefix)
|
|
218 |
| throws SAXException { |
|
219 |
| |
|
220 |
| |
|
221 |
222825
| String uri = element.getNamespaceURI(prefix);
|
|
222 |
222825
| ParentNode parentNode = element.getParent();
|
|
223 |
222825
| Element parent = null;
|
|
224 |
222825
| if (parentNode instanceof Element) {
|
|
225 |
221586
| parent = (Element) parentNode;
|
|
226 |
| } |
|
227 |
| |
|
228 |
222825
| if (parent != null && uri.equals(parent.getNamespaceURI(prefix))) {
|
|
229 |
221538
| return false;
|
|
230 |
| } |
|
231 |
1287
| else if (parent == null && "".equals(uri)) {
|
|
232 |
| |
|
233 |
| |
|
234 |
453
| return false;
|
|
235 |
| } |
|
236 |
834
| contentHandler.startPrefixMapping(prefix, uri);
|
|
237 |
834
| return true;
|
|
238 |
| |
|
239 |
| } |
|
240 |
| |
|
241 |
| |
|
242 |
222512
| private void convertElement(Element element) throws SAXException {
|
|
243 |
| |
|
244 |
222512
| locator.setSystemId(element.getBaseURI());
|
|
245 |
| |
|
246 |
| |
|
247 |
222512
| int namespaceCount = element.getNamespaceDeclarationCount();
|
|
248 |
222512
| String[] prefixes = new String[namespaceCount];
|
|
249 |
222512
| int prefixCount = 0;
|
|
250 |
222512
| for (int i = 0; i < namespaceCount; i++) {
|
|
251 |
222825
| String prefix = element.getNamespacePrefix(i);
|
|
252 |
222825
| boolean converted = convertNamespace(element, prefix);
|
|
253 |
222825
| if (converted) {
|
|
254 |
834
| prefixes[prefixCount] = prefix;
|
|
255 |
834
| prefixCount++;
|
|
256 |
| } |
|
257 |
| } |
|
258 |
| |
|
259 |
| |
|
260 |
222512
| AttributesImpl saxAttributes = new AttributesImpl();
|
|
261 |
222512
| int attributeCount = element.getAttributeCount();
|
|
262 |
222512
| for (int i = 0; i < attributeCount; i++) {
|
|
263 |
10213
| Attribute attribute = element.getAttribute(i);
|
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
10213
| if ("base".equals(attribute.getLocalName())
|
|
269 |
| && "http://www.w3.org/XML/1998/namespace".equals(attribute.getNamespaceURI()) |
|
270 |
| && stripBaseAttributes) { |
|
271 |
1
| continue;
|
|
272 |
| } |
|
273 |
10212
| saxAttributes.addAttribute(attribute.getNamespaceURI(),
|
|
274 |
| attribute.getLocalName(), |
|
275 |
| attribute.getQualifiedName(), |
|
276 |
| getSAXType(attribute), |
|
277 |
| attribute.getValue()); |
|
278 |
| } |
|
279 |
| |
|
280 |
222512
| contentHandler.startElement(
|
|
281 |
| element.getNamespaceURI(), |
|
282 |
| element.getLocalName(), |
|
283 |
| element.getQualifiedName(), |
|
284 |
| saxAttributes); |
|
285 |
222419
| int childCount = element.getChildCount();
|
|
286 |
222419
| for (int i = 0; i < childCount; i++) {
|
|
287 |
585224
| process(element.getChild(i));
|
|
288 |
| } |
|
289 |
222235
| contentHandler.endElement(element.getNamespaceURI(),
|
|
290 |
| element.getLocalName(), element.getQualifiedName()); |
|
291 |
| |
|
292 |
| |
|
293 |
222233
| for (int i = 0; i < prefixCount; i++) {
|
|
294 |
731
| contentHandler.endPrefixMapping(prefixes[i]);
|
|
295 |
| } |
|
296 |
| |
|
297 |
| } |
|
298 |
| |
|
299 |
| |
|
300 |
10212
| private static String getSAXType(Attribute attribute) {
|
|
301 |
| |
|
302 |
10212
| Attribute.Type type = attribute.getType();
|
|
303 |
8
| if (type.equals(Attribute.Type.UNDECLARED)) return "CDATA";
|
|
304 |
9862
| if (type.equals(Attribute.Type.CDATA)) return "CDATA";
|
|
305 |
58
| if (type.equals(Attribute.Type.ID)) return "ID";
|
|
306 |
5
| if (type.equals(Attribute.Type.IDREF)) return "IDREF";
|
|
307 |
1
| if (type.equals(Attribute.Type.IDREFS)) return "IDREFS";
|
|
308 |
273
| if (type.equals(Attribute.Type.NMTOKEN)) return "NMTOKEN";
|
|
309 |
1
| if (type.equals(Attribute.Type.NMTOKENS)) return "NMTOKENS";
|
|
310 |
1
| if (type.equals(Attribute.Type.ENTITY)) return "ENTITY";
|
|
311 |
1
| if (type.equals(Attribute.Type.ENTITIES)) return "ENTITIES";
|
|
312 |
1
| if (type.equals(Attribute.Type.NOTATION)) return "NOTATION";
|
|
313 |
1
| return "NMTOKEN";
|
|
314 |
| |
|
315 |
| } |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
972
| public void convert(Nodes nodes) throws SAXException {
|
|
333 |
| |
|
334 |
972
| if (nodes.size() == 1 && nodes.get(0) instanceof Document) {
|
|
335 |
971
| convert((Document) nodes.get(0));
|
|
336 |
| } |
|
337 |
| else { |
|
338 |
1
| locator = new LocatorImpl();
|
|
339 |
1
| contentHandler.setDocumentLocator(locator);
|
|
340 |
1
| contentHandler.startDocument();
|
|
341 |
1
| for (int i = 0; i < nodes.size(); i++) {
|
|
342 |
1
| process(nodes.get(i));
|
|
343 |
| } |
|
344 |
1
| contentHandler.endDocument();
|
|
345 |
| } |
|
346 |
| |
|
347 |
| } |
|
348 |
| |
|
349 |
| |
|
350 |
| } |