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.impl;
10 import java.util.List;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractSchemaNodeBuilder;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.ImmutableList;
29 public final class AnyXmlBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder {
30 private AnyXmlSchemaNodeImpl instance;
32 private boolean augmenting;
33 private boolean addedByUses;
34 private boolean configuration;
35 private AnyXmlSchemaNode originalNode;
36 private AnyXmlBuilder originalBuilder;
37 private final ConstraintsBuilder constraints;
39 public AnyXmlBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
40 super(moduleName, line, qname);
41 this.schemaPath = path;
42 constraints = new ConstraintsBuilderImpl(moduleName, line);
45 public AnyXmlBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
46 final AnyXmlSchemaNode base) {
47 super(moduleName, line, qname);
48 this.schemaPath = path;
49 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
51 description = base.getDescription();
52 reference = base.getReference();
53 status = base.getStatus();
54 augmenting = base.isAugmenting();
55 addedByUses = base.isAddedByUses();
57 configuration = base.isConfiguration();
58 unknownNodes.addAll(base.getUnknownSchemaNodes());
62 public AnyXmlSchemaNode build() {
63 if (instance != null) {
67 instance = new AnyXmlSchemaNodeImpl(qname, schemaPath);
69 instance.description = description;
70 instance.reference = reference;
71 instance.status = status;
72 instance.augmenting = augmenting;
73 instance.addedByUses = addedByUses;
74 instance.configuration = configuration;
75 instance.constraintsDef = constraints.toInstance();
78 if (originalNode == null && originalBuilder != null) {
79 originalNode = originalBuilder.build();
81 instance.original = originalNode;
84 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
85 unknownNodes.add(b.build());
87 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
93 public ConstraintsBuilder getConstraints() {
98 public boolean isAugmenting() {
103 public void setAugmenting(final boolean augmenting) {
104 this.augmenting = augmenting;
108 public boolean isAddedByUses() {
113 public void setAddedByUses(final boolean addedByUses) {
114 this.addedByUses = addedByUses;
118 public AnyXmlBuilder getOriginal() {
119 return originalBuilder;
123 public void setOriginal(final SchemaNodeBuilder builder) {
124 Preconditions.checkArgument(builder instanceof AnyXmlBuilder, "Original of anyxml cannot be " + builder);
125 this.originalBuilder = (AnyXmlBuilder) builder;
129 public boolean isConfiguration() {
130 return configuration;
134 public void setConfiguration(final boolean configuration) {
135 this.configuration = configuration;
139 public int hashCode() {
140 final int prime = 31;
142 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
147 public boolean equals(final Object obj) {
154 if (getClass() != obj.getClass()) {
157 AnyXmlBuilder other = (AnyXmlBuilder) obj;
158 if (schemaPath == null) {
159 if (other.schemaPath != null) {
162 } else if (!schemaPath.equals(other.schemaPath)) {
165 if (getParent() == null) {
166 if (other.getParent() != null) {
169 } else if (!getParent().equals(other.getParent())) {
176 public String toString() {
177 return "anyxml " + qname.getLocalName();
180 private static final class AnyXmlSchemaNodeImpl implements AnyXmlSchemaNode, DerivableSchemaNode {
181 private final QName qname;
182 private final SchemaPath path;
183 private String description;
184 private String reference;
185 private Status status;
186 private boolean configuration;
187 private AnyXmlSchemaNode original;
188 private ConstraintDefinition constraintsDef;
189 private boolean augmenting;
190 private boolean addedByUses;
191 private ImmutableList<UnknownSchemaNode> unknownNodes;
193 private AnyXmlSchemaNodeImpl(final QName qname, final SchemaPath path) {
199 public QName getQName() {
204 public SchemaPath getPath() {
209 public String getDescription() {
214 public String getReference() {
219 public Status getStatus() {
224 public boolean isAugmenting() {
229 public boolean isAddedByUses() {
234 public Optional<AnyXmlSchemaNode> getOriginal() {
235 return Optional.fromNullable(original);
239 public boolean isConfiguration() {
240 return configuration;
244 public ConstraintDefinition getConstraints() {
245 return constraintsDef;
249 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
254 public int hashCode() {
255 final int prime = 31;
257 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
258 result = prime * result + ((path == null) ? 0 : path.hashCode());
263 public boolean equals(final Object obj) {
270 if (getClass() != obj.getClass()) {
273 AnyXmlSchemaNodeImpl other = (AnyXmlSchemaNodeImpl) obj;
275 if (other.qname != null) {
278 } else if (!qname.equals(other.qname)) {
282 if (other.path != null) {
285 } else if (!path.equals(other.path)) {
292 public String toString() {
293 StringBuilder sb = new StringBuilder(AnyXmlSchemaNodeImpl.class.getSimpleName());
295 sb.append("qname=").append(qname);
296 sb.append(", path=").append(path);
298 return sb.toString();