Added line number to error messages.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / AnyXmlBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.model.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
16 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
17 import org.opendaylight.controller.yang.model.api.SchemaPath;
18 import org.opendaylight.controller.yang.model.api.Status;
19 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
21
22 public class AnyXmlBuilder implements DataSchemaNodeBuilder {
23     private final int line;
24     private final QName qname;
25     private SchemaPath path;
26     private final AnyXmlSchemaNodeImpl instance;
27     private final ConstraintsBuilder constraints;
28     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
29
30     private String description;
31     private String reference;
32     private Status status = Status.CURRENT;
33     private boolean configuration;
34
35     public AnyXmlBuilder(final QName qname, final int line) {
36         this.qname = qname;
37         this.line = line;
38         instance = new AnyXmlSchemaNodeImpl(qname);
39         constraints = new ConstraintsBuilder(line);
40     }
41
42     @Override
43     public AnyXmlSchemaNode build() {
44         instance.setPath(path);
45         instance.setConstraints(constraints.build());
46         instance.setDescription(description);
47         instance.setReference(reference);
48         instance.setStatus(status);
49
50         // UNKNOWN NODES
51         final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
52         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
53             unknownNodes.add(b.build());
54         }
55         instance.setUnknownSchemaNodes(unknownNodes);
56
57         instance.setConfiguration(configuration);
58         return instance;
59     }
60
61     @Override
62     public int getLine() {
63         return line;
64     }
65
66     @Override
67     public QName getQName() {
68         return qname;
69     }
70
71     public SchemaPath getPath() {
72         return path;
73     }
74
75     @Override
76     public void setPath(final SchemaPath path) {
77         this.path = path;
78     }
79
80     @Override
81     public ConstraintsBuilder getConstraints() {
82         return constraints;
83     }
84
85     @Override
86     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
87         addedUnknownNodes.add(unknownNode);
88     }
89
90     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
91         return addedUnknownNodes;
92     }
93
94     public String getDescription() {
95         return description;
96     }
97
98     @Override
99     public void setDescription(final String description) {
100         this.description = description;
101     }
102
103     public String getReference() {
104         return reference;
105     }
106
107     @Override
108     public void setReference(final String reference) {
109         this.reference = reference;
110     }
111
112     public Status getStatus() {
113         return status;
114     }
115
116     @Override
117     public void setStatus(final Status status) {
118         if (status != null) {
119             this.status = status;
120         }
121     }
122
123     @Override
124     public void setAugmenting(final boolean augmenting) {
125         throw new UnsupportedOperationException(
126                 "An anyxml node cannot be augmented.");
127     }
128
129     public boolean isConfiguration() {
130         return configuration;
131     }
132
133     @Override
134     public void setConfiguration(final boolean configuration) {
135         instance.setConfiguration(configuration);
136     }
137
138     private static class AnyXmlSchemaNodeImpl implements AnyXmlSchemaNode {
139         private final QName qname;
140         private SchemaPath path;
141         private String description;
142         private String reference;
143         private Status status = Status.CURRENT;
144         private boolean configuration;
145         private ConstraintDefinition constraintsDef;
146         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
147
148         private AnyXmlSchemaNodeImpl(final QName qname) {
149             this.qname = qname;
150         }
151
152         @Override
153         public QName getQName() {
154             return qname;
155         }
156
157         @Override
158         public SchemaPath getPath() {
159             return path;
160         }
161
162         private void setPath(final SchemaPath path) {
163             this.path = path;
164         }
165
166         @Override
167         public String getDescription() {
168             return description;
169         }
170
171         private void setDescription(String description) {
172             this.description = description;
173         }
174
175         @Override
176         public String getReference() {
177             return reference;
178         }
179
180         private void setReference(String reference) {
181             this.reference = reference;
182         }
183
184         @Override
185         public Status getStatus() {
186             return status;
187         }
188
189         private void setStatus(Status status) {
190             if (status != null) {
191                 this.status = status;
192             }
193         }
194
195         @Override
196         public boolean isAugmenting() {
197             return false;
198         }
199
200         @Override
201         public boolean isConfiguration() {
202             return configuration;
203         }
204
205         private void setConfiguration(boolean configuration) {
206             this.configuration = configuration;
207         }
208
209         @Override
210         public ConstraintDefinition getConstraints() {
211             return constraintsDef;
212         }
213
214         private void setConstraints(ConstraintDefinition constraintsDef) {
215             this.constraintsDef = constraintsDef;
216         }
217
218         @Override
219         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
220             return unknownNodes;
221         }
222
223         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
224             if (unknownNodes != null) {
225                 this.unknownNodes = unknownNodes;
226             }
227         }
228
229         @Override
230         public int hashCode() {
231             final int prime = 31;
232             int result = 1;
233             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
234             result = prime * result + ((path == null) ? 0 : path.hashCode());
235             return result;
236         }
237
238         @Override
239         public boolean equals(Object obj) {
240             if (this == obj) {
241                 return true;
242             }
243             if (obj == null) {
244                 return false;
245             }
246             if (getClass() != obj.getClass()) {
247                 return false;
248             }
249             AnyXmlSchemaNodeImpl other = (AnyXmlSchemaNodeImpl) obj;
250             if (qname == null) {
251                 if (other.qname != null) {
252                     return false;
253                 }
254             } else if (!qname.equals(other.qname)) {
255                 return false;
256             }
257             if (path == null) {
258                 if (other.path != null) {
259                     return false;
260                 }
261             } else if (!path.equals(other.path)) {
262                 return false;
263             }
264             return true;
265         }
266
267         @Override
268         public String toString() {
269             StringBuilder sb = new StringBuilder(
270                     AnyXmlSchemaNodeImpl.class.getSimpleName());
271             sb.append("[");
272             sb.append("qname=" + qname);
273             sb.append(", path=" + path);
274             sb.append("]");
275             return sb.toString();
276         }
277     }
278
279 }