2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.parser.builder.api;
9 import java.util.Collections;
10 import java.util.HashSet;
13 import java.util.TreeMap;
14 import java.util.TreeSet;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UsesNode;
21 import org.opendaylight.yangtools.yang.parser.util.Comparators;
22 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25 * Basic implementation of DataNodeContainerBuilder.
27 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
28 protected final QName qname;
30 protected final Map<QName, DataSchemaNode> childNodes = new TreeMap<>();
31 protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
33 protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
34 protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
36 protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
37 protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
39 protected final Set<UsesNode> usesNodes = new HashSet<>();
40 protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
42 protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
43 super(moduleName, line);
48 public QName getQName() {
52 public Map<QName, DataSchemaNode> getChildNodes() {
57 public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
58 return addedChildNodes;
62 public DataSchemaNodeBuilder getDataChildByName(final String name) {
63 for (DataSchemaNodeBuilder child : addedChildNodes) {
64 if (child.getQName().getLocalName().equals(name)) {
72 public void addChildNode(final DataSchemaNodeBuilder child) {
73 QName childName = child.getQName();
74 for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
75 if (addedChildNode.getQName().equals(childName)) {
76 throw new YangParseException(child.getModuleName(), child.getLine(), String.format(
77 "Can not add '%s' to '%s' in module '%s': node with same name already declared at line %d",
78 child, this, getModuleName(), addedChildNode.getLine()));
81 addedChildNodes.add(child);
85 public void addChildNodeToContext(final DataSchemaNodeBuilder child) {
86 addedChildNodes.add(child);
90 public void addChildNode(final DataSchemaNode child) {
91 QName childName = child.getQName();
92 if (childNodes.containsKey(childName)) {
93 throw new YangParseException(getModuleName(), getLine(),
94 String.format("Can not add '%s' to '%s' in module '%s': node with same name already declared",
95 child, this, getModuleName()));
97 childNodes.put(childName, 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(final 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(), String.format(
119 "Can not add '%s': grouping with same name already declared in module '%s' at line %d",
120 grouping, getModuleName(), 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(final UsesNodeBuilder usesNode) {
142 addedUsesNodes.add(usesNode);
145 protected static DataSchemaNode getChildNode(final Set<DataSchemaNode> childNodes, final QName name) {
146 for (DataSchemaNode node : childNodes) {
147 if (node.getQName().equals(name)) {
154 protected static DataSchemaNode getChildNode(final Set<DataSchemaNode> childNodes, final String name) {
155 for (DataSchemaNode node : childNodes) {
156 if (node.getQName().getLocalName().equals(name)) {