Fixed path correction process of nodes defined by uses statement.
[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 class CopyUtils {
56
57     /**
58      * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
59      * true, qname of node will be corrected based on new parent.
60      *
61      * @param old
62      * @param newParent
63      * @param updateQName
64      * @return
65      */
66     public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
67         if (old instanceof AnyXmlBuilder) {
68             return copy((AnyXmlBuilder) old, newParent, updateQName);
69         } else if (old instanceof ChoiceBuilder) {
70             return copy((ChoiceBuilder) old, newParent, updateQName);
71         } else if (old instanceof ContainerSchemaNodeBuilder) {
72             return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
73         } else if (old instanceof LeafSchemaNodeBuilder) {
74             return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
75         } else if (old instanceof LeafListSchemaNodeBuilder) {
76             return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
77         } else if (old instanceof ListSchemaNodeBuilder) {
78             return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
79         } else if (old instanceof ChoiceCaseBuilder) {
80             return copy((ChoiceCaseBuilder) old, newParent, updateQName);
81         } else {
82             throw new YangParseException(old.getModuleName(), old.getLine(),
83                     "Failed to copy node: Unknown type of DataSchemaNode: " + old);
84         }
85     }
86
87     private static AnyXmlBuilder copy(AnyXmlBuilder old, Builder newParent, boolean updateQName) {
88         DataBean data = getdata(old, newParent, updateQName);
89         QName newQName = data.qname;
90         SchemaPath newSchemaPath = data.schemaPath;
91
92         AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
93         copyConstraints(copy.getConstraints(), old.getConstraints());
94         copy.setParent(newParent);
95         copy.setPath(newSchemaPath);
96         copy.setDescription(old.getDescription());
97         copy.setReference(old.getReference());
98         copy.setStatus(old.getStatus());
99         copy.setAugmenting(old.isAugmenting());
100         copy.setAddedByUses(old.isAddedByUses());
101         copy.setConfiguration(old.isConfiguration());
102         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
103             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
104         }
105
106         return copy;
107     }
108
109     private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) {
110         DataBean data = getdata(old, newParent, updateQName);
111         QName newQName = data.qname;
112         SchemaPath newSchemaPath = data.schemaPath;
113
114         ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
115         copyConstraints(copy.getConstraints(), old.getConstraints());
116         copy.setParent(newParent);
117         copy.setPath(newSchemaPath);
118         copy.setDescription(old.getDescription());
119         copy.setReference(old.getReference());
120         copy.setStatus(old.getStatus());
121         copy.setAugmenting(old.isAugmenting());
122         copy.setAddedByUses(old.isAddedByUses());
123         copy.setConfiguration(old.isConfiguration());
124         for (ChoiceCaseBuilder childNode : old.getCases()) {
125             copy.addCase(copy(childNode, copy, updateQName));
126         }
127         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
128             copy.addAugmentation(copyAugment(augment, copy));
129         }
130         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
131             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
132         }
133
134         return copy;
135     }
136
137     private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) {
138         DataBean data = getdata(old, newParent, updateQName);
139         QName newQName = data.qname;
140         SchemaPath newSchemaPath = data.schemaPath;
141
142         ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
143         copyConstraints(copy.getConstraints(), old.getConstraints());
144         copy.setParent(newParent);
145         copy.setPath(newSchemaPath);
146         copy.setDescription(old.getDescription());
147         copy.setReference(old.getReference());
148         copy.setStatus(old.getStatus());
149         copy.setAugmenting(old.isAugmenting());
150         copy.getChildNodes().addAll(old.getChildNodes());
151         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
152             copy.addChildNode(copy(childNode, copy, updateQName));
153         }
154         copy.getGroupings().addAll(old.getGroupings());
155         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
156             copy.addGrouping(copy(grouping, copy, updateQName));
157         }
158         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
159             copy.addTypedef(copy(tdb, copy, updateQName));
160         }
161         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
162             copy.addUsesNode(copyUses(oldUses, copy));
163         }
164         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
165             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
166         }
167
168         return copy;
169     }
170
171     private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent,
172             boolean updateQName) {
173         DataBean data = getdata(old, newParent, updateQName);
174         QName newQName = data.qname;
175         SchemaPath newSchemaPath = data.schemaPath;
176
177         ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
178                 newQName, newSchemaPath);
179         copyConstraints(copy.getConstraints(), old.getConstraints());
180         copy.setParent(newParent);
181         copy.setPath(newSchemaPath);
182         copy.setDescription(old.getDescription());
183         copy.setReference(old.getReference());
184         copy.setStatus(old.getStatus());
185         copy.setPresence(old.isPresence());
186         copy.setAugmenting(old.isAugmenting());
187         copy.setAddedByUses(old.isAddedByUses());
188         copy.setConfiguration(old.isConfiguration());
189         copy.setChildNodes(old.getChildNodes());
190         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
191             copy.addChildNode(copy(childNode, copy, updateQName));
192         }
193         copy.getGroupings().addAll(old.getGroupings());
194         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
195             copy.addGrouping(copy(grouping, copy, updateQName));
196         }
197         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
198             copy.addTypedef(copy(tdb, copy, updateQName));
199         }
200         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
201             copy.addUsesNode(copyUses(oldUses, copy));
202         }
203         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
204             copy.addAugmentation(copyAugment(augment, copy));
205         }
206         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
207             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
208         }
209
210         return copy;
211     }
212
213     private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
214         DataBean data = getdata(old, newParent, updateQName);
215         QName newQName = data.qname;
216         SchemaPath newSchemaPath = data.schemaPath;
217
218         LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName,
219                 newSchemaPath);
220         copyConstraints(copy.getConstraints(), old.getConstraints());
221         copy.setParent(newParent);
222         copy.setPath(newSchemaPath);
223         copy.setDescription(old.getDescription());
224         copy.setReference(old.getReference());
225         copy.setStatus(old.getStatus());
226         copy.setAugmenting(old.isAugmenting());
227         copy.setAddedByUses(old.isAddedByUses());
228         copy.setConfiguration(old.isConfiguration());
229         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
230             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
231         }
232
233         if (old.getType() == null) {
234             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
235         } else {
236             copy.setType(ParserUtils.createCorrectTypeDefinition(copy.getPath(), old.getType()));
237         }
238
239         copy.setDefaultStr(old.getDefaultStr());
240         copy.setUnits(old.getUnits());
241
242         return copy;
243     }
244
245     public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
246         DataBean data = getdata(old, newParent, updateQName);
247         QName newQName = data.qname;
248         SchemaPath newSchemaPath = data.schemaPath;
249
250         LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
251                 newQName, newSchemaPath);
252         copyConstraints(copy.getConstraints(), old.getConstraints());
253         copy.setParent(newParent);
254         copy.setPath(newSchemaPath);
255         copy.setDescription(old.getDescription());
256         copy.setReference(old.getReference());
257         copy.setStatus(old.getStatus());
258         copy.setAugmenting(old.isAugmenting());
259         copy.setAddedByUses(old.isAddedByUses());
260         copy.setConfiguration(old.isConfiguration());
261         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
262             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
263         }
264
265         if (old.getType() == null) {
266             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
267         } else {
268             copy.setType(ParserUtils.createCorrectTypeDefinition(copy.getPath(), old.getType()));
269         }
270
271         copy.setUserOrdered(old.isUserOrdered());
272
273         return copy;
274     }
275
276     private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
277         DataBean data = getdata(old, newParent, updateQName);
278         QName newQName = data.qname;
279         SchemaPath newSchemaPath = data.schemaPath;
280
281         ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName,
282                 newSchemaPath);
283         copyConstraints(copy.getConstraints(), old.getConstraints());
284         copy.setParent(newParent);
285         copy.setPath(newSchemaPath);
286         copy.setDescription(old.getDescription());
287         copy.setReference(old.getReference());
288         copy.setStatus(old.getStatus());
289         copy.setAugmenting(old.isAugmenting());
290         copy.setAddedByUses(old.isAddedByUses());
291         copy.setConfiguration(old.isConfiguration());
292         copy.setChildNodes(old.getChildNodes());
293         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
294             copy.addChildNode(copy(childNode, copy, updateQName));
295         }
296         copy.getGroupings().addAll(old.getGroupings());
297         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
298             copy.addGrouping(copy(grouping, copy, updateQName));
299         }
300         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
301             copy.addTypedef(copy(tdb, copy, updateQName));
302         }
303         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
304             copy.addUsesNode(copyUses(oldUses, copy));
305         }
306         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
307             copy.addAugmentation(copyAugment(augment, copy));
308         }
309         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
310             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
311         }
312
313         copy.setUserOrdered(old.isUserOrdered());
314         copy.setKeyDefinition(old.getKeyDefinition());
315
316         return copy;
317     }
318
319     public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) {
320         DataBean data = getdata(old, newParent, updateQName);
321         QName newQName = data.qname;
322         SchemaPath newSchemaPath = data.schemaPath;
323
324         GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
325         copy.setParent(newParent);
326         copy.setPath(newSchemaPath);
327         copy.setDescription(old.getDescription());
328         copy.setReference(old.getReference());
329         copy.setStatus(old.getStatus());
330         copy.setAddedByUses(old.isAddedByUses());
331         copy.setChildNodes(old.getChildNodes());
332         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
333             copy.addChildNode(copy(childNode, copy, updateQName));
334         }
335         copy.getGroupings().addAll(old.getGroupings());
336         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
337             copy.addGrouping(copy(grouping, copy, updateQName));
338         }
339         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
340             copy.addTypedef(copy(tdb, copy, updateQName));
341         }
342         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
343             copy.addUsesNode(copyUses(oldUses, copy));
344         }
345         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
346             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
347         }
348
349         return copy;
350     }
351
352     public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
353         DataBean data = getdata(old, newParent, updateQName);
354         QName newQName = data.qname;
355         SchemaPath newSchemaPath = data.schemaPath;
356         TypeDefinitionBuilder type = null;
357
358         if (old instanceof UnionTypeBuilder) {
359             UnionTypeBuilder oldUnion = (UnionTypeBuilder)old;
360             type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
361             type.setParent(newParent);
362             type.setPath(newSchemaPath);
363             for(TypeDefinition<?> td : oldUnion.getTypes()) {
364                 type.setType(ParserUtils.createCorrectTypeDefinition(type.getPath(), td));
365             }
366             for(TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
367                 type.setTypedef(copy(tdb, type, updateQName));
368             }
369         } else if (old instanceof IdentityrefTypeBuilder) {
370             type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
371                     ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
372             type.setParent(newParent);
373             type.setPath(newSchemaPath);
374         } else {
375             type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
376             type.setParent(newParent);
377             type.setPath(newSchemaPath);
378
379             if (old.getType() == null) {
380                 type.setTypedef(copy(old.getTypedef(), type, updateQName));
381             } else {
382                 type.setType(ParserUtils.createCorrectTypeDefinition(type.getPath(), old.getType()));
383             }
384
385             for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
386                 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
387             }
388
389             type.setRanges(old.getRanges());
390             type.setLengths(old.getLengths());
391             type.setPatterns(old.getPatterns());
392             type.setFractionDigits(old.getFractionDigits());
393             type.setDescription(old.getDescription());
394             type.setReference(old.getReference());
395             type.setStatus(old.getStatus());
396             type.setUnits(old.getUnits());
397             type.setDefaultValue(old.getDefaultValue());
398             type.setAddedByUses(old.isAddedByUses());
399         }
400
401         return type;
402     }
403
404     private static ConstraintsBuilder copyConstraints(ConstraintsBuilder newConstraints, ConstraintsBuilder old) {
405         newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
406         newConstraints.addWhenCondition(old.getWhenCondition());
407         newConstraints.setMandatory(old.isMandatory());
408         newConstraints.setMinElements(old.getMinElements());
409         newConstraints.setMaxElements(old.getMaxElements());
410         return newConstraints;
411     }
412
413     static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
414         UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
415                 old.getGroupingName(), true);
416         copy.setParent(newParent);
417         copy.setGroupingPath(old.getGroupingPath());
418         // TODO grouping vs grouping path?
419         copy.setGrouping(old.getGroupingBuilder());
420         copy.setAugmenting(old.isAugmenting());
421         copy.setAddedByUses(old.isAddedByUses());
422         copy.getAugmentations().addAll(old.getAugmentations());
423         copy.getRefineNodes().addAll(old.getRefineNodes());
424         copy.getRefines().addAll(old.getRefines());
425
426         copy.setTargetChildren(old.getTargetChildren());
427         copy.setTargetTypedefs(old.getTargetTypedefs());
428         copy.setTargetGroupings(old.getTargetGroupings());
429         copy.setTargetUnknownNodes(old.getTargetUnknownNodes());
430
431         // add new uses to collection of uses in module
432         ModuleBuilder module = ParserUtils.getParentModule(newParent);
433         module.getAllUsesNodes().add(copy);
434
435         return copy;
436     }
437
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);
442
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));
450         }
451         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
452             copy.addUsesNode(copyUses(oldUses, copy));
453         }
454         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
455             copy.addUnknownNodeBuilder((copy(un, copy, false)));
456         }
457
458         return copy;
459     }
460
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;
465
466         UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
467                 newQName);
468
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)));
477         }
478
479         return c;
480     }
481
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;
487             if (updateQName) {
488                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
489                         .getLocalName());
490                 newPath = Collections.singletonList(newQName);
491             } else {
492                 newQName = old.getQName();
493                 newPath = Collections.singletonList(newQName);
494             }
495         } else if (newParent instanceof AugmentationSchemaBuilder) {
496             ModuleBuilder parent = ParserUtils.getParentModule(newParent);
497             if (updateQName) {
498                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
499                         .getLocalName());
500                 newPath = Collections.singletonList(newQName);
501             } else {
502                 newQName = old.getQName();
503                 newPath = Collections.singletonList(newQName);
504             }
505
506         } else if (newParent instanceof SchemaNodeBuilder) {
507             SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
508             QName parentQName = parent.getQName();
509             if (updateQName) {
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);
514             } else {
515                 newQName = old.getQName();
516                 newPath = new ArrayList<>(parent.getPath().getPath());
517                 newPath.add(newQName);
518             }
519         }
520
521         SchemaPath newSchemaPath = new SchemaPath(newPath, true);
522         return new DataBean(newQName, newSchemaPath);
523     }
524
525     private static class DataBean {
526         private QName qname;
527         private SchemaPath schemaPath;
528
529         private DataBean(QName qname, SchemaPath schemaPath) {
530             this.qname = qname;
531             this.schemaPath = schemaPath;
532         }
533     }
534
535
536     /**
537      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
538      *
539      * @param anyxml
540      * @param qname
541      * @param moduleName
542      *            current module name
543      * @param line
544      *            current line in module
545      * @return anyxml builder based on given anyxml node
546      */
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());
552         return builder;
553     }
554
555     /**
556      * Create GroupingBuilder from given GroupingDefinition.
557      *
558      * @param grouping
559      * @param qname
560      * @param moduleName
561      *            current module name
562      * @param line
563      *            current line in module
564      * @return grouping builder based on given grouping node
565      */
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());
577         return builder;
578     }
579
580     /**
581      * Create TypeDefinitionBuilder from given ExtendedType.
582      *
583      * @param typedef
584      * @param qname
585      * @param moduleName
586      *            current module name
587      * @param line
588      *            current line in module
589      * @return typedef builder based on given typedef node
590      */
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());
607         return builder;
608     }
609
610     /**
611      * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
612      *
613      * @param unknownNode
614      * @param qname
615      * @param moduleName
616      *            current module name
617      * @param line
618      *            current line in module
619      * @return unknown node builder based on given unknown node
620      */
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());
632         return builder;
633     }
634
635
636     /**
637      * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
638      *
639      * @param leaf
640      *            leaf from which to create builder
641      * @param qname
642      * @param moduleName
643      *            current module name
644      * @param line
645      *            line in module
646      * @return leaf builder based on given leaf node
647      */
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());
658         return builder;
659     }
660
661     /**
662      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
663      *
664      * @param container
665      * @param qname
666      * @param moduleName
667      *            current module name
668      * @param line
669      *            current line in module
670      * @return container builder based on given container node
671      */
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());
685         return builder;
686     }
687
688     /**
689      * Create ListSchemaNodeBuilder from given ListSchemaNode.
690      *
691      * @param list
692      * @param qname
693      * @param moduleName
694      *            current module name
695      * @param line
696      *            current line in module
697      * @return list builder based on given list node
698      */
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());
710         return builder;
711     }
712
713     /**
714      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
715      *
716      * @param leafList
717      * @param qname
718      * @param moduleName
719      *            current module name
720      * @param line
721      *            current line in module
722      * @return leaf-list builder based on given leaf-list node
723      */
724     public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
725             int line) {
726         final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
727                 leafList.getPath());
728         convertDataSchemaNode(leafList, builder);
729         builder.setConfiguration(leafList.isConfiguration());
730         builder.setType(leafList.getType());
731         builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
732         builder.setUserOrdered(leafList.isUserOrdered());
733         return builder;
734     }
735
736     /**
737      * Create ChoiceBuilder from given ChoiceNode.
738      *
739      * @param choice
740      * @param qname
741      * @param moduleName
742      *            current module name
743      * @param line
744      *            current line in module
745      * @return choice builder based on given choice node
746      */
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());
754         return builder;
755     }
756
757
758     /**
759      * Set DataSchemaNode arguments to builder object
760      *
761      * @param node
762      *            node from which arguments should be read
763      * @param builder
764      *            builder to which arguments should be set
765      */
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());
773     }
774
775     /**
776      * Copy constraints from constraints definition to constraints builder.
777      *
778      * @param nodeConstraints
779      *            definition from which constraints will be copied
780      * @param constraints
781      *            builder to which constraints will be added
782      */
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();
787
788         if (when != null) {
789             constraints.addWhenCondition(when.toString());
790         }
791         if (must != null) {
792             for (MustDefinition md : must) {
793                 constraints.addMustDefinition(md);
794             }
795         }
796         constraints.setMandatory(nodeConstraints.isMandatory());
797         constraints.setMinElements(nodeConstraints.getMinElements());
798         constraints.setMaxElements(nodeConstraints.getMaxElements());
799     }
800
801 }