X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2FNodeUtils.java;h=bc05425c9aa46c607297bc996749d7a7141f8d61;hb=e609ad90a443547fa613118274a8773fa90c839f;hp=6473c8100a15f7f615eb314e9df2bd31812c8789;hpb=c5b6f823ad2fa88bb1d76751a97a9cdf3e310e99;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/NodeUtils.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/NodeUtils.java index 6473c8100a..bc05425c9a 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/NodeUtils.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/NodeUtils.java @@ -7,12 +7,19 @@ */ package org.opendaylight.yangtools.yang.data.impl; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Deque; 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; @@ -37,16 +44,19 @@ 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 Joiner DOT_JOINER = Joiner.on("."); private static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class); + private static final Function LOCALNAME_FUNCTION = new Function() { + @Override + public String apply(final QName input) { + return input.getLocalName(); + } + }; /** * @@ -57,7 +67,7 @@ public abstract class NodeUtils { * @param node * @return node path up till root node */ - public static String buildPath(Node node) { + public static String buildPath(final Node node) { List breadCrumbs = new ArrayList<>(); Node tmpNode = node; while (tmpNode != null) { @@ -65,7 +75,7 @@ public abstract class NodeUtils { tmpNode = tmpNode.getParent(); } - return Joiner.on(".").join(breadCrumbs); + return DOT_JOINER.join(breadCrumbs); } /** @@ -73,7 +83,7 @@ public abstract class NodeUtils { * @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) { + public static org.w3c.dom.Document buildShadowDomTree(final CompositeNode treeRootNode) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); org.w3c.dom.Document doc = null; try { @@ -84,7 +94,7 @@ public abstract class NodeUtils { return null; } - Stack>> jobQueue = new Stack<>(); + final Deque>> jobQueue = new ArrayDeque<>(); jobQueue.push(new SimpleEntry>(doc, treeRootNode)); while (!jobQueue.isEmpty()) { @@ -96,7 +106,9 @@ public abstract class NodeUtils { itemEl.setUserData(USER_KEY_NODE, item, null); if (item instanceof SimpleNode) { Object value = ((SimpleNode) item).getValue(); - itemEl.setTextContent(String.valueOf(value)); + if(value != null) { + itemEl.setTextContent(String.valueOf(value)); + } } if (item instanceof NodeModification) { ModifyAction modificationAction = ((NodeModification) item).getModificationAction(); @@ -108,7 +120,7 @@ public abstract class NodeUtils { jointPlace.appendChild(itemEl); if (item instanceof CompositeNode) { - for (Node child : ((CompositeNode) item).getChildren()) { + for (Node child : ((CompositeNode) item).getValue()) { jobQueue.push(new SimpleEntry>(itemEl, child)); } } @@ -124,7 +136,7 @@ public abstract class NodeUtils { * @throws XPathExpressionException */ @SuppressWarnings("unchecked") - public static T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx) throws XPathExpressionException { + public static T findNodeByXpath(final org.w3c.dom.Document doc, final String xpathEx) throws XPathExpressionException { T userNode = null; XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); @@ -140,14 +152,14 @@ public abstract class NodeUtils { /** * 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>> buildNodeMap(List> value) { - Map>> nodeMapTmp = new HashMap<>(); - if (value == null || value.isEmpty()) { + public static Map>> buildNodeMap(final List> value) { + Map>> nodeMapTmp = Maps.newLinkedHashMap(); + if (value == null) { throw new IllegalStateException("nodeList should not be null or empty"); } for (Node node : value) { @@ -165,16 +177,16 @@ public abstract class NodeUtils { * @param context * @return map of lists, where key = path; value = {@link DataSchemaNode} */ - public static Map buildMapOfListNodes(SchemaContext context) { + public static Map buildMapOfListNodes(final SchemaContext context) { Map mapOfLists = new HashMap<>(); - Stack jobQueue = new Stack<>(); + final Deque jobQueue = new ArrayDeque<>(); jobQueue.addAll(context.getDataDefinitions()); while (!jobQueue.isEmpty()) { DataSchemaNode dataSchema = jobQueue.pop(); if (dataSchema instanceof ListSchemaNode) { - mapOfLists.put(schemaPathToPath(dataSchema.getPath().getPath()), (ListSchemaNode) dataSchema); + mapOfLists.put(schemaPathToPath(dataSchema.getPath().getPathFromRoot()), (ListSchemaNode) dataSchema); } if (dataSchema instanceof DataNodeContainer) { @@ -186,25 +198,21 @@ public abstract class NodeUtils { } /** - * @param path - * @return + * @param qNamesPath + * @return path */ - private static String schemaPathToPath(List qNamesPath) { - List pathSeed = new ArrayList<>(); - for (QName qNameItem : qNamesPath) { - pathSeed.add(qNameItem.getLocalName()); - } - return Joiner.on(".").join(pathSeed); + private static String schemaPathToPath(final Iterable qNamesPath) { + return DOT_JOINER.join(Iterables.transform(qNamesPath, LOCALNAME_FUNCTION)); } /** * add given node to it's parent's list of children - * + * * @param newNode */ - public static void fixParentRelation(Node newNode) { + public static void fixParentRelation(final Node newNode) { if (newNode.getParent() != null) { - List> siblings = newNode.getParent().getChildren(); + List> siblings = newNode.getParent().getValue(); if (!siblings.contains(newNode)) { siblings.add(newNode); } @@ -213,12 +221,12 @@ public abstract class NodeUtils { /** * 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()) { + public static void fixChildrenRelation(final CompositeNode parentNode) { + if (parentNode.getValue() != null) { + for (Node child : parentNode.getValue()) { if (child instanceof AbstractNodeTO) { ((AbstractNodeTO) child).setParent(parentNode); } @@ -231,7 +239,7 @@ public abstract class NodeUtils { * @param dataMap * @return list of values of map, found by given keys */ - public static List collectMapValues(List keys, Map dataMap) { + public static List collectMapValues(final List keys, final Map dataMap) { List valueSubList = new ArrayList<>(); for (T key : keys) { valueSubList.add(dataMap.get(key)); @@ -244,7 +252,7 @@ public abstract class NodeUtils { * @param nodes * @return list of children in list of appropriate type */ - public static List> buildChildrenList(Node... nodes) { + public static List> buildChildrenList(final Node... nodes) { return Lists.newArrayList(nodes); }