|
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.tests; |
|
23 |
| |
|
24 |
| import java.io.ByteArrayInputStream; |
|
25 |
| import java.io.File; |
|
26 |
| import java.io.IOException; |
|
27 |
| import java.io.Reader; |
|
28 |
| import java.io.StringReader; |
|
29 |
| |
|
30 |
| import javax.xml.parsers.DocumentBuilder; |
|
31 |
| import javax.xml.parsers.DocumentBuilderFactory; |
|
32 |
| import javax.xml.parsers.ParserConfigurationException; |
|
33 |
| |
|
34 |
| import nu.xom.Attribute; |
|
35 |
| import nu.xom.Builder; |
|
36 |
| import nu.xom.Comment; |
|
37 |
| import nu.xom.DocType; |
|
38 |
| import nu.xom.Document; |
|
39 |
| import nu.xom.Element; |
|
40 |
| import nu.xom.Elements; |
|
41 |
| import nu.xom.Node; |
|
42 |
| import nu.xom.NodeFactory; |
|
43 |
| import nu.xom.Nodes; |
|
44 |
| import nu.xom.ParsingException; |
|
45 |
| import nu.xom.ProcessingInstruction; |
|
46 |
| import nu.xom.Text; |
|
47 |
| import nu.xom.XMLException; |
|
48 |
| import nu.xom.converters.DOMConverter; |
|
49 |
| |
|
50 |
| import org.w3c.dom.CDATASection; |
|
51 |
| import org.w3c.dom.DOMImplementation; |
|
52 |
| import org.w3c.dom.DocumentFragment; |
|
53 |
| import org.xml.sax.InputSource; |
|
54 |
| import org.xml.sax.SAXException; |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| public class DOMConverterTest extends XOMTestCase { |
|
68 |
| |
|
69 |
| |
|
70 |
34
| public DOMConverterTest(String name) {
|
|
71 |
34
| super(name);
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| private String source = "<!DOCTYPE test [ ]>\r\n" |
|
76 |
| + "<?xml-stylesheet href=\"file.css\" type=\"text/css\"?>" |
|
77 |
| + "<!-- test -->" |
|
78 |
| + "<test xmlns:xlink='http://www.w3.org/TR/1999/xlink'>Hello dear" |
|
79 |
| + "\r\n<em id=\"p1\" " |
|
80 |
| + "xmlns:none=\"http://www.example.com\">very important</em>" |
|
81 |
| + "<span xlink:type='simple'>here's the link</span>\r\n" |
|
82 |
| + "<svg:svg xmlns:svg='http://www.w3.org/TR/2000/svg'><svg:text>" |
|
83 |
| + "text in a namespace</svg:text></svg:svg>\r\n" |
|
84 |
| + "<svg xmlns='http://www.w3.org/TR/2000/svg'><text>text in a " |
|
85 |
| + "namespace</text></svg>" |
|
86 |
| + "</test>\r\n" |
|
87 |
| + "<!--epilog-->"; |
|
88 |
| |
|
89 |
| |
|
90 |
| private Document xomDocument; |
|
91 |
| private org.w3c.dom.Document domDocument; |
|
92 |
| private DOMImplementation impl; |
|
93 |
| private DocumentBuilder builder; |
|
94 |
| |
|
95 |
| |
|
96 |
34
| protected void setUp() throws ParserConfigurationException {
|
|
97 |
| |
|
98 |
34
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
99 |
34
| factory.setNamespaceAware(true);
|
|
100 |
34
| builder = factory.newDocumentBuilder();
|
|
101 |
34
| impl = builder.getDOMImplementation();
|
|
102 |
| |
|
103 |
34
| DocType type = new DocType("test");
|
|
104 |
34
| Element root = new Element("test");
|
|
105 |
34
| xomDocument = new Document(root);
|
|
106 |
34
| xomDocument.insertChild(type, 0);
|
|
107 |
34
| xomDocument.insertChild(new ProcessingInstruction(
|
|
108 |
| "xml-stylesheet", "href=\"file.css\" type=\"text/css\""), 1); |
|
109 |
34
| xomDocument.insertChild(new Comment(" test "), 2);
|
|
110 |
34
| xomDocument.appendChild(new Comment("epilog"));
|
|
111 |
34
| root.addNamespaceDeclaration("xlink",
|
|
112 |
| "http://www.w3.org/TR/1999/xlink"); |
|
113 |
34
| root.appendChild("Hello dear\r\n");
|
|
114 |
34
| Element em = new Element("em");
|
|
115 |
34
| root.appendChild(em);
|
|
116 |
34
| em.addAttribute(new Attribute("id", "p1"));
|
|
117 |
34
| em.addNamespaceDeclaration("none", "http://www.example.com");
|
|
118 |
34
| em.appendChild("very important");
|
|
119 |
34
| Element span = new Element("span");
|
|
120 |
34
| root.appendChild(span);
|
|
121 |
34
| span.addAttribute(new Attribute("xlink:type",
|
|
122 |
| "http://www.w3.org/TR/1999/xlink", "simple")); |
|
123 |
34
| span.appendChild("here's the link");
|
|
124 |
34
| root.appendChild("\r\n");
|
|
125 |
| |
|
126 |
34
| Element empty = new Element("empty");
|
|
127 |
34
| root.appendChild(empty);
|
|
128 |
34
| empty.addAttribute(new Attribute("xom:temp",
|
|
129 |
| "http://xom.nu/", |
|
130 |
| "Just to see if this can handle namespaced attributes")); |
|
131 |
| |
|
132 |
34
| root.appendChild("\r\n");
|
|
133 |
34
| Element svg = new Element("svg:svg",
|
|
134 |
| "http://www.w3.org/TR/2000/svg"); |
|
135 |
34
| root.appendChild(svg);
|
|
136 |
34
| Element text = new Element("svg:text", "http://www.w3.org/TR/2000/svg");
|
|
137 |
34
| svg.appendChild(text);
|
|
138 |
34
| text.appendChild("text in a namespace");
|
|
139 |
34
| root.appendChild("\r\n");
|
|
140 |
| |
|
141 |
34
| svg = new Element("svg", "http://www.w3.org/TR/2000/svg");
|
|
142 |
34
| root.appendChild(svg);
|
|
143 |
34
| text = new Element("text", "http://www.w3.org/TR/2000/svg");
|
|
144 |
34
| svg.appendChild(text);
|
|
145 |
34
| text.appendChild("text in a namespace");
|
|
146 |
| |
|
147 |
34
| Reader reader = new StringReader(source);
|
|
148 |
34
| InputSource inso = new InputSource(reader);
|
|
149 |
| |
|
150 |
34
| try {
|
|
151 |
34
| domDocument = builder.parse(inso);
|
|
152 |
| } |
|
153 |
| catch (Exception ex) { |
|
154 |
| |
|
155 |
0
| throw new RuntimeException("Oops!");
|
|
156 |
| } |
|
157 |
| |
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
1
| public void testToDOM() {
|
|
162 |
| |
|
163 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocument, impl);
|
|
164 |
1
| org.w3c.dom.DocumentType doctype = domDoc.getDoctype();
|
|
165 |
| |
|
166 |
1
| assertEquals("test", domDoc.getDocumentElement().getNodeName());
|
|
167 |
1
| assertTrue(doctype != null);
|
|
168 |
1
| assertEquals(org.w3c.dom.Node.DOCUMENT_TYPE_NODE,
|
|
169 |
| domDoc.getFirstChild().getNodeType()); |
|
170 |
1
| assertEquals(org.w3c.dom.Node.COMMENT_NODE,
|
|
171 |
| domDoc.getLastChild().getNodeType()); |
|
172 |
| |
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
1
| public void testToXOM() {
|
|
177 |
| |
|
178 |
1
| Document xomDoc = DOMConverter.convert(domDocument);
|
|
179 |
1
| DocType doctype = xomDoc.getDocType();
|
|
180 |
1
| Element root = xomDoc.getRootElement();
|
|
181 |
| |
|
182 |
1
| assertEquals("test", root.getQualifiedName());
|
|
183 |
1
| assertEquals("test", root.getLocalName());
|
|
184 |
1
| assertEquals("", root.getNamespaceURI());
|
|
185 |
| |
|
186 |
1
| assertTrue(doctype != null);
|
|
187 |
1
| assertTrue(xomDoc.getChild(0) instanceof DocType);
|
|
188 |
1
| assertTrue(xomDoc.getChild(4) instanceof nu.xom.Comment);
|
|
189 |
1
| assertTrue(xomDoc.getChild(2) instanceof nu.xom.Comment);
|
|
190 |
1
| assertEquals(" test ", xomDoc.getChild(2).getValue());
|
|
191 |
1
| assertEquals("epilog", xomDoc.getChild(4).getValue());
|
|
192 |
1
| assertTrue(
|
|
193 |
| xomDoc.getChild(1) instanceof nu.xom.ProcessingInstruction); |
|
194 |
1
| assertEquals("test", doctype.getRootElementName());
|
|
195 |
1
| assertNull(doctype.getPublicID());
|
|
196 |
1
| assertNull(doctype.getSystemID());
|
|
197 |
| |
|
198 |
| } |
|
199 |
| |
|
200 |
| |
|
201 |
1
| public void testDefaultNamespacedElement()
|
|
202 |
| throws SAXException, IOException, ParserConfigurationException { |
|
203 |
| |
|
204 |
1
| byte[] data = "<root xmlns=\"http://www.example.com\"/>".getBytes();
|
|
205 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
206 |
1
| Document xomDoc = DOMConverter.convert(doc);
|
|
207 |
| |
|
208 |
1
| Element root = xomDoc.getRootElement();
|
|
209 |
1
| assertEquals("root", root.getQualifiedName());
|
|
210 |
1
| assertEquals("http://www.example.com", root.getNamespaceURI());
|
|
211 |
| |
|
212 |
| } |
|
213 |
| |
|
214 |
| |
|
215 |
1
| public void testPrefixedElement()
|
|
216 |
| throws SAXException, IOException, ParserConfigurationException { |
|
217 |
| |
|
218 |
1
| byte[] data = "<pre:root xmlns:pre=\"http://www.example.com\"/>".getBytes();
|
|
219 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
220 |
1
| Document xomDoc = DOMConverter.convert(doc);
|
|
221 |
| |
|
222 |
1
| Element root = xomDoc.getRootElement();
|
|
223 |
1
| assertEquals("pre:root", root.getQualifiedName());
|
|
224 |
1
| assertEquals("http://www.example.com", root.getNamespaceURI());
|
|
225 |
| |
|
226 |
| } |
|
227 |
| |
|
228 |
| |
|
229 |
1
| public void testConvertAttr()
|
|
230 |
| throws SAXException, IOException, ParserConfigurationException { |
|
231 |
| |
|
232 |
1
| byte[] data = ("<element name='value' " +
|
|
233 |
| "xmlns='http://example.com/' " + |
|
234 |
| "xmlns:pre='http://example.net'/>").getBytes(); |
|
235 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
236 |
| |
|
237 |
1
| org.w3c.dom.Element root = doc.getDocumentElement();
|
|
238 |
1
| Attribute attribute = DOMConverter.convert(root.getAttributeNode("name"));
|
|
239 |
1
| assertEquals("name", attribute.getQualifiedName());
|
|
240 |
1
| assertEquals("", attribute.getNamespacePrefix());
|
|
241 |
1
| assertEquals("", attribute.getNamespaceURI());
|
|
242 |
1
| assertEquals("value", attribute.getValue());
|
|
243 |
| |
|
244 |
1
| try {
|
|
245 |
1
| DOMConverter.convert(root.getAttributeNode("xmlns"));
|
|
246 |
0
| fail("Converted xmlns attribute");
|
|
247 |
| } |
|
248 |
| catch (XMLException ex) { |
|
249 |
1
| assertNotNull(ex.getMessage());
|
|
250 |
| } |
|
251 |
1
| try {
|
|
252 |
1
| DOMConverter.convert(root.getAttributeNode("xmlns:pre"));
|
|
253 |
0
| fail("Converted xmlns:pre attribute");
|
|
254 |
| } |
|
255 |
| catch (XMLException ex) { |
|
256 |
1
| assertNotNull(ex.getMessage());
|
|
257 |
| } |
|
258 |
| |
|
259 |
| } |
|
260 |
| |
|
261 |
| |
|
262 |
1
| public void testConvertElement()
|
|
263 |
| throws SAXException, IOException, ParserConfigurationException { |
|
264 |
| |
|
265 |
1
| byte[] data = ("<element name='value' " +
|
|
266 |
| "xmlns='http://example.com/' " + |
|
267 |
| "xmlns:pre='http://example.net'/>").getBytes(); |
|
268 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
269 |
| |
|
270 |
1
| org.w3c.dom.Element root = doc.getDocumentElement();
|
|
271 |
1
| Element xomRoot = DOMConverter.convert(root);
|
|
272 |
1
| assertEquals("name", xomRoot.getAttribute("name").getQualifiedName());
|
|
273 |
1
| assertEquals("", xomRoot.getAttribute("name").getNamespacePrefix());
|
|
274 |
1
| assertEquals("", xomRoot.getAttribute("name").getNamespaceURI());
|
|
275 |
1
| assertEquals("value", xomRoot.getAttribute("name").getValue());
|
|
276 |
1
| assertEquals("element", xomRoot.getQualifiedName());
|
|
277 |
1
| assertEquals("", xomRoot.getValue());
|
|
278 |
1
| assertEquals(0, xomRoot.getChildCount());
|
|
279 |
1
| assertEquals("http://example.com/", xomRoot.getNamespaceURI());
|
|
280 |
1
| assertEquals("http://example.net", xomRoot.getNamespaceURI("pre"));
|
|
281 |
| |
|
282 |
| } |
|
283 |
| |
|
284 |
| |
|
285 |
1
| public void testConvertComment()
|
|
286 |
| throws SAXException, IOException, ParserConfigurationException { |
|
287 |
| |
|
288 |
1
| byte[] data = "<element><!--data--></element>".getBytes();
|
|
289 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
290 |
| |
|
291 |
1
| org.w3c.dom.Element root = doc.getDocumentElement();
|
|
292 |
1
| org.w3c.dom.Comment comment = (org.w3c.dom.Comment) (root.getChildNodes().item(0));
|
|
293 |
1
| Comment xomComment = DOMConverter.convert(comment);
|
|
294 |
1
| assertEquals(comment.getNodeValue(), xomComment.getValue());
|
|
295 |
| |
|
296 |
| } |
|
297 |
| |
|
298 |
| |
|
299 |
1
| public void testConvertText()
|
|
300 |
| throws SAXException, IOException, ParserConfigurationException { |
|
301 |
| |
|
302 |
1
| byte[] data = "<element> here's the text </element>".getBytes();
|
|
303 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
304 |
| |
|
305 |
1
| org.w3c.dom.Element root = doc.getDocumentElement();
|
|
306 |
1
| org.w3c.dom.Text node = (org.w3c.dom.Text) (root.getChildNodes().item(0));
|
|
307 |
1
| Text text = DOMConverter.convert(node);
|
|
308 |
1
| assertEquals(node.getNodeValue(), text.getValue());
|
|
309 |
| |
|
310 |
| } |
|
311 |
| |
|
312 |
| |
|
313 |
1
| public void testConvertCDATASection()
|
|
314 |
| throws SAXException, IOException, ParserConfigurationException { |
|
315 |
| |
|
316 |
1
| byte[] data = "<element><![CDATA[ here's the text ]]></element>".getBytes();
|
|
317 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
318 |
| |
|
319 |
1
| org.w3c.dom.Element root = doc.getDocumentElement();
|
|
320 |
1
| CDATASection node = (CDATASection) (root.getChildNodes().item(0));
|
|
321 |
1
| Text text = DOMConverter.convert(node);
|
|
322 |
1
| assertEquals(node.getNodeValue(), text.getValue());
|
|
323 |
| |
|
324 |
| |
|
325 |
1
| Document xomDoc = DOMConverter.convert(doc);
|
|
326 |
1
| assertEquals(node.getNodeValue(), xomDoc.getValue());
|
|
327 |
| |
|
328 |
| } |
|
329 |
| |
|
330 |
| |
|
331 |
1
| public void testConvertProcessingInstruction()
|
|
332 |
| throws SAXException, IOException, ParserConfigurationException { |
|
333 |
| |
|
334 |
1
| byte[] data = "<element><?target PI data?></element>".getBytes();
|
|
335 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
336 |
| |
|
337 |
1
| org.w3c.dom.Element root = doc.getDocumentElement();
|
|
338 |
1
| org.w3c.dom.ProcessingInstruction node
|
|
339 |
| = (org.w3c.dom.ProcessingInstruction) (root.getChildNodes().item(0)); |
|
340 |
1
| ProcessingInstruction pi = DOMConverter.convert(node);
|
|
341 |
1
| assertEquals(node.getNodeValue(), pi.getValue());
|
|
342 |
1
| assertEquals(node.getTarget(), pi.getTarget());
|
|
343 |
| |
|
344 |
| } |
|
345 |
| |
|
346 |
| |
|
347 |
1
| public void testConvertDocType()
|
|
348 |
| throws SAXException, IOException, ParserConfigurationException { |
|
349 |
| |
|
350 |
1
| byte[] data = "<!DOCTYPE root ><element />".getBytes();
|
|
351 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
352 |
| |
|
353 |
1
| org.w3c.dom.DocumentType type = doc.getDoctype();
|
|
354 |
1
| DocType xomType = DOMConverter.convert(type);
|
|
355 |
1
| assertEquals(type.getName(), xomType.getRootElementName());
|
|
356 |
| |
|
357 |
| } |
|
358 |
| |
|
359 |
| |
|
360 |
1
| public void testConvertInternalDTDSubset()
|
|
361 |
| throws SAXException, IOException, ParserConfigurationException { |
|
362 |
| |
|
363 |
1
| byte[] data = "<!DOCTYPE root [<!ELEMENT element EMPTY>]><element />".getBytes();
|
|
364 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
365 |
| |
|
366 |
1
| org.w3c.dom.DocumentType type = doc.getDoctype();
|
|
367 |
1
| DocType xomType = DOMConverter.convert(type);
|
|
368 |
1
| assertEquals(type.getName(), xomType.getRootElementName());
|
|
369 |
1
| assertEquals(type.getInternalSubset(), xomType.getInternalDTDSubset());
|
|
370 |
| |
|
371 |
| } |
|
372 |
| |
|
373 |
| |
|
374 |
1
| public void testConvertDocTypePublicAndSystemIdentifier()
|
|
375 |
| throws SAXException, IOException, ParserConfigurationException { |
|
376 |
| |
|
377 |
1
| File inputDir = new File("data");
|
|
378 |
1
| File f = new File(inputDir, "dtdtest.xhtml");
|
|
379 |
1
| org.w3c.dom.Document doc = builder.parse(f);
|
|
380 |
| |
|
381 |
1
| org.w3c.dom.DocumentType type = doc.getDoctype();
|
|
382 |
1
| DocType xomType = DOMConverter.convert(type);
|
|
383 |
1
| assertEquals(type.getName(), xomType.getRootElementName());
|
|
384 |
1
| assertEquals(type.getSystemId(), xomType.getSystemID());
|
|
385 |
1
| assertEquals(type.getPublicId(), xomType.getPublicID());
|
|
386 |
| |
|
387 |
| } |
|
388 |
| |
|
389 |
| |
|
390 |
1
| public void testChildElementAddsNamespace() {
|
|
391 |
| |
|
392 |
1
| Element root = new Element("root");
|
|
393 |
1
| Element child = new Element("pre:child",
|
|
394 |
| "http://www.example.org/"); |
|
395 |
1
| child.addAttribute(new Attribute("xlink:type",
|
|
396 |
| "http://www.w3.org/1999/xlink", "simple")); |
|
397 |
1
| root.appendChild(child);
|
|
398 |
1
| Document doc = new Document(root);
|
|
399 |
| |
|
400 |
1
| assertEquals(
|
|
401 |
| doc, |
|
402 |
| DOMConverter.convert(DOMConverter.convert(doc, impl)) |
|
403 |
| ); |
|
404 |
| |
|
405 |
| } |
|
406 |
| |
|
407 |
| |
|
408 |
1
| public void testChildElementUsesSameNamespace() {
|
|
409 |
| |
|
410 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
411 |
1
| Element child = new Element("pre:child", "http://www.example.org/");
|
|
412 |
1
| root.appendChild(child);
|
|
413 |
1
| Document doc = new Document(root);
|
|
414 |
1
| assertEquals(doc, DOMConverter.convert(DOMConverter.convert(doc, impl)));
|
|
415 |
| |
|
416 |
| } |
|
417 |
| |
|
418 |
| |
|
419 |
1
| public void testPrefixMappingChanges() {
|
|
420 |
| |
|
421 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
422 |
1
| Element child = new Element("pre:child", "http://www.example.net/");
|
|
423 |
1
| root.appendChild(child);
|
|
424 |
1
| Document doc = new Document(root);
|
|
425 |
1
| assertEquals(doc, DOMConverter.convert(DOMConverter.convert(doc, impl)));
|
|
426 |
| |
|
427 |
| } |
|
428 |
| |
|
429 |
| |
|
430 |
1
| public void testConvertDocumentFragment() {
|
|
431 |
| |
|
432 |
1
| DocumentFragment fragment = domDocument.createDocumentFragment();
|
|
433 |
1
| org.w3c.dom.Element element = domDocument.createElement("name");
|
|
434 |
1
| element.setAttribute("name", "value");
|
|
435 |
1
| fragment.appendChild(element);
|
|
436 |
1
| fragment.appendChild(domDocument.createComment("data"));
|
|
437 |
1
| fragment.appendChild(domDocument.createTextNode("text"));
|
|
438 |
| |
|
439 |
1
| Nodes result = DOMConverter.convert(fragment);
|
|
440 |
| |
|
441 |
1
| Element first = (Element) result.get(0);
|
|
442 |
1
| assertEquals("name", first.getQualifiedName());
|
|
443 |
1
| assertEquals("name", first.getAttribute("name").getQualifiedName());
|
|
444 |
1
| assertEquals("value", first.getAttribute("name").getValue());
|
|
445 |
| |
|
446 |
1
| Comment second = (Comment) result.get(1);
|
|
447 |
1
| assertEquals("data", second.getValue());
|
|
448 |
| |
|
449 |
1
| Text third = (Text) result.get(2);
|
|
450 |
1
| assertEquals("text", third.getValue());
|
|
451 |
| |
|
452 |
| } |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
1
| public void testWolfgang() throws ParsingException, IOException {
|
|
457 |
| |
|
458 |
1
| String data = "<m>"
|
|
459 |
| + "<a code='3.1415292'>" |
|
460 |
| + "<u>http://www.example.com</u>" |
|
461 |
| + "<h>Some data</h>" |
|
462 |
| + "<s>StockAccess</s>" |
|
463 |
| + "</a>" |
|
464 |
| + "</m>"; |
|
465 |
| |
|
466 |
1
| Document doc = new Builder().build(data, null);
|
|
467 |
1
| Element article = doc.getRootElement().getChildElements().get(0);
|
|
468 |
1
| Document temp = new Document(new Element(article));
|
|
469 |
| |
|
470 |
1
| org.w3c.dom.Element domArticle
|
|
471 |
| = DOMConverter.convert(temp, impl).getDocumentElement(); |
|
472 |
1
| assertEquals(3, domArticle.getChildNodes().getLength());
|
|
473 |
1
| Element out = DOMConverter.convert(domArticle);
|
|
474 |
1
| assertEquals(article, out);
|
|
475 |
| |
|
476 |
| } |
|
477 |
| |
|
478 |
| |
|
479 |
1
| public void testConvertNullXOMDocument() {
|
|
480 |
| |
|
481 |
1
| try {
|
|
482 |
1
| DOMConverter.convert((Document) null, impl);
|
|
483 |
0
| fail("Converted null document");
|
|
484 |
| } |
|
485 |
| catch (NullPointerException success) { |
|
486 |
| } |
|
487 |
| |
|
488 |
| |
|
489 |
| } |
|
490 |
| |
|
491 |
| |
|
492 |
1
| public void testConvertNullDOMElement() {
|
|
493 |
| |
|
494 |
1
| try {
|
|
495 |
1
| DOMConverter.convert((org.w3c.dom.Element) null);
|
|
496 |
0
| fail("Converted null element");
|
|
497 |
| } |
|
498 |
| catch (NullPointerException success) { |
|
499 |
| } |
|
500 |
| |
|
501 |
| |
|
502 |
| } |
|
503 |
| |
|
504 |
| |
|
505 |
1
| public void testConvertNullDOMDocument() {
|
|
506 |
| |
|
507 |
1
| try {
|
|
508 |
1
| DOMConverter.convert((org.w3c.dom.Document) null);
|
|
509 |
0
| fail("Converted null element");
|
|
510 |
| } |
|
511 |
| catch (NullPointerException success) { |
|
512 |
| } |
|
513 |
| |
|
514 |
| |
|
515 |
| } |
|
516 |
| |
|
517 |
| |
|
518 |
| |
|
519 |
| |
|
520 |
| |
|
521 |
1
| public void testFullHoschek() throws ParsingException, IOException {
|
|
522 |
| |
|
523 |
1
| File f = new File("data");
|
|
524 |
1
| f = new File(f, "hoschektest.xml");
|
|
525 |
1
| Builder builder = new Builder();
|
|
526 |
1
| Document xomDocIn = builder.build(f);
|
|
527 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl);
|
|
528 |
1
| Document xomDocOut = DOMConverter.convert(domDoc);
|
|
529 |
1
| assertEquals(xomDocIn, xomDocOut);
|
|
530 |
| |
|
531 |
| } |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
1
| public void testSimplifiedHoschekDOMToXOM()
|
|
538 |
| throws SAXException, IOException { |
|
539 |
| |
|
540 |
1
| File f = new File("data");
|
|
541 |
1
| f = new File(f, "simplehoschektest.xml");
|
|
542 |
1
| org.w3c.dom.Document domDocIn = builder.parse(f);
|
|
543 |
1
| int domRootNumber = domDocIn.getDocumentElement().getChildNodes().getLength();
|
|
544 |
1
| Document xomDoc = DOMConverter.convert(domDocIn);
|
|
545 |
1
| Element xomRoot = xomDoc.getRootElement();
|
|
546 |
1
| assertEquals(domRootNumber, xomRoot.getChildCount());
|
|
547 |
| |
|
548 |
| } |
|
549 |
| |
|
550 |
| |
|
551 |
1
| public void testChildNodesRemainChildNodes()
|
|
552 |
| throws ParsingException, IOException { |
|
553 |
| |
|
554 |
1
| String data = "<root><scope><a>1</a><b>2</b><c /></scope></root>";
|
|
555 |
1
| Builder builder = new Builder();
|
|
556 |
1
| Document xomDocIn = builder.build(data, null);
|
|
557 |
1
| int xomRootNumber = xomDocIn.getRootElement().getChildCount();
|
|
558 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl);
|
|
559 |
1
| org.w3c.dom.Element domRoot = domDoc.getDocumentElement();
|
|
560 |
1
| assertEquals(xomRootNumber, domRoot.getChildNodes().getLength());
|
|
561 |
1
| Document xomDocOut = DOMConverter.convert(domDoc);
|
|
562 |
1
| assertEquals(xomDocIn, xomDocOut);
|
|
563 |
| |
|
564 |
| } |
|
565 |
| |
|
566 |
| |
|
567 |
1
| public void testLastChildNodeIsLeafNode()
|
|
568 |
| throws ParsingException, IOException { |
|
569 |
| |
|
570 |
1
| String data = "<root><top><empty />ABCD</top></root>";
|
|
571 |
1
| Builder builder = new Builder();
|
|
572 |
1
| Document xomDocIn = builder.build(data, null);
|
|
573 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl);
|
|
574 |
1
| org.w3c.dom.Element domRoot = domDoc.getDocumentElement();
|
|
575 |
1
| org.w3c.dom.Node domTop = domRoot.getFirstChild();
|
|
576 |
1
| assertEquals(2, domTop.getChildNodes().getLength());
|
|
577 |
| |
|
578 |
| } |
|
579 |
| |
|
580 |
| |
|
581 |
1
| public void testNamespaceAttributes() {
|
|
582 |
| |
|
583 |
1
| Element root = new Element("root", "http://www.example.org/");
|
|
584 |
1
| root.appendChild("text");
|
|
585 |
1
| Document xomDocIn = new Document(root);
|
|
586 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl);
|
|
587 |
1
| org.w3c.dom.Element domRoot = domDoc.getDocumentElement();
|
|
588 |
1
| assertTrue(domRoot.hasAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns"));
|
|
589 |
| |
|
590 |
| } |
|
591 |
| |
|
592 |
| |
|
593 |
1
| public void testConvertSingleElementDocumentFromXOMToDOM() {
|
|
594 |
| |
|
595 |
1
| Element root = new Element("root");
|
|
596 |
1
| Document xomDocIn = new Document(root);
|
|
597 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl);
|
|
598 |
1
| org.w3c.dom.Element domRoot = domDoc.getDocumentElement();
|
|
599 |
1
| assertEquals(0, domRoot.getChildNodes().getLength());
|
|
600 |
1
| assertEquals("root", domRoot.getNodeName());
|
|
601 |
| |
|
602 |
| } |
|
603 |
| |
|
604 |
| |
|
605 |
1
| public void testConvertSingleElementDocumentFromDOMToXOM()
|
|
606 |
| throws SAXException, IOException { |
|
607 |
| |
|
608 |
1
| byte[] data = "<element />".getBytes();
|
|
609 |
1
| org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream(data));
|
|
610 |
1
| Document xomDoc = DOMConverter.convert(doc);
|
|
611 |
1
| Element root = xomDoc.getRootElement();
|
|
612 |
1
| assertEquals(0, root.getChildCount());
|
|
613 |
1
| assertEquals("element", root.getLocalName());
|
|
614 |
| |
|
615 |
| } |
|
616 |
| |
|
617 |
| |
|
618 |
1
| public void testConvertXMLSpaceAttributeFromXOMToDOM()
|
|
619 |
| throws SAXException, IOException { |
|
620 |
| |
|
621 |
1
| Element root = new Element("element");
|
|
622 |
1
| Document doc = new Document(root);
|
|
623 |
1
| root.addAttribute(new Attribute("xml:space",
|
|
624 |
| "http://www.w3.org/XML/1998/namespace", "preserve")); |
|
625 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(doc, impl);
|
|
626 |
1
| org.w3c.dom.Element domRoot = domDoc.getDocumentElement();
|
|
627 |
1
| assertEquals(1, domRoot.getAttributes().getLength());
|
|
628 |
| |
|
629 |
| } |
|
630 |
| |
|
631 |
| |
|
632 |
1
| public void testConvertXMLPrefixedElementFromXOMToDOM()
|
|
633 |
| throws SAXException, IOException { |
|
634 |
| |
|
635 |
1
| Element root = new Element(
|
|
636 |
| "xml:element", "http://www.w3.org/XML/1998/namespace"); |
|
637 |
1
| Document doc = new Document(root);
|
|
638 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(doc, impl);
|
|
639 |
1
| org.w3c.dom.Element domRoot = domDoc.getDocumentElement();
|
|
640 |
1
| assertEquals(0, domRoot.getAttributes().getLength());
|
|
641 |
| |
|
642 |
| } |
|
643 |
| |
|
644 |
| |
|
645 |
1
| public void testDeepConvert() {
|
|
646 |
| |
|
647 |
1
| Element top = new Element("e");
|
|
648 |
1
| Element parent = top;
|
|
649 |
1
| for (int i = 0; i < 100; i++) {
|
|
650 |
100
| Element child = new Element("e" + i);
|
|
651 |
100
| parent.appendChild(child);
|
|
652 |
100
| parent = child;
|
|
653 |
| } |
|
654 |
1
| Document xomDocIn = new Document(top);
|
|
655 |
1
| org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl);
|
|
656 |
1
| Document xomDocOut = DOMConverter.convert(domDoc);
|
|
657 |
| |
|
658 |
1
| assertEquals(xomDocIn, xomDocOut);
|
|
659 |
| |
|
660 |
| } |
|
661 |
| |
|
662 |
1
| public void testUseFactory() {
|
|
663 |
| |
|
664 |
1
| Document xomDocOut = DOMConverter.convert(domDocument, new ANodeFactory());
|
|
665 |
1
| assertTrue(xomDocOut instanceof ADocument);
|
|
666 |
1
| Element root = xomDocOut.getRootElement();
|
|
667 |
1
| assertTrue(root instanceof AElement);
|
|
668 |
1
| Node doctype = xomDocOut.getChild(0);
|
|
669 |
1
| assertTrue(doctype instanceof ADocType);
|
|
670 |
1
| Node pi = xomDocOut.getChild(1);
|
|
671 |
1
| assertTrue(pi instanceof AProcessingInstruction);
|
|
672 |
1
| Node comment = xomDocOut.getChild(2);
|
|
673 |
1
| assertTrue(comment instanceof AComment);
|
|
674 |
1
| assertTrue(root.getChild(0) instanceof AText);
|
|
675 |
1
| Elements children = root.getChildElements();
|
|
676 |
1
| for (int i = 0; i < children.size(); i++) {
|
|
677 |
4
| Element child = children.get(i);
|
|
678 |
4
| assertTrue(child instanceof AElement);
|
|
679 |
4
| for (int j = 0; j < child.getAttributeCount(); j++) {
|
|
680 |
2
| assertTrue(child.getAttribute(j) instanceof AnAttribute);
|
|
681 |
| } |
|
682 |
| } |
|
683 |
| |
|
684 |
| } |
|
685 |
| |
|
686 |
1
| public void testUseMinimizingFactory() {
|
|
687 |
1
| Document xomDocOut = DOMConverter.convert(
|
|
688 |
| domDocument, new NodeFactoryTest.MinimizingFactory()); |
|
689 |
1
| assertEquals(0, xomDocOut.getRootElement().getChildCount());
|
|
690 |
| } |
|
691 |
| |
|
692 |
1
| public void testUseStrippingFactory() {
|
|
693 |
1
| Document xomDocOut = DOMConverter.convert(
|
|
694 |
| domDocument, new StrippingFactory()); |
|
695 |
1
| assertEquals("Hello dear\nvery importanthere's the link\n"
|
|
696 |
| + "text in a namespace\ntext in a namespace", xomDocOut.getRootElement().getValue()); |
|
697 |
| } |
|
698 |
| |
|
699 |
| private static class StrippingFactory extends NodeFactory { |
|
700 |
| |
|
701 |
8
| public Element startMakingElement(String name, String namespaceURI) {
|
|
702 |
6
| if (! "test".equals(name)) return null;
|
|
703 |
2
| return super.startMakingElement(name, namespaceURI);
|
|
704 |
| } |
|
705 |
| |
|
706 |
| } |
|
707 |
| |
|
708 |
| } |