2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/eplv10.html
8 package org.opendaylight.yangtools.yang.parser.util;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.Stack;
17 import org.antlr.v4.runtime.tree.ParseTree;
18 import org.opendaylight.yangtools.antlrv4.code.gen.*;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Bit_stmtContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Bits_specificationContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Config_argContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Config_stmtContext;
25 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Decimal64_specificationContext;
26 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Default_stmtContext;
27 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Description_stmtContext;
28 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Enum_specificationContext;
29 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Enum_stmtContext;
30 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Error_app_tag_stmtContext;
31 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Error_message_stmtContext;
32 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Fraction_digits_stmtContext;
33 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Identityref_specificationContext;
34 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Instance_identifier_specificationContext;
35 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leafref_specificationContext;
36 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Length_stmtContext;
37 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Mandatory_argContext;
38 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Mandatory_stmtContext;
39 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Max_elements_stmtContext;
40 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Max_value_argContext;
41 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Min_elements_stmtContext;
42 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Min_value_argContext;
43 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Must_stmtContext;
44 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Numerical_restrictionsContext;
45 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_argContext;
46 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
47 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Path_stmtContext;
48 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Pattern_stmtContext;
49 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Position_stmtContext;
50 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Presence_stmtContext;
51 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Range_stmtContext;
52 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Reference_stmtContext;
53 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_anyxml_stmtsContext;
54 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_choice_stmtsContext;
55 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_container_stmtsContext;
56 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_leaf_list_stmtsContext;
57 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_leaf_stmtsContext;
58 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_list_stmtsContext;
59 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_pomContext;
60 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Refine_stmtContext;
61 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Require_instance_argContext;
62 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Require_instance_stmtContext;
63 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_argContext;
64 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_stmtContext;
65 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.StringContext;
66 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.String_restrictionsContext;
67 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
68 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Units_stmtContext;
69 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Value_stmtContext;
70 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
71 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yin_element_argContext;
72 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yin_element_stmtContext;
73 import org.opendaylight.yangtools.yang.common.QName;
74 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
75 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
76 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
77 import org.opendaylight.yangtools.yang.model.api.Status;
78 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
79 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
81 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
82 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
83 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
84 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
85 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
86 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
87 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
88 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
89 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
90 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
91 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
92 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
93 import org.opendaylight.yangtools.yang.model.util.BinaryType;
94 import org.opendaylight.yangtools.yang.model.util.BitsType;
95 import org.opendaylight.yangtools.yang.model.util.Decimal64;
96 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
97 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
98 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
99 import org.opendaylight.yangtools.yang.model.util.Int16;
100 import org.opendaylight.yangtools.yang.model.util.Int32;
101 import org.opendaylight.yangtools.yang.model.util.Int64;
102 import org.opendaylight.yangtools.yang.model.util.Int8;
103 import org.opendaylight.yangtools.yang.model.util.Leafref;
104 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
105 import org.opendaylight.yangtools.yang.model.util.StringType;
106 import org.opendaylight.yangtools.yang.model.util.Uint16;
107 import org.opendaylight.yangtools.yang.model.util.Uint32;
108 import org.opendaylight.yangtools.yang.model.util.Uint64;
109 import org.opendaylight.yangtools.yang.model.util.Uint8;
110 import org.opendaylight.yangtools.yang.model.util.UnknownType;
111 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
112 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
113 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
114 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
115 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
116 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
117 import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder;
118 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
119 import org.slf4j.Logger;
120 import org.slf4j.LoggerFactory;
122 public final class ParserListenerUtils {
123 private static final Logger LOG = LoggerFactory.getLogger(ParserListenerUtils.class);
125 private ParserListenerUtils() {
129 * Parse given tree and get first string value.
133 * @return first string value from given tree
135 public static String stringFromNode(final ParseTree treeNode) {
136 final String result = "";
137 for (int i = 0; i < treeNode.getChildCount(); ++i) {
138 if (treeNode.getChild(i) instanceof StringContext) {
139 final StringContext context = (StringContext) treeNode.getChild(i);
140 if (context != null) {
141 return context.getChild(0).getText().replace("\"", "");
149 * Parse 'description', 'reference' and 'status' statements and fill in
155 * builder to fill in with parsed statements
157 public static void parseSchemaNodeArgs(final ParseTree ctx, final SchemaNodeBuilder builder) {
158 for (int i = 0; i < ctx.getChildCount(); i++) {
159 final ParseTree child = ctx.getChild(i);
160 if (child instanceof Description_stmtContext) {
161 final String desc = stringFromNode(child);
162 builder.setDescription(desc);
163 } else if (child instanceof Reference_stmtContext) {
164 final String ref = stringFromNode(child);
165 builder.setReference(ref);
166 } else if (child instanceof Status_stmtContext) {
167 final Status status = parseStatus((Status_stmtContext) child);
168 builder.setStatus(status);
174 * Parse given context and return its value;
178 * @return value parsed from context
180 public static Status parseStatus(final Status_stmtContext ctx) {
181 Status result = null;
182 for (int i = 0; i < ctx.getChildCount(); i++) {
183 ParseTree statusArg = ctx.getChild(i);
184 if (statusArg instanceof Status_argContext) {
185 String statusArgStr = stringFromNode(statusArg);
186 if ("current".equals(statusArgStr)) {
187 result = Status.CURRENT;
188 } else if ("deprecated".equals(statusArgStr)) {
189 result = Status.DEPRECATED;
190 } else if ("obsolete".equals(statusArgStr)) {
191 result = Status.OBSOLETE;
193 LOG.warn("Invalid 'status' statement: " + statusArgStr);
201 * Parse given tree and returns units statement as string.
205 * @return value of units statement as string or null if there is no units
208 public static String parseUnits(final ParseTree ctx) {
210 for (int i = 0; i < ctx.getChildCount(); i++) {
211 ParseTree child = ctx.getChild(i);
212 if (child instanceof Units_stmtContext) {
213 units = stringFromNode(child);
221 * Parse given tree and returns default statement as string.
225 * @return value of default statement as string or null if there is no
228 public static String parseDefault(final ParseTree ctx) {
229 String defaultValue = null;
230 for (int i = 0; i < ctx.getChildCount(); i++) {
231 ParseTree child = ctx.getChild(i);
232 if (child instanceof Default_stmtContext) {
233 defaultValue = stringFromNode(child);
241 * Create SchemaPath from actualPath and new node name.
244 * current position in model
245 * @return SchemaPath object
247 public static SchemaPath createActualSchemaPath(final Stack<QName> actualPath) {
248 final List<QName> path = new ArrayList<QName>(actualPath);
249 return new SchemaPath(path, true);
253 * Create java.util.List of QName objects from given key definition as
256 * @param keyDefinition
257 * key definition as string
264 * @return YANG list key as java.util.List of QName objects
266 public static List<QName> createListKey(final String keyDefinition, final URI namespace, final Date revision,
267 final String prefix) {
268 List<QName> key = new ArrayList<QName>();
269 String[] splittedKey = keyDefinition.split(" ");
272 for (String keyElement : splittedKey) {
273 if (keyElement.length() != 0) {
274 qname = new QName(namespace, revision, prefix, keyElement);
282 * Parse given type body of enumeration statement.
285 * type body context to parse
287 * actual position in YANG model
289 * current module name
290 * @return List of EnumPair object parsed from given context
292 private static List<EnumTypeDefinition.EnumPair> getEnumConstants(final Type_body_stmtsContext ctx,
293 final Stack<QName> path, final String moduleName) {
294 List<EnumTypeDefinition.EnumPair> enumConstants = new ArrayList<EnumTypeDefinition.EnumPair>();
296 for (int i = 0; i < ctx.getChildCount(); i++) {
297 ParseTree enumSpecChild = ctx.getChild(i);
298 if (enumSpecChild instanceof Enum_specificationContext) {
299 int highestValue = -1;
300 for (int j = 0; j < enumSpecChild.getChildCount(); j++) {
301 ParseTree enumChild = enumSpecChild.getChild(j);
302 if (enumChild instanceof Enum_stmtContext) {
303 EnumPair enumPair = createEnumPair((Enum_stmtContext) enumChild, highestValue, path, moduleName);
304 if (enumPair.getValue() > highestValue) {
305 highestValue = enumPair.getValue();
307 enumConstants.add(enumPair);
312 return enumConstants;
316 * Parse enum statement context
319 * enum statement context
320 * @param highestValue
321 * current highest value in enumeration
323 * actual position in YANG model
325 * current module name
326 * @return EnumPair object parsed from given context
328 private static EnumTypeDefinition.EnumPair createEnumPair(final Enum_stmtContext ctx, final int highestValue,
329 final Stack<QName> actualPath, final String moduleName) {
330 final String name = stringFromNode(ctx);
331 SchemaPath path = createTypePath(actualPath, name);
332 Integer value = null;
334 String description = null;
335 String reference = null;
336 Status status = null;
338 for (int i = 0; i < ctx.getChildCount(); i++) {
339 ParseTree child = ctx.getChild(i);
340 if (child instanceof Value_stmtContext) {
341 String valueStr = stringFromNode(child);
342 value = Integer.valueOf(valueStr);
343 } else if (child instanceof Description_stmtContext) {
344 description = stringFromNode(child);
345 } else if (child instanceof Reference_stmtContext) {
346 reference = stringFromNode(child);
347 } else if (child instanceof Status_stmtContext) {
348 status = parseStatus((Status_stmtContext) child);
353 value = highestValue + 1;
355 if (value < -2147483648 || value > 2147483647) {
356 throw new YangParseException(moduleName, ctx.getStart().getLine(), "Error on enum '" + name
357 + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: " + value);
360 EnumPairImpl result = new EnumPairImpl();
361 result.qname = path.getPath().get(path.getPath().size() - 1);
363 result.description = description;
364 result.reference = reference;
365 result.status = status;
367 result.value = value;
372 * Internal implementation of EnumPair.
374 private static class EnumPairImpl implements EnumTypeDefinition.EnumPair {
376 private SchemaPath path;
377 private String description;
378 private String reference;
379 private Status status;
380 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
382 private Integer value;
385 public QName getQName() {
390 public SchemaPath getPath() {
395 public String getDescription() {
400 public String getReference() {
405 public Status getStatus() {
410 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
415 public String getName() {
420 public Integer getValue() {
425 public int hashCode() {
426 final int prime = 31;
428 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
429 result = prime * result + ((path == null) ? 0 : path.hashCode());
430 result = prime * result + ((unknownNodes == null) ? 0 : unknownNodes.hashCode());
431 result = prime * result + ((name == null) ? 0 : name.hashCode());
432 result = prime * result + ((value == null) ? 0 : value.hashCode());
437 public boolean equals(Object obj) {
444 if (getClass() != obj.getClass()) {
447 EnumPairImpl other = (EnumPairImpl) obj;
449 if (other.qname != null) {
452 } else if (!qname.equals(other.qname)) {
456 if (other.path != null) {
459 } else if (!path.equals(other.path)) {
462 if (unknownNodes == null) {
463 if (other.unknownNodes != null) {
466 } else if (!unknownNodes.equals(other.unknownNodes)) {
470 if (other.name != null) {
473 } else if (!name.equals(other.name)) {
477 if (other.value != null) {
480 } else if (!value.equals(other.value)) {
487 public String toString() {
488 return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name=" + name + ", value=" + value + "]";
493 * Get and parse range from given type body context.
496 * type body context to parse
498 * @return List of RangeConstraint created from this context
500 private static List<RangeConstraint> getRangeConstraints(final Type_body_stmtsContext ctx, final String moduleName) {
501 List<RangeConstraint> rangeConstraints = Collections.emptyList();
502 outer: for (int i = 0; i < ctx.getChildCount(); i++) {
503 ParseTree numRestrChild = ctx.getChild(i);
504 if (numRestrChild instanceof Numerical_restrictionsContext) {
505 for (int j = 0; j < numRestrChild.getChildCount(); j++) {
506 ParseTree rangeChild = numRestrChild.getChild(j);
507 if (rangeChild instanceof Range_stmtContext) {
508 rangeConstraints = parseRangeConstraints((Range_stmtContext) rangeChild, moduleName);
514 return rangeConstraints;
518 * Parse given range context.
521 * range context to parse
523 * @return List of RangeConstraints parsed from this context
525 private static List<RangeConstraint> parseRangeConstraints(final Range_stmtContext ctx, final String moduleName) {
526 final int line = ctx.getStart().getLine();
527 List<RangeConstraint> rangeConstraints = new ArrayList<RangeConstraint>();
528 String description = null;
529 String reference = null;
531 for (int i = 0; i < ctx.getChildCount(); i++) {
532 ParseTree child = ctx.getChild(i);
533 if (child instanceof Description_stmtContext) {
534 description = stringFromNode(child);
535 } else if (child instanceof Reference_stmtContext) {
536 reference = stringFromNode(child);
540 String rangeStr = stringFromNode(ctx);
541 String trimmed = rangeStr.replace(" ", "");
542 String[] splittedRange = trimmed.split("\\|");
543 for (String rangeDef : splittedRange) {
544 String[] splittedRangeDef = rangeDef.split("\\.\\.");
547 if (splittedRangeDef.length == 1) {
548 min = max = parseNumberConstraintValue(splittedRangeDef[0], moduleName, line);
550 min = parseNumberConstraintValue(splittedRangeDef[0], moduleName, line);
551 max = parseNumberConstraintValue(splittedRangeDef[1], moduleName, line);
553 RangeConstraint range = BaseConstraints.rangeConstraint(min, max, description, reference);
554 rangeConstraints.add(range);
557 return rangeConstraints;
561 * Get and parse length from given type body context.
564 * type body context to parse
566 * @return List of LengthConstraint created from this context
568 private static List<LengthConstraint> getLengthConstraints(final Type_body_stmtsContext ctx, final String moduleName) {
569 List<LengthConstraint> lengthConstraints = Collections.emptyList();
570 outer: for (int i = 0; i < ctx.getChildCount(); i++) {
571 ParseTree stringRestrChild = ctx.getChild(i);
572 if (stringRestrChild instanceof String_restrictionsContext) {
573 for (int j = 0; j < stringRestrChild.getChildCount(); j++) {
574 ParseTree lengthChild = stringRestrChild.getChild(j);
575 if (lengthChild instanceof Length_stmtContext) {
576 lengthConstraints = parseLengthConstraints((Length_stmtContext) lengthChild, moduleName);
582 return lengthConstraints;
586 * Parse given length context.
589 * length context to parse
591 * @return List of LengthConstraints parsed from this context
593 private static List<LengthConstraint> parseLengthConstraints(final Length_stmtContext ctx, final String moduleName) {
594 final int line = ctx.getStart().getLine();
595 List<LengthConstraint> lengthConstraints = new ArrayList<LengthConstraint>();
596 String description = null;
597 String reference = null;
599 for (int i = 0; i < ctx.getChildCount(); i++) {
600 ParseTree child = ctx.getChild(i);
601 if (child instanceof Description_stmtContext) {
602 description = stringFromNode(child);
603 } else if (child instanceof Reference_stmtContext) {
604 reference = stringFromNode(child);
608 String lengthStr = stringFromNode(ctx);
609 String trimmed = lengthStr.replace(" ", "");
610 String[] splittedRange = trimmed.split("\\|");
611 for (String rangeDef : splittedRange) {
612 String[] splittedRangeDef = rangeDef.split("\\.\\.");
615 if (splittedRangeDef.length == 1) {
616 min = max = parseNumberConstraintValue(splittedRangeDef[0], moduleName, line);
618 min = parseNumberConstraintValue(splittedRangeDef[0], moduleName, line);
619 max = parseNumberConstraintValue(splittedRangeDef[1], moduleName, line);
621 LengthConstraint range = BaseConstraints.lengthConstraint(min, max, description, reference);
622 lengthConstraints.add(range);
625 return lengthConstraints;
632 * name of current module
634 * current line in module
635 * @return wrapper object of primitive java type or UnknownBoundaryNumber if
636 * type is one of special YANG values 'min' or 'max'
638 private static Number parseNumberConstraintValue(final String value, final String moduleName, final int line) {
639 Number result = null;
640 if ("min".equals(value) || "max".equals(value)) {
641 result = new UnknownBoundaryNumber(value);
644 result = Long.valueOf(value);
645 } catch (NumberFormatException e) {
646 throw new YangParseException(moduleName, line, "Unable to parse range value '" + value + "'.", e);
653 * Parse type body and return pattern constraints.
657 * @return list of pattern constraints
659 private static List<PatternConstraint> getPatternConstraint(final Type_body_stmtsContext ctx) {
660 List<PatternConstraint> patterns = new ArrayList<PatternConstraint>();
662 for (int i = 0; i < ctx.getChildCount(); i++) {
663 ParseTree stringRestrChild = ctx.getChild(i);
664 if (stringRestrChild instanceof String_restrictionsContext) {
665 for (int j = 0; j < stringRestrChild.getChildCount(); j++) {
666 ParseTree lengthChild = stringRestrChild.getChild(j);
667 if (lengthChild instanceof Pattern_stmtContext) {
668 patterns.add(parsePatternConstraint((Pattern_stmtContext) lengthChild));
677 * Internal helper method.
681 * @return PatternConstraint object
683 private static PatternConstraint parsePatternConstraint(final Pattern_stmtContext ctx) {
684 String description = null;
685 String reference = null;
686 for (int i = 0; i < ctx.getChildCount(); i++) {
687 ParseTree child = ctx.getChild(i);
688 if (child instanceof Description_stmtContext) {
689 description = stringFromNode(child);
690 } else if (child instanceof Reference_stmtContext) {
691 reference = stringFromNode(child);
694 String pattern = patternStringFromNode(ctx);
695 return BaseConstraints.patternConstraint(pattern, description, reference);
699 * Parse given context and return pattern value.
703 * @return pattern value as String
705 public static String patternStringFromNode(final Pattern_stmtContext ctx) {
706 StringBuilder result = new StringBuilder();
707 for (int i = 0; i < ctx.getChildCount(); ++i) {
708 ParseTree child = ctx.getChild(i);
709 if (child instanceof StringContext) {
710 for (int j = 0; j < child.getChildCount(); j++) {
712 String patternToken = child.getChild(j).getText();
713 result.append(patternToken.substring(1, patternToken.length() - 1));
718 return result.toString();
722 * Get fraction digits value from type body.
725 * type body context to parse
727 * name of current module
728 * @return 'fraction-digits' value if present in given context, null
731 private static Integer getFractionDigits(Type_body_stmtsContext ctx, String moduleName) {
732 Integer result = null;
733 for (int i = 0; i < ctx.getChildCount(); i++) {
734 ParseTree dec64specChild = ctx.getChild(i);
735 if (dec64specChild instanceof Decimal64_specificationContext) {
736 result = parseFractionDigits((Decimal64_specificationContext) dec64specChild, moduleName);
743 * Parse decimal64 fraction-digits value.
748 * name of current module
749 * @return fraction-digits value as Integer
751 private static Integer parseFractionDigits(Decimal64_specificationContext ctx, String moduleName) {
752 Integer result = null;
753 for (int i = 0; i < ctx.getChildCount(); i++) {
754 ParseTree fdChild = ctx.getChild(i);
755 if (fdChild instanceof Fraction_digits_stmtContext) {
756 String value = stringFromNode(fdChild);
758 result = Integer.valueOf(value);
759 } catch (NumberFormatException e) {
760 throw new YangParseException(moduleName, ctx.getStart().getLine(),
761 "Unable to parse fraction digits value '" + value + "'.", e);
769 * Internal helper method for parsing bit statements from given type body
773 * type body context to parse
775 * current position in YANG model
777 * current module name
778 * @return List of Bit objects created from this context
780 private static List<BitsTypeDefinition.Bit> getBits(Type_body_stmtsContext ctx, Stack<QName> actualPath,
782 final List<BitsTypeDefinition.Bit> bits = new ArrayList<BitsTypeDefinition.Bit>();
783 for (int j = 0; j < ctx.getChildCount(); j++) {
784 ParseTree bitsSpecChild = ctx.getChild(j);
785 if (bitsSpecChild instanceof Bits_specificationContext) {
786 long highestPosition = -1;
787 for (int k = 0; k < bitsSpecChild.getChildCount(); k++) {
788 ParseTree bitChild = bitsSpecChild.getChild(k);
789 if (bitChild instanceof Bit_stmtContext) {
790 Bit bit = parseBit((Bit_stmtContext) bitChild, highestPosition, actualPath, moduleName);
791 if (bit.getPosition() > highestPosition) {
792 highestPosition = bit.getPosition();
803 * Internal helper method for parsing bit context.
806 * bit statement context to parse
807 * @param highestPosition
808 * current highest position in bits type
810 * current position in YANG model
812 * current module name
813 * @return Bit object parsed from this context
815 private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, long highestPosition,
816 Stack<QName> actualPath, final String moduleName) {
817 String name = stringFromNode(ctx);
818 Long position = null;
820 String description = null;
821 String reference = null;
822 Status status = Status.CURRENT;
824 SchemaPath schemaPath = createBaseTypePath(actualPath, name);
826 for (int i = 0; i < ctx.getChildCount(); i++) {
827 ParseTree child = ctx.getChild(i);
828 if (child instanceof Position_stmtContext) {
829 String positionStr = stringFromNode(child);
830 position = Long.valueOf(positionStr);
831 } else if (child instanceof Description_stmtContext) {
832 description = stringFromNode(child);
833 } else if (child instanceof Reference_stmtContext) {
834 reference = stringFromNode(child);
835 } else if (child instanceof Status_stmtContext) {
836 status = parseStatus((Status_stmtContext) child);
840 if (position == null) {
841 position = highestPosition + 1;
843 if (position < 0 || position > 4294967295L) {
844 throw new YangParseException(moduleName, ctx.getStart().getLine(), "Error on bit '" + name
845 + "': the position value MUST be in the range 0 to 4294967295");
848 final List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
849 return new BitImpl(position, schemaPath.getPath().get(schemaPath.getPath().size() - 1), schemaPath,
850 description, reference, status, unknownNodes);
854 * Parse 'ordered-by' statement.
856 * The 'ordered-by' statement defines whether the order of entries within a
857 * list are determined by the user or the system. The argument is one of the
858 * strings "system" or "user". If not present, order defaults to "system".
861 * Ordered_by_stmtContext
862 * @return true, if ordered-by contains value 'user', false otherwise
864 public static boolean parseUserOrdered(Ordered_by_stmtContext ctx) {
865 boolean result = false;
866 for (int j = 0; j < ctx.getChildCount(); j++) {
867 ParseTree orderArg = ctx.getChild(j);
868 if (orderArg instanceof Ordered_by_argContext) {
869 String orderStr = stringFromNode(orderArg);
870 if ("system".equals(orderStr)) {
872 } else if ("user".equals(orderStr)) {
875 LOG.warn("Invalid 'ordered-by' statement.");
883 * Get config statement from given context. If there is no config statement,
884 * return config value of parent
891 * name of current module
893 * line in current module
894 * @return config statement parsed from given context
896 public static Boolean getConfig(final ParseTree ctx, final Builder parent, final String moduleName, final int line) {
897 Boolean result = null;
898 // parse configuration statement
899 Boolean config = null;
900 for (int i = 0; i < ctx.getChildCount(); i++) {
901 ParseTree child = ctx.getChild(i);
902 if (child instanceof Config_stmtContext) {
903 config = parseConfig((Config_stmtContext) child, moduleName);
908 // If 'config' is not specified, the default is the same as the parent
909 // schema node's 'config' value
910 if (config == null) {
911 if (parent instanceof DataSchemaNodeBuilder) {
912 Boolean parentConfig = ((DataSchemaNodeBuilder) parent).isConfiguration();
913 // If the parent node is a rpc input or output, it can has
914 // config set to null
915 result = parentConfig == null ? true : parentConfig;
916 } else if (parent instanceof ChoiceCaseBuilder) {
917 // If the parent node is a 'case' node, the value is the same as
918 // the 'case' node's parent 'choice' node
919 ChoiceCaseBuilder choiceCase = (ChoiceCaseBuilder) parent;
920 Builder choice = choiceCase.getParent();
921 Boolean parentConfig = null;
922 if (choice instanceof ChoiceBuilder) {
923 parentConfig = ((ChoiceBuilder) choice).isConfiguration();
927 result = parentConfig;
932 // Check first: if a node has 'config' set to 'false', no node
933 // underneath it can have 'config' set to 'true'
934 if (parent instanceof DataSchemaNodeBuilder && !(parent instanceof ChoiceCaseBuilder)) {
935 Boolean parentConfig = ((DataSchemaNodeBuilder) parent).isConfiguration();
936 if (!parentConfig && config) {
937 throw new YangParseException(moduleName, line,
938 "Can not set 'config' to 'true' if parent node has 'config' set to 'false'");
948 * Parse config statement.
951 * config context to parse
953 * current module name
954 * @return true if given context contains string 'true', false otherwise
956 private static Boolean parseConfig(final Config_stmtContext ctx, final String moduleName) {
957 Boolean result = null;
959 for (int i = 0; i < ctx.getChildCount(); ++i) {
960 final ParseTree configContext = ctx.getChild(i);
961 if (configContext instanceof Config_argContext) {
962 final String value = stringFromNode(configContext);
963 if ("true".equals(value)) {
966 } else if ("false".equals(value)) {
970 throw new YangParseException(moduleName, ctx.getStart().getLine(),
971 "Failed to parse 'config' statement value: '" + value + "'.");
980 * Parse type body and create UnknownType definition.
982 * @param typedefQName
983 * qname of current type
991 * @return UnknownType object with constraints from parsed type body
993 public static TypeDefinition<?> parseUnknownTypeWithBody(final QName typedefQName,
994 final Type_body_stmtsContext ctx, final Stack<QName> actualPath, final URI namespace, final Date revision,
995 final String prefix, final Builder parent) {
996 String moduleName = parent.getModuleName();
997 String typeName = typedefQName.getLocalName();
999 UnknownType.Builder unknownType = new UnknownType.Builder(typedefQName);
1002 List<RangeConstraint> rangeStatements = getRangeConstraints(ctx, moduleName);
1003 List<LengthConstraint> lengthStatements = getLengthConstraints(ctx, moduleName);
1004 List<PatternConstraint> patternStatements = getPatternConstraint(ctx);
1005 Integer fractionDigits = getFractionDigits(ctx, moduleName);
1007 if (parent instanceof TypeDefinitionBuilder) {
1008 TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
1009 typedef.setRanges(rangeStatements);
1010 typedef.setLengths(lengthStatements);
1011 typedef.setPatterns(patternStatements);
1012 typedef.setFractionDigits(fractionDigits);
1013 return unknownType.build();
1015 TypeDefinition<?> baseType = unknownType.build();
1016 TypeDefinition<?> result = null;
1017 QName qname = new QName(namespace, revision, prefix, typeName);
1018 SchemaPath schemaPath = createTypePath(actualPath, typeName);
1020 ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, null, null, schemaPath);
1021 typeBuilder.ranges(rangeStatements);
1022 typeBuilder.lengths(lengthStatements);
1023 typeBuilder.patterns(patternStatements);
1024 typeBuilder.fractionDigits(fractionDigits);
1025 result = typeBuilder.build();
1030 unknownType.setReferenceOnly(true);
1033 return unknownType.build();
1037 * Create TypeDefinition object based on given type name and type body.
1044 * current path in schema
1053 * @return TypeDefinition object based on parsed values.
1055 public static TypeDefinition<?> parseTypeWithBody(final String typeName, final Type_body_stmtsContext typeBody,
1056 final Stack<QName> actualPath, final URI namespace, final Date revision, final String prefix,
1057 final Builder parent) {
1059 final String moduleName = parent.getModuleName();
1060 final int line = typeBody.getStart().getLine();
1061 TypeDefinition<?> baseType = null;
1063 Integer fractionDigits = getFractionDigits(typeBody, moduleName);
1064 List<LengthConstraint> lengthStatements = getLengthConstraints(typeBody, moduleName);
1065 List<PatternConstraint> patternStatements = getPatternConstraint(typeBody);
1066 List<RangeConstraint> rangeStatements = getRangeConstraints(typeBody, moduleName);
1068 TypeConstraints constraints = new TypeConstraints(moduleName, line);
1069 constraints.addFractionDigits(fractionDigits);
1070 constraints.addLengths(lengthStatements);
1071 constraints.addPatterns(patternStatements);
1072 constraints.addRanges(rangeStatements);
1074 SchemaPath baseTypePath = createBaseTypePath(actualPath, typeName);
1075 SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, namespace, revision, prefix, typeName);
1077 if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
1078 extBaseTypePath = baseTypePath;
1081 if ("decimal64".equals(typeName)) {
1082 if (rangeStatements.isEmpty()) {
1084 return new Decimal64(baseTypePath, fractionDigits);
1085 } catch(Exception e) {
1086 throw new YangParseException(moduleName, line, e.getMessage());
1089 Decimal64 decimalType = new Decimal64(extBaseTypePath, fractionDigits);
1090 constraints.addRanges(decimalType.getRangeStatements());
1091 baseType = decimalType;
1092 } else if (typeName.startsWith("int")) {
1093 IntegerTypeDefinition intType = null;
1094 if ("int8".equals(typeName)) {
1095 intType = Int8.getInstance();
1096 } else if ("int16".equals(typeName)) {
1097 intType = Int16.getInstance();
1098 } else if ("int32".equals(typeName)) {
1099 intType = Int32.getInstance();
1100 } else if ("int64".equals(typeName)) {
1101 intType = Int64.getInstance();
1103 if (intType == null) {
1104 throw new YangParseException(moduleName, line, "Unknown yang type " + typeName);
1106 constraints.addRanges(intType.getRangeStatements());
1108 } else if (typeName.startsWith("uint")) {
1109 UnsignedIntegerTypeDefinition uintType = null;
1110 if ("uint8".equals(typeName)) {
1111 uintType = Uint8.getInstance();
1112 } else if ("uint16".equals(typeName)) {
1113 uintType = Uint16.getInstance();
1114 } else if ("uint32".equals(typeName)) {
1115 uintType = Uint32.getInstance();
1116 } else if ("uint64".equals(typeName)) {
1117 uintType = Uint64.getInstance();
1119 if (uintType == null) {
1120 throw new YangParseException(moduleName, line, "Unknown yang type " + typeName);
1122 constraints.addRanges(uintType.getRangeStatements());
1123 baseType = uintType;
1124 } else if ("enumeration".equals(typeName)) {
1125 List<EnumTypeDefinition.EnumPair> enumConstants = getEnumConstants(typeBody, actualPath, moduleName);
1126 return new EnumerationType(baseTypePath, enumConstants);
1127 } else if ("string".equals(typeName)) {
1128 StringTypeDefinition stringType = StringType.getIntance();
1129 constraints.addLengths(stringType.getLengthStatements());
1130 baseType = stringType;
1131 } else if ("bits".equals(typeName)) {
1132 return new BitsType(baseTypePath, getBits(typeBody, actualPath, moduleName));
1133 } else if ("leafref".equals(typeName)) {
1134 final String path = parseLeafrefPath(typeBody);
1135 final boolean absolute = path.startsWith("/");
1136 RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, absolute);
1137 return new Leafref(xpath);
1138 } else if ("binary".equals(typeName)) {
1139 BinaryTypeDefinition binaryType = BinaryType.getInstance();
1140 constraints.addLengths(binaryType.getLengthConstraints());
1141 baseType = binaryType;
1142 } else if ("instance-identifier".equals(typeName)) {
1143 boolean requireInstance = isRequireInstance(typeBody);
1144 return new InstanceIdentifier(null, requireInstance);
1147 if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
1148 TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
1149 typedef.setRanges(constraints.getRange());
1150 typedef.setLengths(constraints.getLength());
1151 typedef.setPatterns(constraints.getPatterns());
1152 typedef.setFractionDigits(constraints.getFractionDigits());
1156 TypeDefinition<?> result = null;
1157 ExtendedType.Builder typeBuilder = null;
1159 List<QName> path = new ArrayList<QName>(actualPath);
1160 path.add(new QName(namespace, revision, prefix, typeName));
1161 SchemaPath schemaPath = new SchemaPath(path, true);
1163 QName qname = schemaPath.getPath().get(schemaPath.getPath().size() - 1);
1164 typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath);
1166 typeBuilder.ranges(constraints.getRange());
1167 typeBuilder.lengths(constraints.getLength());
1168 typeBuilder.patterns(constraints.getPatterns());
1169 typeBuilder.fractionDigits(constraints.getFractionDigits());
1171 result = typeBuilder.build();
1175 private static SchemaPath createTypePath(Stack<QName> actual, String typeName) {
1176 QName last = actual.peek();
1177 QName typeQName = new QName(last.getNamespace(), last.getRevision(), last.getPrefix(), typeName);
1178 List<QName> path = new ArrayList<QName>(actual);
1179 path.add(typeQName);
1180 return new SchemaPath(path, true);
1183 private static SchemaPath createBaseTypePath(Stack<QName> actual, String typeName) {
1184 List<QName> path = new ArrayList<QName>(actual);
1185 path.add(BaseTypes.constructQName(typeName));
1186 return new SchemaPath(path, true);
1189 private static SchemaPath createExtendedBaseTypePath(Stack<QName> actual, URI namespace, Date revision,
1190 String prefix, String typeName) {
1191 QName extTypeName = new QName(namespace, revision, prefix, typeName);
1192 QName baseTypeName = BaseTypes.constructQName(typeName);
1193 List<QName> path = new ArrayList<QName>(actual);
1194 path.add(extTypeName);
1195 path.add(baseTypeName);
1196 return new SchemaPath(path, true);
1200 * Parse given context and find identityref base value.
1204 * @return identityref base value as String
1206 public static String getIdentityrefBase(Type_body_stmtsContext ctx) {
1207 String result = null;
1208 outer: for (int i = 0; i < ctx.getChildCount(); i++) {
1209 ParseTree child = ctx.getChild(i);
1210 if (child instanceof Identityref_specificationContext) {
1211 for (int j = 0; j < child.getChildCount(); j++) {
1212 ParseTree baseArg = child.getChild(j);
1213 if (baseArg instanceof Base_stmtContext) {
1214 result = stringFromNode(baseArg);
1224 * Parse type body statement and find require-instance value.
1228 * @return require-instance value
1230 private static boolean isRequireInstance(Type_body_stmtsContext ctx) {
1231 for (int i = 0; i < ctx.getChildCount(); i++) {
1232 ParseTree child = ctx.getChild(i);
1233 if (child instanceof Instance_identifier_specificationContext) {
1234 for (int j = 0; j < child.getChildCount(); j++) {
1235 ParseTree reqStmt = child.getChild(j);
1236 if (reqStmt instanceof Require_instance_stmtContext) {
1237 for (int k = 0; k < reqStmt.getChildCount(); k++) {
1238 ParseTree reqArg = reqStmt.getChild(k);
1239 if (reqArg instanceof Require_instance_argContext) {
1240 return Boolean.valueOf(stringFromNode(reqArg));
1251 * Parse type body statement and find leafref path.
1255 * @return leafref path as String
1257 private static String parseLeafrefPath(Type_body_stmtsContext ctx) {
1258 for (int i = 0; i < ctx.getChildCount(); i++) {
1259 ParseTree child = ctx.getChild(i);
1260 if (child instanceof Leafref_specificationContext) {
1261 for (int j = 0; j < child.getChildCount(); j++) {
1262 ParseTree leafRefSpec = child.getChild(j);
1263 if (leafRefSpec instanceof Path_stmtContext) {
1264 return stringFromNode(leafRefSpec);
1273 * Internal helper method for parsing must statement.
1277 * @return MustDefinition object based on parsed context
1279 public static MustDefinition parseMust(final YangParser.Must_stmtContext ctx) {
1280 StringBuilder mustText = new StringBuilder();
1281 String description = null;
1282 String reference = null;
1283 String errorAppTag = null;
1284 String errorMessage = null;
1285 for (int i = 0; i < ctx.getChildCount(); ++i) {
1286 ParseTree child = ctx.getChild(i);
1287 if (child instanceof StringContext) {
1288 final StringContext context = (StringContext) child;
1289 if (context.getChildCount() == 1) {
1290 String mustPart = context.getChild(0).getText();
1291 // trim start and end quotation
1292 mustText.append(mustPart.substring(1, mustPart.length() - 1));
1294 for (int j = 0; j < context.getChildCount(); j++) {
1295 String mustPart = context.getChild(j).getText();
1297 mustText.append(mustPart.substring(0, mustPart.length() - 1));
1301 mustText.append(mustPart.substring(1));
1305 } else if (child instanceof Description_stmtContext) {
1306 description = stringFromNode(child);
1307 } else if (child instanceof Reference_stmtContext) {
1308 reference = stringFromNode(child);
1309 } else if (child instanceof Error_app_tag_stmtContext) {
1310 errorAppTag = stringFromNode(child);
1311 } else if (child instanceof Error_message_stmtContext) {
1312 errorMessage = stringFromNode(child);
1316 MustDefinition must = new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag,
1322 * Parse given context and set constraints to constraints builder.
1326 * @param constraints
1327 * ConstraintsBuilder to fill
1329 public static void parseConstraints(final ParseTree ctx, final ConstraintsBuilder constraints) {
1330 for (int i = 0; i < ctx.getChildCount(); ++i) {
1331 final ParseTree childNode = ctx.getChild(i);
1332 if (childNode instanceof Max_elements_stmtContext) {
1333 Integer max = parseMaxElements((Max_elements_stmtContext) childNode, constraints.getModuleName());
1334 constraints.setMaxElements(max);
1335 } else if (childNode instanceof Min_elements_stmtContext) {
1336 Integer min = parseMinElements((Min_elements_stmtContext) childNode, constraints.getModuleName());
1337 constraints.setMinElements(min);
1338 } else if (childNode instanceof Must_stmtContext) {
1339 MustDefinition must = parseMust((Must_stmtContext) childNode);
1340 constraints.addMustDefinition(must);
1341 } else if (childNode instanceof Mandatory_stmtContext) {
1342 for (int j = 0; j < childNode.getChildCount(); j++) {
1343 ParseTree mandatoryTree = childNode.getChild(j);
1344 if (mandatoryTree instanceof Mandatory_argContext) {
1345 Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree));
1346 constraints.setMandatory(mandatory);
1349 } else if (childNode instanceof When_stmtContext) {
1350 constraints.addWhenCondition(stringFromNode(childNode));
1355 private static Integer parseMinElements(Min_elements_stmtContext ctx, String moduleName) {
1356 Integer result = null;
1358 for (int i = 0; i < ctx.getChildCount(); i++) {
1359 ParseTree minArg = ctx.getChild(i);
1360 if (minArg instanceof Min_value_argContext) {
1361 result = Integer.valueOf(stringFromNode(minArg));
1364 if (result == null) {
1365 throw new IllegalArgumentException();
1368 } catch (Exception e) {
1369 throw new YangParseException(moduleName, ctx.getStart().getLine(), "Failed to parse min-elements.", e);
1373 private static Integer parseMaxElements(Max_elements_stmtContext ctx, String moduleName) {
1374 Integer result = null;
1376 for (int i = 0; i < ctx.getChildCount(); i++) {
1377 ParseTree maxArg = ctx.getChild(i);
1378 if (maxArg instanceof Max_value_argContext) {
1379 result = Integer.valueOf(stringFromNode(maxArg));
1382 if (result == null) {
1383 throw new IllegalArgumentException();
1386 } catch (Exception e) {
1387 throw new YangParseException(moduleName, ctx.getStart().getLine(), "Failed to parse max-elements.", e);
1392 * Parse given context and return yin value.
1396 * @return true if value is 'true', false otherwise
1398 public static boolean parseYinValue(Argument_stmtContext ctx) {
1399 boolean yinValue = false;
1400 outer: for (int i = 0; i < ctx.getChildCount(); i++) {
1401 ParseTree yin = ctx.getChild(i);
1402 if (yin instanceof Yin_element_stmtContext) {
1403 for (int j = 0; j < yin.getChildCount(); j++) {
1404 ParseTree yinArg = yin.getChild(j);
1405 if (yinArg instanceof Yin_element_argContext) {
1406 String yinString = stringFromNode(yinArg);
1407 if ("true".equals(yinString)) {
1419 * Check this base type.
1422 * base YANG type name
1424 * name of current module
1427 * @throws YangParseException
1428 * if this is one of YANG type which MUST contain additional
1429 * informations in its body
1431 public static void checkMissingBody(final String typeName, final String moduleName, final int line) {
1432 if ("decimal64".equals(typeName)) {
1433 throw new YangParseException(moduleName, line,
1434 "The 'fraction-digits' statement MUST be present if the type is 'decimal64'.");
1435 } else if ("identityref".equals(typeName)) {
1436 throw new YangParseException(moduleName, line,
1437 "The 'base' statement MUST be present if the type is 'identityref'.");
1438 } else if ("leafref".equals(typeName)) {
1439 throw new YangParseException(moduleName, line,
1440 "The 'path' statement MUST be present if the type is 'leafref'.");
1441 } else if ("bits".equals(typeName)) {
1442 throw new YangParseException(moduleName, line, "The 'bit' statement MUST be present if the type is 'bits'.");
1443 } else if ("enumeration".equals(typeName)) {
1444 throw new YangParseException(moduleName, line,
1445 "The 'enum' statement MUST be present if the type is 'enumeration'.");
1450 * Parse refine statement.
1455 * name of current module
1456 * @return RefineHolder object representing this refine statement
1458 public static RefineHolder parseRefine(Refine_stmtContext refineCtx, String moduleName) {
1459 final String refineTarget = stringFromNode(refineCtx);
1460 final RefineHolder refine = new RefineHolder(moduleName, refineCtx.getStart().getLine(), refineTarget);
1461 for (int i = 0; i < refineCtx.getChildCount(); i++) {
1462 ParseTree refinePom = refineCtx.getChild(i);
1463 if (refinePom instanceof Refine_pomContext) {
1464 for (int j = 0; j < refinePom.getChildCount(); j++) {
1465 ParseTree refineStmt = refinePom.getChild(j);
1466 parseRefineDefault(refine, refineStmt);
1468 if (refineStmt instanceof Refine_leaf_stmtsContext) {
1469 parseRefine(refine, (Refine_leaf_stmtsContext) refineStmt);
1470 } else if (refineStmt instanceof Refine_container_stmtsContext) {
1471 parseRefine(refine, (Refine_container_stmtsContext) refineStmt);
1472 } else if (refineStmt instanceof Refine_list_stmtsContext) {
1473 parseRefine(refine, (Refine_list_stmtsContext) refineStmt);
1474 } else if (refineStmt instanceof Refine_leaf_list_stmtsContext) {
1475 parseRefine(refine, (Refine_leaf_list_stmtsContext) refineStmt);
1476 } else if (refineStmt instanceof Refine_choice_stmtsContext) {
1477 parseRefine(refine, (Refine_choice_stmtsContext) refineStmt);
1478 } else if (refineStmt instanceof Refine_anyxml_stmtsContext) {
1479 parseRefine(refine, (Refine_anyxml_stmtsContext) refineStmt);
1487 private static void parseRefineDefault(RefineHolder refine, ParseTree refineStmt) {
1488 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1489 ParseTree refineArg = refineStmt.getChild(i);
1490 if (refineArg instanceof Description_stmtContext) {
1491 String description = stringFromNode(refineArg);
1492 refine.setDescription(description);
1493 } else if (refineArg instanceof Reference_stmtContext) {
1494 String reference = stringFromNode(refineArg);
1495 refine.setReference(reference);
1496 } else if (refineArg instanceof Config_stmtContext) {
1497 Boolean config = parseConfig((Config_stmtContext) refineArg, refine.getModuleName());
1498 refine.setConfiguration(config);
1503 private static RefineHolder parseRefine(RefineHolder refine, Refine_leaf_stmtsContext refineStmt) {
1504 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1505 ParseTree refineArg = refineStmt.getChild(i);
1506 if (refineArg instanceof Default_stmtContext) {
1507 String defaultStr = stringFromNode(refineArg);
1508 refine.setDefaultStr(defaultStr);
1509 } else if (refineArg instanceof Mandatory_stmtContext) {
1510 for (int j = 0; j < refineArg.getChildCount(); j++) {
1511 ParseTree mandatoryTree = refineArg.getChild(j);
1512 if (mandatoryTree instanceof Mandatory_argContext) {
1513 Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree));
1514 refine.setMandatory(mandatory);
1517 } else if (refineArg instanceof Must_stmtContext) {
1518 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1519 refine.setMust(must);
1526 private static RefineHolder parseRefine(RefineHolder refine, Refine_container_stmtsContext refineStmt) {
1527 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1528 ParseTree refineArg = refineStmt.getChild(i);
1529 if (refineArg instanceof Must_stmtContext) {
1530 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1531 refine.setMust(must);
1532 } else if (refineArg instanceof Presence_stmtContext) {
1533 refine.setPresence(true);
1539 private static RefineHolder parseRefine(RefineHolder refine, Refine_list_stmtsContext refineStmt) {
1540 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1541 ParseTree refineArg = refineStmt.getChild(i);
1542 if (refineArg instanceof Must_stmtContext) {
1543 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1544 refine.setMust(must);
1545 } else if (refineArg instanceof Max_elements_stmtContext) {
1546 Integer max = parseMaxElements((Max_elements_stmtContext) refineArg, refine.getModuleName());
1547 refine.setMaxElements(max);
1548 } else if (refineArg instanceof Min_elements_stmtContext) {
1549 Integer min = parseMinElements((Min_elements_stmtContext) refineArg, refine.getModuleName());
1550 refine.setMinElements(min);
1556 private static RefineHolder parseRefine(RefineHolder refine, Refine_leaf_list_stmtsContext refineStmt) {
1557 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1558 ParseTree refineArg = refineStmt.getChild(i);
1559 if (refineArg instanceof Must_stmtContext) {
1560 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1561 refine.setMust(must);
1562 } else if (refineArg instanceof Max_elements_stmtContext) {
1563 Integer max = parseMaxElements((Max_elements_stmtContext) refineArg, refine.getModuleName());
1564 refine.setMaxElements(max);
1565 } else if (refineArg instanceof Min_elements_stmtContext) {
1566 Integer min = parseMinElements((Min_elements_stmtContext) refineArg, refine.getModuleName());
1567 refine.setMinElements(min);
1573 private static RefineHolder parseRefine(RefineHolder refine, Refine_choice_stmtsContext refineStmt) {
1574 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1575 ParseTree refineArg = refineStmt.getChild(i);
1576 if (refineArg instanceof Default_stmtContext) {
1577 String defaultStr = stringFromNode(refineArg);
1578 refine.setDefaultStr(defaultStr);
1579 } else if (refineArg instanceof Mandatory_stmtContext) {
1580 for (int j = 0; j < refineArg.getChildCount(); j++) {
1581 ParseTree mandatoryTree = refineArg.getChild(j);
1582 if (mandatoryTree instanceof Mandatory_argContext) {
1583 Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree));
1584 refine.setMandatory(mandatory);
1592 private static RefineHolder parseRefine(RefineHolder refine, Refine_anyxml_stmtsContext refineStmt) {
1593 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1594 ParseTree refineArg = refineStmt.getChild(i);
1595 if (refineArg instanceof Must_stmtContext) {
1596 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1597 refine.setMust(must);
1598 } else if (refineArg instanceof Mandatory_stmtContext) {
1599 for (int j = 0; j < refineArg.getChildCount(); j++) {
1600 ParseTree mandatoryTree = refineArg.getChild(j);
1601 if (mandatoryTree instanceof Mandatory_argContext) {
1602 Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree));
1603 refine.setMandatory(mandatory);