2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.transform.dom;
10 import com.google.common.collect.LinkedListMultimap;
11 import com.google.common.collect.Lists;
13 import java.util.HashMap;
14 import java.util.List;
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.DerivedType;
35 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.NamedNodeMap;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
41 public final class DomUtils {
46 public static XmlCodecProvider defaultValueCodecProvider() {
47 return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
50 public static Object parseXmlValue(final Element xml, final XmlCodecProvider codecProvider, final TypeDefinition<?> type) {
51 final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(type);
53 String text = xml.getTextContent();
58 value = codec.deserialize(text);
66 public static void serializeXmlValue(final Element element, final TypeDefinition<? extends TypeDefinition<?>> type, final XmlCodecProvider codecProvider, final Object value) {
68 final XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(new DOMResult(element));
69 XmlStreamUtils.create(codecProvider).writeValue(writer, type, value);
70 } catch (final XMLStreamException e) {
71 throw new IllegalStateException("XML encoding failed", e);
75 public static LinkedListMultimap<QName, Element> mapChildElementsForSingletonNode(final Element node) {
76 final List<Element> childNodesCollection = Lists.newArrayList();
77 final NodeList childNodes = node.getChildNodes();
78 for (int i = 0; i < childNodes.getLength(); i++) {
79 if (childNodes.item(i) instanceof Element) {
80 childNodesCollection.add((Element) childNodes.item(i));
84 return mapChildElements(childNodesCollection);
87 public static LinkedListMultimap<QName, Element> mapChildElements(final Iterable<Element> childNodesCollection) {
88 final LinkedListMultimap<QName, Element> mappedChildElements = LinkedListMultimap.create();
90 for (final Element element : childNodesCollection) {
91 final QName childQName = XmlDocumentUtils.qNameFromElement(element);
92 mappedChildElements.put(childQName, element);
95 return mappedChildElements;
99 public static Map<QName, String> toAttributes(final NamedNodeMap xmlAttributes) {
100 final Map<QName, String> attributes = new HashMap<>();
102 for (int i = 0; i < xmlAttributes.getLength(); i++) {
103 final Node node = xmlAttributes.item(i);
104 String namespace = node.getNamespaceURI();
105 if (namespace == null) {
109 // Skip namespace definitions
110 if (namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
114 final QName qName = new QName(URI.create(namespace), node.getLocalName());
115 attributes.put(qName, node.getNodeValue());
120 public static Object parseXmlValue(final Element xml, final XmlCodecProvider codecProvider, final DataSchemaNode schema, final TypeDefinition<?> type, final SchemaContext schemaCtx) {
121 TypeDefinition<?> baseType = DerivedType.from(type);
123 String text = xml.getTextContent();
127 if (baseType instanceof LeafrefTypeDefinition) {
128 final LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) baseType;
129 baseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypeDefinition, schemaCtx, schema);
130 value = parseXmlValue(xml, codecProvider, schema, baseType, schemaCtx);
131 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
132 value = InstanceIdentifierForXmlCodec.deserialize(xml, schemaCtx);
133 } else if (baseType instanceof IdentityrefTypeDefinition) {
134 value = InstanceIdentifierForXmlCodec.toIdentity(text, xml, schemaCtx);
136 value = parseXmlValue(xml, codecProvider, type);