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=aadf22441be765ab15949e8d3e7dd83d6446f7f8;hb=61f49cd2ff55f24af91c232d3a82ab3f2b565255;hp=030185d7c77196c08506fddf250e8db881593d23;hpb=2c95faed5a453756b654584ffcc2a1d05a25d7be;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 030185d7c7..aadf22441b 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 @@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.spi.meta; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; -import com.google.common.base.CharMatcher; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import java.util.Collection; @@ -30,14 +29,11 @@ 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.source.BelongsToPrefixToModuleName; import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; @@ -45,20 +41,15 @@ import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public final class StmtContextUtils { - private static final CharMatcher IDENTIFIER_START = - CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z').or(CharMatcher.is('_'))).precomputed(); - private static final CharMatcher NOT_IDENTIFIER_PART = - IDENTIFIER_START.or(CharMatcher.inRange('0', '9')).or(CharMatcher.anyOf("-.")).negate().precomputed(); - 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(); } } @@ -68,7 +59,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( @@ -80,7 +71,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; } } @@ -88,6 +79,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) { @@ -95,7 +87,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); } @@ -108,7 +100,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); } } @@ -120,7 +112,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); } } @@ -139,7 +131,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; } } @@ -157,7 +149,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); @@ -166,9 +160,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) { @@ -184,9 +178,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; } @@ -199,21 +193,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; } /** @@ -244,8 +236,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, @@ -292,7 +283,7 @@ public final class StmtContextUtils { } private static boolean containsPresenceSubStmt(final StmtContext stmtCtx) { - return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null; + return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class); } /** @@ -382,12 +373,9 @@ public final class StmtContextUtils { StmtContext current = ctx.coerceParentContext(); 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.getPublicDefinition()) + && !current.hasSubstatement(ancestorChildType.getEffectiveRepresentationClass())) { + return false; } current = parent; @@ -409,7 +397,7 @@ public final class StmtContextUtils { public static boolean hasParentOfType(final StmtContext ctx, final StatementDefinition parentType) { requireNonNull(parentType); final StmtContext parentContext = ctx.getParentContext(); - return parentContext != null ? parentType.equals(parentContext.getPublicDefinition()) : false; + return parentContext != null && parentType.equals(parentContext.getPublicDefinition()); } /** @@ -427,16 +415,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 (isListKey(ctx, keyStmtCtx)) { disallowIfFeatureAndWhenOnListKeys(ctx); } } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) { - StmtContextUtils.findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> { + findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> { if (isListKey(leafStmtCtx, keyStmtCtx)) { disallowIfFeatureAndWhenOnListKeys(leafStmtCtx); } @@ -445,21 +432,13 @@ 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) { @@ -485,20 +464,20 @@ 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); } } @@ -519,10 +498,30 @@ public final class StmtContextUtils { public static QName parseIdentifier(final StmtContext ctx, final String str) { SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(), "Identifier may not be an empty string"); - checkIdentifierString(ctx, str); return internedQName(ctx, str); } + public static QName parseNodeIdentifier(StmtContext ctx, final String prefix, + final String localName) { + final QNameModule module = 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 = getModuleQNameByPrefix(ctx, prefix); + if (origModule != null) { + return internedQName(ctx, origModule, localName); + } + } + } + + throw new InferenceException(ctx.getStatementSourceReference(), "Cannot resolve QNameModule for '%s'", prefix); + } + /** * Parse a YANG node identifier string in context of a statement. * @@ -532,13 +531,12 @@ public final class StmtContextUtils { * @throws NullPointerException if any of the arguments are null * @throws SourceException if the string is not a valid YANG node identifier */ - public static QName parseNodeIdentifier(StmtContext ctx, final String str) { + public static QName parseNodeIdentifier(final StmtContext ctx, final String str) { SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(), "Node identifier may not be an empty string"); final int colon = str.indexOf(':'); if (colon == -1) { - checkIdentifierString(ctx, str); return internedQName(ctx, str); } @@ -548,39 +546,23 @@ public final class StmtContextUtils { final String localName = str.substring(colon + 1); SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(), "String '%s' has an empty identifier", str); - checkIdentifierString(ctx, 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'", str); - } - - private static void checkIdentifierString(final StmtContext ctx, final String str) { - SourceException.throwIf(!IDENTIFIER_START.matches(str.charAt(0)) || NOT_IDENTIFIER_PART.indexIn(str, 1) != -1, - ctx.getStatementSourceReference(), "String '%s' is not a valid 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, final String localName) { - return ctx.getFromNamespace(QNameCacheNamespace.class, QName.create(module, localName)); + final QName template; + try { + template = QName.create(module, localName); + } catch (IllegalArgumentException e) { + throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName); + } + return template.intern(); } public static QNameModule getRootModuleQName(final StmtContext ctx) { @@ -591,9 +573,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); @@ -606,38 +588,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 {