BUG-865: eliminate ExtendedType and DerivedType
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / dom / DomUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.transform.dom;
9
10 import com.google.common.collect.LinkedListMultimap;
11 import com.google.common.collect.Lists;
12 import java.net.URI;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import javax.xml.XMLConstants;
17 import javax.xml.stream.XMLOutputFactory;
18 import javax.xml.stream.XMLStreamException;
19 import javax.xml.stream.XMLStreamWriter;
20 import javax.xml.transform.dom.DOMResult;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.xml.InstanceIdentifierForXmlCodec;
24 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
25 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
26 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlStreamUtils;
27 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.NamedNodeMap;
37 import org.w3c.dom.Node;
38 import org.w3c.dom.NodeList;
39
40 public final class DomUtils {
41
42     private DomUtils() {
43     }
44
45     public static XmlCodecProvider defaultValueCodecProvider() {
46         return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
47     }
48
49     public static Object parseXmlValue(final Element xml, final XmlCodecProvider codecProvider, final TypeDefinition<?> type) {
50         final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(type);
51
52         String text = xml.getTextContent();
53         text = text.trim();
54
55         Object value;
56         if (codec != null) {
57             value = codec.deserialize(text);
58         } else {
59             value = text;
60         }
61
62         return value;
63     }
64
65     public static void serializeXmlValue(final Element element, final TypeDefinition<? extends TypeDefinition<?>> type, final XmlCodecProvider codecProvider, final Object value) {
66         try {
67             final XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(new DOMResult(element));
68             XmlStreamUtils.create(codecProvider).writeValue(writer, type, value);
69         } catch (final XMLStreamException e) {
70             throw new IllegalStateException("XML encoding failed", e);
71         }
72     }
73
74     public static LinkedListMultimap<QName, Element> mapChildElementsForSingletonNode(final Element node) {
75         final List<Element> childNodesCollection = Lists.newArrayList();
76         final NodeList childNodes = node.getChildNodes();
77         for (int i = 0; i < childNodes.getLength(); i++) {
78             if (childNodes.item(i) instanceof Element) {
79                 childNodesCollection.add((Element) childNodes.item(i));
80             }
81         }
82
83         return mapChildElements(childNodesCollection);
84     }
85
86     public static LinkedListMultimap<QName, Element> mapChildElements(final Iterable<Element> childNodesCollection) {
87         final LinkedListMultimap<QName, Element> mappedChildElements = LinkedListMultimap.create();
88
89         for (final Element element : childNodesCollection) {
90             final QName childQName = XmlDocumentUtils.qNameFromElement(element);
91             mappedChildElements.put(childQName, element);
92         }
93
94         return mappedChildElements;
95     }
96
97
98     public static Map<QName, String> toAttributes(final NamedNodeMap xmlAttributes) {
99         final Map<QName, String> attributes = new HashMap<>();
100
101         for (int i = 0; i < xmlAttributes.getLength(); i++) {
102             final Node node = xmlAttributes.item(i);
103             String namespace = node.getNamespaceURI();
104             if (namespace == null) {
105                 namespace = "";
106             }
107
108             // Skip namespace definitions
109             if (namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
110                 continue;
111             }
112
113             final QName qName = new QName(URI.create(namespace), node.getLocalName());
114             attributes.put(qName, node.getNodeValue());
115         }
116         return attributes;
117     }
118
119     public static Object parseXmlValue(final Element xml, final XmlCodecProvider codecProvider, final DataSchemaNode schema, final TypeDefinition<?> type, final SchemaContext schemaCtx) {
120         String text = xml.getTextContent();
121         text = text.trim();
122         final Object value;
123
124         if (type instanceof LeafrefTypeDefinition) {
125             final LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) type;
126             TypeDefinition<?> baseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypeDefinition, schemaCtx, schema);
127             value = parseXmlValue(xml, codecProvider, schema, baseType, schemaCtx);
128         } else if (type instanceof InstanceIdentifierTypeDefinition) {
129             value = InstanceIdentifierForXmlCodec.deserialize(xml, schemaCtx);
130         } else if (type instanceof IdentityrefTypeDefinition) {
131             value = InstanceIdentifierForXmlCodec.toIdentity(text, xml, schemaCtx);
132         } else {
133             value = parseXmlValue(xml, codecProvider, type);
134         }
135
136         return value;
137     }
138 }