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() {
54 public Set<DataSchemaNodeBuilder> getChildNodes() {
55 return addedChildNodes;
59 public DataSchemaNodeBuilder getDataChildByName(final String name) {
60 for (DataSchemaNodeBuilder child : addedChildNodes) {
61 if (child.getQName().getLocalName().equals(name)) {
69 public void addChildNode(DataSchemaNodeBuilder child) {
70 String childName = child.getQName().getLocalName();
71 for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
72 if (addedChildNode.getQName().getLocalName().equals(childName)) {
73 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
74 + this + "' in module '" + moduleName + "': node with same name already declared at line "
75 + addedChildNode.getLine());
78 addedChildNodes.add(child);
82 public void addChildNodeToContext(DataSchemaNodeBuilder child) {
83 addedChildNodes.add(child);
87 public void addChildNode(DataSchemaNode child) {
88 QName childName = child.getQName();
89 for (QName qname : childNodes.keySet()) {
90 if (qname.equals(childName)) {
91 throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
92 + "' in module '" + moduleName + "': node with same name already declared");
95 childNodes.put(child.getQName(), child);
99 public Set<GroupingDefinition> getGroupings() {
100 if (groupings == null) {
101 return Collections.emptySet();
106 public Set<GroupingBuilder> getGroupingBuilders() {
107 return addedGroupings;
111 public void addGrouping(GroupingBuilder grouping) {
112 String groupingName = grouping.getQName().getLocalName();
113 for (GroupingBuilder addedGrouping : addedGroupings) {
114 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
115 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
116 + "': grouping with same name already declared in module '" + moduleName + "' at line "
117 + addedGrouping.getLine());
120 addedGroupings.add(grouping);
124 public Set<UsesNodeBuilder> getUsesNodes() {
125 return addedUsesNodes;
129 public void addUsesNode(UsesNodeBuilder usesNode) {
130 addedUsesNodes.add(usesNode);