2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.api;
10 import java.util.Collections;
11 import java.util.HashSet;
14 import java.util.TreeMap;
15 import java.util.TreeSet;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.UsesNode;
22 import org.opendaylight.yangtools.yang.parser.util.Comparators;
23 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
26 * Basic implementation of DataNodeContainerBuilder.
28 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
29 protected QName qname;
31 protected final Map<QName, DataSchemaNode> childNodes = new TreeMap<>(Comparators.QNAME_COMP);
32 protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
34 protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
35 protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
37 protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
38 protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
40 protected final Set<UsesNode> usesNodes = new HashSet<>();
41 protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
43 protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
44 super(moduleName, line);
49 public QName getQName() {
53 public Map<QName, DataSchemaNode> getChildNodes() {
58 public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
59 return addedChildNodes;
63 public DataSchemaNodeBuilder getDataChildByName(final String name) {
64 for (DataSchemaNodeBuilder child : addedChildNodes) {
65 if (child.getQName().getLocalName().equals(name)) {
73 public void addChildNode(DataSchemaNodeBuilder child) {
74 QName childName = child.getQName();
75 for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
76 if (addedChildNode.getQName().equals(childName)) {
77 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
78 + this + "' in module '" + moduleName + "': node with same name already declared at line "
79 + addedChildNode.getLine());
82 addedChildNodes.add(child);
86 public void addChildNodeToContext(DataSchemaNodeBuilder child) {
87 addedChildNodes.add(child);
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");
99 childNodes.put(child.getQName(), child);
103 public Set<GroupingDefinition> getGroupings() {
104 if (groupings == null) {
105 return Collections.emptySet();
110 public Set<GroupingBuilder> getGroupingBuilders() {
111 return addedGroupings;
115 public void addGrouping(GroupingBuilder grouping) {
116 QName groupingName = grouping.getQName();
117 for (GroupingBuilder addedGrouping : addedGroupings) {
118 if (addedGrouping.getQName().equals(groupingName)) {
119 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
120 + "': grouping with same name already declared in module '" + moduleName + "' at line "
121 + addedGrouping.getLine());
124 addedGroupings.add(grouping);
128 public Set<TypeDefinition<?>> getTypeDefinitions() {
132 public Set<UsesNode> getUsesNodes() {
137 public Set<UsesNodeBuilder> getUsesNodeBuilders() {
138 return addedUsesNodes;
142 public void addUsesNode(UsesNodeBuilder usesNode) {
143 addedUsesNodes.add(usesNode);