Added support for parsing submodules & added dependency utility parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
index 033fd352f1a58d7ae702493ee2054c6003daf4ce..39c4c5e78af6883458f551327048b4c4f1b400af 100644 (file)
@@ -9,24 +9,36 @@ package org.opendaylight.yangtools.yang.parser.builder.api;
 
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
 
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.UsesNode;
+import org.opendaylight.yangtools.yang.parser.util.Comparators;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
 /**
  * Basic implementation of DataNodeContainerBuilder.
  */
 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
-    protected final QName qname;
+    protected QName qname;
 
-    protected Set<DataSchemaNode> childNodes;
-    protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
+    protected final Map<QName, DataSchemaNode> childNodes = new TreeMap<>(Comparators.QNAME_COMP);
+    protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
 
-    protected Set<GroupingDefinition> groupings;
-    protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
+    protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
+
+    protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
+
+    protected final Set<UsesNode> usesNodes = new HashSet<>();
+    protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
 
     protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
         super(moduleName, line);
@@ -38,18 +50,10 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         return qname;
     }
 
-    @Override
-    public Set<DataSchemaNode> getChildNodes() {
-        if (childNodes == null) {
-            return Collections.emptySet();
-        }
+    public Map<QName, DataSchemaNode> getChildNodes() {
         return childNodes;
     }
 
-    public void setChildNodes(Set<DataSchemaNode> childNodes) {
-        this.childNodes = childNodes;
-    }
-
     @Override
     public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
         return addedChildNodes;
@@ -67,9 +71,9 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
 
     @Override
     public void addChildNode(DataSchemaNodeBuilder child) {
-        String childName = child.getQName().getLocalName();
+        QName childName = child.getQName();
         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
-            if (addedChildNode.getQName().getLocalName().equals(childName)) {
+            if (addedChildNode.getQName().equals(childName)) {
                 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
                         + this + "' in module '" + moduleName + "': node with same name already declared at line "
                         + addedChildNode.getLine());
@@ -78,6 +82,23 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         addedChildNodes.add(child);
     }
 
+    @Override
+    public void addChildNodeToContext(DataSchemaNodeBuilder child) {
+        addedChildNodes.add(child);
+    }
+
+    @Override
+    public void addChildNode(DataSchemaNode child) {
+        QName childName = child.getQName();
+        for (QName qname : childNodes.keySet()) {
+            if (qname.equals(childName)) {
+                throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
+                        + "' in module '" + moduleName + "': node with same name already declared");
+            }
+        }
+        childNodes.put(child.getQName(), child);
+    }
+
     @Override
     public Set<GroupingDefinition> getGroupings() {
         if (groupings == null) {
@@ -86,19 +107,15 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         return groupings;
     }
 
-    public void setGroupings(final Set<GroupingDefinition> groupings) {
-        this.groupings = groupings;
-    }
-
     public Set<GroupingBuilder> getGroupingBuilders() {
         return addedGroupings;
     }
 
     @Override
     public void addGrouping(GroupingBuilder grouping) {
-        String groupingName = grouping.getQName().getLocalName();
+        QName groupingName = grouping.getQName();
         for (GroupingBuilder addedGrouping : addedGroupings) {
-            if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
+            if (addedGrouping.getQName().equals(groupingName)) {
                 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
                         + "': grouping with same name already declared in module '" + moduleName + "' at line "
                         + addedGrouping.getLine());
@@ -107,4 +124,23 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         addedGroupings.add(grouping);
     }
 
+    @Override
+    public Set<TypeDefinition<?>> getTypeDefinitions() {
+        return typedefs;
+    }
+
+    public Set<UsesNode> getUsesNodes() {
+        return usesNodes;
+    }
+
+    @Override
+    public Set<UsesNodeBuilder> getUsesNodeBuilders() {
+        return addedUsesNodes;
+    }
+
+    @Override
+    public void addUsesNode(UsesNodeBuilder usesNode) {
+        addedUsesNodes.add(usesNode);
+    }
+
 }