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;
13 import java.util.TreeSet;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UsesNode;
20 import org.opendaylight.yangtools.yang.parser.util.Comparators;
21 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
24 * Basic implementation of DataNodeContainerBuilder.
26 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
27 protected final QName qname;
29 protected final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
30 protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
32 protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
33 protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
35 protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
36 protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
38 protected final Set<UsesNode> usesNodes = new HashSet<>();
39 protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
41 protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
42 super(moduleName, line);
47 public QName getQName() {
51 public Set<DataSchemaNode> getChildNodes() {
56 public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
57 return addedChildNodes;
61 public DataSchemaNodeBuilder getDataChildByName(final String name) {
62 for (DataSchemaNodeBuilder child : addedChildNodes) {
63 if (child.getQName().getLocalName().equals(name)) {
71 public void addChildNode(DataSchemaNodeBuilder child) {
72 QName childName = child.getQName();
73 for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
74 if (addedChildNode.getQName().equals(childName)) {
75 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
76 + this + "' in module '" + moduleName + "': node with same name already declared at line "
77 + addedChildNode.getLine());
80 addedChildNodes.add(child);
84 public void addChildNodeToContext(DataSchemaNodeBuilder child) {
85 addedChildNodes.add(child);
89 public void addChildNode(DataSchemaNode child) {
90 QName childName = child.getQName();
91 for (DataSchemaNode childNode : childNodes) {
92 if (childNode.getQName().equals(childName)) {
93 throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
94 + "' in module '" + moduleName + "': node with same name already declared");
97 childNodes.add(child);
101 public Set<GroupingDefinition> getGroupings() {
102 if (groupings == null) {
103 return Collections.emptySet();
109 public Set<GroupingBuilder> getGroupingBuilders() {
110 return addedGroupings;
114 public void addGrouping(GroupingBuilder grouping) {
115 QName groupingName = grouping.getQName();
116 for (GroupingBuilder addedGrouping : addedGroupings) {
117 if (addedGrouping.getQName().equals(groupingName)) {
118 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
119 + "': grouping with same name already declared in module '" + moduleName + "' at line "
120 + addedGrouping.getLine());
123 addedGroupings.add(grouping);
127 public Set<TypeDefinition<?>> getTypeDefinitions() {
131 public Set<UsesNode> getUsesNodes() {
136 public Set<UsesNodeBuilder> getUsesNodeBuilders() {
137 return addedUsesNodes;
141 public void addUsesNode(UsesNodeBuilder usesNode) {
142 addedUsesNodes.add(usesNode);
145 protected static DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, QName name) {
146 for (DataSchemaNode node : childNodes) {
147 if (node.getQName().equals(name)) {
154 protected static DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, String name) {
155 for (DataSchemaNode node : childNodes) {
156 if (node.getQName().getLocalName().equals(name)) {