1 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
5 import javax.activation.UnsupportedDataTypeException;
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException;
10 import org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
12 import org.opendaylight.yangtools.yang.data.api.Node;
13 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
14 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.YangNode;
25 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
31 import com.google.common.base.Preconditions;
33 public class XmlDocumentUtils {
35 private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
38 public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
39 return TypeDefinitionAwareCodec.from(baseType);
43 private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
45 public static Document toDocument(CompositeNode data, DataNodeContainer schema, XmlCodecProvider codecProvider)
46 throws UnsupportedDataTypeException {
47 Preconditions.checkNotNull(data);
48 Preconditions.checkNotNull(schema);
50 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
53 DocumentBuilder bob = dbf.newDocumentBuilder();
54 doc = bob.newDocument();
55 } catch (ParserConfigurationException e) {
59 if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
60 doc.appendChild(createXmlRootElement(doc, data, schema, codecProvider));
63 throw new UnsupportedDataTypeException(
64 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
68 private static Element createXmlRootElement(Document doc, Node<?> data, YangNode schema,
69 XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
70 QName dataType = data.getNodeType();
71 Element itemEl = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
72 if (data instanceof SimpleNode<?>) {
73 if (schema instanceof LeafListSchemaNode) {
74 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
75 (DataSchemaNode) schema, codecProvider);
76 } else if (schema instanceof LeafSchemaNode) {
77 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
78 (DataSchemaNode) schema, codecProvider);
80 Object value = data.getValue();
82 itemEl.setTextContent(String.valueOf(value));
85 } else { // CompositeNode
86 for (Node<?> child : ((CompositeNode) data).getChildren()) {
87 DataSchemaNode childSchema = null;
89 childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
90 if (logger.isDebugEnabled()) {
91 if (childSchema == null) {
92 logger.debug("Probably the data node \""
93 + ((child == null) ? "" : child.getNodeType().getLocalName())
94 + "\" is not conform to schema");
98 itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
104 public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
105 DataSchemaNode schema, XmlCodecProvider codecProvider) {
107 TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
109 if (baseType instanceof IdentityrefTypeDefinition) {
110 if (node.getValue() instanceof QName) {
111 QName value = (QName) node.getValue();
113 if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
114 prefix = value.getPrefix();
116 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
117 element.setTextContent(prefix + ":" + value.getLocalName());
119 logger.debug("Value of {}:{} is not instance of QName but is {}",
120 baseType.getQName().getNamespace(), //
121 baseType.getQName().getLocalName(), //
122 node.getValue().getClass());
123 element.setTextContent(String.valueOf(node.getValue()));
126 if (node.getValue() != null) {
128 String value = codecProvider.codecFor(baseType).serialize(node.getValue());
129 element.setTextContent(value);
130 } catch (ClassCastException e) {
131 element.setTextContent(String.valueOf(node.getValue()));
132 logger.error("Provided node did not have type required by mapping. Using stream instead. {}",e);
138 public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
139 TypeDefinition<?> superType = type;
140 while (superType.getBaseType() != null) {
141 superType = superType.getBaseType();
146 private static final DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
147 if (dataSchemaNode != null && node != null) {
148 for (DataSchemaNode dsn : dataSchemaNode) {
149 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
151 } else if (dsn instanceof ChoiceNode) {
152 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
153 DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
154 if (foundDsn != null) {
164 public static final XmlCodecProvider defaultValueCodecProvider() {
165 return DEFAULT_XML_VALUE_CODEC_PROVIDER;