/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl.schema.transform.dom; import java.net.URI; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.XMLConstants; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider; import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.Lists; public class DomUtils { private DomUtils() { } private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() { @Override public TypeDefinitionAwareCodec> codecFor(TypeDefinition baseType) { return TypeDefinitionAwareCodec.from(baseType); } }; public static XmlCodecProvider defaultValueCodecProvider() { return DEFAULT_XML_VALUE_CODEC_PROVIDER; } public static Object parseXmlValue(Element xml, XmlCodecProvider codecProvider, TypeDefinition type) { TypeDefinitionAwareCodec> codec = codecProvider.codecFor(type); String text = xml.getTextContent(); text = text.trim(); Object value; if (codec != null) { value = codec.deserialize(text); } else { value = text; } return value; } public static void serializeXmlValue(Element itemEl, TypeDefinition> type, XmlCodecProvider codecProvider, Object value) { XmlDocumentUtils.writeValueByType(itemEl, type, codecProvider, value); } public static LinkedListMultimap mapChildElementsForSingletonNode(Element node) { List childNodesCollection = Lists.newArrayList(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if(childNodes.item(i) instanceof Element) { childNodesCollection.add((Element) childNodes.item(i)); } } return mapChildElements(childNodesCollection); } public static LinkedListMultimap mapChildElements(Iterable childNodesCollection) { LinkedListMultimap mappedChildElements = LinkedListMultimap.create(); for (Element element : childNodesCollection) { QName childQName = XmlDocumentUtils.qNameFromElement(element); mappedChildElements.put(childQName, element); } return mappedChildElements; } public static Map toAttributes(NamedNodeMap xmlAttributes) { Map attributes = new HashMap<>(); for (int i = 0; i < xmlAttributes.getLength(); i++) { Node node = xmlAttributes.item(i); String namespace = node.getNamespaceURI(); if (namespace == null) { namespace = ""; } // Skip namespace definitions if(namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { continue; } QName qName = new QName(URI.create(namespace), node.getLocalName()); attributes.put(qName, node.getNodeValue()); } return attributes; } }