Minor code refactoring.
[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.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
27 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
32 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
38 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.impl.AugmentationSchemaBuilderImpl;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingBuilderImpl;
46 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
51 import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
52 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
53 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
54 import org.opendaylight.yangtools.yang.parser.builder.impl.UsesNodeBuilderImpl;
55
56 public final class CopyUtils {
57
58     private CopyUtils() {
59     }
60
61     /**
62      * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
63      * true, qname of node will be corrected based on new parent.
64      * 
65      * @param old
66      *            builder to copy
67      * @param newParent
68      *            new parent
69      * @param updateQName
70      *            flag to indicate if qname should be updated based on new
71      *            parent location
72      * @return copy of given builder
73      */
74     public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
75         if (old instanceof AnyXmlBuilder) {
76             return copy((AnyXmlBuilder) old, newParent, updateQName);
77         } else if (old instanceof ChoiceBuilder) {
78             return copy((ChoiceBuilder) old, newParent, updateQName);
79         } else if (old instanceof ContainerSchemaNodeBuilder) {
80             return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
81         } else if (old instanceof LeafSchemaNodeBuilder) {
82             return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
83         } else if (old instanceof LeafListSchemaNodeBuilder) {
84             return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
85         } else if (old instanceof ListSchemaNodeBuilder) {
86             return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
87         } else if (old instanceof ChoiceCaseBuilder) {
88             return copy((ChoiceCaseBuilder) old, newParent, updateQName);
89         } else {
90             throw new YangParseException(old.getModuleName(), old.getLine(),
91                     "Failed to copy node: Unknown type of DataSchemaNode: " + old);
92         }
93     }
94
95     private static AnyXmlBuilder copy(AnyXmlBuilder old, Builder newParent, boolean updateQName) {
96         DataBean data = getdata(old, newParent, updateQName);
97         QName newQName = data.qname;
98         SchemaPath newSchemaPath = data.schemaPath;
99
100         AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
101         copyConstraints(copy.getConstraints(), old.getConstraints());
102         copy.setParent(newParent);
103         copy.setPath(newSchemaPath);
104         copy.setDescription(old.getDescription());
105         copy.setReference(old.getReference());
106         copy.setStatus(old.getStatus());
107         copy.setAugmenting(old.isAugmenting());
108         copy.setAddedByUses(old.isAddedByUses());
109         copy.setConfiguration(old.isConfiguration());
110         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
111             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
112         }
113
114         return copy;
115     }
116
117     private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) {
118         DataBean data = getdata(old, newParent, updateQName);
119         QName newQName = data.qname;
120         SchemaPath newSchemaPath = data.schemaPath;
121
122         ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
123         copyConstraints(copy.getConstraints(), old.getConstraints());
124         copy.setParent(newParent);
125         copy.setPath(newSchemaPath);
126         copy.setDescription(old.getDescription());
127         copy.setReference(old.getReference());
128         copy.setStatus(old.getStatus());
129         copy.setAugmenting(old.isAugmenting());
130         copy.setAddedByUses(old.isAddedByUses());
131         copy.setConfiguration(old.isConfiguration());
132         for (ChoiceCaseBuilder childNode : old.getCases()) {
133             copy.addCase(copy(childNode, copy, updateQName));
134         }
135         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
136             copy.addAugmentation(copyAugment(augment, copy));
137         }
138         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
139             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
140         }
141
142         return copy;
143     }
144
145     private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) {
146         DataBean data = getdata(old, newParent, updateQName);
147         QName newQName = data.qname;
148         SchemaPath newSchemaPath = data.schemaPath;
149
150         ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
151         copyConstraints(copy.getConstraints(), old.getConstraints());
152         copy.setParent(newParent);
153         copy.setPath(newSchemaPath);
154         copy.setDescription(old.getDescription());
155         copy.setReference(old.getReference());
156         copy.setStatus(old.getStatus());
157         copy.setAugmenting(old.isAugmenting());
158         copy.getChildNodes().addAll(old.getChildNodes());
159         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
160             copy.addChildNode(copy(childNode, copy, updateQName));
161         }
162         copy.getGroupings().addAll(old.getGroupings());
163         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
164             copy.addGrouping(copy(grouping, copy, updateQName));
165         }
166         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
167             copy.addTypedef(copy(tdb, copy, updateQName));
168         }
169         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
170             copy.addUsesNode(copyUses(oldUses, copy));
171         }
172         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
173             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
174         }
175
176         return copy;
177     }
178
179     private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent,
180             boolean updateQName) {
181         DataBean data = getdata(old, newParent, updateQName);
182         QName newQName = data.qname;
183         SchemaPath newSchemaPath = data.schemaPath;
184
185         ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(),
186                 newParent.getLine(), newQName, newSchemaPath);
187         copyConstraints(copy.getConstraints(), old.getConstraints());
188         copy.setParent(newParent);
189         copy.setPath(newSchemaPath);
190         copy.setDescription(old.getDescription());
191         copy.setReference(old.getReference());
192         copy.setStatus(old.getStatus());
193         copy.setPresence(old.isPresence());
194         copy.setAugmenting(old.isAugmenting());
195         copy.setAddedByUses(old.isAddedByUses());
196         copy.setConfiguration(old.isConfiguration());
197         copy.setChildNodes(old.getChildNodes());
198         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
199             copy.addChildNode(copy(childNode, copy, updateQName));
200         }
201         copy.getGroupings().addAll(old.getGroupings());
202         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
203             copy.addGrouping(copy(grouping, copy, updateQName));
204         }
205         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
206             copy.addTypedef(copy(tdb, copy, updateQName));
207         }
208         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
209             copy.addUsesNode(copyUses(oldUses, copy));
210         }
211         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
212             copy.addAugmentation(copyAugment(augment, copy));
213         }
214         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
215             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
216         }
217
218         return copy;
219     }
220
221     private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
222         DataBean data = getdata(old, newParent, updateQName);
223         QName newQName = data.qname;
224         SchemaPath newSchemaPath = data.schemaPath;
225
226         LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
227                 newQName, newSchemaPath);
228         copyConstraints(copy.getConstraints(), old.getConstraints());
229         copy.setParent(newParent);
230         copy.setPath(newSchemaPath);
231         copy.setDescription(old.getDescription());
232         copy.setReference(old.getReference());
233         copy.setStatus(old.getStatus());
234         copy.setAugmenting(old.isAugmenting());
235         copy.setAddedByUses(old.isAddedByUses());
236         copy.setConfiguration(old.isConfiguration());
237         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
238             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
239         }
240
241         if (old.getType() == null) {
242             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
243         } else {
244             copy.setType(old.getType());
245         }
246
247         copy.setDefaultStr(old.getDefaultStr());
248         copy.setUnits(old.getUnits());
249
250         return copy;
251     }
252
253     public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
254         DataBean data = getdata(old, newParent, updateQName);
255         QName newQName = data.qname;
256         SchemaPath newSchemaPath = data.schemaPath;
257
258         LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
259                 newQName, newSchemaPath);
260         copyConstraints(copy.getConstraints(), old.getConstraints());
261         copy.setParent(newParent);
262         copy.setPath(newSchemaPath);
263         copy.setDescription(old.getDescription());
264         copy.setReference(old.getReference());
265         copy.setStatus(old.getStatus());
266         copy.setAugmenting(old.isAugmenting());
267         copy.setAddedByUses(old.isAddedByUses());
268         copy.setConfiguration(old.isConfiguration());
269         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
270             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
271         }
272
273         if (old.getType() == null) {
274             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
275         } else {
276             copy.setType(old.getType());
277         }
278
279         copy.setUserOrdered(old.isUserOrdered());
280
281         return copy;
282     }
283
284     private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
285         DataBean data = getdata(old, newParent, updateQName);
286         QName newQName = data.qname;
287         SchemaPath newSchemaPath = data.schemaPath;
288
289         ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
290                 newQName, newSchemaPath);
291         copyConstraints(copy.getConstraints(), old.getConstraints());
292         copy.setParent(newParent);
293         copy.setPath(newSchemaPath);
294         copy.setDescription(old.getDescription());
295         copy.setReference(old.getReference());
296         copy.setStatus(old.getStatus());
297         copy.setAugmenting(old.isAugmenting());
298         copy.setAddedByUses(old.isAddedByUses());
299         copy.setConfiguration(old.isConfiguration());
300         copy.setChildNodes(old.getChildNodes());
301         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
302             copy.addChildNode(copy(childNode, copy, updateQName));
303         }
304         copy.getGroupings().addAll(old.getGroupings());
305         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
306             copy.addGrouping(copy(grouping, copy, updateQName));
307         }
308         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
309             copy.addTypedef(copy(tdb, copy, updateQName));
310         }
311         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
312             copy.addUsesNode(copyUses(oldUses, copy));
313         }
314         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
315             copy.addAugmentation(copyAugment(augment, copy));
316         }
317         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
318             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
319         }
320
321         copy.setUserOrdered(old.isUserOrdered());
322         copy.setKeyDefinition(old.getKeyDefinition());
323
324         return copy;
325     }
326
327     public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) {
328         DataBean data = getdata(old, newParent, updateQName);
329         QName newQName = data.qname;
330         SchemaPath newSchemaPath = data.schemaPath;
331
332         GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
333         copy.setParent(newParent);
334         copy.setPath(newSchemaPath);
335         copy.setDescription(old.getDescription());
336         copy.setReference(old.getReference());
337         copy.setStatus(old.getStatus());
338         copy.setAddedByUses(old.isAddedByUses());
339         copy.setChildNodes(old.getChildNodes());
340         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
341             copy.addChildNode(copy(childNode, copy, updateQName));
342         }
343         copy.getGroupings().addAll(old.getGroupings());
344         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
345             copy.addGrouping(copy(grouping, copy, updateQName));
346         }
347         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
348             copy.addTypedef(copy(tdb, copy, updateQName));
349         }
350         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
351             copy.addUsesNode(copyUses(oldUses, copy));
352         }
353         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
354             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
355         }
356
357         return copy;
358     }
359
360     public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
361         DataBean data = getdata(old, newParent, updateQName);
362         QName newQName = data.qname;
363         SchemaPath newSchemaPath = data.schemaPath;
364         TypeDefinitionBuilder type;
365
366         if (old instanceof UnionTypeBuilder) {
367             UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
368             type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
369             type.setParent(newParent);
370             for (TypeDefinition<?> td : oldUnion.getTypes()) {
371                 type.setType(td);
372             }
373             for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
374                 type.setTypedef(copy(tdb, type, updateQName));
375             }
376         } else if (old instanceof IdentityrefTypeBuilder) {
377             type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
378                     ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
379             type.setParent(newParent);
380             type.setPath(newSchemaPath);
381         } else {
382             type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
383             type.setParent(newParent);
384             // TODO
385             // type.setPath(newSchemaPath);
386             type.setPath(old.getPath());
387
388             if (old.getType() == null) {
389                 type.setTypedef(copy(old.getTypedef(), type, updateQName));
390             } else {
391                 type.setType(old.getType());
392             }
393
394             for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
395                 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
396             }
397
398             type.setRanges(old.getRanges());
399             type.setLengths(old.getLengths());
400             type.setPatterns(old.getPatterns());
401             type.setFractionDigits(old.getFractionDigits());
402             type.setDescription(old.getDescription());
403             type.setReference(old.getReference());
404             type.setStatus(old.getStatus());
405             type.setUnits(old.getUnits());
406             type.setDefaultValue(old.getDefaultValue());
407             type.setAddedByUses(old.isAddedByUses());
408         }
409
410         return type;
411     }
412
413     private static ConstraintsBuilder copyConstraints(ConstraintsBuilder newConstraints, ConstraintsBuilder old) {
414         newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
415         newConstraints.addWhenCondition(old.getWhenCondition());
416         newConstraints.setMandatory(old.isMandatory());
417         newConstraints.setMinElements(old.getMinElements());
418         newConstraints.setMaxElements(old.getMaxElements());
419         return newConstraints;
420     }
421
422     static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
423         UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
424                 old.getGroupingPathAsString(), true);
425         copy.setParent(newParent);
426         copy.setGroupingDefinition(old.getGroupingDefinition());
427         copy.setGrouping(old.getGroupingBuilder());
428         copy.setAddedByUses(old.isAddedByUses());
429         copy.getAugmentations().addAll(old.getAugmentations());
430         copy.getRefineNodes().addAll(old.getRefineNodes());
431         copy.getRefines().addAll(old.getRefines());
432         copy.setAugmenting(old.isAugmenting());
433         copy.setParentAugment(old.getParentAugment());
434
435         // target child nodes
436         Set<DataSchemaNodeBuilder> newTargetChildren = new HashSet<>();
437         for (DataSchemaNodeBuilder dnb : old.getTargetChildren()) {
438             newTargetChildren.add(copy(dnb, newParent, true));
439         }
440         copy.getTargetChildren().addAll(newTargetChildren);
441
442         // target typedefs
443         Set<TypeDefinitionBuilder> newTargetTypedefs = new HashSet<>();
444         for (TypeDefinitionBuilder tdb : old.getTargetTypedefs()) {
445             newTargetTypedefs.add(copy(tdb, newParent, true));
446         }
447         copy.getTargetTypedefs().addAll(newTargetTypedefs);
448
449         // target groupings
450         Set<GroupingBuilder> newTargetGroupings = new HashSet<>();
451         for (GroupingBuilder gb : old.getTargetGroupings()) {
452             newTargetGroupings.add(copy(gb, newParent, true));
453         }
454         copy.getTargetGroupings().addAll(newTargetGroupings);
455
456         // target unknown nodes
457         Set<UnknownSchemaNodeBuilder> newTargetUnknownNodes = new HashSet<>();
458         for (UnknownSchemaNodeBuilder unb : old.getTargetUnknownNodes()) {
459             newTargetUnknownNodes.add(copy(unb, newParent, true));
460         }
461         copy.getTargetUnknownNodes().addAll(newTargetUnknownNodes);
462
463         // add new uses to collection of uses in module
464         ModuleBuilder module = ParserUtils.getParentModule(newParent);
465         module.getAllUsesNodes().add(copy);
466
467         return copy;
468     }
469
470     private static AugmentationSchemaBuilder copyAugment(AugmentationSchemaBuilder old, Builder newParent) {
471         AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
472                 newParent.getLine(), old.getTargetPathAsString());
473         copy.setParent(newParent);
474
475         copy.setDescription(old.getDescription());
476         copy.setReference(old.getReference());
477         copy.setStatus(old.getStatus());
478         copy.addWhenCondition(old.getWhenCondition());
479         copy.setChildNodes(old.getChildNodes());
480         copy.setTargetNodeSchemaPath(old.getTargetNodeSchemaPath());
481         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
482             copy.addChildNode(copy(childNode, copy, false));
483         }
484         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
485             copy.addUsesNode(copyUses(oldUses, copy));
486         }
487         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
488             copy.addUnknownNodeBuilder((copy(un, copy, false)));
489         }
490
491         return copy;
492     }
493
494     static UnknownSchemaNodeBuilder copy(UnknownSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
495         DataBean data = getdata(old, newParent, updateQName);
496         QName newQName = data.qname;
497         SchemaPath newSchemaPath = data.schemaPath;
498
499         UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
500                 newQName);
501
502         c.setParent(newParent);
503         c.setPath(newSchemaPath);
504         c.setDescription(old.getDescription());
505         c.setReference(old.getReference());
506         c.setStatus(old.getStatus());
507         c.setAddedByUses(old.isAddedByUses());
508         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
509             c.addUnknownNodeBuilder((copy(un, c, updateQName)));
510         }
511
512         return c;
513     }
514
515     private static DataBean getdata(SchemaNodeBuilder old, Builder newParent, boolean updateQName) {
516         List<QName> newPath = null;
517         QName newQName = null;
518         if (newParent instanceof ModuleBuilder) {
519             ModuleBuilder parent = (ModuleBuilder) newParent;
520             if (updateQName) {
521                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
522                         .getLocalName());
523                 newPath = Collections.singletonList(newQName);
524             } else {
525                 newQName = old.getQName();
526                 newPath = Collections.singletonList(newQName);
527             }
528         } else if (newParent instanceof AugmentationSchemaBuilder) {
529             AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
530             ModuleBuilder parent = ParserUtils.getParentModule(newParent);
531             if (updateQName) {
532                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
533                         .getLocalName());
534                 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
535                 newPath.add(newQName);
536             } else {
537                 newQName = old.getQName();
538                 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
539                 newPath.add(newQName);
540             }
541
542         } else if (newParent instanceof SchemaNodeBuilder) {
543             SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
544             QName parentQName = parent.getQName();
545             if (updateQName) {
546                 newQName = new QName(parentQName.getNamespace(), parentQName.getRevision(), parentQName.getPrefix(),
547                         old.getQName().getLocalName());
548                 newPath = new ArrayList<>(parent.getPath().getPath());
549                 newPath.add(newQName);
550             } else {
551                 newQName = old.getQName();
552                 newPath = new ArrayList<>(parent.getPath().getPath());
553                 newPath.add(newQName);
554             }
555         }
556
557         SchemaPath newSchemaPath = new SchemaPath(newPath, true);
558         return new DataBean(newQName, newSchemaPath);
559     }
560
561     private static final class DataBean {
562         private QName qname;
563         private SchemaPath schemaPath;
564
565         private DataBean(QName qname, SchemaPath schemaPath) {
566             this.qname = qname;
567             this.schemaPath = schemaPath;
568         }
569     }
570
571     /**
572      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
573      * 
574      * @param anyxml
575      *            base anyxml
576      * @param qname
577      *            new qname
578      * @param moduleName
579      *            current module name
580      * @param line
581      *            current line in module
582      * @return anyxml builder based on given anyxml node
583      */
584     public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) {
585         final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath());
586         convertDataSchemaNode(anyxml, builder);
587         builder.setConfiguration(anyxml.isConfiguration());
588         builder.setUnknownNodes(anyxml.getUnknownSchemaNodes());
589         return builder;
590     }
591
592     /**
593      * Create GroupingBuilder from given GroupingDefinition.
594      * 
595      * @param grouping
596      *            base grouping
597      * @param qname
598      *            new qname
599      * @param moduleName
600      *            current module name
601      * @param line
602      *            current line in module
603      * @return grouping builder based on given grouping node
604      */
605     public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) {
606         final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname);
607         builder.setPath(grouping.getPath());
608         builder.setChildNodes(grouping.getChildNodes());
609         builder.setGroupings(grouping.getGroupings());
610         builder.setTypedefs(grouping.getTypeDefinitions());
611         builder.setUsesnodes(grouping.getUses());
612         builder.setUnknownNodes(grouping.getUnknownSchemaNodes());
613         builder.setDescription(grouping.getDescription());
614         builder.setReference(grouping.getReference());
615         builder.setStatus(grouping.getStatus());
616         return builder;
617     }
618
619     /**
620      * Create TypeDefinitionBuilder from given ExtendedType.
621      * 
622      * @param typedef
623      *            base typedef
624      * @param qname
625      *            new qname
626      * @param moduleName
627      *            current module name
628      * @param line
629      *            current line in module
630      * @return typedef builder based on given typedef node
631      */
632     public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) {
633         final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname);
634         builder.setPath(typedef.getPath());
635         builder.setDefaultValue(typedef.getDefaultValue());
636         builder.setUnits(typedef.getUnits());
637         builder.setDescription(typedef.getDescription());
638         builder.setReference(typedef.getReference());
639         builder.setStatus(typedef.getStatus());
640         builder.setRanges(typedef.getRangeConstraints());
641         builder.setLengths(typedef.getLengthConstraints());
642         builder.setPatterns(typedef.getPatternConstraints());
643         builder.setFractionDigits(typedef.getFractionDigits());
644         final TypeDefinition<?> type = typedef.getBaseType();
645         builder.setType(type);
646         builder.setUnits(typedef.getUnits());
647         builder.setUnknownNodes(typedef.getUnknownSchemaNodes());
648         return builder;
649     }
650
651     /**
652      * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
653      * 
654      * @param unknownNode
655      *            base unknown node
656      * @param qname
657      *            new qname
658      * @param moduleName
659      *            current module name
660      * @param line
661      *            current line in module
662      * @return unknown node builder based on given unknown node
663      */
664     public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname,
665             String moduleName, int line) {
666         final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname);
667         builder.setPath(unknownNode.getPath());
668         builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes());
669         builder.setDescription(unknownNode.getDescription());
670         builder.setReference(unknownNode.getReference());
671         builder.setStatus(unknownNode.getStatus());
672         builder.setAddedByUses(unknownNode.isAddedByUses());
673         builder.setNodeType(unknownNode.getNodeType());
674         builder.setNodeParameter(unknownNode.getNodeParameter());
675         return builder;
676     }
677
678     /**
679      * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
680      * 
681      * @param leaf
682      *            leaf from which to create builder
683      * @param qname
684      *            new qname
685      * @param moduleName
686      *            current module name
687      * @param line
688      *            line in module
689      * @return leaf builder based on given leaf node
690      */
691     public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) {
692         final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath());
693         convertDataSchemaNode(leaf, builder);
694         builder.setConfiguration(leaf.isConfiguration());
695         final TypeDefinition<?> type = leaf.getType();
696         builder.setType(type);
697         builder.setPath(leaf.getPath());
698         builder.setUnknownNodes(leaf.getUnknownSchemaNodes());
699         builder.setDefaultStr(leaf.getDefault());
700         builder.setUnits(leaf.getUnits());
701         return builder;
702     }
703
704     /**
705      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
706      * 
707      * @param container
708      *            base container
709      * @param qname
710      *            new qname
711      * @param moduleName
712      *            current module name
713      * @param line
714      *            current line in module
715      * @return container builder based on given container node
716      */
717     public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname,
718             String moduleName, int line) {
719         final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname,
720                 container.getPath());
721         convertDataSchemaNode(container, builder);
722         builder.setConfiguration(container.isConfiguration());
723         builder.setUnknownNodes(container.getUnknownSchemaNodes());
724         builder.setChildNodes(container.getChildNodes());
725         builder.setGroupings(container.getGroupings());
726         builder.setTypedefs(container.getTypeDefinitions());
727         builder.setAugmentations(container.getAvailableAugmentations());
728         builder.setUsesnodes(container.getUses());
729         builder.setPresence(container.isPresenceContainer());
730         return builder;
731     }
732
733     /**
734      * Create ListSchemaNodeBuilder from given ListSchemaNode.
735      * 
736      * @param list
737      *            base list
738      * @param qname
739      *            new qname
740      * @param moduleName
741      *            current module name
742      * @param line
743      *            current line in module
744      * @return list builder based on given list node
745      */
746     public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) {
747         ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath());
748         convertDataSchemaNode(list, builder);
749         builder.setConfiguration(list.isConfiguration());
750         builder.setUnknownNodes(list.getUnknownSchemaNodes());
751         builder.setTypedefs(list.getTypeDefinitions());
752         builder.setChildNodes(list.getChildNodes());
753         builder.setGroupings(list.getGroupings());
754         builder.setAugmentations(list.getAvailableAugmentations());
755         builder.setUsesnodes(list.getUses());
756         builder.setUserOrdered(builder.isUserOrdered());
757         return builder;
758     }
759
760     /**
761      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
762      * 
763      * @param leafList
764      *            base leaf-list
765      * @param qname
766      *            new qname
767      * @param moduleName
768      *            current module name
769      * @param line
770      *            current line in module
771      * @return leaf-list builder based on given leaf-list node
772      */
773     public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
774             int line) {
775         final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
776                 leafList.getPath());
777         convertDataSchemaNode(leafList, builder);
778         builder.setConfiguration(leafList.isConfiguration());
779         builder.setType(leafList.getType());
780         builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
781         builder.setUserOrdered(leafList.isUserOrdered());
782         return builder;
783     }
784
785     /**
786      * Create ChoiceBuilder from given ChoiceNode.
787      * 
788      * @param choice
789      *            base choice
790      * @param qname
791      *            new qname
792      * @param moduleName
793      *            current module name
794      * @param line
795      *            current line in module
796      * @return choice builder based on given choice node
797      */
798     public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) {
799         final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname);
800         convertDataSchemaNode(choice, builder);
801         builder.setConfiguration(choice.isConfiguration());
802         builder.setCases(choice.getCases());
803         builder.setUnknownNodes(choice.getUnknownSchemaNodes());
804         builder.setDefaultCase(choice.getDefaultCase());
805         return builder;
806     }
807
808     /**
809      * Set DataSchemaNode arguments to builder object
810      * 
811      * @param node
812      *            node from which arguments should be read
813      * @param builder
814      *            builder to which arguments should be set
815      */
816     private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) {
817         builder.setPath(node.getPath());
818         builder.setDescription(node.getDescription());
819         builder.setReference(node.getReference());
820         builder.setStatus(node.getStatus());
821         builder.setAugmenting(node.isAugmenting());
822         copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints());
823     }
824
825     /**
826      * Copy constraints from constraints definition to constraints builder.
827      * 
828      * @param nodeConstraints
829      *            definition from which constraints will be copied
830      * @param constraints
831      *            builder to which constraints will be added
832      */
833     private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints,
834             final ConstraintsBuilder constraints) {
835         final RevisionAwareXPath when = nodeConstraints.getWhenCondition();
836         final Set<MustDefinition> must = nodeConstraints.getMustConstraints();
837
838         if (when != null) {
839             constraints.addWhenCondition(when.toString());
840         }
841         if (must != null) {
842             for (MustDefinition md : must) {
843                 constraints.addMustDefinition(md);
844             }
845         }
846         constraints.setMandatory(nodeConstraints.isMandatory());
847         constraints.setMinElements(nodeConstraints.getMinElements());
848         constraints.setMaxElements(nodeConstraints.getMaxElements());
849     }
850
851 }