Introduce moduleQName for sharing 51/8751/1
authorRobert Varga <rovarga@cisco.com>
Mon, 7 Jul 2014 19:33:28 +0000 (21:33 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 7 Jul 2014 19:39:31 +0000 (21:39 +0200)
If we stire namespace/revision/prefix in a QName, we are able to create
related QNames using a utility method, which will automatically share
the resulting QNameModule.

Change-Id: I53fbf10cf853cb3a4b265fd1f9823f604f148e73
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/ParserListenerUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java

index f8de37a57c85384377e59747eaf3538df52d5ec9..c434f3397549f3f42b9ca493b0d2da60fc95c74a 100644 (file)
@@ -772,7 +772,7 @@ public class ModuleBuilder extends AbstractDocumentedDataNodeContainerBuilder im
         ((TypeAwareBuilder) parent).setType(type);
     }
 
-    public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
+    public UnionTypeBuilder addUnionType(final int line, final QNameModule module) {
         final Builder parent = getActualNode();
         if (parent == null) {
             throw new YangParseException(name, line, "Unresolved parent of union type");
index b242cb7eee777f98397daa9250e54d36247249c0..74d26afb2454031c024e0e0e51d41a5611d18f6a 100644 (file)
@@ -16,10 +16,8 @@ import com.google.common.collect.Lists;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
@@ -1059,8 +1057,7 @@ public final class ParserListenerUtils {
      * @return UnknownType object with constraints from parsed type body
      */
     public static TypeDefinition<?> parseUnknownTypeWithBody(final QName typedefQName,
-            final Type_body_stmtsContext ctx, final SchemaPath actualPath, final URI namespace, final Date revision,
-            final String prefix, final Builder parent) {
+            final Type_body_stmtsContext ctx, final SchemaPath actualPath, final QName moduleQName, final Builder parent) {
         String moduleName = parent.getModuleName();
         String typeName = typedefQName.getLocalName();
 
@@ -1081,7 +1078,7 @@ public final class ParserListenerUtils {
                 return unknownType.build();
             } else {
                 TypeDefinition<?> baseType = unknownType.build();
-                QName qname = new QName(namespace, revision, prefix, typeName);
+                QName qname = QName.create(moduleQName, typeName);
                 SchemaPath schemaPath = createTypePath(actualPath, typeName);
 
                 ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, null, null, schemaPath);
@@ -1117,8 +1114,7 @@ public final class ParserListenerUtils {
      * @return TypeDefinition object based on parsed values.
      */
     public static TypeDefinition<?> parseTypeWithBody(final String typeName, final Type_body_stmtsContext typeBody,
-            final SchemaPath actualPath, final URI namespace, final Date revision, final String prefix,
-            final Builder parent) {
+            final SchemaPath actualPath, final QName moduleQName, final Builder parent) {
 
         final String moduleName = parent.getModuleName();
         final int line = typeBody.getStart().getLine();
@@ -1136,7 +1132,7 @@ public final class ParserListenerUtils {
         constraints.addRanges(rangeStatements);
 
         SchemaPath baseTypePath = createBaseTypePath(actualPath, typeName);
-        SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, namespace, revision, prefix, typeName);
+        SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, moduleQName, typeName);
 
         if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
             extBaseTypePath = baseTypePath;
@@ -1145,12 +1141,12 @@ public final class ParserListenerUtils {
         if ("decimal64".equals(typeName)) {
             if (rangeStatements.isEmpty()) {
                 try {
-                    return new Decimal64(baseTypePath, fractionDigits);
+                    return Decimal64.create(baseTypePath, fractionDigits);
                 } catch(Exception e) {
                     throw new YangParseException(moduleName, line, e.getMessage());
                 }
             }
-            Decimal64 decimalType = new Decimal64(extBaseTypePath, fractionDigits);
+            Decimal64 decimalType = Decimal64.create(extBaseTypePath, fractionDigits);
             constraints.addRanges(decimalType.getRangeConstraints());
             baseType = decimalType;
         } else if (typeName.startsWith("int")) {
@@ -1226,7 +1222,7 @@ public final class ParserListenerUtils {
             return baseType;
         }
 
-        QName qname = new QName(namespace, revision, prefix, typeName);
+        QName qname = QName.create(moduleQName, typeName);
         SchemaPath schemaPath = actualPath.createChild(qname);
         ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath);
 
@@ -1247,10 +1243,9 @@ public final class ParserListenerUtils {
         return actual.createChild(BaseTypes.constructQName(typeName));
     }
 
-    private static SchemaPath createExtendedBaseTypePath(final SchemaPath actual, final URI namespace, final Date revision,
-            final String prefix, final String typeName) {
+    private static SchemaPath createExtendedBaseTypePath(final SchemaPath actual, final QName moduleQName, final String typeName) {
         return actual.createChild(
-                new QName(namespace, revision, prefix, typeName),
+                QName.create(moduleQName, typeName),
                 BaseTypes.constructQName(typeName));
     }
 
index 89ef07ba99aba51b252b2f937a2316e39b83e513..55b610e1e3f7ceada209cf4fae926cf75848ac62 100644 (file)
@@ -106,11 +106,9 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
     private final Stack<Stack<QName>> actualPath = new Stack<>();
     private final String sourcePath;
+    private QName moduleQName = new QName(null, new Date(0L), null, "dummy");
     private ModuleBuilder moduleBuilder;
     private String moduleName;
-    private URI namespace;
-    private String yangModelPrefix;
-    private Date revision = new Date(0L);
     private int augmentOrder;
 
     private void addNodeToPath(final QName name) {
@@ -162,7 +160,8 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         actualPath.pop();
     }
 
-    @Override public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
+    @Override
+    public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
         moduleName = stringFromNode(ctx);
         LOGGER.trace("entering submodule {}", moduleName);
         enterLog("submodule", moduleName, 0);
@@ -207,11 +206,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             final ParseTree treeNode = ctx.getChild(i);
             if (treeNode instanceof Namespace_stmtContext) {
                 final String namespaceStr = stringFromNode(treeNode);
-                namespace = URI.create(namespaceStr);
-                moduleBuilder.setNamespace(namespace);
+                final URI namespace = URI.create(namespaceStr);
+                this.moduleQName = new QName(namespace, moduleQName.getRevision(), moduleQName.getPrefix(), moduleQName.getLocalName());
+                moduleBuilder.setQNameModule(moduleQName.getModule());
                 setLog("namespace", namespaceStr);
             } else if (treeNode instanceof Prefix_stmtContext) {
-                yangModelPrefix = stringFromNode(treeNode);
+                final String yangModelPrefix = stringFromNode(treeNode);
+                this.moduleQName = new QName(moduleQName.getNamespace(), moduleQName.getRevision(), yangModelPrefix, moduleQName.getLocalName());
                 moduleBuilder.setPrefix(yangModelPrefix);
                 setLog("prefix", yangModelPrefix);
             } else if (treeNode instanceof Yang_version_stmtContext) {
@@ -281,10 +282,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String revisionDateStr = stringFromNode(treeNode);
         try {
             final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
-            if ((revisionDate != null) && (this.revision.compareTo(revisionDate) < 0)) {
-                this.revision = revisionDate;
-                moduleBuilder.setRevision(this.revision);
-                setLog("revision", this.revision.toString());
+            if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
+                this.moduleQName = new QName(moduleQName.getNamespace(), revisionDate, moduleQName.getPrefix(), moduleQName.getLocalName());
+                moduleBuilder.setQNameModule(moduleQName.getModule());
+                setLog("revision", revisionDate.toString());
                 for (int i = 0; i < treeNode.getChildCount(); ++i) {
                     ParseTree child = treeNode.getChild(i);
                     if (child instanceof Reference_stmtContext) {
@@ -367,7 +368,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final int line = ctx.getStart().getLine();
         final String extName = stringFromNode(ctx);
         enterLog("extension", extName, line);
-        QName qname = new QName(namespace, revision, yangModelPrefix, extName);
+        QName qname = QName.create(moduleQName, extName);
         addNodeToPath(qname);
         SchemaPath path = currentSchemaPath();
 
@@ -401,7 +402,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final int line = ctx.getStart().getLine();
         final String typedefName = stringFromNode(ctx);
         enterLog("typedef", typedefName, line);
-        QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName);
+        QName typedefQName = QName.create(moduleQName, typedefName);
         addNodeToPath(typedefQName);
         SchemaPath path = currentSchemaPath();
 
@@ -451,7 +452,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                 case "union":
                     qname = BaseTypes.UNION_QNAME;
                     addNodeToPath(qname);
-                    UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
+                    UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
                     Builder parent = moduleBuilder.getActualNode();
                     unionBuilder.setParent(parent);
                     moduleBuilder.enterNode(unionBuilder);
@@ -463,15 +464,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                     moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
                     break;
                 default:
-                    type = parseTypeWithBody(typeName, typeBody, currentSchemaPath(), namespace, revision,
-                            yangModelPrefix, moduleBuilder.getActualNode());
+                    type = parseTypeWithBody(typeName, typeBody, currentSchemaPath(), moduleQName, moduleBuilder.getActualNode());
                     moduleBuilder.setType(type);
                     addNodeToPath(type.getQName());
                 }
             }
         } else {
-            type = parseUnknownTypeWithBody(typeQName, typeBody, currentSchemaPath(), namespace, revision,
-                    yangModelPrefix, moduleBuilder.getActualNode());
+            type = parseUnknownTypeWithBody(typeQName, typeBody, currentSchemaPath(), moduleQName, moduleBuilder.getActualNode());
             // add parent node of this type statement to dirty nodes
             moduleBuilder.markActualNodeDirty();
             moduleBuilder.setType(type);
@@ -486,13 +485,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             final Iterator<String> split = COLON_SPLITTER.split(typeName).iterator();
             final String prefix = split.next();
             final String name = split.next();
-            if (prefix.equals(yangModelPrefix)) {
-                typeQName = new QName(namespace, revision, prefix, name);
+            if (prefix.equals(moduleQName.getPrefix())) {
+                typeQName = QName.create(moduleQName, name);
             } else {
                 typeQName = new QName(null, null, prefix, name);
             }
         } else {
-            typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
+            typeQName = QName.create(moduleQName, typeName);
         }
         return typeQName;
     }
@@ -511,7 +510,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final int line = ctx.getStart().getLine();
         final String groupName = stringFromNode(ctx);
         enterLog("grouping", groupName, line);
-        QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
+        QName groupQName = QName.create(moduleQName, groupName);
         addNodeToPath(groupQName);
         SchemaPath path = currentSchemaPath();
 
@@ -533,7 +532,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String containerName = stringFromNode(ctx);
         enterLog("container", containerName, line);
 
-        QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
+        QName containerQName = QName.create(moduleQName, containerName);
         addNodeToPath(containerQName);
         SchemaPath path = currentSchemaPath();
 
@@ -565,7 +564,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String leafName = stringFromNode(ctx);
         enterLog("leaf", leafName, line);
 
-        QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
+        QName leafQName = QName.create(moduleQName, leafName);
         addNodeToPath(leafQName);
         SchemaPath path = currentSchemaPath();
 
@@ -666,7 +665,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final int line = ctx.getStart().getLine();
         final String leafListName = stringFromNode(ctx);
         enterLog("leaf-list", leafListName, line);
-        QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
+        QName leafListQName = QName.create(moduleQName, leafListName);
         addNodeToPath(leafListQName);
         SchemaPath path = currentSchemaPath();
 
@@ -700,7 +699,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String listName = stringFromNode(ctx);
         enterLog("list", listName, line);
 
-        QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
+        QName listQName = QName.create(moduleQName, listName);
         addNodeToPath(listQName);
         SchemaPath path = currentSchemaPath();
 
@@ -736,7 +735,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String anyXmlName = stringFromNode(ctx);
         enterLog("anyxml", anyXmlName, line);
 
-        QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
+        QName anyXmlQName = QName.create(moduleQName, anyXmlName);
         addNodeToPath(anyXmlQName);
         SchemaPath path = currentSchemaPath();
 
@@ -760,7 +759,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String choiceName = stringFromNode(ctx);
         enterLog("choice", choiceName, line);
 
-        QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
+        QName choiceQName = QName.create(moduleQName, choiceName);
         addNodeToPath(choiceQName);
         SchemaPath path = currentSchemaPath();
 
@@ -794,7 +793,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String caseName = stringFromNode(ctx);
         enterLog("case", caseName, line);
 
-        QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
+        QName caseQName = QName.create(moduleQName, caseName);
         addNodeToPath(caseQName);
         SchemaPath path = currentSchemaPath();
 
@@ -817,7 +816,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String notificationName = stringFromNode(ctx);
         enterLog("notification", notificationName, line);
 
-        QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
+        QName notificationQName = QName.create(moduleQName, notificationName);
         addNodeToPath(notificationQName);
         SchemaPath path = currentSchemaPath();
 
@@ -878,7 +877,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String rpcName = stringFromNode(ctx);
         enterLog("rpc", rpcName, line);
 
-        QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
+        QName rpcQName = QName.create(moduleQName, rpcName);
         addNodeToPath(rpcQName);
         SchemaPath path = currentSchemaPath();
 
@@ -901,7 +900,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String input = "input";
         enterLog(input, input, line);
 
-        QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
+        QName rpcQName = QName.create(moduleQName, input);
         addNodeToPath(rpcQName);
         SchemaPath path = currentSchemaPath();
 
@@ -925,7 +924,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String output = "output";
         enterLog(output, output, line);
 
-        QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
+        QName rpcQName = QName.create(moduleQName, output);
         addNodeToPath(rpcQName);
         SchemaPath path = currentSchemaPath();
 
@@ -949,7 +948,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String featureName = stringFromNode(ctx);
         enterLog("feature", featureName, line);
 
-        QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
+        QName featureQName = QName.create(moduleQName, featureName);
         addNodeToPath(featureQName);
         SchemaPath path = currentSchemaPath();
 
@@ -1006,14 +1005,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String identityName = stringFromNode(ctx);
         enterLog("identity", identityName, line);
 
-        final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
+        final QName identityQName = QName.create(moduleQName, identityName);
         addNodeToPath(identityQName);
         SchemaPath path = currentSchemaPath();
 
         IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
         moduleBuilder.enterNode(builder);
 
-
         parseSchemaNodeArgs(ctx, builder);
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
@@ -1060,9 +1058,9 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String e0 = splittedElement.next();
         final QName nodeType;
         if (splittedElement.hasNext()) {
-            nodeType = new QName(namespace, revision, e0, splittedElement.next());
+            nodeType = new QName(moduleQName.getNamespace(), moduleQName.getRevision(), e0, splittedElement.next());
         } else {
-            nodeType = new QName(namespace, revision, yangModelPrefix, e0);
+            nodeType = QName.create(moduleQName, e0);
         }
 
         QName qname;
@@ -1074,7 +1072,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                 if (Iterables.size(splittedName) == 2) {
                     qname = new QName(null, null, it.next(), it.next());
                 } else {
-                    qname = new QName(namespace, revision, yangModelPrefix, it.next());
+                    qname = QName.create(moduleQName, it.next());
                 }
             } else {
                 qname = nodeType;