Added getParent() method to DataSchemaNode and DataNodeContainer. Fixed Bugs.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.builder.api;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.TreeMap;
16 import java.util.TreeSet;
17
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UsesNode;
23 import org.opendaylight.yangtools.yang.parser.util.Comparators;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25
26 /**
27  * Basic implementation of DataNodeContainerBuilder.
28  */
29 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
30     protected QName qname;
31
32     protected Map<QName, DataSchemaNode> childNodes = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
33     protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
34
35     protected Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
36     protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
37
38     protected Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
39     protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
40
41     protected Set<UsesNode> usesNodes = new HashSet<>();
42     protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
43
44     protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
45         super(moduleName, line);
46         this.qname = qname;
47     }
48
49     @Override
50     public QName getQName() {
51         return qname;
52     }
53
54     @Override
55     public Collection<DataSchemaNode> getChildNodes() {
56         if (childNodes == null) {
57             return Collections.emptySet();
58         }
59         return childNodes.values();
60     }
61
62     @Override
63     public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
64         return addedChildNodes;
65     }
66
67     @Override
68     public DataSchemaNodeBuilder getDataChildByName(final String name) {
69         for (DataSchemaNodeBuilder child : addedChildNodes) {
70             if (child.getQName().getLocalName().equals(name)) {
71                 return child;
72             }
73         }
74         return null;
75     }
76
77     @Override
78     public void addChildNode(DataSchemaNodeBuilder child) {
79         String childName = child.getQName().getLocalName();
80         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
81             if (addedChildNode.getQName().getLocalName().equals(childName)) {
82                 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
83                         + this + "' in module '" + moduleName + "': node with same name already declared at line "
84                         + addedChildNode.getLine());
85             }
86         }
87         addedChildNodes.add(child);
88     }
89
90     @Override
91     public void addChildNode(DataSchemaNode child) {
92         QName childName = child.getQName();
93         for (QName qname : childNodes.keySet()) {
94             if (qname.equals(childName)) {
95                 throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
96                         + "' in module '" + moduleName + "': node with same name already declared");
97             }
98         }
99         childNodes.put(child.getQName(), child);
100     }
101
102     @Override
103     public Set<GroupingDefinition> getGroupings() {
104         if (groupings == null) {
105             return Collections.emptySet();
106         }
107         return groupings;
108     }
109
110     public void setGroupings(final Set<GroupingDefinition> groupings) {
111         this.groupings = groupings;
112     }
113
114     public Set<GroupingBuilder> getGroupingBuilders() {
115         return addedGroupings;
116     }
117
118     @Override
119     public void addGrouping(GroupingBuilder grouping) {
120         String groupingName = grouping.getQName().getLocalName();
121         for (GroupingBuilder addedGrouping : addedGroupings) {
122             if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
123                 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
124                         + "': grouping with same name already declared in module '" + moduleName + "' at line "
125                         + addedGrouping.getLine());
126             }
127         }
128         addedGroupings.add(grouping);
129     }
130
131     @Override
132     public Set<UsesNodeBuilder> getUsesNodes() {
133         return addedUsesNodes;
134     }
135
136     public void setUsesnodes(final Set<UsesNode> usesNodes) {
137         this.usesNodes = usesNodes;
138     }
139
140     @Override
141     public void addUsesNode(UsesNodeBuilder usesNode) {
142         addedUsesNodes.add(usesNode);
143     }
144
145 }