Merge "Revert "Added support for parsing submodules & added dependency utility parser""
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlDocumentUtils.java
1 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
2
3 import static com.google.common.base.Preconditions.checkState;
4
5 import java.net.URI;
6 import java.util.List;
7 import java.util.Set;
8
9 import javax.activation.UnsupportedDataTypeException;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
16 import org.opendaylight.yangtools.yang.data.api.Node;
17 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
18 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
19 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
20 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
21 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SimpleValueSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.NodeList;
39
40 import com.google.common.base.Function;
41 import com.google.common.base.Objects;
42 import com.google.common.base.Optional;
43 import com.google.common.base.Preconditions;
44 import com.google.common.base.Strings;
45 import com.google.common.collect.ImmutableList;
46
47 public class XmlDocumentUtils {
48
49     private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
50
51         @Override
52         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
53             return TypeDefinitionAwareCodec.from(baseType);
54         }
55     };
56
57     private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
58
59     public static Document toDocument(CompositeNode data, DataNodeContainer schema, XmlCodecProvider codecProvider)
60             throws UnsupportedDataTypeException {
61         Preconditions.checkNotNull(data);
62         Preconditions.checkNotNull(schema);
63
64         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
65         Document doc = null;
66         try {
67             DocumentBuilder bob = dbf.newDocumentBuilder();
68             doc = bob.newDocument();
69         } catch (ParserConfigurationException e) {
70             return null;
71         }
72
73         if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
74             doc.appendChild(createXmlRootElement(doc, data, (SchemaNode) schema, codecProvider));
75             return doc;
76         } else {
77             throw new UnsupportedDataTypeException(
78                     "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
79         }
80     }
81
82     public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
83             Optional<XmlCodecProvider> codecProvider) {
84         if (schema.isPresent()) {
85             return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
86         }
87         return toDomNode(xmlElement);
88     }
89
90     public static CompositeNode fromElement(Element xmlElement) {
91         CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
92         node.setQName(qNameFromElement(xmlElement));
93
94         return node.toInstance();
95     }
96
97     private static QName qNameFromElement(Element xmlElement) {
98         String namespace = xmlElement.getNamespaceURI();
99         String localName = xmlElement.getLocalName();
100         return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
101     }
102
103     private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
104         checkQName(xmlElement, schema.getQName());
105         if (schema instanceof DataNodeContainer) {
106             return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider);
107         } else if (schema instanceof SimpleValueSchemaNode) {
108             return toSimpleNodeWithType(xmlElement, (SimpleValueSchemaNode) schema, codecProvider);
109         }
110         return null;
111     }
112
113     private static Node<?> toSimpleNodeWithType(Element xmlElement, SimpleValueSchemaNode schema,
114             XmlCodecProvider codecProvider) {
115         TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
116         String text = xmlElement.getTextContent();
117         Object value = codec.deserialize(text);
118         return new SimpleNodeTOImpl<Object>(schema.getQName(), null, value);
119     }
120
121     private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
122             XmlCodecProvider codecProvider) {
123         List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
124         return new ImmutableCompositeNode(qName, values );
125     }
126
127     private static void checkQName(Element xmlElement, QName qName) {
128         checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
129         checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
130     }
131
132     private static Element createXmlRootElement(Document doc, Node<?> data, SchemaNode schema,
133             XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
134         QName dataType = data.getNodeType();
135         Element itemEl = createElementFor(doc, dataType);
136         if (data instanceof SimpleNode<?>) {
137             if (schema instanceof LeafListSchemaNode) {
138                 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
139                         (DataSchemaNode) schema, codecProvider);
140             } else if (schema instanceof LeafSchemaNode) {
141                 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
142                         (DataSchemaNode) schema, codecProvider);
143             } else {
144                 Object value = data.getValue();
145                 if (value != null) {
146                     itemEl.setTextContent(String.valueOf(value));
147                 }
148             }
149         } else { // CompositeNode
150             for (Node<?> child : ((CompositeNode) data).getChildren()) {
151                 DataSchemaNode childSchema = null;
152                 if (schema != null) {
153                     childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
154                     if (logger.isDebugEnabled()) {
155                         if (childSchema == null) {
156                             logger.debug("Probably the data node \""
157                                     + ((child == null) ? "" : child.getNodeType().getLocalName())
158                                     + "\" is not conform to schema");
159                         }
160                     }
161                 }
162                 itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
163             }
164         }
165         return itemEl;
166     }
167
168     private static Element createElementFor(Document doc, QName dataType) {
169         if (dataType.getNamespace() != null) {
170             return doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
171         } else {
172             return doc.createElementNS(null, dataType.getLocalName());
173         }
174     }
175
176     public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
177             DataSchemaNode schema, XmlCodecProvider codecProvider) {
178
179         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
180
181         if (baseType instanceof IdentityrefTypeDefinition) {
182             if (node.getValue() instanceof QName) {
183                 QName value = (QName) node.getValue();
184                 String prefix = "x";
185                 if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
186                     prefix = value.getPrefix();
187                 }
188                 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
189                 element.setTextContent(prefix + ":" + value.getLocalName());
190             } else {
191                 logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(), //
192                         baseType.getQName().getLocalName(), //
193                         node.getValue().getClass());
194                 element.setTextContent(String.valueOf(node.getValue()));
195             }
196         } else {
197             if (node.getValue() != null) {
198                 try {
199                     String value = codecProvider.codecFor(baseType).serialize(node.getValue());
200                     element.setTextContent(value);
201                 } catch (ClassCastException e) {
202                     element.setTextContent(String.valueOf(node.getValue()));
203                     logger.error("Provided node did not have type required by mapping. Using stream instead. {}", e);
204                 }
205             }
206         }
207     }
208
209     public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
210         TypeDefinition<?> superType = type;
211         while (superType.getBaseType() != null) {
212             superType = superType.getBaseType();
213         }
214         return superType;
215     }
216
217     private static final DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
218         if (dataSchemaNode != null && node != null) {
219             for (DataSchemaNode dsn : dataSchemaNode) {
220                 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
221                     return dsn;
222                 } else if (dsn instanceof ChoiceNode) {
223                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
224                         DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
225                         if (foundDsn != null) {
226                             return foundDsn;
227                         }
228                     }
229                 }
230             }
231         }
232         return null;
233     }
234
235     private static final Optional<DataSchemaNode> findFirstSchema(QName qname, Set<DataSchemaNode> dataSchemaNode) {
236         if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
237             for (DataSchemaNode dsn : dataSchemaNode) {
238                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
239                     return Optional.<DataSchemaNode> of(dsn);
240                 } else if (dsn instanceof ChoiceNode) {
241                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
242                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
243                         if (foundDsn != null) {
244                             return foundDsn;
245                         }
246                     }
247                 }
248             }
249         }
250         return Optional.absent();
251     }
252
253     public static final XmlCodecProvider defaultValueCodecProvider() {
254         return DEFAULT_XML_VALUE_CODEC_PROVIDER;
255     }
256
257     public static Node<?> toDomNode(Document doc) {
258         return toDomNode(doc.getDocumentElement());
259     }
260
261     private static Node<?> toDomNode(Element element) {
262         QName qname = qNameFromElement(element);
263
264         ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
265         NodeList nodes = element.getChildNodes();
266         boolean isSimpleObject = true;
267         String value = null;
268         for (int i = 0; i < nodes.getLength(); i++) {
269             org.w3c.dom.Node child = nodes.item(i);
270             if (child instanceof Element) {
271                 isSimpleObject = false;
272                 values.add(toDomNode((Element) child));
273             }
274             if (isSimpleObject && child instanceof org.w3c.dom.Text) {
275                 value = element.getTextContent();
276                 if (!Strings.isNullOrEmpty(value)) {
277                     isSimpleObject = true;
278                 }
279             }
280         }
281         if (isSimpleObject) {
282             return new SimpleNodeTOImpl<>(qname, null, value);
283         }
284         return new ImmutableCompositeNode(qname, values.build());
285     }
286
287     public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
288         return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
289
290             @Override
291             public Optional<Node<?>> apply(Element input) {
292                 if (context.isPresent()) {
293                     QName partialQName = qNameFromElement(input);
294                     Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
295                     if (schemaNode.isPresent()) {
296                         return Optional.<Node<?>> fromNullable(//
297                                 toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
298                     }
299                 }
300                 return Optional.<Node<?>> fromNullable(toDomNode(element));
301             }
302
303         });
304
305     }
306
307     private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
308         ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
309         for (int i = 0; i < nodes.getLength(); i++) {
310             org.w3c.dom.Node child = nodes.item(i);
311             if(child instanceof Element) {
312                 Optional<T> result = forBody.apply((Element) child);
313                 if(result.isPresent()) {
314                     ret.add(result.get());
315                 }
316             }
317         }
318         return ret.build();
319     }
320
321 }