X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-spi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fspi%2Fmeta%2FStmtContextUtils.java;h=f5fde6a803fddcd88fe09b0f4e00c5943e724fdc;hb=5db72610e7358baa1f5c9dd85abc9e93f3c8b020;hp=b21b46f80e104ce9d53066e5172683975a82866d;hpb=c8668229ad6e73d5ae03a52f4b87e8e4d2a67c6e;p=yangtools.git diff --git a/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index b21b46f80e..f5fde6a803 100644 --- a/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -22,6 +22,7 @@ import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.common.YangVersion; import org.opendaylight.yangtools.yang.model.api.YangStmtMapping; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition; import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement; import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement; @@ -29,14 +30,12 @@ import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement; import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement; import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement; import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement; import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement; -import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; -import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; +import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent; import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName; import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; @@ -45,14 +44,14 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public final class StmtContextUtils { private StmtContextUtils() { - throw new UnsupportedOperationException("Utility class"); + // Hidden on purpose } @SuppressWarnings("unchecked") public static > A firstAttributeOf( final Iterable> contexts, final Class declaredType) { for (final StmtContext ctx : contexts) { - if (producesDeclared(ctx, declaredType)) { + if (ctx.producesDeclared(declaredType)) { return (A) ctx.getStatementArgument(); } } @@ -62,7 +61,7 @@ public final class StmtContextUtils { @SuppressWarnings("unchecked") public static > A firstAttributeOf(final StmtContext ctx, final Class declaredType) { - return producesDeclared(ctx, declaredType) ? (A) ctx.getStatementArgument() : null; + return ctx.producesDeclared(declaredType) ? (A) ctx.getStatementArgument() : null; } public static > A firstSubstatementAttributeOf( @@ -74,7 +73,7 @@ public final class StmtContextUtils { public static > StmtContext findFirstDeclaredSubstatement( final StmtContext stmtContext, final Class declaredType) { for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { - if (producesDeclared(subStmtContext, declaredType)) { + if (subStmtContext.producesDeclared(declaredType)) { return (StmtContext) subStmtContext; } } @@ -82,6 +81,7 @@ public final class StmtContextUtils { } @SafeVarargs + @SuppressWarnings({ "rawtypes", "unchecked" }) public static StmtContext findFirstDeclaredSubstatement(final StmtContext stmtContext, int startIndex, final Class>... types) { if (startIndex >= types.length) { @@ -89,7 +89,7 @@ public final class StmtContextUtils { } for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { - if (producesDeclared(subStmtContext, types[startIndex])) { + if (subStmtContext.producesDeclared((Class) types[startIndex])) { return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types); } @@ -102,7 +102,7 @@ public final class StmtContextUtils { final StmtContext stmtContext, final Class declaredType) { final ImmutableList.Builder> listBuilder = ImmutableList.builder(); for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { - if (producesDeclared(subStmtContext, declaredType)) { + if (subStmtContext.producesDeclared(declaredType)) { listBuilder.add((StmtContext) subStmtContext); } } @@ -114,7 +114,7 @@ public final class StmtContextUtils { final StmtContext stmtContext, final Class type) { final ImmutableList.Builder> listBuilder = ImmutableList.builder(); for (final StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { - if (producesDeclared(subStmtContext, type)) { + if (subStmtContext.producesDeclared(type)) { listBuilder.add((StmtContext) subStmtContext); } } @@ -133,7 +133,7 @@ public final class StmtContextUtils { public static > StmtContext findFirstEffectiveSubstatement( final StmtContext stmtContext, final Class declaredType) { for (final StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { - if (producesDeclared(subStmtContext, declaredType)) { + if (subStmtContext.producesDeclared(declaredType)) { return (StmtContext) subStmtContext; } } @@ -151,7 +151,9 @@ public final class StmtContextUtils { * @param statement argument type * @param declared statement type * @return statement context that was searched for or null if was not found + * @deprecated Use {@link StmtContext#findSubstatementArgument(Class)} instead. */ + @Deprecated(forRemoval = true) public static > StmtContext findFirstSubstatement( final StmtContext stmtContext, final Class declaredType) { final StmtContext effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType); @@ -160,9 +162,9 @@ public final class StmtContextUtils { } public static > StmtContext findFirstDeclaredSubstatementOnSublevel( - final StmtContext stmtContext, final Class declaredType, int sublevel) { + final StmtContext stmtContext, final Class declaredType, int sublevel) { for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { - if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) { + if (sublevel == 1 && subStmtContext.producesDeclared(declaredType)) { return subStmtContext; } if (sublevel > 1) { @@ -178,9 +180,9 @@ public final class StmtContextUtils { } public static > StmtContext findDeepFirstDeclaredSubstatement( - final StmtContext stmtContext, final Class declaredType) { + final StmtContext stmtContext, final Class declaredType) { for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { - if (producesDeclared(subStmtContext, declaredType)) { + if (subStmtContext.producesDeclared(declaredType)) { return subStmtContext; } @@ -193,21 +195,19 @@ public final class StmtContextUtils { return null; } - public static boolean producesDeclared(final StmtContext ctx, - final Class> type) { - return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass()); - } - public static boolean isInExtensionBody(final StmtContext stmtCtx) { StmtContext current = stmtCtx; - while (current.coerceParentContext().getParentContext() != null) { - current = current.getParentContext(); - if (isUnknownStatement(current)) { + + while (true) { + final StmtContext parent = current.coerceParentContext(); + if (parent.getParentContext() == null) { + return false; + } + if (isUnknownStatement(parent)) { return true; } + current = parent; } - - return false; } /** @@ -222,8 +222,7 @@ public final class StmtContextUtils { * if supplied statement context is null */ public static boolean isUnknownStatement(final StmtContext stmtCtx) { - return UnknownStatement.class - .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass()); + return UnknownStatement.class.isAssignableFrom(stmtCtx.publicDefinition().getDeclaredRepresentationClass()); } /** @@ -238,8 +237,7 @@ public final class StmtContextUtils { * if supplied statement context is null */ public static boolean isUnrecognizedStatement(final StmtContext stmtCtx) { - return UnrecognizedStatement.class - .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass()); + return stmtCtx.producesDeclared(UnrecognizedStatement.class); } public static boolean checkFeatureSupport(final StmtContext stmtContext, @@ -247,7 +245,7 @@ public final class StmtContextUtils { boolean isSupported = false; boolean containsIfFeature = false; for (final StmtContext stmt : stmtContext.declaredSubstatements()) { - if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) { + if (YangStmtMapping.IF_FEATURE.equals(stmt.publicDefinition())) { containsIfFeature = true; @SuppressWarnings("unchecked") final Predicate> argument = (Predicate>) stmt.coerceStatementArgument(); @@ -271,7 +269,7 @@ public final class StmtContextUtils { * @return true if it is a presence container */ public static boolean isPresenceContainer(final StmtContext stmtCtx) { - return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx); + return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx); } /** @@ -282,11 +280,11 @@ public final class StmtContextUtils { * @return true if it is a non-presence container */ public static boolean isNonPresenceContainer(final StmtContext stmtCtx) { - return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx); + return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx); } private static boolean containsPresenceSubStmt(final StmtContext stmtCtx) { - return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null; + return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class); } /** @@ -299,10 +297,10 @@ public final class StmtContextUtils { * according to RFC6020. */ public static boolean isMandatoryNode(final StmtContext stmtCtx) { - if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) { + if (!(stmtCtx.publicDefinition() instanceof YangStmtMapping)) { return false; } - switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) { + switch ((YangStmtMapping) stmtCtx.publicDefinition()) { case LEAF: case CHOICE: case ANYXML: @@ -331,29 +329,27 @@ public final class StmtContextUtils { */ public static boolean isNotMandatoryNodeOfType(final StmtContext stmtCtx, final StatementDefinition stmtDef) { - return stmtCtx.getPublicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx); + return stmtCtx.publicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx); } /** * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement * definitions. * - * @param ctx - * StatementContext to be checked - * @param ancestorTypes - * collection of statement definitions + * @param stmt EffectiveStmtCtx to be checked + * @param ancestorTypes collection of statement definitions * @return true if at least one ancestor of a StatementContext matches one * from collection of statement definitions, otherwise false. */ - public static boolean hasAncestorOfType(final StmtContext ctx, + public static boolean hasAncestorOfType(final EffectiveStmtCtx stmt, final Collection ancestorTypes) { requireNonNull(ancestorTypes); - StmtContext current = ctx.getParentContext(); + Parent current = stmt.effectiveParent(); while (current != null) { - if (ancestorTypes.contains(current.getPublicDefinition())) { + if (ancestorTypes.contains(current.publicDefinition())) { return true; } - current = current.getParentContext(); + current = current.effectiveParent(); } return false; } @@ -361,27 +357,23 @@ public final class StmtContextUtils { /** * Checks whether all of StmtContext's ancestors of specified type have a child of specified type. * - * @param ctx StmtContext to be checked + * @param stmt EffectiveStmtCtx to be checked * @param ancestorType type of ancestor to search for * @param ancestorChildType type of child to search for in the specified ancestor type * @return true if all of StmtContext's ancestors of specified type have a child of specified type, otherwise false */ public static > boolean hasAncestorOfTypeWithChildOfType( - final StmtContext ctx, final StatementDefinition ancestorType, + final EffectiveStmtCtx.Current stmt, final StatementDefinition ancestorType, final StatementDefinition ancestorChildType) { - requireNonNull(ctx); + requireNonNull(stmt); requireNonNull(ancestorType); - requireNonNull(ancestorChildType); - StmtContext current = ctx.coerceParentContext(); + final Class> repr = ancestorChildType.getEffectiveRepresentationClass(); + StmtContext current = stmt.caerbannog().getParentContext(); StmtContext parent = current.getParentContext(); while (parent != null) { - if (ancestorType.equals(current.getPublicDefinition())) { - @SuppressWarnings("unchecked") - final Class ancestorChildTypeClass = (Class) ancestorChildType.getDeclaredRepresentationClass(); - if (findFirstSubstatement(current, ancestorChildTypeClass) == null) { - return false; - } + if (ancestorType.equals(current.publicDefinition()) && !current.hasSubstatement(repr)) { + return false; } current = parent; @@ -391,19 +383,29 @@ public final class StmtContextUtils { return true; } + /** + * Checks whether the parent of EffectiveStmtCtx is of specified type. + * + * @param stmt EffectiveStmtCtx to be checked + * @param parentType type of parent to check + * @return true if the parent of StmtContext is of specified type, otherwise false + */ + public static boolean hasParentOfType(final EffectiveStmtCtx.Current stmt, + final StatementDefinition parentType) { + return hasParentOfType(stmt.caerbannog(), parentType); + } + /** * Checks whether the parent of StmtContext is of specified type. * - * @param ctx - * StmtContext to be checked - * @param parentType - * type of parent to check + * @param ctx StmtContext to be checked + * @param parentType type of parent to check * @return true if the parent of StmtContext is of specified type, otherwise false */ public static boolean hasParentOfType(final StmtContext ctx, final StatementDefinition parentType) { requireNonNull(parentType); final StmtContext parentContext = ctx.getParentContext(); - return parentContext != null && parentType.equals(parentContext.getPublicDefinition()); + return parentContext != null && parentType.equals(parentContext.publicDefinition()); } /** @@ -421,16 +423,15 @@ public final class StmtContextUtils { return; } - final StmtContext listStmtCtx = ctx.getParentContext(); - final StmtContext, ?, ?> keyStmtCtx = - StmtContextUtils.findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class); + final StmtContext listStmtCtx = ctx.coerceParentContext(); + final StmtContext, ?, ?> keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class); - if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) { + if (YangStmtMapping.LEAF.equals(ctx.publicDefinition())) { if (isListKey(ctx, keyStmtCtx)) { disallowIfFeatureAndWhenOnListKeys(ctx); } - } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) { - StmtContextUtils.findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> { + } else if (YangStmtMapping.USES.equals(ctx.publicDefinition())) { + findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> { if (isListKey(leafStmtCtx, keyStmtCtx)) { disallowIfFeatureAndWhenOnListKeys(leafStmtCtx); } @@ -439,28 +440,20 @@ public final class StmtContextUtils { } private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext ctx) { - return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) - && StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.LIST) - && StmtContextUtils.findFirstDeclaredSubstatement(ctx.coerceParentContext(), - KeyStatement.class) != null; + return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST) + && findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null; } private static boolean isListKey(final StmtContext leafStmtCtx, - final StmtContext, ?, ?> keyStmtCtx) { - for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.coerceStatementArgument()) { - if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) { - return true; - } - } - - return false; + final StmtContext, ?, ?> keyStmtCtx) { + return keyStmtCtx.coerceStatementArgument().contains(leafStmtCtx.getStatementArgument()); } private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext leafStmtCtx) { leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> { - final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition(); + final StatementDefinition statementDef = leafSubstmtCtx.publicDefinition(); SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef) - || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(), + || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.sourceReference(), "%s statement is not allowed in %s leaf statement which is specified as a list key.", statementDef.getStatementName(), leafStmtCtx.getStatementArgument()); }); @@ -468,7 +461,7 @@ public final class StmtContextUtils { public static QName qnameFromArgument(StmtContext ctx, final String value) { if (Strings.isNullOrEmpty(value)) { - return ctx.getPublicDefinition().getStatementName(); + return ctx.publicDefinition().getStatementName(); } String prefix; @@ -479,25 +472,24 @@ public final class StmtContextUtils { switch (namesParts.length) { case 1: localName = namesParts[0]; - qnameModule = StmtContextUtils.getRootModuleQName(ctx); + qnameModule = getRootModuleQName(ctx); break; default: prefix = namesParts[0]; localName = namesParts[1]; - qnameModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix); + qnameModule = getModuleQNameByPrefix(ctx, prefix); // in case of unknown statement argument, we're not going to parse it if (qnameModule == null && isUnknownStatement(ctx)) { localName = value; - qnameModule = StmtContextUtils.getRootModuleQName(ctx); + qnameModule = getRootModuleQName(ctx); } if (qnameModule == null && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) { ctx = ctx.getOriginalCtx().orElse(null); - qnameModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix); + qnameModule = getModuleQNameByPrefix(ctx, prefix); } } - return internedQName(ctx, - InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(), + return internedQName(ctx, InferenceException.throwIfNull(qnameModule, ctx.sourceReference(), "Cannot resolve QNameModule for '%s'", value), localName); } @@ -511,30 +503,16 @@ public final class StmtContextUtils { * @throws SourceException if the string is not a valid YANG identifier */ public static QName parseIdentifier(final StmtContext ctx, final String str) { - SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(), - "Identifier may not be an empty string"); + SourceException.throwIf(str.isEmpty(), ctx.sourceReference(), "Identifier may not be an empty string"); return internedQName(ctx, str); } - public static QName parseNodeIdentifier(StmtContext ctx, final String prefix, + public static QName parseNodeIdentifier(final StmtContext ctx, final String prefix, final String localName) { - final QNameModule module = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix); - if (module != null) { - return internedQName(ctx, module, localName); - } - - if (ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) { - final Optional> optOrigCtx = ctx.getOriginalCtx(); - if (optOrigCtx.isPresent()) { - ctx = optOrigCtx.get(); - final QNameModule origModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix); - if (origModule != null) { - return internedQName(ctx, origModule, localName); - } - } - } - - throw new InferenceException(ctx.getStatementSourceReference(), "Cannot resolve QNameModule for '%s'", prefix); + return internedQName(ctx, + InferenceException.throwIfNull(getModuleQNameByPrefix(ctx, prefix), ctx.sourceReference(), + "Cannot resolve QNameModule for '%s'", prefix), + localName); } /** @@ -547,8 +525,7 @@ public final class StmtContextUtils { * @throws SourceException if the string is not a valid YANG node identifier */ public static QName parseNodeIdentifier(final StmtContext ctx, final String str) { - SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(), - "Node identifier may not be an empty string"); + SourceException.throwIf(str.isEmpty(), ctx.sourceReference(), "Node identifier may not be an empty string"); final int colon = str.indexOf(':'); if (colon == -1) { @@ -556,17 +533,15 @@ public final class StmtContextUtils { } final String prefix = str.substring(0, colon); - SourceException.throwIf(prefix.isEmpty(), ctx.getStatementSourceReference(), - "String '%s' has an empty prefix", str); + SourceException.throwIf(prefix.isEmpty(), ctx.sourceReference(), "String '%s' has an empty prefix", str); final String localName = str.substring(colon + 1); - SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(), - "String '%s' has an empty identifier", str); + SourceException.throwIf(localName.isEmpty(), ctx.sourceReference(), "String '%s' has an empty identifier", str); return parseNodeIdentifier(ctx, prefix, localName); } private static QName internedQName(final StmtContext ctx, final String localName) { - return internedQName(ctx, StmtContextUtils.getRootModuleQName(ctx), localName); + return internedQName(ctx, getRootModuleQName(ctx), localName); } private static QName internedQName(final StmtContext ctx, final QNameModule module, @@ -575,10 +550,9 @@ public final class StmtContextUtils { try { template = QName.create(module, localName); } catch (IllegalArgumentException e) { - throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName); + throw new SourceException(ctx.sourceReference(), e, "Invalid identifier '%s'", localName); } - - return ctx.getFromNamespace(QNameCacheNamespace.class, template); + return template.intern(); } public static QNameModule getRootModuleQName(final StmtContext ctx) { @@ -589,9 +563,9 @@ public final class StmtContextUtils { final StmtContext rootCtx = ctx.getRoot(); final QNameModule qnameModule; - if (producesDeclared(rootCtx, ModuleStatement.class)) { + if (rootCtx.producesDeclared(ModuleStatement.class)) { qnameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx); - } else if (producesDeclared(rootCtx, SubmoduleStatement.class)) { + } else if (rootCtx.producesDeclared(SubmoduleStatement.class)) { final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(), BelongsToStatement.class); qnameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName); @@ -604,38 +578,25 @@ public final class StmtContextUtils { } public static QNameModule getModuleQNameByPrefix(final StmtContext ctx, final String prefix) { - final StmtContext importedModule = ctx.getRoot().getFromNamespace(ImportPrefixToModuleCtx.class, - prefix); + final StmtContext root = ctx.getRoot(); + final StmtContext importedModule = root.getFromNamespace(ImportPrefixToModuleCtx.class, prefix); final QNameModule qnameModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, importedModule); if (qnameModule != null) { return qnameModule; } - if (producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) { - final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix); + if (root.producesDeclared(SubmoduleStatement.class)) { + final String moduleName = root.getFromNamespace(BelongsToPrefixToModuleName.class, prefix); return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName); } return null; } - public static SourceIdentifier createSourceIdentifier(final StmtContext root) { - final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root); - if (qNameModule != null) { - // creates SourceIdentifier for a module - return RevisionSourceIdentifier.create((String) root.getStatementArgument(), qNameModule.getRevision()); - } - - // creates SourceIdentifier for a submodule - final Optional revision = getLatestRevision(root.declaredSubstatements()); - return RevisionSourceIdentifier.create((String) root.getStatementArgument(), revision); - } - public static Optional getLatestRevision(final Iterable> subStmts) { Revision revision = null; for (final StmtContext subStmt : subStmts) { - if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom( - RevisionStatement.class)) { + if (subStmt.producesDeclared(RevisionStatement.class)) { if (revision == null && subStmt.getStatementArgument() != null) { revision = (Revision) subStmt.getStatementArgument(); } else {