| 1 |
package com.fasterxml.jackson.module.jaxb.deser; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.util.Iterator; |
| 5 |
|
| 6 |
import javax.xml.parsers.DocumentBuilder; |
| 7 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 8 |
import javax.xml.parsers.ParserConfigurationException; |
| 9 |
|
| 10 |
import org.w3c.dom.Document; |
| 11 |
import org.w3c.dom.Element; |
| 12 |
|
| 13 |
import com.fasterxml.jackson.core.*; |
| 14 |
|
| 15 |
import com.fasterxml.jackson.databind.DeserializationContext; |
| 16 |
import com.fasterxml.jackson.databind.JsonMappingException; |
| 17 |
import com.fasterxml.jackson.databind.JsonNode; |
| 18 |
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 19 |
import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 |
|
| 21 |
/** |
| 22 |
* @author Ryan Heaton |
| 23 |
*/ |
| 24 |
public class DomElementJsonDeserializer |
| 25 |
extends StdDeserializer<Element> |
| 26 |
{ |
| 27 |
private static final long serialVersionUID = 1L; |
| 28 |
|
| 29 |
private final DocumentBuilder builder; |
| 30 |
|
| 31 |
public DomElementJsonDeserializer() |
| 32 |
{ |
| 33 |
super(Element.class); |
| 34 |
try { |
| 35 |
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance(); |
| 36 |
bf.setNamespaceAware(true); |
| 37 |
builder = bf.newDocumentBuilder(); |
| 38 |
} catch (ParserConfigurationException e) { |
| 39 |
throw new RuntimeException(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public DomElementJsonDeserializer(DocumentBuilder builder) |
| 44 |
{ |
| 45 |
super(Element.class); |
| 46 |
this.builder = builder; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public Element deserialize(JsonParser p, DeserializationContext ctxt) throws IOException |
| 51 |
{ |
| 52 |
Document document = builder.newDocument(); |
| 53 |
JsonNode n = p.readValueAsTree(); |
| 54 |
return fromNode(p, document, n); |
| 55 |
} |
| 56 |
|
| 57 |
protected Element fromNode(JsonParser p, Document document, JsonNode jsonNode) |
| 58 |
throws IOException |
| 59 |
{ |
| 60 |
String ns = jsonNode.get("namespace") != null ? jsonNode.get("namespace").asText() : null; |
| 61 |
String name = jsonNode.get("name") != null ? jsonNode.get("name").asText() : null; |
| 62 |
if (name == null) { |
| 63 |
throw JsonMappingException.from(p, "No name for DOM element was provided in the JSON object."); |
| 64 |
} |
| 65 |
Element element = document.createElementNS(ns, name); |
| 66 |
|
| 67 |
JsonNode attributesNode = jsonNode.get("attributes"); |
| 68 |
if (attributesNode != null && attributesNode instanceof ArrayNode) { |
| 69 |
Iterator<JsonNode> atts = attributesNode.elements(); |
| 70 |
while (atts.hasNext()) { |
| 71 |
JsonNode node = atts.next(); |
| 72 |
ns = node.get("namespace") != null ? node.get("namespace").asText() : null; |
| 73 |
name = node.get("name") != null ? node.get("name").asText() : null; |
| 74 |
String value = node.get("$") != null ? node.get("$").asText() : null; |
| 75 |
|
| 76 |
if (name != null) { |
| 77 |
element.setAttributeNS(ns, name, value); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
JsonNode childsNode = jsonNode.get("children"); |
| 83 |
if (childsNode != null && childsNode instanceof ArrayNode) { |
| 84 |
Iterator<JsonNode> els = childsNode.elements(); |
| 85 |
while (els.hasNext()) { |
| 86 |
JsonNode node = els.next(); |
| 87 |
name = node.get("name") != null ? node.get("name").asText() : null; |
| 88 |
String value = node.get("$") != null ? node.get("$").asText() : null; |
| 89 |
|
| 90 |
if (value != null) { |
| 91 |
element.appendChild(document.createTextNode(value)); |
| 92 |
} |
| 93 |
else if (name != null) { |
| 94 |
element.appendChild(fromNode(p, document, node)); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return element; |
| 100 |
} |
| 101 |
} |