Added more descriptive parsing exceptions.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
index c56b40488c525978811566670fbe94396065d244..d6867ce73592a3a3511e88ef451628cd915602a7 100644 (file)
@@ -7,18 +7,29 @@
  */
 package org.opendaylight.controller.yang.parser.builder.api;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
 import org.opendaylight.controller.yang.common.QName;
+import org.opendaylight.controller.yang.model.api.DataSchemaNode;
+import org.opendaylight.controller.yang.model.api.GroupingDefinition;
+import org.opendaylight.controller.yang.parser.util.YangParseException;
 
-public abstract class AbstractDataNodeContainerBuilder implements DataNodeContainerBuilder {
+/**
+ * Basic implementation of DataNodeContainerBuilder.
+ */
+public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
+    protected final QName qname;
+
+    protected Set<DataSchemaNode> childNodes;
+    protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
 
-    private final QName qname;
-    protected final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
-    protected final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
+    protected Set<GroupingDefinition> groupings;
+    protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
 
-    protected AbstractDataNodeContainerBuilder(QName qname) {
+    protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
+        super(moduleName, line);
         this.qname = qname;
     }
 
@@ -28,22 +39,72 @@ public abstract class AbstractDataNodeContainerBuilder implements DataNodeContai
     }
 
     @Override
-    public Set<DataSchemaNodeBuilder> getChildNodes() {
+    public Set<DataSchemaNode> getChildNodes() {
+        if (childNodes == null) {
+            return Collections.emptySet();
+        }
         return childNodes;
     }
 
+    public void setChildNodes(Set<DataSchemaNode> childNodes) {
+        this.childNodes = childNodes;
+    }
+
     @Override
-    public void addChildNode(DataSchemaNodeBuilder childNode) {
-        childNodes.add(childNode);
+    public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
+        return addedChildNodes;
     }
 
-    public Set<GroupingBuilder> getGroupings() {
+    @Override
+    public DataSchemaNodeBuilder getDataChildByName(final String name) {
+        for (DataSchemaNodeBuilder child : addedChildNodes) {
+            if (child.getQName().getLocalName().equals(name)) {
+                return child;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public void addChildNode(DataSchemaNodeBuilder child) {
+        String childName = child.getQName().getLocalName();
+        for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
+            if (addedChildNode.getQName().getLocalName().equals(childName)) {
+                throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child
+                        + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
+                        + "': node with same name already declared at line " + addedChildNode.getLine());
+            }
+        }
+        addedChildNodes.add(child);
+    }
+
+    @Override
+    public Set<GroupingDefinition> getGroupings() {
+        if (groupings == null) {
+            return Collections.emptySet();
+        }
         return groupings;
     }
 
+    public void setGroupings(final Set<GroupingDefinition> groupings) {
+        this.groupings = groupings;
+    }
+
+    public Set<GroupingBuilder> getGroupingBuilders() {
+        return addedGroupings;
+    }
+
     @Override
     public void addGrouping(GroupingBuilder grouping) {
-        groupings.add(grouping);
+        String groupingName = grouping.getQName().getLocalName();
+        for (GroupingBuilder addedGrouping : addedGroupings) {
+            if (addedGrouping.getQName().getLocalName().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());
+            }
+        }
+        addedGroupings.add(grouping);
     }
 
 }