-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.yang.data.impl;\r
-\r
-import java.util.AbstractMap.SimpleEntry;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Stack;\r
-import java.util.Vector;\r
-\r
-import javax.xml.parsers.DocumentBuilder;\r
-import javax.xml.parsers.DocumentBuilderFactory;\r
-import javax.xml.parsers.ParserConfigurationException;\r
-import javax.xml.xpath.XPath;\r
-import javax.xml.xpath.XPathConstants;\r
-import javax.xml.xpath.XPathExpression;\r
-import javax.xml.xpath.XPathExpressionException;\r
-import javax.xml.xpath.XPathFactory;\r
-\r
-import org.opendaylight.yangtools.yang.common.QName;\r
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;\r
-import org.opendaylight.yangtools.yang.data.api.ModifyAction;\r
-import org.opendaylight.yangtools.yang.data.api.Node;\r
-import org.opendaylight.yangtools.yang.data.api.NodeModification;\r
-import org.opendaylight.yangtools.yang.data.api.SimpleNode;\r
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;\r
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;\r
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;\r
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-import org.w3c.dom.Element;\r
-\r
-import com.google.common.base.Joiner;\r
-import com.google.common.collect.Lists;\r
-\r
-\r
-/**\r
- * @author michal.rehak\r
- *\r
- */\r
-public abstract class NodeUtils {\r
-\r
- private static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class);\r
-\r
- /**\r
- *\r
- */\r
- private static final String USER_KEY_NODE = "node";\r
-\r
- /**\r
- * @param node\r
- * @return node path up till root node\r
- */\r
- public static String buildPath(Node<?> node) {\r
- Vector<String> breadCrumbs = new Vector<>();\r
- Node<?> tmpNode = node;\r
- while (tmpNode != null) {\r
- breadCrumbs.insertElementAt(tmpNode.getNodeType().getLocalName(), 0);\r
- tmpNode = tmpNode.getParent();\r
- }\r
-\r
- return Joiner.on(".").join(breadCrumbs);\r
- }\r
-\r
-\r
- /**\r
- * @param treeRootNode\r
- * @return dom tree, containing same node structure, yang nodes are associated\r
- * to dom nodes as user data\r
- */\r
+/*
+ * 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;
+
+import java.util.AbstractMap.SimpleEntry;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import com.google.common.collect.Maps;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.NodeModification;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Element;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.Lists;
+
+/**
+ * @author michal.rehak
+ *
+ */
+public abstract class NodeUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class);
+
+ /**
+ *
+ */
+ private static final String USER_KEY_NODE = "node";
+
+ /**
+ * @param node
+ * @return node path up till root node
+ */
+ public static String buildPath(Node<?> node) {
+ List<String> breadCrumbs = new ArrayList<>();
+ Node<?> tmpNode = node;
+ while (tmpNode != null) {
+ breadCrumbs.add(0, tmpNode.getNodeType().getLocalName());
+ tmpNode = tmpNode.getParent();
+ }
+
+ return Joiner.on(".").join(breadCrumbs);
+ }
+
+ /**
+ * @param treeRootNode
+ * @return dom tree, containing same node structure, yang nodes are
+ * associated to dom nodes as user data
+ */
public static org.w3c.dom.Document buildShadowDomTree(CompositeNode treeRootNode) {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r
- org.w3c.dom.Document doc = null;\r
- try {\r
- DocumentBuilder bob = dbf.newDocumentBuilder();\r
- doc = bob.newDocument();\r
- } catch (ParserConfigurationException e) {\r
- LOG.error("documentBuilder problem", e);\r
- return null;\r
- }\r
-\r
-\r
- Stack<SimpleEntry<org.w3c.dom.Node, Node<?>>> jobQueue = new Stack<>();\r
- jobQueue.push(new SimpleEntry<org.w3c.dom.Node, Node<?>>(doc, treeRootNode));\r
-\r
- while (!jobQueue.isEmpty()) {\r
- SimpleEntry<org.w3c.dom.Node, Node<?>> job = jobQueue.pop();\r
- org.w3c.dom.Node jointPlace = job.getKey();\r
- Node<?> item = job.getValue();\r
- QName nodeType = item.getNodeType();\r
- Element itemEl = doc.createElementNS(nodeType.getNamespace().toString(),\r
- item.getNodeType().getLocalName());\r
- itemEl.setUserData(USER_KEY_NODE, item, null);\r
- if (item instanceof SimpleNode<?>) {\r
- Object value = ((SimpleNode<?>) item).getValue();\r
- itemEl.setTextContent(String.valueOf(value));\r
- //itemEl.setAttribute("type", value.getClass().getSimpleName());\r
- }\r
- if (item instanceof NodeModification) {\r
- ModifyAction modificationAction = ((NodeModification) item).getModificationAction();\r
- if (modificationAction != null) {\r
- itemEl.setAttribute("modifyAction", modificationAction.toString());\r
- }\r
- }\r
-\r
- jointPlace.appendChild(itemEl);\r
-\r
- if (item instanceof CompositeNode) {\r
- for (Node<?> child : ((CompositeNode) item).getChildren()) {\r
- jobQueue.push(new SimpleEntry<org.w3c.dom.Node, Node<?>>(itemEl, child));\r
- }\r
- }\r
- }\r
-\r
- return doc;\r
- }\r
-\r
- /**\r
- * @param doc\r
- * @param xpathEx\r
- * @return user data value on found node\r
- * @throws XPathExpressionException\r
- */\r
- @SuppressWarnings("unchecked")\r
- public static <T> T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx)\r
- throws XPathExpressionException {\r
- T userNode = null;\r
- XPathFactory xPathfactory = XPathFactory.newInstance();\r
- XPath xpath = xPathfactory.newXPath();\r
- XPathExpression expr = xpath.compile(xpathEx);\r
-\r
- org.w3c.dom.Node result = (org.w3c.dom.Node) expr.evaluate(doc, XPathConstants.NODE);\r
- if (result != null) {\r
- userNode = (T) result.getUserData(USER_KEY_NODE);\r
- }\r
-\r
- return userNode;\r
- }\r
-\r
-\r
- /**\r
- * build NodeMap, where key = qName; value = node\r
- *\r
- * @param value\r
- * @return map of children, where key = qName and value is list of children groupped by qName\r
- */\r
- public static Map<QName, List<Node<?>>> buildNodeMap(List<Node<?>> value) {\r
- Map<QName, List<Node<?>>> nodeMapTmp = new HashMap<>();\r
- if (value == null || value.isEmpty()) {\r
- throw new IllegalStateException(\r
- "nodeList should not be null or empty");\r
- }\r
- for (Node<?> node : value) {\r
- List<Node<?>> qList = nodeMapTmp.get(node.getNodeType());\r
- if (qList == null) {\r
- qList = new ArrayList<>();\r
- nodeMapTmp.put(node.getNodeType(), qList);\r
- }\r
- qList.add(node);\r
- }\r
- return nodeMapTmp;\r
- }\r
-\r
-\r
- /**\r
- * @param context\r
- * @return map of lists, where key = path; value = {@link DataSchemaNode}\r
- */\r
- public static Map<String, ListSchemaNode> buildMapOfListNodes(\r
- SchemaContext context) {\r
- Map<String, ListSchemaNode> mapOfLists = new HashMap<>();\r
-\r
- Stack<DataSchemaNode> jobQueue = new Stack<>();\r
- jobQueue.addAll(context.getDataDefinitions());\r
-\r
- while (!jobQueue.isEmpty()) {\r
- DataSchemaNode dataSchema = jobQueue.pop();\r
- if (dataSchema instanceof ListSchemaNode) {\r
- mapOfLists.put(schemaPathToPath(dataSchema.getPath().getPath()), (ListSchemaNode) dataSchema);\r
- }\r
-\r
- if (dataSchema instanceof DataNodeContainer) {\r
- jobQueue.addAll(((DataNodeContainer) dataSchema).getChildNodes());\r
- }\r
- }\r
-\r
- return mapOfLists;\r
- }\r
-\r
- /**\r
- * @param path\r
- * @return\r
- */\r
- private static String schemaPathToPath(List<QName> qNamesPath) {\r
- List<String> pathSeed = new ArrayList<>();\r
- for (QName qNameItem : qNamesPath) {\r
- pathSeed.add(qNameItem.getLocalName());\r
- }\r
- return Joiner.on(".").join(pathSeed);\r
- }\r
-\r
- /**\r
- * add given node to it's parent's list of children\r
- * @param newNode\r
- */\r
- public static void fixParentRelation(Node<?> newNode) {\r
- if (newNode.getParent() != null) {\r
- List<Node<?>> siblings = newNode.getParent().getChildren();\r
- if (!siblings.contains(newNode)) {\r
- siblings.add(newNode);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * crawl all children of given node and assign it as their parent\r
- * @param parentNode\r
- */\r
- public static void fixChildrenRelation(CompositeNode parentNode) {\r
- if (parentNode.getChildren() != null) {\r
- for (Node<?> child : parentNode.getChildren()) {\r
- if (child instanceof AbstractNodeTO<?>) {\r
- ((AbstractNodeTO<?>) child).setParent(parentNode);\r
- }\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * @param keys\r
- * @param dataMap\r
- * @return list of values of map, found by given keys\r
- */\r
- public static <T, K> List<K> collectMapValues(List<T> keys,\r
- Map<T, K> dataMap) {\r
- List<K> valueSubList = new ArrayList<>();\r
- for (T key : keys) {\r
- valueSubList.add(dataMap.get(key));\r
- }\r
-\r
- return valueSubList;\r
- }\r
-\r
- /**\r
- * @param nodes\r
- * @return list of children in list of appropriate type\r
- */\r
- public static List<Node<?>> buildChildrenList(Node<?>...nodes) {\r
- return Lists.newArrayList(nodes);\r
- }\r
-\r
-}\r
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ org.w3c.dom.Document doc = null;
+ try {
+ DocumentBuilder bob = dbf.newDocumentBuilder();
+ doc = bob.newDocument();
+ } catch (ParserConfigurationException e) {
+ LOG.error("documentBuilder problem", e);
+ return null;
+ }
+
+ Stack<SimpleEntry<org.w3c.dom.Node, Node<?>>> jobQueue = new Stack<>();
+ jobQueue.push(new SimpleEntry<org.w3c.dom.Node, Node<?>>(doc, treeRootNode));
+
+ while (!jobQueue.isEmpty()) {
+ SimpleEntry<org.w3c.dom.Node, Node<?>> job = jobQueue.pop();
+ org.w3c.dom.Node jointPlace = job.getKey();
+ Node<?> item = job.getValue();
+ QName nodeType = item.getNodeType();
+ Element itemEl = doc.createElementNS(nodeType.getNamespace().toString(), item.getNodeType().getLocalName());
+ itemEl.setUserData(USER_KEY_NODE, item, null);
+ if (item instanceof SimpleNode<?>) {
+ Object value = ((SimpleNode<?>) item).getValue();
+ if(value != null) {
+ itemEl.setTextContent(String.valueOf(value));
+ }
+ }
+ if (item instanceof NodeModification) {
+ ModifyAction modificationAction = ((NodeModification) item).getModificationAction();
+ if (modificationAction != null) {
+ itemEl.setAttribute("modifyAction", modificationAction.toString());
+ }
+ }
+
+ jointPlace.appendChild(itemEl);
+
+ if (item instanceof CompositeNode) {
+ for (Node<?> child : ((CompositeNode) item).getChildren()) {
+ jobQueue.push(new SimpleEntry<org.w3c.dom.Node, Node<?>>(itemEl, child));
+ }
+ }
+ }
+
+ return doc;
+ }
+
+ /**
+ * @param doc
+ * @param xpathEx
+ * @return user data value on found node
+ * @throws XPathExpressionException
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx) throws XPathExpressionException {
+ T userNode = null;
+ XPathFactory xPathfactory = XPathFactory.newInstance();
+ XPath xpath = xPathfactory.newXPath();
+ XPathExpression expr = xpath.compile(xpathEx);
+
+ org.w3c.dom.Node result = (org.w3c.dom.Node) expr.evaluate(doc, XPathConstants.NODE);
+ if (result != null) {
+ userNode = (T) result.getUserData(USER_KEY_NODE);
+ }
+
+ return userNode;
+ }
+
+ /**
+ * build NodeMap, where key = qName; value = node
+ *
+ * @param value
+ * @return map of children, where key = qName and value is list of children
+ * groupped by qName
+ */
+ public static Map<QName, List<Node<?>>> buildNodeMap(List<Node<?>> value) {
+ Map<QName, List<Node<?>>> nodeMapTmp = Maps.newLinkedHashMap();
+ if (value == null) {
+ throw new IllegalStateException("nodeList should not be null or empty");
+ }
+ for (Node<?> node : value) {
+ List<Node<?>> qList = nodeMapTmp.get(node.getNodeType());
+ if (qList == null) {
+ qList = new ArrayList<>();
+ nodeMapTmp.put(node.getNodeType(), qList);
+ }
+ qList.add(node);
+ }
+ return nodeMapTmp;
+ }
+
+ /**
+ * @param context
+ * @return map of lists, where key = path; value = {@link DataSchemaNode}
+ */
+ public static Map<String, ListSchemaNode> buildMapOfListNodes(SchemaContext context) {
+ Map<String, ListSchemaNode> mapOfLists = new HashMap<>();
+
+ Stack<DataSchemaNode> jobQueue = new Stack<>();
+ jobQueue.addAll(context.getDataDefinitions());
+
+ while (!jobQueue.isEmpty()) {
+ DataSchemaNode dataSchema = jobQueue.pop();
+ if (dataSchema instanceof ListSchemaNode) {
+ mapOfLists.put(schemaPathToPath(dataSchema.getPath().getPath()), (ListSchemaNode) dataSchema);
+ }
+
+ if (dataSchema instanceof DataNodeContainer) {
+ jobQueue.addAll(((DataNodeContainer) dataSchema).getChildNodes());
+ }
+ }
+
+ return mapOfLists;
+ }
+
+ /**
+ * @param qNamesPath
+ * @return path
+ */
+ private static String schemaPathToPath(List<QName> qNamesPath) {
+ List<String> pathSeed = new ArrayList<>();
+ for (QName qNameItem : qNamesPath) {
+ pathSeed.add(qNameItem.getLocalName());
+ }
+ return Joiner.on(".").join(pathSeed);
+ }
+
+ /**
+ * add given node to it's parent's list of children
+ *
+ * @param newNode
+ */
+ public static void fixParentRelation(Node<?> newNode) {
+ if (newNode.getParent() != null) {
+ List<Node<?>> siblings = newNode.getParent().getChildren();
+ if (!siblings.contains(newNode)) {
+ siblings.add(newNode);
+ }
+ }
+ }
+
+ /**
+ * crawl all children of given node and assign it as their parent
+ *
+ * @param parentNode
+ */
+ public static void fixChildrenRelation(CompositeNode parentNode) {
+ if (parentNode.getChildren() != null) {
+ for (Node<?> child : parentNode.getChildren()) {
+ if (child instanceof AbstractNodeTO<?>) {
+ ((AbstractNodeTO<?>) child).setParent(parentNode);
+ }
+ }
+ }
+ }
+
+ /**
+ * @param keys
+ * @param dataMap
+ * @return list of values of map, found by given keys
+ */
+ public static <T, K> List<K> collectMapValues(List<T> keys, Map<T, K> dataMap) {
+ List<K> valueSubList = new ArrayList<>();
+ for (T key : keys) {
+ valueSubList.add(dataMap.get(key));
+ }
+
+ return valueSubList;
+ }
+
+ /**
+ * @param nodes
+ * @return list of children in list of appropriate type
+ */
+ public static List<Node<?>> buildChildrenList(Node<?>... nodes) {
+ return Lists.newArrayList(nodes);
+ }
+
+}