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 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 public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
70 if (old instanceof AnyXmlBuilder) {
71 return copy((AnyXmlBuilder) old, newParent, updateQName);
72 } else if (old instanceof ChoiceBuilder) {
73 return copy((ChoiceBuilder) old, newParent, updateQName);
74 } else if (old instanceof ContainerSchemaNodeBuilder) {
75 return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
76 } else if (old instanceof LeafSchemaNodeBuilder) {
77 return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
78 } else if (old instanceof LeafListSchemaNodeBuilder) {
79 return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
80 } else if (old instanceof ListSchemaNodeBuilder) {
81 return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
82 } else if (old instanceof ChoiceCaseBuilder) {
83 return copy((ChoiceCaseBuilder) old, newParent, updateQName);
85 throw new YangParseException(old.getModuleName(), old.getLine(),
86 "Failed to copy node: Unknown type of DataSchemaNode: " + old);
90 private static AnyXmlBuilder copy(AnyXmlBuilder old, Builder newParent, boolean updateQName) {
91 DataBean data = getdata(old, newParent, updateQName);
92 QName newQName = data.qname;
93 SchemaPath newSchemaPath = data.schemaPath;
95 AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
96 copyConstraints(copy.getConstraints(), old.getConstraints());
97 copy.setParent(newParent);
98 copy.setPath(newSchemaPath);
99 copy.setDescription(old.getDescription());
100 copy.setReference(old.getReference());
101 copy.setStatus(old.getStatus());
102 copy.setAugmenting(old.isAugmenting());
103 copy.setAddedByUses(old.isAddedByUses());
104 copy.setConfiguration(old.isConfiguration());
105 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
106 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
112 private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) {
113 DataBean data = getdata(old, newParent, updateQName);
114 QName newQName = data.qname;
115 SchemaPath newSchemaPath = data.schemaPath;
117 ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
118 copyConstraints(copy.getConstraints(), old.getConstraints());
119 copy.setParent(newParent);
120 copy.setPath(newSchemaPath);
121 copy.setDescription(old.getDescription());
122 copy.setReference(old.getReference());
123 copy.setStatus(old.getStatus());
124 copy.setAugmenting(old.isAugmenting());
125 copy.setAddedByUses(old.isAddedByUses());
126 copy.setConfiguration(old.isConfiguration());
127 for (ChoiceCaseBuilder childNode : old.getCases()) {
128 copy.addCase(copy(childNode, copy, updateQName));
130 for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
131 copy.addAugmentation(copyAugment(augment, copy));
133 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
134 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
140 private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) {
141 DataBean data = getdata(old, newParent, updateQName);
142 QName newQName = data.qname;
143 SchemaPath newSchemaPath = data.schemaPath;
145 ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
146 copyConstraints(copy.getConstraints(), old.getConstraints());
147 copy.setParent(newParent);
148 copy.setPath(newSchemaPath);
149 copy.setDescription(old.getDescription());
150 copy.setReference(old.getReference());
151 copy.setStatus(old.getStatus());
152 copy.setAugmenting(old.isAugmenting());
153 copy.getChildNodes().addAll(old.getChildNodes());
154 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
155 copy.addChildNode(copy(childNode, copy, updateQName));
157 copy.getGroupings().addAll(old.getGroupings());
158 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
159 copy.addGrouping(copy(grouping, copy, updateQName));
161 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
162 copy.addTypedef(copy(tdb, copy, updateQName));
164 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
165 copy.addUsesNode(copyUses(oldUses, copy));
167 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
168 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
174 private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent,
175 boolean updateQName) {
176 DataBean data = getdata(old, newParent, updateQName);
177 QName newQName = data.qname;
178 SchemaPath newSchemaPath = data.schemaPath;
180 ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
181 newQName, newSchemaPath);
182 copyConstraints(copy.getConstraints(), old.getConstraints());
183 copy.setParent(newParent);
184 copy.setPath(newSchemaPath);
185 copy.setDescription(old.getDescription());
186 copy.setReference(old.getReference());
187 copy.setStatus(old.getStatus());
188 copy.setPresence(old.isPresence());
189 copy.setAugmenting(old.isAugmenting());
190 copy.setAddedByUses(old.isAddedByUses());
191 copy.setConfiguration(old.isConfiguration());
192 copy.setChildNodes(old.getChildNodes());
193 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
194 copy.addChildNode(copy(childNode, copy, updateQName));
196 copy.getGroupings().addAll(old.getGroupings());
197 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
198 copy.addGrouping(copy(grouping, copy, updateQName));
200 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
201 copy.addTypedef(copy(tdb, copy, updateQName));
203 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
204 copy.addUsesNode(copyUses(oldUses, copy));
206 for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
207 copy.addAugmentation(copyAugment(augment, copy));
209 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
210 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
216 private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
217 DataBean data = getdata(old, newParent, updateQName);
218 QName newQName = data.qname;
219 SchemaPath newSchemaPath = data.schemaPath;
221 LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName,
223 copyConstraints(copy.getConstraints(), old.getConstraints());
224 copy.setParent(newParent);
225 copy.setPath(newSchemaPath);
226 copy.setDescription(old.getDescription());
227 copy.setReference(old.getReference());
228 copy.setStatus(old.getStatus());
229 copy.setAugmenting(old.isAugmenting());
230 copy.setAddedByUses(old.isAddedByUses());
231 copy.setConfiguration(old.isConfiguration());
232 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
233 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
236 if (old.getType() == null) {
237 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
239 copy.setType(old.getType());
242 copy.setDefaultStr(old.getDefaultStr());
243 copy.setUnits(old.getUnits());
248 public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
249 DataBean data = getdata(old, newParent, updateQName);
250 QName newQName = data.qname;
251 SchemaPath newSchemaPath = data.schemaPath;
253 LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
254 newQName, newSchemaPath);
255 copyConstraints(copy.getConstraints(), old.getConstraints());
256 copy.setParent(newParent);
257 copy.setPath(newSchemaPath);
258 copy.setDescription(old.getDescription());
259 copy.setReference(old.getReference());
260 copy.setStatus(old.getStatus());
261 copy.setAugmenting(old.isAugmenting());
262 copy.setAddedByUses(old.isAddedByUses());
263 copy.setConfiguration(old.isConfiguration());
264 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
265 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
268 if (old.getType() == null) {
269 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
271 copy.setType(old.getType());
274 copy.setUserOrdered(old.isUserOrdered());
279 private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
280 DataBean data = getdata(old, newParent, updateQName);
281 QName newQName = data.qname;
282 SchemaPath newSchemaPath = data.schemaPath;
284 ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName,
286 copyConstraints(copy.getConstraints(), old.getConstraints());
287 copy.setParent(newParent);
288 copy.setPath(newSchemaPath);
289 copy.setDescription(old.getDescription());
290 copy.setReference(old.getReference());
291 copy.setStatus(old.getStatus());
292 copy.setAugmenting(old.isAugmenting());
293 copy.setAddedByUses(old.isAddedByUses());
294 copy.setConfiguration(old.isConfiguration());
295 copy.setChildNodes(old.getChildNodes());
296 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
297 copy.addChildNode(copy(childNode, copy, updateQName));
299 copy.getGroupings().addAll(old.getGroupings());
300 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
301 copy.addGrouping(copy(grouping, copy, updateQName));
303 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
304 copy.addTypedef(copy(tdb, copy, updateQName));
306 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
307 copy.addUsesNode(copyUses(oldUses, copy));
309 for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
310 copy.addAugmentation(copyAugment(augment, copy));
312 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
313 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
316 copy.setUserOrdered(old.isUserOrdered());
317 copy.setKeyDefinition(old.getKeyDefinition());
322 public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) {
323 DataBean data = getdata(old, newParent, updateQName);
324 QName newQName = data.qname;
325 SchemaPath newSchemaPath = data.schemaPath;
327 GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
328 copy.setParent(newParent);
329 copy.setPath(newSchemaPath);
330 copy.setDescription(old.getDescription());
331 copy.setReference(old.getReference());
332 copy.setStatus(old.getStatus());
333 copy.setAddedByUses(old.isAddedByUses());
334 copy.setChildNodes(old.getChildNodes());
335 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
336 copy.addChildNode(copy(childNode, copy, updateQName));
338 copy.getGroupings().addAll(old.getGroupings());
339 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
340 copy.addGrouping(copy(grouping, copy, updateQName));
342 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
343 copy.addTypedef(copy(tdb, copy, updateQName));
345 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
346 copy.addUsesNode(copyUses(oldUses, copy));
348 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
349 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
355 public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
356 DataBean data = getdata(old, newParent, updateQName);
357 QName newQName = data.qname;
358 SchemaPath newSchemaPath = data.schemaPath;
359 TypeDefinitionBuilder type = null;
361 if (old instanceof UnionTypeBuilder) {
362 UnionTypeBuilder oldUnion = (UnionTypeBuilder)old;
363 type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
364 type.setParent(newParent);
365 for(TypeDefinition<?> td : oldUnion.getTypes()) {
368 for(TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
369 type.setTypedef(copy(tdb, type, updateQName));
371 } else if (old instanceof IdentityrefTypeBuilder) {
372 type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
373 ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
374 type.setParent(newParent);
375 type.setPath(newSchemaPath);
377 type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
378 type.setParent(newParent);
379 type.setPath(newSchemaPath);
381 if (old.getType() == null) {
382 type.setTypedef(copy(old.getTypedef(), type, updateQName));
384 type.setType(old.getType());
387 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
388 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
391 type.setRanges(old.getRanges());
392 type.setLengths(old.getLengths());
393 type.setPatterns(old.getPatterns());
394 type.setFractionDigits(old.getFractionDigits());
395 type.setDescription(old.getDescription());
396 type.setReference(old.getReference());
397 type.setStatus(old.getStatus());
398 type.setUnits(old.getUnits());
399 type.setDefaultValue(old.getDefaultValue());
400 type.setAddedByUses(old.isAddedByUses());
406 private static ConstraintsBuilder copyConstraints(ConstraintsBuilder newConstraints, ConstraintsBuilder old) {
407 newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
408 newConstraints.addWhenCondition(old.getWhenCondition());
409 newConstraints.setMandatory(old.isMandatory());
410 newConstraints.setMinElements(old.getMinElements());
411 newConstraints.setMaxElements(old.getMaxElements());
412 return newConstraints;
415 static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
416 UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
417 old.getGroupingPathAsString(), true);
418 copy.setParent(newParent);
419 copy.setGroupingDefinition(old.getGroupingDefinition());
420 copy.setGrouping(old.getGroupingBuilder());
421 copy.setAddedByUses(old.isAddedByUses());
422 copy.getAugmentations().addAll(old.getAugmentations());
423 copy.getRefineNodes().addAll(old.getRefineNodes());
424 copy.getRefines().addAll(old.getRefines());
426 copy.setTargetChildren(old.getTargetChildren());
427 copy.setTargetTypedefs(old.getTargetTypedefs());
428 copy.setTargetGroupings(old.getTargetGroupings());
429 copy.setTargetUnknownNodes(old.getTargetUnknownNodes());
431 // add new uses to collection of uses in module
432 ModuleBuilder module = ParserUtils.getParentModule(newParent);
433 module.getAllUsesNodes().add(copy);
438 private static AugmentationSchemaBuilder copyAugment(AugmentationSchemaBuilder old, Builder newParent) {
439 AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
440 newParent.getLine(), old.getTargetPathAsString());
441 copy.setParent(newParent);
443 copy.setDescription(old.getDescription());
444 copy.setReference(old.getReference());
445 copy.setStatus(old.getStatus());
446 copy.addWhenCondition(old.getWhenCondition());
447 copy.setChildNodes(old.getChildNodes());
448 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
449 copy.addChildNode(copy(childNode, copy, false));
451 for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
452 copy.addUsesNode(copyUses(oldUses, copy));
454 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
455 copy.addUnknownNodeBuilder((copy(un, copy, false)));
461 static UnknownSchemaNodeBuilder copy(UnknownSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
462 DataBean data = getdata(old, newParent, updateQName);
463 QName newQName = data.qname;
464 SchemaPath newSchemaPath = data.schemaPath;
466 UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
469 c.setParent(newParent);
470 c.setPath(newSchemaPath);
471 c.setDescription(old.getDescription());
472 c.setReference(old.getReference());
473 c.setStatus(old.getStatus());
474 c.setAddedByUses(old.isAddedByUses());
475 for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
476 c.addUnknownNodeBuilder((copy(un, c, updateQName)));
482 private static DataBean getdata(SchemaNodeBuilder old, Builder newParent, boolean updateQName) {
483 List<QName> newPath = null;
484 QName newQName = null;
485 if (newParent instanceof ModuleBuilder) {
486 ModuleBuilder parent = (ModuleBuilder) newParent;
488 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
490 newPath = Collections.singletonList(newQName);
492 newQName = old.getQName();
493 newPath = Collections.singletonList(newQName);
495 } else if (newParent instanceof AugmentationSchemaBuilder) {
496 ModuleBuilder parent = ParserUtils.getParentModule(newParent);
498 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
500 newPath = Collections.singletonList(newQName);
502 newQName = old.getQName();
503 newPath = Collections.singletonList(newQName);
506 } else if (newParent instanceof SchemaNodeBuilder) {
507 SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
508 QName parentQName = parent.getQName();
510 newQName = new QName(parentQName.getNamespace(), parentQName.getRevision(), parentQName.getPrefix(),
511 old.getQName().getLocalName());
512 newPath = new ArrayList<>(parent.getPath().getPath());
513 newPath.add(newQName);
515 newQName = old.getQName();
516 newPath = new ArrayList<>(parent.getPath().getPath());
517 newPath.add(newQName);
521 SchemaPath newSchemaPath = new SchemaPath(newPath, true);
522 return new DataBean(newQName, newSchemaPath);
525 private static class DataBean {
527 private SchemaPath schemaPath;
529 private DataBean(QName qname, SchemaPath schemaPath) {
531 this.schemaPath = schemaPath;
537 * Create AnyXmlBuilder from given AnyXmlSchemaNode.
542 * current module name
544 * current line in module
545 * @return anyxml builder based on given anyxml node
547 public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) {
548 final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath());
549 convertDataSchemaNode(anyxml, builder);
550 builder.setConfiguration(anyxml.isConfiguration());
551 builder.setUnknownNodes(anyxml.getUnknownSchemaNodes());
556 * Create GroupingBuilder from given GroupingDefinition.
561 * current module name
563 * current line in module
564 * @return grouping builder based on given grouping node
566 public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) {
567 final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname);
568 builder.setPath(grouping.getPath());
569 builder.setChildNodes(grouping.getChildNodes());
570 builder.setGroupings(grouping.getGroupings());
571 builder.setTypedefs(grouping.getTypeDefinitions());
572 builder.setUsesnodes(grouping.getUses());
573 builder.setUnknownNodes(grouping.getUnknownSchemaNodes());
574 builder.setDescription(grouping.getDescription());
575 builder.setReference(grouping.getReference());
576 builder.setStatus(grouping.getStatus());
581 * Create TypeDefinitionBuilder from given ExtendedType.
586 * current module name
588 * current line in module
589 * @return typedef builder based on given typedef node
591 public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) {
592 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname);
593 builder.setPath(typedef.getPath());
594 builder.setDefaultValue(typedef.getDefaultValue());
595 builder.setUnits(typedef.getUnits());
596 builder.setDescription(typedef.getDescription());
597 builder.setReference(typedef.getReference());
598 builder.setStatus(typedef.getStatus());
599 builder.setRanges(typedef.getRanges());
600 builder.setLengths(typedef.getLengths());
601 builder.setPatterns(typedef.getPatterns());
602 builder.setFractionDigits(typedef.getFractionDigits());
603 final TypeDefinition<?> type = typedef.getBaseType();
604 builder.setType(type);
605 builder.setUnits(typedef.getUnits());
606 builder.setUnknownNodes(typedef.getUnknownSchemaNodes());
611 * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
616 * current module name
618 * current line in module
619 * @return unknown node builder based on given unknown node
621 public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname,
622 String moduleName, int line) {
623 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname);
624 builder.setPath(unknownNode.getPath());
625 builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes());
626 builder.setDescription(unknownNode.getDescription());
627 builder.setReference(unknownNode.getReference());
628 builder.setStatus(unknownNode.getStatus());
629 builder.setAddedByUses(unknownNode.isAddedByUses());
630 builder.setNodeType(unknownNode.getNodeType());
631 builder.setNodeParameter(unknownNode.getNodeParameter());
637 * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
640 * leaf from which to create builder
643 * current module name
646 * @return leaf builder based on given leaf node
648 public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) {
649 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath());
650 convertDataSchemaNode(leaf, builder);
651 builder.setConfiguration(leaf.isConfiguration());
652 final TypeDefinition<?> type = leaf.getType();
653 builder.setType(type);
654 builder.setPath(leaf.getPath());
655 builder.setUnknownNodes(leaf.getUnknownSchemaNodes());
656 builder.setDefaultStr(leaf.getDefault());
657 builder.setUnits(leaf.getUnits());
662 * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
667 * current module name
669 * current line in module
670 * @return container builder based on given container node
672 public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname,
673 String moduleName, int line) {
674 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname,
675 container.getPath());
676 convertDataSchemaNode(container, builder);
677 builder.setConfiguration(container.isConfiguration());
678 builder.setUnknownNodes(container.getUnknownSchemaNodes());
679 builder.setChildNodes(container.getChildNodes());
680 builder.setGroupings(container.getGroupings());
681 builder.setTypedefs(container.getTypeDefinitions());
682 builder.setAugmentations(container.getAvailableAugmentations());
683 builder.setUsesnodes(container.getUses());
684 builder.setPresence(container.isPresenceContainer());
689 * Create ListSchemaNodeBuilder from given ListSchemaNode.
694 * current module name
696 * current line in module
697 * @return list builder based on given list node
699 public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) {
700 ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath());
701 convertDataSchemaNode(list, builder);
702 builder.setConfiguration(list.isConfiguration());
703 builder.setUnknownNodes(list.getUnknownSchemaNodes());
704 builder.setTypedefs(list.getTypeDefinitions());
705 builder.setChildNodes(list.getChildNodes());
706 builder.setGroupings(list.getGroupings());
707 builder.setAugmentations(list.getAvailableAugmentations());
708 builder.setUsesnodes(list.getUses());
709 builder.setUserOrdered(builder.isUserOrdered());
714 * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
719 * current module name
721 * current line in module
722 * @return leaf-list builder based on given leaf-list node
724 public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
726 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
728 convertDataSchemaNode(leafList, builder);
729 builder.setConfiguration(leafList.isConfiguration());
730 builder.setType(leafList.getType());
731 builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
732 builder.setUserOrdered(leafList.isUserOrdered());
737 * Create ChoiceBuilder from given ChoiceNode.
742 * current module name
744 * current line in module
745 * @return choice builder based on given choice node
747 public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) {
748 final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname);
749 convertDataSchemaNode(choice, builder);
750 builder.setConfiguration(choice.isConfiguration());
751 builder.setCases(choice.getCases());
752 builder.setUnknownNodes(choice.getUnknownSchemaNodes());
753 builder.setDefaultCase(choice.getDefaultCase());
759 * Set DataSchemaNode arguments to builder object
762 * node from which arguments should be read
764 * builder to which arguments should be set
766 private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) {
767 builder.setPath(node.getPath());
768 builder.setDescription(node.getDescription());
769 builder.setReference(node.getReference());
770 builder.setStatus(node.getStatus());
771 builder.setAugmenting(node.isAugmenting());
772 copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints());
776 * Copy constraints from constraints definition to constraints builder.
778 * @param nodeConstraints
779 * definition from which constraints will be copied
781 * builder to which constraints will be added
783 private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints,
784 final ConstraintsBuilder constraints) {
785 final RevisionAwareXPath when = nodeConstraints.getWhenCondition();
786 final Set<MustDefinition> must = nodeConstraints.getMustConstraints();
789 constraints.addWhenCondition(when.toString());
792 for (MustDefinition md : must) {
793 constraints.addMustDefinition(md);
796 constraints.setMandatory(nodeConstraints.isMandatory());
797 constraints.setMinElements(nodeConstraints.getMinElements());
798 constraints.setMaxElements(nodeConstraints.getMaxElements());