Refactored uses statement handling in parser.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / CopyUtils.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.yangtools.yang.parser.util;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Set;
14
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;
54
55 public final class CopyUtils {
56
57     private CopyUtils() {
58     }
59
60     /**
61      * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
62      * true, qname of node will be corrected based on new parent.
63      *
64      * @param old
65      *            builder to copy
66      * @param newParent
67      *            new parent
68      * @param updateQName
69      *            flag to indicate if qname should be updated based on new
70      *            parent location
71      * @return copy of given builder
72      */
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);
88         } else {
89             throw new YangParseException(old.getModuleName(), old.getLine(),
90                     "Failed to copy node: Unknown type of DataSchemaNode: " + old);
91         }
92     }
93
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;
98
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)));
111         }
112
113         return copy;
114     }
115
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;
120
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));
133         }
134         for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
135             copy.addAugmentation(copyAugment(augment, copy));
136         }
137         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
138             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
139         }
140
141         return copy;
142     }
143
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;
148
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));
160         }
161         copy.getGroupings().addAll(old.getGroupings());
162         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
163             copy.addGrouping(copy(grouping, copy, updateQName));
164         }
165         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
166             copy.addTypedef(copy(tdb, copy, updateQName));
167         }
168         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
169             copy.addUsesNode(copyUses(oldUses, copy));
170         }
171         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
172             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
173         }
174
175         return copy;
176     }
177
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;
183
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));
198         }
199         copy.getGroupings().addAll(old.getGroupings());
200         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
201             copy.addGrouping(copy(grouping, copy, updateQName));
202         }
203         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
204             copy.addTypedef(copy(tdb, copy, updateQName));
205         }
206         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
207             copy.addUsesNode(copyUses(oldUses, copy));
208         }
209         for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
210             copy.addAugmentation(copyAugment(augment, copy));
211         }
212         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
213             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
214         }
215
216         return copy;
217     }
218
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;
223
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)));
237         }
238
239         if (old.getType() == null) {
240             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
241         } else {
242             copy.setType(old.getType());
243         }
244
245         copy.setDefaultStr(old.getDefaultStr());
246         copy.setUnits(old.getUnits());
247
248         return copy;
249     }
250
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;
255
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)));
269         }
270
271         if (old.getType() == null) {
272             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
273         } else {
274             copy.setType(old.getType());
275         }
276
277         copy.setUserOrdered(old.isUserOrdered());
278
279         return copy;
280     }
281
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;
286
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));
300         }
301         copy.getGroupings().addAll(old.getGroupings());
302         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
303             copy.addGrouping(copy(grouping, copy, updateQName));
304         }
305         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
306             copy.addTypedef(copy(tdb, copy, updateQName));
307         }
308         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
309             copy.addUsesNode(copyUses(oldUses, copy));
310         }
311         for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
312             copy.addAugmentation(copyAugment(augment, copy));
313         }
314         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
315             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
316         }
317
318         copy.setUserOrdered(old.isUserOrdered());
319         copy.setKeyDefinition(old.getKeyDefinition());
320
321         return copy;
322     }
323
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;
328
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));
338         }
339         copy.getGroupings().addAll(old.getGroupings());
340         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
341             copy.addGrouping(copy(grouping, copy, updateQName));
342         }
343         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
344             copy.addTypedef(copy(tdb, copy, updateQName));
345         }
346         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
347             copy.addUsesNode(copyUses(oldUses, copy));
348         }
349         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
350             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
351         }
352
353         return copy;
354     }
355
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;
361
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()) {
367                 type.setType(td);
368             }
369             for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
370                 type.setTypedef(copy(tdb, type, updateQName));
371             }
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);
377         } else {
378             type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
379             type.setParent(newParent);
380             // TODO
381             // type.setPath(newSchemaPath);
382             type.setPath(old.getPath());
383
384             if (old.getType() == null) {
385                 type.setTypedef(copy(old.getTypedef(), type, updateQName));
386             } else {
387                 type.setType(old.getType());
388             }
389
390             for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
391                 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
392             }
393
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());
404         }
405
406         return type;
407     }
408
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;
416     }
417
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());
430         return copy;
431     }
432
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);
437
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));
445         }
446         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
447             copy.addUsesNode(copyUses(oldUses, copy));
448         }
449         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
450             copy.addUnknownNodeBuilder((copy(un, copy, false)));
451         }
452
453         return copy;
454     }
455
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;
460
461         UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
462                 newQName);
463
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)));
472         }
473
474         return c;
475     }
476
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;
482             if (updateQName) {
483                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
484                         .getLocalName());
485                 newPath = Collections.singletonList(newQName);
486             } else {
487                 newQName = old.getQName();
488                 newPath = Collections.singletonList(newQName);
489             }
490         } else if (newParent instanceof AugmentationSchemaBuilder) {
491             AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
492             ModuleBuilder parent = ParserUtils.getParentModule(newParent);
493             if (updateQName) {
494                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
495                         .getLocalName());
496                 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
497                 newPath.add(newQName);
498             } else {
499                 newQName = old.getQName();
500                 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
501                 newPath.add(newQName);
502             }
503
504         } else if (newParent instanceof SchemaNodeBuilder) {
505             SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
506             QName parentQName = parent.getQName();
507             if (updateQName) {
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);
512             } else {
513                 newQName = old.getQName();
514                 newPath = new ArrayList<>(parent.getPath().getPath());
515                 newPath.add(newQName);
516             }
517         }
518
519         SchemaPath newSchemaPath = new SchemaPath(newPath, true);
520         return new DataBean(newQName, newSchemaPath);
521     }
522
523     private static final class DataBean {
524         private QName qname;
525         private SchemaPath schemaPath;
526
527         private DataBean(QName qname, SchemaPath schemaPath) {
528             this.qname = qname;
529             this.schemaPath = schemaPath;
530         }
531     }
532
533     /**
534      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
535      *
536      * @param anyxml
537      *            base anyxml
538      * @param qname
539      *            new qname
540      * @param moduleName
541      *            current module name
542      * @param line
543      *            current line in module
544      * @return anyxml builder based on given anyxml node
545      */
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());
551         return builder;
552     }
553
554     /**
555      * Create GroupingBuilder from given GroupingDefinition.
556      *
557      * @param grouping
558      *            base grouping
559      * @param qname
560      *            new qname
561      * @param moduleName
562      *            current module name
563      * @param line
564      *            current line in module
565      * @return grouping builder based on given grouping node
566      */
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);
572         }
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());
580         return builder;
581     }
582
583     /**
584      * Create TypeDefinitionBuilder from given ExtendedType.
585      *
586      * @param typedef
587      *            base typedef
588      * @param qname
589      *            new qname
590      * @param moduleName
591      *            current module name
592      * @param line
593      *            current line in module
594      * @return typedef builder based on given typedef node
595      */
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());
612         return builder;
613     }
614
615     /**
616      * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
617      *
618      * @param unknownNode
619      *            base unknown node
620      * @param qname
621      *            new qname
622      * @param moduleName
623      *            current module name
624      * @param line
625      *            current line in module
626      * @return unknown node builder based on given unknown node
627      */
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());
639         return builder;
640     }
641
642     /**
643      * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
644      *
645      * @param leaf
646      *            leaf from which to create builder
647      * @param qname
648      *            new qname
649      * @param moduleName
650      *            current module name
651      * @param line
652      *            line in module
653      * @return leaf builder based on given leaf node
654      */
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());
665         return builder;
666     }
667
668     /**
669      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
670      *
671      * @param container
672      *            base container
673      * @param qname
674      *            new qname
675      * @param moduleName
676      *            current module name
677      * @param line
678      *            current line in module
679      * @return container builder based on given container node
680      */
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);
690         }
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());
696         return builder;
697     }
698
699     /**
700      * Create ListSchemaNodeBuilder from given ListSchemaNode.
701      *
702      * @param list
703      *            base list
704      * @param qname
705      *            new qname
706      * @param moduleName
707      *            current module name
708      * @param line
709      *            current line in module
710      * @return list builder based on given list node
711      */
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);
720         }
721         builder.setGroupings(list.getGroupings());
722         builder.getAugmentations().addAll(list.getAvailableAugmentations());
723         builder.setUsesnodes(list.getUses());
724         builder.setUserOrdered(builder.isUserOrdered());
725         return builder;
726     }
727
728     /**
729      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
730      *
731      * @param leafList
732      *            base leaf-list
733      * @param qname
734      *            new qname
735      * @param moduleName
736      *            current module name
737      * @param line
738      *            current line in module
739      * @return leaf-list builder based on given leaf-list node
740      */
741     public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
742             int line) {
743         final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
744                 leafList.getPath());
745         convertDataSchemaNode(leafList, builder);
746         builder.setConfiguration(leafList.isConfiguration());
747         builder.setType(leafList.getType());
748         builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
749         builder.setUserOrdered(leafList.isUserOrdered());
750         return builder;
751     }
752
753     /**
754      * Create ChoiceBuilder from given ChoiceNode.
755      *
756      * @param choice
757      *            base choice
758      * @param qname
759      *            new qname
760      * @param moduleName
761      *            current module name
762      * @param line
763      *            current line in module
764      * @return choice builder based on given choice node
765      */
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());
773         return builder;
774     }
775
776     /**
777      * Set DataSchemaNode arguments to builder object
778      *
779      * @param node
780      *            node from which arguments should be read
781      * @param builder
782      *            builder to which arguments should be set
783      */
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());
791     }
792
793     /**
794      * Copy constraints from constraints definition to constraints builder.
795      *
796      * @param nodeConstraints
797      *            definition from which constraints will be copied
798      * @param constraints
799      *            builder to which constraints will be added
800      */
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();
805
806         if (when != null) {
807             constraints.addWhenCondition(when.toString());
808         }
809         if (must != null) {
810             for (MustDefinition md : must) {
811                 constraints.addMustDefinition(md);
812             }
813         }
814         constraints.setMandatory(nodeConstraints.isMandatory());
815         constraints.setMinElements(nodeConstraints.getMinElements());
816         constraints.setMaxElements(nodeConstraints.getMaxElements());
817     }
818
819 }