1 package org.opendaylight.controller.sal.rest.impl;
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.model.api.ChoiceCaseNode;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.YangNode;
24 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
28 import com.google.common.base.Preconditions;
30 public class XmlMapper {
32 public Document write(CompositeNode data, DataNodeContainer schema) throws UnsupportedDataTypeException {
33 Preconditions.checkNotNull(data);
34 Preconditions.checkNotNull(schema);
36 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
39 DocumentBuilder bob = dbf.newDocumentBuilder();
40 doc = bob.newDocument();
41 } catch (ParserConfigurationException e) {
45 if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
46 doc.appendChild(translateToXmlAndReturnRootElement(doc, data, schema));
49 throw new UnsupportedDataTypeException(
50 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
54 private Element translateToXmlAndReturnRootElement(Document doc, Node<?> data, YangNode schema)
55 throws UnsupportedDataTypeException {
56 QName dataType = data.getNodeType();
57 Element itemEl = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
59 if (data instanceof SimpleNode<?>) {
60 if (schema instanceof LeafListSchemaNode) {
61 writeValueOfNodeByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType());
62 } else if (schema instanceof LeafSchemaNode) {
63 writeValueOfNodeByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType());
65 Object value = data.getValue();
67 itemEl.setTextContent(String.valueOf(value));
70 } else { // CompositeNode
71 for (Node<?> child : ((CompositeNode) data).getChildren()) {
72 DataSchemaNode childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
73 if (childSchema == null) {
74 throw new UnsupportedDataTypeException("Probably the data node \""
75 + child.getNodeType().getLocalName() + "\" is not conform to schema");
77 itemEl.appendChild(translateToXmlAndReturnRootElement(doc, child, childSchema));
83 private void writeValueOfNodeByType(Element element, SimpleNode<?> node, TypeDefinition<?> type) {
85 TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
87 if (baseType instanceof IdentityrefTypeDefinition && node.getValue() instanceof QName) {
88 QName value = (QName) node.getValue();
89 element.setAttribute("xmlns:x", value.getNamespace().toString());
90 element.setTextContent("x:" + value.getLocalName());
92 Object value = node.getValue();
94 element.setTextContent(String.valueOf(value));
99 private DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
100 for (DataSchemaNode dsn : dataSchemaNode) {
101 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
103 } else if (dsn instanceof ChoiceNode) {
104 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
105 DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
106 if (foundDsn != null) {
115 private TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
116 return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type;