1 package com.fasterxml.jackson.databind.ext; 2 3 import java.io.StringReader; 4 import javax.xml.parsers.DocumentBuilderFactory; 5 6 import org.xml.sax.InputSource; 7 import org.w3c.dom.*; 8 9 import com.fasterxml.jackson.databind.ObjectMapper; 10 11 public class TestDOM extends com.fasterxml.jackson.databind.BaseMapTest 12 { 13 final static String SIMPLE_XML = 14 "<root attr='3'><leaf>Rock & Roll!</leaf><?proc instr?></root>"; 15 final static String SIMPLE_XML_NS = 16 "<root ns:attr='abc' xmlns:ns='http://foo' />"; 17 18 private final ObjectMapper MAPPER = new ObjectMapper(); 19 testSerializeSimpleNonNS()20 public void testSerializeSimpleNonNS() throws Exception 21 { 22 // Let's just parse first, easiest 23 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse 24 (new InputSource(new StringReader(SIMPLE_XML))); 25 assertNotNull(doc); 26 // need to strip xml declaration, if any 27 String outputRaw = MAPPER.writeValueAsString(doc); 28 // And re-parse as String, since JSON has quotes... 29 String output = MAPPER.readValue(outputRaw, String.class); 30 /* ... and finally, normalize to (close to) canonical XML 31 * output (single vs double quotes, xml declaration etc) 32 */ 33 assertEquals(SIMPLE_XML, normalizeOutput(output)); 34 } 35 testDeserializeNonNS()36 public void testDeserializeNonNS() throws Exception 37 { 38 for (int i = 0; i < 2; ++i) { 39 Document doc; 40 41 if (i == 0) { 42 // First, as Document: 43 doc = MAPPER.readValue(quote(SIMPLE_XML), Document.class); 44 } else { 45 // and then as plain Node (no difference) 46 Node node = MAPPER.readValue(quote(SIMPLE_XML), Node.class); 47 doc = (Document) node; 48 } 49 Element root = doc.getDocumentElement(); 50 assertNotNull(root); 51 // non-ns, simple... 52 assertEquals("root", root.getTagName()); 53 assertEquals("3", root.getAttribute("attr")); 54 assertEquals(1, root.getAttributes().getLength()); 55 NodeList nodes = root.getChildNodes(); 56 assertEquals(2, nodes.getLength()); 57 Element leaf = (Element) nodes.item(0); 58 assertEquals("leaf", leaf.getTagName()); 59 assertEquals(0, leaf.getAttributes().getLength()); 60 //"<root attr='3'><leaf>Rock & Roll!</leaf><?proc instr?></root>"; 61 ProcessingInstruction pi = (ProcessingInstruction) nodes.item(1); 62 assertEquals("proc", pi.getTarget()); 63 assertEquals("instr", pi.getData()); 64 } 65 } 66 testDeserializeNS()67 public void testDeserializeNS() throws Exception 68 { 69 Document doc = MAPPER.readValue(quote(SIMPLE_XML_NS), Document.class); 70 Element root = doc.getDocumentElement(); 71 assertNotNull(root); 72 assertEquals("root", root.getTagName()); 73 // Not sure if it ought to be "" or null... 74 String uri = root.getNamespaceURI(); 75 assertTrue((uri == null) || "".equals(uri)); 76 // no child nodes: 77 assertEquals(0, root.getChildNodes().getLength()); 78 // DOM is weird, includes ns decls as attributes... 79 assertEquals(2, root.getAttributes().getLength()); 80 assertEquals("abc", root.getAttributeNS("http://foo", "attr")); 81 } 82 83 /* 84 /********************************************************** 85 /* Helper methods 86 /********************************************************** 87 */ 88 normalizeOutput(String output)89 protected static String normalizeOutput(String output) 90 { 91 // XML declaration to get rid of? 92 output = output.trim(); 93 if (output.startsWith("<?xml")) { 94 // can find closing '>' of xml decl... 95 output = output.substring(output.indexOf('>')+1).trim(); 96 } 97 // And replace double quotes with single-quotes... 98 return output.replace('"', '\''); 99 } 100 } 101