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.controller.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.controller.antlrv4.code.gen.YangParser;
19 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Argument_stmtContext;
20 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Base_stmtContext;
21 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Bit_stmtContext;
22 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Bits_specificationContext;
23 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Config_argContext;
24 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Config_stmtContext;
25 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Decimal64_specificationContext;
26 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Default_stmtContext;
27 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Description_stmtContext;
28 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Enum_specificationContext;
29 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Enum_stmtContext;
30 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Error_app_tag_stmtContext;
31 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Error_message_stmtContext;
32 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Fraction_digits_stmtContext;
33 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Identityref_specificationContext;
34 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leafref_specificationContext;
35 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Length_stmtContext;
36 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Mandatory_argContext;
37 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Mandatory_stmtContext;
38 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Max_elements_stmtContext;
39 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Max_value_argContext;
40 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Min_elements_stmtContext;
41 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Min_value_argContext;
42 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Must_stmtContext;
43 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Numerical_restrictionsContext;
44 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_argContext;
45 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
46 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Path_stmtContext;
47 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Pattern_stmtContext;
48 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Position_stmtContext;
49 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Presence_stmtContext;
50 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Range_stmtContext;
51 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Reference_stmtContext;
52 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_anyxml_stmtsContext;
53 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_choice_stmtsContext;
54 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_container_stmtsContext;
55 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_leaf_list_stmtsContext;
56 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_leaf_stmtsContext;
57 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_list_stmtsContext;
58 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_pomContext;
59 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Refine_stmtContext;
60 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Require_instance_argContext;
61 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Require_instance_stmtContext;
62 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_argContext;
63 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext;
64 import org.opendaylight.controller.antlrv4.code.gen.YangParser.StringContext;
65 import org.opendaylight.controller.antlrv4.code.gen.YangParser.String_restrictionsContext;
66 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
67 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Units_stmtContext;
68 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Value_stmtContext;
69 import org.opendaylight.controller.antlrv4.code.gen.YangParser.When_stmtContext;
70 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yin_element_argContext;
71 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yin_element_stmtContext;
72 import org.opendaylight.controller.yang.common.QName;
73 import org.opendaylight.controller.yang.model.api.MustDefinition;
74 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
75 import org.opendaylight.controller.yang.model.api.SchemaPath;
76 import org.opendaylight.controller.yang.model.api.Status;
77 import org.opendaylight.controller.yang.model.api.TypeDefinition;
78 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
79 import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition;
80 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition;
81 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
82 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition;
83 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
84 import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition;
85 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
86 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
87 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
88 import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition;
89 import org.opendaylight.controller.yang.model.api.type.UnsignedIntegerTypeDefinition;
90 import org.opendaylight.controller.yang.model.util.BaseConstraints;
91 import org.opendaylight.controller.yang.model.util.BaseTypes;
92 import org.opendaylight.controller.yang.model.util.BinaryType;
93 import org.opendaylight.controller.yang.model.util.BitsType;
94 import org.opendaylight.controller.yang.model.util.Decimal64;
95 import org.opendaylight.controller.yang.model.util.EnumerationType;
96 import org.opendaylight.controller.yang.model.util.ExtendedType;
97 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
98 import org.opendaylight.controller.yang.model.util.Int16;
99 import org.opendaylight.controller.yang.model.util.Int32;
100 import org.opendaylight.controller.yang.model.util.Int64;
101 import org.opendaylight.controller.yang.model.util.Int8;
102 import org.opendaylight.controller.yang.model.util.Leafref;
103 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
104 import org.opendaylight.controller.yang.model.util.StringType;
105 import org.opendaylight.controller.yang.model.util.Uint16;
106 import org.opendaylight.controller.yang.model.util.Uint32;
107 import org.opendaylight.controller.yang.model.util.Uint64;
108 import org.opendaylight.controller.yang.model.util.Uint8;
109 import org.opendaylight.controller.yang.model.util.UnknownType;
110 import org.opendaylight.controller.yang.parser.builder.api.Builder;
111 import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder;
112 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
113 import org.opendaylight.controller.yang.parser.builder.impl.ConstraintsBuilder;
114 import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder;
115 import org.opendaylight.controller.yang.parser.builder.impl.UnionTypeBuilder;
116 import org.slf4j.Logger;
117 import org.slf4j.LoggerFactory;
119 public final class YangModelBuilderUtil {
121 private static final Logger logger = LoggerFactory
122 .getLogger(YangModelBuilderUtil.class);
124 private YangModelBuilderUtil() {
128 * Parse given tree and get first string value.
132 * @return first string value from given tree
134 public static String stringFromNode(final ParseTree treeNode) {
135 final String result = "";
136 for (int i = 0; i < treeNode.getChildCount(); ++i) {
137 if (treeNode.getChild(i) instanceof StringContext) {
138 final StringContext context = (StringContext) treeNode
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,
158 final SchemaNodeBuilder builder) {
159 for (int i = 0; i < ctx.getChildCount(); i++) {
160 final ParseTree child = ctx.getChild(i);
161 if (child instanceof Description_stmtContext) {
162 final String desc = stringFromNode(child);
163 builder.setDescription(desc);
164 } else if (child instanceof Reference_stmtContext) {
165 final String ref = stringFromNode(child);
166 builder.setReference(ref);
167 } else if (child instanceof Status_stmtContext) {
168 final Status status = parseStatus((Status_stmtContext) child);
169 builder.setStatus(status);
175 * Parse given context and return its value;
179 * @return value parsed from context
181 public static Status parseStatus(final Status_stmtContext ctx) {
182 Status result = null;
183 for (int i = 0; i < ctx.getChildCount(); i++) {
184 ParseTree statusArg = ctx.getChild(i);
185 if (statusArg instanceof Status_argContext) {
186 String statusArgStr = stringFromNode(statusArg);
187 if ("current".equals(statusArgStr)) {
188 result = Status.CURRENT;
189 } else if ("deprecated".equals(statusArgStr)) {
190 result = Status.DEPRECATED;
191 } else if ("obsolete".equals(statusArgStr)) {
192 result = Status.OBSOLETE;
194 logger.warn("Invalid 'status' statement: " + statusArgStr);
202 * Parse given tree and returns units statement as string.
206 * @return value of units statement as string or null if there is no units
209 public static String parseUnits(final ParseTree ctx) {
211 for (int i = 0; i < ctx.getChildCount(); i++) {
212 ParseTree child = ctx.getChild(i);
213 if (child instanceof Units_stmtContext) {
214 units = stringFromNode(child);
222 * Create SchemaPath object from given path list with namespace, revision
223 * and prefix based on given values.
226 * current position in model
230 * @return SchemaPath object.
232 public static SchemaPath createActualSchemaPath(
233 final List<String> actualPath, final URI namespace,
234 final Date revision, final String prefix) {
235 final List<QName> path = new ArrayList<QName>();
237 // start from index 1 - module name omited
238 for (int i = 1; i < actualPath.size(); i++) {
239 qname = new QName(namespace, revision, prefix, actualPath.get(i));
242 return new SchemaPath(path, true);
246 * Create SchemaPath from given string.
249 * string representation of path
250 * @return SchemaPath object
252 public static SchemaPath parseAugmentPath(final String augmentPath) {
253 final boolean absolute = augmentPath.startsWith("/");
254 final String[] splittedPath = augmentPath.split("/");
255 List<QName> path = new ArrayList<QName>();
257 for (String pathElement : splittedPath) {
258 if (pathElement.length() > 0) {
259 String[] splittedElement = pathElement.split(":");
260 if (splittedElement.length == 1) {
261 name = new QName(null, null, null, splittedElement[0]);
263 name = new QName(null, null, splittedElement[0],
269 return new SchemaPath(path, absolute);
273 * Create java.util.List of QName objects from given key definition as
276 * @param keyDefinition
277 * key definition as string
284 * @return YANG list key as java.util.List of QName objects
286 public static List<QName> createListKey(final String keyDefinition,
287 final URI namespace, final Date revision, final String prefix) {
288 List<QName> key = new ArrayList<QName>();
289 String[] splittedKey = keyDefinition.split(" ");
292 for (String keyElement : splittedKey) {
293 if (keyElement.length() != 0) {
294 qname = new QName(namespace, revision, prefix, keyElement);
302 * Parse given type body of enumeration statement.
305 * type body context to parse
307 * actual position in YANG model
311 * @return List of EnumPair object parsed from given context
313 private static List<EnumTypeDefinition.EnumPair> getEnumConstants(
314 final Type_body_stmtsContext ctx, final List<String> path,
315 final URI namespace, final Date revision, final String prefix) {
316 List<EnumTypeDefinition.EnumPair> enumConstants = new ArrayList<EnumTypeDefinition.EnumPair>();
318 for (int j = 0; j < ctx.getChildCount(); j++) {
319 ParseTree enumSpecChild = ctx.getChild(j);
320 if (enumSpecChild instanceof Enum_specificationContext) {
321 int highestValue = -1;
322 for (int k = 0; k < enumSpecChild.getChildCount(); k++) {
323 ParseTree enumChild = enumSpecChild.getChild(k);
324 if (enumChild instanceof Enum_stmtContext) {
325 EnumPair enumPair = createEnumPair(
326 (Enum_stmtContext) enumChild, highestValue,
327 path, namespace, revision, prefix);
328 if (enumPair.getValue() > highestValue) {
329 highestValue = enumPair.getValue();
331 enumConstants.add(enumPair);
336 return enumConstants;
340 * Parse enum statement context
343 * enum statement context
344 * @param highestValue
345 * current highest value in enumeration
347 * actual position in YANG model
351 * @return EnumPair object parsed from given context
353 private static EnumTypeDefinition.EnumPair createEnumPair(
354 final Enum_stmtContext ctx, final int highestValue,
355 final List<String> path, final URI namespace, final Date revision,
356 final String prefix) {
357 final String name = stringFromNode(ctx);
358 final QName qname = new QName(namespace, revision, prefix, name);
359 Integer value = null;
361 String description = null;
362 String reference = null;
363 Status status = null;
365 List<String> enumPairPath = new ArrayList<String>(path);
366 enumPairPath.add(name);
368 for (int i = 0; i < ctx.getChildCount(); i++) {
369 ParseTree child = ctx.getChild(i);
370 if (child instanceof Value_stmtContext) {
371 String valueStr = stringFromNode(child);
372 value = Integer.valueOf(valueStr);
373 } else if (child instanceof Description_stmtContext) {
374 description = stringFromNode(child);
375 } else if (child instanceof Reference_stmtContext) {
376 reference = stringFromNode(child);
377 } else if (child instanceof Status_stmtContext) {
378 status = parseStatus((Status_stmtContext) child);
383 value = highestValue + 1;
385 if (value < -2147483648 || value > 2147483647) {
386 throw new YangParseException(
387 ctx.getStart().getLine(),
390 + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: "
394 EnumPairImpl result = new EnumPairImpl();
395 result.qname = qname;
396 result.path = createActualSchemaPath(enumPairPath, namespace, revision,
398 result.description = description;
399 result.reference = reference;
400 result.status = status;
402 result.value = value;
407 * Internal implementation of EnumPair.
409 private static class EnumPairImpl implements EnumTypeDefinition.EnumPair {
411 private SchemaPath path;
412 private String description;
413 private String reference;
414 private Status status;
415 private List<UnknownSchemaNode> extensionSchemaNodes = Collections
418 private Integer value;
421 public QName getQName() {
426 public SchemaPath getPath() {
431 public String getDescription() {
436 public String getReference() {
441 public Status getStatus() {
446 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
447 return extensionSchemaNodes;
451 public String getName() {
456 public Integer getValue() {
461 public int hashCode() {
462 final int prime = 31;
464 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
465 result = prime * result + ((path == null) ? 0 : path.hashCode());
468 + ((extensionSchemaNodes == null) ? 0
469 : extensionSchemaNodes.hashCode());
470 result = prime * result + ((name == null) ? 0 : name.hashCode());
471 result = prime * result + ((value == null) ? 0 : value.hashCode());
476 public boolean equals(Object obj) {
483 if (getClass() != obj.getClass()) {
486 EnumPairImpl other = (EnumPairImpl) obj;
488 if (other.qname != null) {
491 } else if (!qname.equals(other.qname)) {
495 if (other.path != null) {
498 } else if (!path.equals(other.path)) {
501 if (extensionSchemaNodes == null) {
502 if (other.extensionSchemaNodes != null) {
505 } else if (!extensionSchemaNodes.equals(other.extensionSchemaNodes)) {
509 if (other.name != null) {
512 } else if (!name.equals(other.name)) {
516 if (other.value != null) {
519 } else if (!value.equals(other.value)) {
526 public String toString() {
527 return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name="
528 + name + ", value=" + value + "]";
533 * Get and parse range from given type body context.
536 * type body context to parse
537 * @return List of RangeConstraint created from this context
539 private static List<RangeConstraint> getRangeConstraints(
540 final Type_body_stmtsContext ctx) {
541 List<RangeConstraint> rangeConstraints = Collections.emptyList();
542 outer: for (int j = 0; j < ctx.getChildCount(); j++) {
543 ParseTree numRestrChild = ctx.getChild(j);
544 if (numRestrChild instanceof Numerical_restrictionsContext) {
545 for (int k = 0; k < numRestrChild.getChildCount(); k++) {
546 ParseTree rangeChild = numRestrChild.getChild(k);
547 if (rangeChild instanceof Range_stmtContext) {
548 rangeConstraints = parseRangeConstraints((Range_stmtContext) rangeChild);
554 return rangeConstraints;
558 * Parse given range context.
561 * range context to parse
562 * @return List of RangeConstraints parsed from this context
564 private static List<RangeConstraint> parseRangeConstraints(
565 final Range_stmtContext ctx) {
566 final int line = ctx.getStart().getLine();
567 List<RangeConstraint> rangeConstraints = new ArrayList<RangeConstraint>();
568 String description = null;
569 String reference = null;
571 for (int i = 0; i < ctx.getChildCount(); i++) {
572 ParseTree child = ctx.getChild(i);
573 if (child instanceof Description_stmtContext) {
574 description = stringFromNode(child);
575 } else if (child instanceof Reference_stmtContext) {
576 reference = stringFromNode(child);
580 String rangeStr = stringFromNode(ctx);
581 String trimmed = rangeStr.replace(" ", "");
582 String[] splittedRange = trimmed.split("\\|");
583 for (String rangeDef : splittedRange) {
584 String[] splittedRangeDef = rangeDef.split("\\.\\.");
587 if (splittedRangeDef.length == 1) {
588 min = max = parseNumberConstraintValue(splittedRangeDef[0],
591 min = parseNumberConstraintValue(splittedRangeDef[0], line);
592 max = parseNumberConstraintValue(splittedRangeDef[1], line);
594 RangeConstraint range = BaseConstraints.rangeConstraint(min, max,
595 description, reference);
596 rangeConstraints.add(range);
599 return rangeConstraints;
603 * Get and parse length from given type body context.
606 * type body context to parse
607 * @return List of LengthConstraint created from this context
609 private static List<LengthConstraint> getLengthConstraints(
610 final Type_body_stmtsContext ctx) {
611 List<LengthConstraint> lengthConstraints = Collections.emptyList();
612 outer: for (int j = 0; j < ctx.getChildCount(); j++) {
613 ParseTree stringRestrChild = ctx.getChild(j);
614 if (stringRestrChild instanceof String_restrictionsContext) {
615 for (int k = 0; k < stringRestrChild.getChildCount(); k++) {
616 ParseTree lengthChild = stringRestrChild.getChild(k);
617 if (lengthChild instanceof Length_stmtContext) {
618 lengthConstraints = parseLengthConstraints((Length_stmtContext) lengthChild);
624 return lengthConstraints;
628 * Parse given length context.
631 * length context to parse
632 * @return List of LengthConstraints parsed from this context
634 private static List<LengthConstraint> parseLengthConstraints(
635 final Length_stmtContext ctx) {
636 final int line = ctx.getStart().getLine();
637 List<LengthConstraint> lengthConstraints = new ArrayList<LengthConstraint>();
638 String description = null;
639 String reference = null;
641 for (int i = 0; i < ctx.getChildCount(); i++) {
642 ParseTree child = ctx.getChild(i);
643 if (child instanceof Description_stmtContext) {
644 description = stringFromNode(child);
645 } else if (child instanceof Reference_stmtContext) {
646 reference = stringFromNode(child);
650 String lengthStr = stringFromNode(ctx);
651 String trimmed = lengthStr.replace(" ", "");
652 String[] splittedRange = trimmed.split("\\|");
653 for (String rangeDef : splittedRange) {
654 String[] splittedRangeDef = rangeDef.split("\\.\\.");
657 if (splittedRangeDef.length == 1) {
658 min = max = parseNumberConstraintValue(splittedRangeDef[0],
661 min = parseNumberConstraintValue(splittedRangeDef[0], line);
662 max = parseNumberConstraintValue(splittedRangeDef[1], line);
664 LengthConstraint range = BaseConstraints.lengthConstraint(min, max,
665 description, reference);
666 lengthConstraints.add(range);
669 return lengthConstraints;
675 * @return wrapper object of primitive java type or UnknownBoundaryNumber if
676 * type is one of special YANG values 'min' or 'max'
678 private static Number parseNumberConstraintValue(final String value,
680 Number result = null;
681 if ("min".equals(value) || "max".equals(value)) {
682 result = new UnknownBoundaryNumber(value);
685 result = Long.valueOf(value);
686 } catch (NumberFormatException e) {
687 throw new YangParseException(line,
688 "Unable to parse range value '" + value + "'.", e);
695 * Parse type body and return pattern constraints.
699 * @return list of pattern constraints
701 private static List<PatternConstraint> getPatternConstraint(
702 final Type_body_stmtsContext ctx) {
703 List<PatternConstraint> patterns = new ArrayList<PatternConstraint>();
705 outer: for (int j = 0; j < ctx.getChildCount(); j++) {
706 ParseTree stringRestrChild = ctx.getChild(j);
707 if (stringRestrChild instanceof String_restrictionsContext) {
708 for (int k = 0; k < stringRestrChild.getChildCount(); k++) {
709 ParseTree lengthChild = stringRestrChild.getChild(k);
710 if (lengthChild instanceof Pattern_stmtContext) {
711 patterns.add(parsePatternConstraint((Pattern_stmtContext) lengthChild));
712 if (k == lengthChild.getChildCount() - 1) {
723 * Internal helper method.
727 * @return PatternConstraint object
729 private static PatternConstraint parsePatternConstraint(
730 final Pattern_stmtContext ctx) {
731 String description = null;
732 String reference = null;
733 for (int i = 0; i < ctx.getChildCount(); i++) {
734 ParseTree child = ctx.getChild(i);
735 if (child instanceof Description_stmtContext) {
736 description = stringFromNode(child);
737 } else if (child instanceof Reference_stmtContext) {
738 reference = stringFromNode(child);
741 String pattern = patternStringFromNode(ctx);
742 return BaseConstraints.patternConstraint(pattern, description,
747 * Parse given context and return pattern value.
751 * @return pattern value as String
753 public static String patternStringFromNode(final Pattern_stmtContext ctx) {
754 StringBuilder result = new StringBuilder();
755 for (int i = 0; i < ctx.getChildCount(); ++i) {
756 ParseTree child = ctx.getChild(i);
757 if (child instanceof StringContext) {
758 for (int j = 0; j < child.getChildCount(); j++) {
760 String patternToken = child.getChild(j).getText();
761 result.append(patternToken.substring(1,
762 patternToken.length() - 1));
767 return result.toString();
771 * Get fraction digits value from type body.
774 * type body context to parse
775 * @return 'fraction-digits' value if present in given context, null
778 private static Integer getFractionDigits(Type_body_stmtsContext ctx) {
779 Integer result = null;
780 for (int j = 0; j < ctx.getChildCount(); j++) {
781 ParseTree dec64specChild = ctx.getChild(j);
782 if (dec64specChild instanceof Decimal64_specificationContext) {
783 result = parseFractionDigits((Decimal64_specificationContext) dec64specChild);
790 * Parse decimal64 fraction-digits value.
794 * @return fraction-digits value as Integer
796 private static Integer parseFractionDigits(
797 Decimal64_specificationContext ctx) {
798 Integer result = null;
799 for (int k = 0; k < ctx.getChildCount(); k++) {
800 ParseTree fdChild = ctx.getChild(k);
801 if (fdChild instanceof Fraction_digits_stmtContext) {
802 String value = stringFromNode(fdChild);
804 result = Integer.valueOf(value);
805 } catch (NumberFormatException e) {
806 throw new YangParseException(ctx.getStart().getLine(),
807 "Unable to parse fraction digits value '" + value
816 * Internal helper method for parsing bit statements from given type body
820 * type body context to parse
822 * current position in YANG model
826 * @return List of Bit objects created from this context
828 private static List<BitsTypeDefinition.Bit> getBits(
829 Type_body_stmtsContext ctx, List<String> actualPath, URI namespace,
830 Date revision, String prefix) {
831 final List<BitsTypeDefinition.Bit> bits = new ArrayList<BitsTypeDefinition.Bit>();
832 for (int j = 0; j < ctx.getChildCount(); j++) {
833 ParseTree bitsSpecChild = ctx.getChild(j);
834 if (bitsSpecChild instanceof Bits_specificationContext) {
835 long highestPosition = -1;
836 for (int k = 0; k < bitsSpecChild.getChildCount(); k++) {
837 ParseTree bitChild = bitsSpecChild.getChild(k);
838 if (bitChild instanceof Bit_stmtContext) {
839 Bit bit = parseBit((Bit_stmtContext) bitChild,
840 highestPosition, actualPath, namespace,
842 if (bit.getPosition() > highestPosition) {
843 highestPosition = bit.getPosition();
854 * Internal helper method for parsing bit context.
857 * bit statement context to parse
858 * @param highestPosition
859 * current highest position in bits type
861 * current position in YANG model
865 * @return Bit object parsed from this context
867 private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx,
868 long highestPosition, List<String> actualPath, final URI namespace,
869 final Date revision, final String prefix) {
870 String name = stringFromNode(ctx);
871 final QName qname = new QName(namespace, revision, prefix, name);
872 Long position = null;
874 String description = null;
875 String reference = null;
876 Status status = Status.CURRENT;
878 Stack<String> bitPath = new Stack<String>();
879 bitPath.addAll(actualPath);
882 SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace,
885 for (int i = 0; i < ctx.getChildCount(); i++) {
886 ParseTree child = ctx.getChild(i);
887 if (child instanceof Position_stmtContext) {
888 String positionStr = stringFromNode(child);
889 position = Long.valueOf(positionStr);
890 } else if (child instanceof Description_stmtContext) {
891 description = stringFromNode(child);
892 } else if (child instanceof Reference_stmtContext) {
893 reference = stringFromNode(child);
894 } else if (child instanceof Status_stmtContext) {
895 status = parseStatus((Status_stmtContext) child);
899 if (position == null) {
900 position = highestPosition + 1;
902 if (position < 0 || position > 4294967295L) {
903 throw new YangParseException(
904 ctx.getStart().getLine(),
907 + "': the position value MUST be in the range 0 to 4294967295");
910 final List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
911 return new BitImpl(position, qname, schemaPath, description, reference,
912 status, unknownNodes);
916 * Parse orderedby statement.
919 * Ordered_by_stmtContext
920 * @return true, if orderedby contains value 'user' or false otherwise
922 public static boolean parseUserOrdered(Ordered_by_stmtContext childNode) {
923 boolean result = false;
924 for (int j = 0; j < childNode.getChildCount(); j++) {
925 ParseTree orderArg = childNode.getChild(j);
926 if (orderArg instanceof Ordered_by_argContext) {
927 String orderStr = stringFromNode(orderArg);
928 if ("system".equals(orderStr)) {
930 } else if ("user".equals(orderStr)) {
933 logger.warn("Invalid 'orderedby' statement.");
941 * Parse given config context and return true if it contains string 'true',
945 * config context to parse.
946 * @return true if given context contains string 'true', false otherwise
948 public static boolean parseConfig(final Config_stmtContext ctx) {
949 boolean result = false;
951 for (int i = 0; i < ctx.getChildCount(); ++i) {
952 final ParseTree configContext = ctx.getChild(i);
953 if (configContext instanceof Config_argContext) {
954 final String value = stringFromNode(configContext);
955 if ("true".equals(value)) {
966 * Parse given type body and creates UnknownType definition.
968 * @param typedefQName
969 * qname of current type
972 * @return UnknownType object with constraints from parsed type body
974 public static TypeDefinition<?> parseUnknownTypeBody(QName typedefQName,
975 Type_body_stmtsContext ctx, final List<String> actualPath,
976 final URI namespace, final Date revision, final String prefix,
977 Builder parent, ModuleBuilder moduleBuilder) {
978 String typeName = typedefQName.getLocalName();
980 UnknownType.Builder unknownType = new UnknownType.Builder(typedefQName);
983 List<RangeConstraint> rangeStatements = getRangeConstraints(ctx);
984 List<LengthConstraint> lengthStatements = getLengthConstraints(ctx);
985 List<PatternConstraint> patternStatements = getPatternConstraint(ctx);
986 Integer fractionDigits = getFractionDigits(ctx);
988 if (parent instanceof TypeDefinitionBuilder) {
989 TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
990 typedef.setRanges(rangeStatements);
991 typedef.setLengths(lengthStatements);
992 typedef.setPatterns(patternStatements);
993 typedef.setFractionDigits(fractionDigits);
994 return unknownType.build();
996 TypeDefinition<?> baseType = unknownType.build();
997 TypeDefinition<?> result = null;
998 QName qname = new QName(namespace, revision, prefix, typeName);
999 ExtendedType.Builder typeBuilder = null;
1001 SchemaPath schemaPath = createTypeSchemaPath(actualPath,
1002 namespace, revision, prefix, typeName, false, false);
1003 typeBuilder = new ExtendedType.Builder(qname, baseType, "", "",
1006 typeBuilder.ranges(rangeStatements);
1007 typeBuilder.lengths(lengthStatements);
1008 typeBuilder.patterns(patternStatements);
1009 typeBuilder.fractionDigits(fractionDigits);
1011 result = typeBuilder.build();
1017 return unknownType.build();
1021 * Create TypeDefinition object based on given type name and type body.
1028 * current path in schema
1035 * @return TypeDefinition object based on parsed values.
1037 public static TypeDefinition<?> parseTypeBody(final String typeName,
1038 final Type_body_stmtsContext typeBody,
1039 final List<String> actualPath, final URI namespace,
1040 final Date revision, final String prefix, Builder parent) {
1041 TypeDefinition<?> baseType = null;
1043 List<RangeConstraint> rangeStatements = getRangeConstraints(typeBody);
1044 Integer fractionDigits = getFractionDigits(typeBody);
1045 List<LengthConstraint> lengthStatements = getLengthConstraints(typeBody);
1046 List<PatternConstraint> patternStatements = getPatternConstraint(typeBody);
1047 List<EnumTypeDefinition.EnumPair> enumConstants = getEnumConstants(
1048 typeBody, actualPath, namespace, revision, prefix);
1050 TypeConstraints constraints = new TypeConstraints();
1051 constraints.addFractionDigits(fractionDigits);
1052 constraints.addLengths(lengthStatements);
1053 constraints.addPatterns(patternStatements);
1054 constraints.addRanges(rangeStatements);
1056 SchemaPath baseTypePathFinal = createTypeSchemaPath(actualPath,
1057 namespace, revision, prefix, typeName, true, true);
1058 SchemaPath baseTypePath = createTypeSchemaPath(actualPath, namespace,
1059 revision, prefix, typeName, true, false);
1061 if ("decimal64".equals(typeName)) {
1062 if (rangeStatements.isEmpty()) {
1063 return new Decimal64(baseTypePathFinal, fractionDigits);
1065 Decimal64 decimalType = new Decimal64(baseTypePath, fractionDigits);
1066 constraints.addRanges(decimalType.getRangeStatements());
1067 baseType = decimalType;
1068 } else if (typeName.startsWith("int")) {
1069 IntegerTypeDefinition intType = null;
1070 if ("int8".equals(typeName)) {
1071 intType = new Int8(baseTypePath);
1072 } else if ("int16".equals(typeName)) {
1073 intType = new Int16(baseTypePath);
1074 } else if ("int32".equals(typeName)) {
1075 intType = new Int32(baseTypePath);
1076 } else if ("int64".equals(typeName)) {
1077 intType = new Int64(baseTypePath);
1079 constraints.addRanges(intType.getRangeStatements());
1081 } else if (typeName.startsWith("uint")) {
1082 UnsignedIntegerTypeDefinition uintType = null;
1083 if ("uint8".equals(typeName)) {
1084 uintType = new Uint8(baseTypePath);
1085 } else if ("uint16".equals(typeName)) {
1086 uintType = new Uint16(baseTypePath);
1087 } else if ("uint32".equals(typeName)) {
1088 uintType = new Uint32(baseTypePath);
1089 } else if ("uint64".equals(typeName)) {
1090 uintType = new Uint64(baseTypePath);
1092 constraints.addRanges(uintType.getRangeStatements());
1093 baseType = uintType;
1094 } else if ("enumeration".equals(typeName)) {
1095 return new EnumerationType(baseTypePathFinal, enumConstants);
1096 } else if ("string".equals(typeName)) {
1097 StringTypeDefinition stringType = new StringType(baseTypePath);
1098 constraints.addLengths(stringType.getLengthStatements());
1099 baseType = stringType;
1100 } else if ("bits".equals(typeName)) {
1101 return new BitsType(baseTypePathFinal, getBits(typeBody,
1102 actualPath, namespace, revision, prefix));
1103 } else if ("leafref".equals(typeName)) {
1104 final String path = parseLeafrefPath(typeBody);
1105 final boolean absolute = path.startsWith("/");
1106 RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path,
1108 return new Leafref(baseTypePathFinal, xpath);
1109 } else if ("binary".equals(typeName)) {
1110 BinaryTypeDefinition binaryType = new BinaryType(baseTypePath);
1111 constraints.addLengths(binaryType.getLengthConstraints());
1112 baseType = binaryType;
1113 } else if ("instance-identifier".equals(typeName)) {
1114 boolean requireInstance = isRequireInstance(typeBody);
1115 baseType = new InstanceIdentifier(baseTypePath, null,
1119 if (parent instanceof TypeDefinitionBuilder
1120 && !(parent instanceof UnionTypeBuilder)) {
1121 TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
1122 typedef.setRanges(constraints.getRange());
1123 typedef.setLengths(constraints.getLength());
1124 typedef.setPatterns(constraints.getPatterns());
1125 typedef.setFractionDigits(constraints.getFractionDigits());
1129 TypeDefinition<?> result = null;
1130 QName qname = new QName(namespace, revision, prefix, typeName);
1131 ExtendedType.Builder typeBuilder = null;
1133 SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace,
1134 revision, prefix, typeName, false, false);
1135 typeBuilder = new ExtendedType.Builder(qname, baseType, "", "",
1138 typeBuilder.ranges(constraints.getRange());
1139 typeBuilder.lengths(constraints.getLength());
1140 typeBuilder.patterns(constraints.getPatterns());
1141 typeBuilder.fractionDigits(constraints.getFractionDigits());
1143 result = typeBuilder.build();
1148 * Create SchemaPath object from given path list with namespace, revision
1149 * and prefix based on given values.
1152 * current position in model
1157 * @param isBaseYangType
1158 * if this is base yang type
1159 * @param isBaseYangTypeFinal
1160 * if this is base yang type without restrictions
1161 * @return SchemaPath object.
1163 private static SchemaPath createTypeSchemaPath(
1164 final List<String> actualPath, final URI namespace,
1165 final Date revision, final String prefix, final String typeName,
1166 final boolean isBaseYangType, final boolean isBaseYangTypeFinal) {
1167 List<String> typePath = new ArrayList<String>(actualPath);
1168 if (isBaseYangType && !isBaseYangTypeFinal) {
1169 typePath.add(typeName);
1172 final List<QName> path = new ArrayList<QName>();
1174 // start from index 1 -> module name omited
1175 for (int i = 1; i < typePath.size(); i++) {
1176 qname = new QName(namespace, revision, prefix, typePath.get(i));
1180 if (isBaseYangType) {
1181 typeQName = new QName(BaseTypes.BaseTypesNamespace, typeName);
1183 typeQName = new QName(namespace, revision, prefix, typeName);
1185 path.add(typeQName);
1186 return new SchemaPath(path, true);
1190 * Parse given context and find identityref base value.
1194 * @return identityref base value as String
1196 public static String getIdentityrefBase(Type_body_stmtsContext ctx) {
1197 String result = null;
1198 outer: for (int i = 0; i < ctx.getChildCount(); i++) {
1199 ParseTree child = ctx.getChild(i);
1200 if (child instanceof Identityref_specificationContext) {
1201 for (int j = 0; j < child.getChildCount(); j++) {
1202 ParseTree baseArg = child.getChild(j);
1203 if (baseArg instanceof Base_stmtContext) {
1204 result = stringFromNode(baseArg);
1214 * Parse given context and find require-instance value.
1218 * @return require-instance value
1220 private static boolean isRequireInstance(Type_body_stmtsContext ctx) {
1221 for (int i = 0; i < ctx.getChildCount(); i++) {
1222 ParseTree child = ctx.getChild(i);
1223 if (child instanceof Require_instance_stmtContext) {
1224 for (int j = 0; j < child.getChildCount(); j++) {
1225 ParseTree reqArg = child.getChild(j);
1226 if (reqArg instanceof Require_instance_argContext) {
1227 return Boolean.valueOf(stringFromNode(reqArg));
1236 * Parse given context and find leafref path.
1240 * @return leafref path as String
1242 private static String parseLeafrefPath(Type_body_stmtsContext ctx) {
1243 for (int i = 0; i < ctx.getChildCount(); i++) {
1244 ParseTree child = ctx.getChild(i);
1245 if (child instanceof Leafref_specificationContext) {
1246 for (int j = 0; j < child.getChildCount(); j++) {
1247 ParseTree leafRefSpec = child.getChild(j);
1248 if (leafRefSpec instanceof Path_stmtContext) {
1249 return stringFromNode(leafRefSpec);
1258 * Internal helper method for parsing Must_stmtContext.
1262 * @return MustDefinition object based on parsed context
1264 public static MustDefinition parseMust(final YangParser.Must_stmtContext ctx) {
1265 StringBuilder mustText = new StringBuilder();
1266 String description = null;
1267 String reference = null;
1268 String errorAppTag = null;
1269 String errorMessage = null;
1270 for (int i = 0; i < ctx.getChildCount(); ++i) {
1271 ParseTree child = ctx.getChild(i);
1272 if (child instanceof StringContext) {
1273 final StringContext context = (StringContext) child;
1274 if (context.getChildCount() == 1) {
1275 String mustPart = context.getChild(0).getText();
1276 // trim start and end quotation
1277 mustText.append(mustPart.substring(1, mustPart.length() - 1));
1279 for (int j = 0; j < context.getChildCount(); j++) {
1280 String mustPart = context.getChild(j).getText();
1282 mustText.append(mustPart.substring(0,
1283 mustPart.length() - 1));
1287 mustText.append(mustPart.substring(1));
1291 } else if (child instanceof Description_stmtContext) {
1292 description = stringFromNode(child);
1293 } else if (child instanceof Reference_stmtContext) {
1294 reference = stringFromNode(child);
1295 } else if (child instanceof Error_app_tag_stmtContext) {
1296 errorAppTag = stringFromNode(child);
1297 } else if (child instanceof Error_message_stmtContext) {
1298 errorMessage = stringFromNode(child);
1302 MustDefinition must = new MustDefinitionImpl(mustText.toString(),
1303 description, reference, errorAppTag, errorMessage);
1308 * Parse given tree and set constraints to given builder.
1312 * @param constraints
1313 * ConstraintsBuilder to fill
1315 public static void parseConstraints(final ParseTree ctx,
1316 final ConstraintsBuilder constraints) {
1317 for (int i = 0; i < ctx.getChildCount(); ++i) {
1318 final ParseTree childNode = ctx.getChild(i);
1319 if (childNode instanceof Max_elements_stmtContext) {
1320 Integer max = parseMaxElements((Max_elements_stmtContext) childNode);
1321 constraints.setMaxElements(max);
1322 } else if (childNode instanceof Min_elements_stmtContext) {
1323 Integer min = parseMinElements((Min_elements_stmtContext) childNode);
1324 constraints.setMinElements(min);
1325 } else if (childNode instanceof Must_stmtContext) {
1326 MustDefinition must = parseMust((Must_stmtContext) childNode);
1327 constraints.addMustDefinition(must);
1328 } else if (childNode instanceof Mandatory_stmtContext) {
1329 for (int j = 0; j < childNode.getChildCount(); j++) {
1330 ParseTree mandatoryTree = ctx.getChild(j);
1331 if (mandatoryTree instanceof Mandatory_argContext) {
1332 Boolean mandatory = Boolean
1333 .valueOf(stringFromNode(mandatoryTree));
1334 constraints.setMandatory(mandatory);
1337 } else if (childNode instanceof When_stmtContext) {
1338 constraints.addWhenCondition(stringFromNode(childNode));
1343 private static Integer parseMinElements(Min_elements_stmtContext ctx) {
1344 Integer result = null;
1346 for (int j = 0; j < ctx.getChildCount(); j++) {
1347 ParseTree minArg = ctx.getChild(j);
1348 if (minArg instanceof Min_value_argContext) {
1349 result = Integer.valueOf(stringFromNode(minArg));
1352 if (result == null) {
1353 throw new IllegalArgumentException();
1356 } catch (Exception e) {
1357 throw new YangParseException(ctx.getStart().getLine(),
1358 "Failed to parse min-elements.", e);
1362 private static Integer parseMaxElements(Max_elements_stmtContext ctx) {
1363 Integer result = null;
1365 for (int j = 0; j < ctx.getChildCount(); j++) {
1366 ParseTree maxArg = ctx.getChild(j);
1367 if (maxArg instanceof Max_value_argContext) {
1368 result = Integer.valueOf(stringFromNode(maxArg));
1371 if (result == null) {
1372 throw new IllegalArgumentException();
1375 } catch (Exception e) {
1376 throw new YangParseException(ctx.getStart().getLine(),
1377 "Failed to parse max-elements.", e);
1382 * Parse given context and return yin value.
1386 * @return true if value is 'true', false otherwise
1388 public static boolean parseYinValue(Argument_stmtContext ctx) {
1389 boolean yinValue = false;
1390 outer: for (int j = 0; j < ctx.getChildCount(); j++) {
1391 ParseTree yin = ctx.getChild(j);
1392 if (yin instanceof Yin_element_stmtContext) {
1393 for (int k = 0; k < yin.getChildCount(); k++) {
1394 ParseTree yinArg = yin.getChild(k);
1395 if (yinArg instanceof Yin_element_argContext) {
1396 String yinString = stringFromNode(yinArg);
1397 if ("true".equals(yinString)) {
1409 * Check this base type.
1412 * base YANG type name
1414 * name of current module
1417 * @throws YangParseException
1418 * if this is one of YANG type which MUST contain additional
1419 * informations in its body
1421 public static void checkMissingBody(final String typeName,
1422 final String moduleName, final int line) throws YangParseException {
1423 if ("decimal64".equals(typeName)) {
1424 throw new YangParseException(moduleName, line,
1425 "The 'fraction-digits' statement MUST be present if the type is 'decimal64'.");
1426 } else if ("identityref".equals(typeName)) {
1427 throw new YangParseException(moduleName, line,
1428 "The 'base' statement MUST be present if the type is 'identityref'.");
1429 } else if ("leafref".equals(typeName)) {
1430 throw new YangParseException(moduleName, line,
1431 "The 'path' statement MUST be present if the type is 'leafref'.");
1432 } else if ("bits".equals(typeName)) {
1433 throw new YangParseException(moduleName, line,
1434 "The 'bit' statement MUST be present if the type is 'bits'.");
1435 } else if ("enumeration".equals(typeName)) {
1436 throw new YangParseException(moduleName, line,
1437 "The 'enum' statement MUST be present if the type is 'enumeration'.");
1442 * Parse refine statement.
1447 * current line in yang model
1448 * @return RefineHolder object representing this refine statement
1450 public static RefineHolder parseRefine(Refine_stmtContext refineCtx) {
1451 final String refineTarget = stringFromNode(refineCtx);
1452 final RefineHolder refine = new RefineHolder(refineTarget, refineCtx
1453 .getStart().getLine());
1454 for (int j = 0; j < refineCtx.getChildCount(); j++) {
1455 ParseTree refinePom = refineCtx.getChild(j);
1456 if (refinePom instanceof Refine_pomContext) {
1457 for (int k = 0; k < refinePom.getChildCount(); k++) {
1458 ParseTree refineStmt = refinePom.getChild(k);
1459 parseRefineDefault(refine, refineStmt);
1461 if (refineStmt instanceof Refine_leaf_stmtsContext) {
1463 (Refine_leaf_stmtsContext) refineStmt);
1464 } else if (refineStmt instanceof Refine_container_stmtsContext) {
1466 (Refine_container_stmtsContext) refineStmt);
1467 } else if (refineStmt instanceof Refine_list_stmtsContext) {
1469 (Refine_list_stmtsContext) refineStmt);
1470 } else if (refineStmt instanceof Refine_leaf_list_stmtsContext) {
1472 (Refine_leaf_list_stmtsContext) refineStmt);
1473 } else if (refineStmt instanceof Refine_choice_stmtsContext) {
1475 (Refine_choice_stmtsContext) refineStmt);
1476 } else if (refineStmt instanceof Refine_anyxml_stmtsContext) {
1478 (Refine_anyxml_stmtsContext) refineStmt);
1486 private static void parseRefineDefault(RefineHolder refine,
1487 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);
1498 refine.setConfig(config);
1503 private static RefineHolder parseRefine(RefineHolder refine,
1504 Refine_leaf_stmtsContext refineStmt) {
1505 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1506 ParseTree refineArg = refineStmt.getChild(i);
1507 if (refineArg instanceof Default_stmtContext) {
1508 String defaultStr = stringFromNode(refineArg);
1509 refine.setDefaultStr(defaultStr);
1510 } else if (refineArg instanceof Mandatory_stmtContext) {
1511 for (int j = 0; j < refineArg.getChildCount(); j++) {
1512 ParseTree mandatoryTree = refineArg.getChild(j);
1513 if (mandatoryTree instanceof Mandatory_argContext) {
1514 Boolean mandatory = Boolean
1515 .valueOf(stringFromNode(mandatoryTree));
1516 refine.setMandatory(mandatory);
1519 } else if (refineArg instanceof Must_stmtContext) {
1520 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1521 refine.setMust(must);
1528 private static RefineHolder parseRefine(RefineHolder refine,
1529 Refine_container_stmtsContext refineStmt) {
1530 for (int m = 0; m < refineStmt.getChildCount(); m++) {
1531 ParseTree refineArg = refineStmt.getChild(m);
1532 if (refineArg instanceof Must_stmtContext) {
1533 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1534 refine.setMust(must);
1535 } else if (refineArg instanceof Presence_stmtContext) {
1536 refine.setPresence(true);
1542 private static RefineHolder parseRefine(RefineHolder refine,
1543 Refine_list_stmtsContext refineStmt) {
1544 for (int m = 0; m < refineStmt.getChildCount(); m++) {
1545 ParseTree refineArg = refineStmt.getChild(m);
1546 if (refineArg instanceof Must_stmtContext) {
1547 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1548 refine.setMust(must);
1549 } else if (refineArg instanceof Max_elements_stmtContext) {
1550 Integer max = parseMaxElements((Max_elements_stmtContext) refineArg);
1551 refine.setMaxElements(max);
1552 } else if (refineArg instanceof Min_elements_stmtContext) {
1553 Integer min = parseMinElements((Min_elements_stmtContext) refineArg);
1554 refine.setMinElements(min);
1560 private static RefineHolder parseRefine(RefineHolder refine,
1561 Refine_leaf_list_stmtsContext refineStmt) {
1562 for (int m = 0; m < refineStmt.getChildCount(); m++) {
1563 ParseTree refineArg = refineStmt.getChild(m);
1564 if (refineArg instanceof Must_stmtContext) {
1565 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1566 refine.setMust(must);
1567 } else if (refineArg instanceof Max_elements_stmtContext) {
1568 Integer max = parseMaxElements((Max_elements_stmtContext) refineArg);
1569 refine.setMaxElements(max);
1570 } else if (refineArg instanceof Min_elements_stmtContext) {
1571 Integer min = parseMinElements((Min_elements_stmtContext) refineArg);
1572 refine.setMinElements(min);
1578 private static RefineHolder parseRefine(RefineHolder refine,
1579 Refine_choice_stmtsContext refineStmt) {
1580 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1581 ParseTree refineArg = refineStmt.getChild(i);
1582 if (refineArg instanceof Default_stmtContext) {
1583 String defaultStr = stringFromNode(refineArg);
1584 refine.setDefaultStr(defaultStr);
1585 } else if (refineArg instanceof Mandatory_stmtContext) {
1586 for (int j = 0; j < refineArg.getChildCount(); j++) {
1587 ParseTree mandatoryTree = refineArg.getChild(j);
1588 if (mandatoryTree instanceof Mandatory_argContext) {
1589 Boolean mandatory = Boolean
1590 .valueOf(stringFromNode(mandatoryTree));
1591 refine.setMandatory(mandatory);
1599 private static RefineHolder parseRefine(RefineHolder refine,
1600 Refine_anyxml_stmtsContext refineStmt) {
1601 for (int i = 0; i < refineStmt.getChildCount(); i++) {
1602 ParseTree refineArg = refineStmt.getChild(i);
1603 if (refineArg instanceof Must_stmtContext) {
1604 MustDefinition must = parseMust((Must_stmtContext) refineArg);
1605 refine.setMust(must);
1606 } else if (refineArg instanceof Mandatory_stmtContext) {
1607 for (int j = 0; j < refineArg.getChildCount(); j++) {
1608 ParseTree mandatoryTree = refineArg.getChild(j);
1609 if (mandatoryTree instanceof Mandatory_argContext) {
1610 Boolean mandatory = Boolean
1611 .valueOf(stringFromNode(mandatoryTree));
1612 refine.setMandatory(mandatory);