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.util;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
18 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
38 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.impl.AugmentationSchemaBuilderImpl;
40 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingBuilderImpl;
45 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
51 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
52 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
53 import org.opendaylight.yangtools.yang.parser.builder.impl.UsesNodeBuilderImpl;
55 public final class CopyUtils {
61 * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
62 * true, qname of node will be corrected based on new parent.
69 * flag to indicate if qname should be updated based on new
71 * @return copy of given builder
73 public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
74 if (old instanceof AnyXmlBuilder) {
75 return copy((AnyXmlBuilder) old, newParent, updateQName);
76 } else if (old instanceof ChoiceBuilder) {
77 return copy((ChoiceBuilder) old, newParent, updateQName);
78 } else if (old instanceof ContainerSchemaNodeBuilder) {
79 return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
80 } else if (old instanceof LeafSchemaNodeBuilder) {
81 return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
82 } else if (old instanceof LeafListSchemaNodeBuilder) {
83 return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
84 } else if (old instanceof ListSchemaNodeBuilder) {
85 return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
86 } else if (old instanceof ChoiceCaseBuilder) {
87 return copy((ChoiceCaseBuilder) old, newParent, updateQName);
89 throw new YangParseException(old.getModuleName(), old.getLine(),
90 "Failed to copy node: Unknown type of DataSchemaNode: " + old);
94 private static AnyXmlBuilder copy(AnyXmlBuilder old, Builder newParent, boolean updateQName) {
95 DataBean data = getdata(old, newParent, updateQName);
96 QName newQName = data.qname;
97 SchemaPath newSchemaPath = data.schemaPath;
99 AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
100 copyConstraints(copy.getConstraints(), old.getConstraints());
101 copy.setParent(newParent);
102 copy.setPath(newSchemaPath);
103 copy.setDescription(old.getDescription());
104 copy.setReference(old.getReference());
105 copy.setStatus(old.getStatus());
106 copy.setAugmenting(old.isAugmenting());
107 copy.setAddedByUses(old.isAddedByUses());
108 copy.setConfiguration(old.isConfiguration());
109 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
110 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
116 private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) {
117 DataBean data = getdata(old, newParent, updateQName);
118 QName newQName = data.qname;
119 SchemaPath newSchemaPath = data.schemaPath;
121 ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
122 copyConstraints(copy.getConstraints(), old.getConstraints());
123 copy.setParent(newParent);
124 copy.setPath(newSchemaPath);
125 copy.setDescription(old.getDescription());
126 copy.setReference(old.getReference());
127 copy.setStatus(old.getStatus());
128 copy.setAugmenting(old.isAugmenting());
129 copy.setAddedByUses(old.isAddedByUses());
130 copy.setConfiguration(old.isConfiguration());
131 for (ChoiceCaseBuilder childNode : old.getCases()) {
132 copy.addCase(copy(childNode, copy, updateQName));
134 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
135 copy.addAugmentation(copyAugment(augment, copy));
137 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
138 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
144 private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) {
145 DataBean data = getdata(old, newParent, updateQName);
146 QName newQName = data.qname;
147 SchemaPath newSchemaPath = data.schemaPath;
149 ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
150 copyConstraints(copy.getConstraints(), old.getConstraints());
151 copy.setParent(newParent);
152 copy.setPath(newSchemaPath);
153 copy.setDescription(old.getDescription());
154 copy.setReference(old.getReference());
155 copy.setStatus(old.getStatus());
156 copy.setAugmenting(old.isAugmenting());
157 copy.getChildNodes().addAll(old.getChildNodes());
158 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
159 copy.addChildNode(copy(childNode, copy, updateQName));
161 copy.getGroupings().addAll(old.getGroupings());
162 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
163 copy.addGrouping(copy(grouping, copy, updateQName));
165 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
166 copy.addTypedef(copy(tdb, copy, updateQName));
168 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
169 copy.addUsesNode(copyUses(oldUses, copy));
171 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
172 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
178 private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent,
179 boolean updateQName) {
180 DataBean data = getdata(old, newParent, updateQName);
181 QName newQName = data.qname;
182 SchemaPath newSchemaPath = data.schemaPath;
184 ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(),
185 newParent.getLine(), newQName, newSchemaPath);
186 copyConstraints(copy.getConstraints(), old.getConstraints());
187 copy.setParent(newParent);
188 copy.setPath(newSchemaPath);
189 copy.setDescription(old.getDescription());
190 copy.setReference(old.getReference());
191 copy.setStatus(old.getStatus());
192 copy.setPresence(old.isPresence());
193 copy.setAugmenting(old.isAugmenting());
194 copy.setAddedByUses(old.isAddedByUses());
195 copy.setConfiguration(old.isConfiguration());
196 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
197 copy.addChildNode(copy(childNode, copy, updateQName));
199 copy.getGroupings().addAll(old.getGroupings());
200 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
201 copy.addGrouping(copy(grouping, copy, updateQName));
203 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
204 copy.addTypedef(copy(tdb, copy, updateQName));
206 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
207 copy.addUsesNode(copyUses(oldUses, copy));
209 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
210 copy.addAugmentation(copyAugment(augment, copy));
212 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
213 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
219 private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
220 DataBean data = getdata(old, newParent, updateQName);
221 QName newQName = data.qname;
222 SchemaPath newSchemaPath = data.schemaPath;
224 LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
225 newQName, newSchemaPath);
226 copyConstraints(copy.getConstraints(), old.getConstraints());
227 copy.setParent(newParent);
228 copy.setPath(newSchemaPath);
229 copy.setDescription(old.getDescription());
230 copy.setReference(old.getReference());
231 copy.setStatus(old.getStatus());
232 copy.setAugmenting(old.isAugmenting());
233 copy.setAddedByUses(old.isAddedByUses());
234 copy.setConfiguration(old.isConfiguration());
235 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
236 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
239 if (old.getType() == null) {
240 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
242 copy.setType(old.getType());
245 copy.setDefaultStr(old.getDefaultStr());
246 copy.setUnits(old.getUnits());
251 public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
252 DataBean data = getdata(old, newParent, updateQName);
253 QName newQName = data.qname;
254 SchemaPath newSchemaPath = data.schemaPath;
256 LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
257 newQName, newSchemaPath);
258 copyConstraints(copy.getConstraints(), old.getConstraints());
259 copy.setParent(newParent);
260 copy.setPath(newSchemaPath);
261 copy.setDescription(old.getDescription());
262 copy.setReference(old.getReference());
263 copy.setStatus(old.getStatus());
264 copy.setAugmenting(old.isAugmenting());
265 copy.setAddedByUses(old.isAddedByUses());
266 copy.setConfiguration(old.isConfiguration());
267 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
268 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
271 if (old.getType() == null) {
272 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
274 copy.setType(old.getType());
277 copy.setUserOrdered(old.isUserOrdered());
282 private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
283 DataBean data = getdata(old, newParent, updateQName);
284 QName newQName = data.qname;
285 SchemaPath newSchemaPath = data.schemaPath;
287 ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
288 newQName, newSchemaPath);
289 copyConstraints(copy.getConstraints(), old.getConstraints());
290 copy.setParent(newParent);
291 copy.setPath(newSchemaPath);
292 copy.setDescription(old.getDescription());
293 copy.setReference(old.getReference());
294 copy.setStatus(old.getStatus());
295 copy.setAugmenting(old.isAugmenting());
296 copy.setAddedByUses(old.isAddedByUses());
297 copy.setConfiguration(old.isConfiguration());
298 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
299 copy.addChildNode(copy(childNode, copy, updateQName));
301 copy.getGroupings().addAll(old.getGroupings());
302 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
303 copy.addGrouping(copy(grouping, copy, updateQName));
305 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
306 copy.addTypedef(copy(tdb, copy, updateQName));
308 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
309 copy.addUsesNode(copyUses(oldUses, copy));
311 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
312 copy.addAugmentation(copyAugment(augment, copy));
314 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
315 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
318 copy.setUserOrdered(old.isUserOrdered());
319 copy.setKeyDefinition(old.getKeyDefinition());
324 public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) {
325 DataBean data = getdata(old, newParent, updateQName);
326 QName newQName = data.qname;
327 SchemaPath newSchemaPath = data.schemaPath;
329 GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
330 copy.setParent(newParent);
331 copy.setPath(newSchemaPath);
332 copy.setDescription(old.getDescription());
333 copy.setReference(old.getReference());
334 copy.setStatus(old.getStatus());
335 copy.setAddedByUses(old.isAddedByUses());
336 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
337 copy.addChildNode(copy(childNode, copy, updateQName));
339 copy.getGroupings().addAll(old.getGroupings());
340 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
341 copy.addGrouping(copy(grouping, copy, updateQName));
343 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
344 copy.addTypedef(copy(tdb, copy, updateQName));
346 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
347 copy.addUsesNode(copyUses(oldUses, copy));
349 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
350 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
356 public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
357 DataBean data = getdata(old, newParent, updateQName);
358 QName newQName = data.qname;
359 SchemaPath newSchemaPath = data.schemaPath;
360 TypeDefinitionBuilder type;
362 if (old instanceof UnionTypeBuilder) {
363 UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
364 type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
365 type.setParent(newParent);
366 for (TypeDefinition<?> td : oldUnion.getTypes()) {
369 for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
370 type.setTypedef(copy(tdb, type, updateQName));
372 } else if (old instanceof IdentityrefTypeBuilder) {
373 type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
374 ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
375 type.setParent(newParent);
376 type.setPath(newSchemaPath);
378 type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
379 type.setParent(newParent);
381 // type.setPath(newSchemaPath);
382 type.setPath(old.getPath());
384 if (old.getType() == null) {
385 type.setTypedef(copy(old.getTypedef(), type, updateQName));
387 type.setType(old.getType());
390 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
391 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
394 type.setRanges(old.getRanges());
395 type.setLengths(old.getLengths());
396 type.setPatterns(old.getPatterns());
397 type.setFractionDigits(old.getFractionDigits());
398 type.setDescription(old.getDescription());
399 type.setReference(old.getReference());
400 type.setStatus(old.getStatus());
401 type.setUnits(old.getUnits());
402 type.setDefaultValue(old.getDefaultValue());
403 type.setAddedByUses(old.isAddedByUses());
409 private static ConstraintsBuilder copyConstraints(ConstraintsBuilder newConstraints, ConstraintsBuilder old) {
410 newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
411 newConstraints.addWhenCondition(old.getWhenCondition());
412 newConstraints.setMandatory(old.isMandatory());
413 newConstraints.setMinElements(old.getMinElements());
414 newConstraints.setMaxElements(old.getMaxElements());
415 return newConstraints;
418 static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
419 UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
420 old.getGroupingPathAsString(), true);
421 copy.setParent(newParent);
422 copy.setGroupingDefinition(old.getGroupingDefinition());
423 copy.setGrouping(old.getGroupingBuilder());
424 copy.setAddedByUses(old.isAddedByUses());
425 copy.getAugmentations().addAll(old.getAugmentations());
426 copy.getRefineNodes().addAll(old.getRefineNodes());
427 copy.getRefines().addAll(old.getRefines());
428 copy.setAugmenting(old.isAugmenting());
429 copy.setParentAugment(old.getParentAugment());
433 private static AugmentationSchemaBuilder copyAugment(AugmentationSchemaBuilder old, Builder newParent) {
434 AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
435 newParent.getLine(), old.getTargetPathAsString());
436 copy.setParent(newParent);
438 copy.setDescription(old.getDescription());
439 copy.setReference(old.getReference());
440 copy.setStatus(old.getStatus());
441 copy.addWhenCondition(old.getWhenCondition());
442 copy.setTargetNodeSchemaPath(old.getTargetNodeSchemaPath());
443 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
444 copy.addChildNode(copy(childNode, copy, false));
446 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
447 copy.addUsesNode(copyUses(oldUses, copy));
449 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
450 copy.addUnknownNodeBuilder((copy(un, copy, false)));
456 public static UnknownSchemaNodeBuilder copy(UnknownSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
457 DataBean data = getdata(old, newParent, updateQName);
458 QName newQName = data.qname;
459 SchemaPath newSchemaPath = data.schemaPath;
461 UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
464 c.setParent(newParent);
465 c.setPath(newSchemaPath);
466 c.setDescription(old.getDescription());
467 c.setReference(old.getReference());
468 c.setStatus(old.getStatus());
469 c.setAddedByUses(old.isAddedByUses());
470 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
471 c.addUnknownNodeBuilder((copy(un, c, updateQName)));
477 private static DataBean getdata(SchemaNodeBuilder old, Builder newParent, boolean updateQName) {
478 List<QName> newPath = null;
479 QName newQName = null;
480 if (newParent instanceof ModuleBuilder) {
481 ModuleBuilder parent = (ModuleBuilder) newParent;
483 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
485 newPath = Collections.singletonList(newQName);
487 newQName = old.getQName();
488 newPath = Collections.singletonList(newQName);
490 } else if (newParent instanceof AugmentationSchemaBuilder) {
491 AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
492 ModuleBuilder parent = ParserUtils.getParentModule(newParent);
494 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
496 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
497 newPath.add(newQName);
499 newQName = old.getQName();
500 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
501 newPath.add(newQName);
504 } else if (newParent instanceof SchemaNodeBuilder) {
505 SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
506 QName parentQName = parent.getQName();
508 newQName = new QName(parentQName.getNamespace(), parentQName.getRevision(), parentQName.getPrefix(),
509 old.getQName().getLocalName());
510 newPath = new ArrayList<>(parent.getPath().getPath());
511 newPath.add(newQName);
513 newQName = old.getQName();
514 newPath = new ArrayList<>(parent.getPath().getPath());
515 newPath.add(newQName);
519 SchemaPath newSchemaPath = new SchemaPath(newPath, true);
520 return new DataBean(newQName, newSchemaPath);
523 private static final class DataBean {
525 private SchemaPath schemaPath;
527 private DataBean(QName qname, SchemaPath schemaPath) {
529 this.schemaPath = schemaPath;
534 * Create AnyXmlBuilder from given AnyXmlSchemaNode.
541 * current module name
543 * current line in module
544 * @return anyxml builder based on given anyxml node
546 public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) {
547 final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath());
548 convertDataSchemaNode(anyxml, builder);
549 builder.setConfiguration(anyxml.isConfiguration());
550 builder.setUnknownNodes(anyxml.getUnknownSchemaNodes());
555 * Create GroupingBuilder from given GroupingDefinition.
562 * current module name
564 * current line in module
565 * @return grouping builder based on given grouping node
567 public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) {
568 final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname);
569 builder.setPath(grouping.getPath());
570 for (DataSchemaNode child : grouping.getChildNodes()) {
571 builder.addChildNode(child);
573 builder.setGroupings(grouping.getGroupings());
574 builder.setTypedefs(grouping.getTypeDefinitions());
575 builder.setUsesnodes(grouping.getUses());
576 builder.setUnknownNodes(grouping.getUnknownSchemaNodes());
577 builder.setDescription(grouping.getDescription());
578 builder.setReference(grouping.getReference());
579 builder.setStatus(grouping.getStatus());
584 * Create TypeDefinitionBuilder from given ExtendedType.
591 * current module name
593 * current line in module
594 * @return typedef builder based on given typedef node
596 public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) {
597 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname);
598 builder.setPath(typedef.getPath());
599 builder.setDefaultValue(typedef.getDefaultValue());
600 builder.setUnits(typedef.getUnits());
601 builder.setDescription(typedef.getDescription());
602 builder.setReference(typedef.getReference());
603 builder.setStatus(typedef.getStatus());
604 builder.setRanges(typedef.getRangeConstraints());
605 builder.setLengths(typedef.getLengthConstraints());
606 builder.setPatterns(typedef.getPatternConstraints());
607 builder.setFractionDigits(typedef.getFractionDigits());
608 final TypeDefinition<?> type = typedef.getBaseType();
609 builder.setType(type);
610 builder.setUnits(typedef.getUnits());
611 builder.setUnknownNodes(typedef.getUnknownSchemaNodes());
616 * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
623 * current module name
625 * current line in module
626 * @return unknown node builder based on given unknown node
628 public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname,
629 String moduleName, int line) {
630 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname);
631 builder.setPath(unknownNode.getPath());
632 builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes());
633 builder.setDescription(unknownNode.getDescription());
634 builder.setReference(unknownNode.getReference());
635 builder.setStatus(unknownNode.getStatus());
636 builder.setAddedByUses(unknownNode.isAddedByUses());
637 builder.setNodeType(unknownNode.getNodeType());
638 builder.setNodeParameter(unknownNode.getNodeParameter());
643 * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
646 * leaf from which to create builder
650 * current module name
653 * @return leaf builder based on given leaf node
655 public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) {
656 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath());
657 convertDataSchemaNode(leaf, builder);
658 builder.setConfiguration(leaf.isConfiguration());
659 final TypeDefinition<?> type = leaf.getType();
660 builder.setType(type);
661 builder.setPath(leaf.getPath());
662 builder.setUnknownNodes(leaf.getUnknownSchemaNodes());
663 builder.setDefaultStr(leaf.getDefault());
664 builder.setUnits(leaf.getUnits());
669 * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
676 * current module name
678 * current line in module
679 * @return container builder based on given container node
681 public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname,
682 String moduleName, int line) {
683 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname,
684 container.getPath());
685 convertDataSchemaNode(container, builder);
686 builder.setConfiguration(container.isConfiguration());
687 builder.setUnknownNodes(container.getUnknownSchemaNodes());
688 for (DataSchemaNode child : container.getChildNodes()) {
689 builder.addChildNode(child);
691 builder.setGroupings(container.getGroupings());
692 builder.setTypedefs(container.getTypeDefinitions());
693 builder.getAugmentations().addAll(container.getAvailableAugmentations());
694 builder.setUsesnodes(container.getUses());
695 builder.setPresence(container.isPresenceContainer());
700 * Create ListSchemaNodeBuilder from given ListSchemaNode.
707 * current module name
709 * current line in module
710 * @return list builder based on given list node
712 public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) {
713 ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath());
714 convertDataSchemaNode(list, builder);
715 builder.setConfiguration(list.isConfiguration());
716 builder.setUnknownNodes(list.getUnknownSchemaNodes());
717 builder.setTypedefs(list.getTypeDefinitions());
718 for (DataSchemaNode child : list.getChildNodes()) {
719 builder.addChildNode(child);
721 builder.setGroupings(list.getGroupings());
722 builder.getAugmentations().addAll(list.getAvailableAugmentations());
723 builder.setUsesnodes(list.getUses());
724 builder.setUserOrdered(builder.isUserOrdered());
729 * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
736 * current module name
738 * current line in module
739 * @return leaf-list builder based on given leaf-list node
741 public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
743 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
745 convertDataSchemaNode(leafList, builder);
746 builder.setConfiguration(leafList.isConfiguration());
747 builder.setType(leafList.getType());
748 builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
749 builder.setUserOrdered(leafList.isUserOrdered());
754 * Create ChoiceBuilder from given ChoiceNode.
761 * current module name
763 * current line in module
764 * @return choice builder based on given choice node
766 public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) {
767 final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname);
768 convertDataSchemaNode(choice, builder);
769 builder.setConfiguration(choice.isConfiguration());
770 builder.setCases(choice.getCases());
771 builder.setUnknownNodes(choice.getUnknownSchemaNodes());
772 builder.setDefaultCase(choice.getDefaultCase());
777 * Set DataSchemaNode arguments to builder object
780 * node from which arguments should be read
782 * builder to which arguments should be set
784 private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) {
785 builder.setPath(node.getPath());
786 builder.setDescription(node.getDescription());
787 builder.setReference(node.getReference());
788 builder.setStatus(node.getStatus());
789 builder.setAugmenting(node.isAugmenting());
790 copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints());
794 * Copy constraints from constraints definition to constraints builder.
796 * @param nodeConstraints
797 * definition from which constraints will be copied
799 * builder to which constraints will be added
801 private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints,
802 final ConstraintsBuilder constraints) {
803 final RevisionAwareXPath when = nodeConstraints.getWhenCondition();
804 final Set<MustDefinition> must = nodeConstraints.getMustConstraints();
807 constraints.addWhenCondition(when.toString());
810 for (MustDefinition md : must) {
811 constraints.addMustDefinition(md);
814 constraints.setMandatory(nodeConstraints.isMandatory());
815 constraints.setMinElements(nodeConstraints.getMinElements());
816 constraints.setMaxElements(nodeConstraints.getMaxElements());