X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fspi%2Fmeta%2FStmtContextUtils.java;h=984c9e3eaba285ff026946486321eeba45d20ec4;hb=16db760aab4b98e1980877bc6adb971b413af198;hp=fa2e78bddb21689f1b33376e572be8ceea10b0d0;hpb=6af38a495f688cbfa0ff2c09db7e62e4d530bb3a;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index fa2e78bddb..984c9e3eab 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -7,140 +7,583 @@ */ package org.opendaylight.yangtools.yang.parser.spi.meta; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; import java.util.Collection; -import com.google.common.base.Function; +import java.util.Date; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; +import org.opendaylight.yangtools.yang.common.YangVersion; +import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; +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; +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.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.ImpPrefixToModuleIdentifier; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName; +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 Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { - @Override - public DeclaredStatement apply(StmtContext input) { - return input.buildDeclared(); - } - }; - - private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { - @Override - public EffectiveStatement apply(StmtContext input) { - return input.buildEffective(); - } - }; + public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults(); private StmtContextUtils() { throw new UnsupportedOperationException("Utility class"); } @SuppressWarnings("unchecked") - public static > Function, D> buildDeclared() { - return Function.class.cast(BUILD_DECLARED); + public static > A firstAttributeOf( + final Iterable> contexts, final Class declaredType) { + for (final StmtContext ctx : contexts) { + if (producesDeclared(ctx, declaredType)) { + return (A) ctx.getStatementArgument(); + } + } + return null; } @SuppressWarnings("unchecked") + public static > A firstAttributeOf(final StmtContext ctx, + final Class declaredType) { + return producesDeclared(ctx, declaredType) ? (A) ctx.getStatementArgument() : null; + } - public static final > Function, E> buildEffective() { - return Function.class.cast(BUILD_EFFECTIVE); + public static > A firstSubstatementAttributeOf( + final StmtContext ctx, final Class declaredType) { + return firstAttributeOf(ctx.allSubstatements(), declaredType); } @SuppressWarnings("unchecked") - public static final > AT firstAttributeOf( - Iterable> contexts, - Class
declaredType) { - for (StmtContext ctx : contexts) { - if (producesDeclared(ctx, declaredType)) { - return (AT) ctx.getStatementArgument(); + public static > StmtContext findFirstDeclaredSubstatement( + final StmtContext stmtContext, final Class declaredType) { + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + return (StmtContext) subStmtContext; } } return null; } @SuppressWarnings("unchecked") - public static final > AT firstAttributeOf( - StmtContext ctx, Class
declaredType) { + public static > Collection> findAllDeclaredSubstatements( + final StmtContext stmtContext, final Class declaredType) { + final ImmutableList.Builder> listBuilder = ImmutableList.builder(); + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + listBuilder.add((StmtContext) subStmtContext); + } + } + return listBuilder.build(); + } - if (producesDeclared(ctx, declaredType)) { - return (AT) ctx.getStatementArgument(); + @SuppressWarnings("unchecked") + public static > Collection> findAllEffectiveSubstatements( + final StmtContext stmtContext, final Class type) { + final ImmutableList.Builder> listBuilder = ImmutableList.builder(); + for (final StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext, type)) { + listBuilder.add((StmtContext) subStmtContext); + } } + return listBuilder.build(); + } - return null; + public static > Collection> findAllSubstatements( + final StmtContext stmtContext, final Class type) { + final ImmutableList.Builder> listBuilder = ImmutableList.builder(); + listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type)); + listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type)); + return listBuilder.build(); } - public static final
> StmtContext findFirstDeclaredSubstatement( - StmtContext stmtContext, Class
declaredType) { - Collection> declaredSubstatements = stmtContext - .declaredSubstatements(); - for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext,declaredType)) { - return subStmtContext; + @SuppressWarnings("unchecked") + public static > StmtContext findFirstEffectiveSubstatement( + final StmtContext stmtContext, final Class declaredType) { + for (final StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + return (StmtContext) subStmtContext; } } return null; } - public static final StmtContext findFirstDeclaredSubstatement( - StmtContext stmtContext, int startIndex, Class>... types) { + /** + * Searches for the first substatement of the specified type in the specified statement context. + * First, it tries to find the substatement in the effective substatements of the statement context. + * If it was not found, then it proceeds to search in the declared substatements. If it still was not found, + * the method returns null. + * + * @param stmtContext statement context to search in + * @param declaredType substatement type to search for + * @param statement argument type + * @param declared statement type + * @return statement context that was searched for or null if was not found + */ + public static > StmtContext findFirstSubstatement( + final StmtContext stmtContext, final Class declaredType) { + final StmtContext effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType); + return effectiveSubstatement != null ? effectiveSubstatement : findFirstDeclaredSubstatement(stmtContext, + declaredType); + } - if (startIndex >= types.length) + @SafeVarargs + public static StmtContext findFirstDeclaredSubstatement(final StmtContext stmtContext, + int startIndex, final Class>... types) { + if (startIndex >= types.length) { return null; + } - Collection> declaredSubstatements = stmtContext - .declaredSubstatements(); - for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext,types[startIndex])) { - if (startIndex + 1 == types.length) - return subStmtContext; - else - return findFirstDeclaredSubstatement(subStmtContext, - ++startIndex, types); + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, types[startIndex])) { + return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext, + ++startIndex, types); } } return null; } - public static final
> StmtContext findFirstDeclaredSubstatementOnSublevel( - StmtContext stmtContext, Class
declaredType, - int sublevel) { - Collection> declaredSubstatements = stmtContext - .declaredSubstatements(); - for (StmtContext subStmtContext : declaredSubstatements) { - if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) { + public static > StmtContext findFirstDeclaredSubstatementOnSublevel( + final StmtContext stmtContext, final Class declaredType, int sublevel) { + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) { return subStmtContext; - } else { - if (sublevel > 1) { - StmtContext result = findFirstDeclaredSubstatementOnSublevel( - subStmtContext, declaredType, --sublevel); - if (result != null) - return result; + } + if (sublevel > 1) { + final StmtContext result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, + declaredType, --sublevel); + if (result != null) { + return result; } } } + return null; } - public static final
> StmtContext findDeepFirstDeclaredSubstatement( - StmtContext stmtContext, Class
declaredType) { - - Collection> declaredSubstatements = stmtContext - .declaredSubstatements(); - - for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext,declaredType)) { + public static > StmtContext findDeepFirstDeclaredSubstatement( + final StmtContext stmtContext, final Class declaredType) { + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { return subStmtContext; - } else { - StmtContext result = findDeepFirstDeclaredSubstatement( - subStmtContext, declaredType); - if (result != null) - return result; + } + final StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); + if (result != null) { + return result; } } + return null; } - public static final boolean producesDeclared(StmtContext ctx, - Class> type) { - return type.isAssignableFrom(ctx.getPublicDefinition() - .getDeclaredRepresentationClass()); + 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.getParentContext().getParentContext() != null) { + current = current.getParentContext(); + if (isUnknownStatement(current)) { + return true; + } + } + + return false; + } + + /** + * Returns true if supplied statement context represents unknown statement, + * otherwise returns false. + * + * @param stmtCtx + * statement context to be checked + * @return true if supplied statement context represents unknown statement, + * otherwise false + * @throws NullPointerException + * if supplied statement context is null + */ + public static boolean isUnknownStatement(final StmtContext stmtCtx) { + return UnknownStatement.class + .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass()); + } + + /** + * Returns true if supplied statement context represents unrecognized + * statement, otherwise returns false. + * + * @param stmtCtx + * statement context to be checked + * @return true if supplied statement context represents unrecognized + * statement, otherwise false + * @throws NullPointerException + * if supplied statement context is null + */ + public static boolean isUnrecognizedStatement(final StmtContext stmtCtx) { + return UnrecognizedStatement.class + .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass()); + } + + public static boolean checkFeatureSupport(final StmtContext stmtContext, + final Set supportedFeatures) { + boolean isSupported = false; + boolean containsIfFeature = false; + for (final StmtContext stmt : stmtContext.declaredSubstatements()) { + if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) { + containsIfFeature = true; + if (((Predicate>) stmt.getStatementArgument()).test(supportedFeatures)) { + isSupported = true; + } else { + isSupported = false; + break; + } + } + } + + return !containsIfFeature || isSupported; + } + + /** + * Checks whether statement context is a presence container or not. + * + * @param stmtCtx + * statement context + * @return true if it is a presence container + */ + public static boolean isPresenceContainer(final StmtContext stmtCtx) { + return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx); + } + + /** + * Checks whether statement context is a non-presence container or not. + * + * @param stmtCtx + * statement context + * @return true if it is a non-presence container + */ + public static boolean isNonPresenceContainer(final StmtContext stmtCtx) { + return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx); + } + + private static boolean containsPresenceSubStmt(final StmtContext stmtCtx) { + return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null; + } + + /** + * Checks whether statement context is a mandatory leaf, choice, anyxml, + * list or leaf-list according to RFC6020 or not. + * + * @param stmtCtx + * statement context + * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list + * according to RFC6020. + */ + public static boolean isMandatoryNode(final StmtContext stmtCtx) { + if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) { + return false; + } + switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) { + case LEAF: + case CHOICE: + case ANYXML: + return Boolean.TRUE.equals(firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class)); + case LIST: + case LEAF_LIST: + final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class); + return minElements != null && minElements > 0; + default: + return false; + } + } + + /** + * Checks whether a statement context is a statement of supplied statement + * definition and whether it is not mandatory leaf, choice, anyxml, list or + * leaf-list according to RFC6020. + * + * @param stmtCtx + * statement context + * @param stmtDef + * statement definition + * @return true if supplied statement context is a statement of supplied + * statement definition and if it is not mandatory leaf, choice, + * anyxml, list or leaf-list according to RFC6020 + */ + public static boolean isNotMandatoryNodeOfType(final StmtContext stmtCtx, + final StatementDefinition stmtDef) { + return stmtCtx.getPublicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx); + } + + /** + * Checks whether at least one ancestor of a StatementContext matches one + * from collection of statement definitions. + * + * @param ctx + * StatementContext 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, + final Collection ancestorTypes) { + Preconditions.checkNotNull(ctx); + Preconditions.checkNotNull(ancestorTypes); + StmtContext current = ctx.getParentContext(); + while (current != null) { + if (ancestorTypes.contains(current.getPublicDefinition())) { + return true; + } + current = current.getParentContext(); + } + return false; + } + + /** + * Checks whether all of StmtContext's ancestors of specified type have a child of specified type + * + * @param ctx StmtContext 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 StatementDefinition ancestorChildType) { + Preconditions.checkNotNull(ctx); + Preconditions.checkNotNull(ancestorType); + Preconditions.checkNotNull(ancestorChildType); + + StmtContext current = ctx.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; + } + } + + current = parent; + parent = current.getParentContext(); + } + + return true; + } + + /** + * Checks whether the parent of StmtContext is of specified type + * + * @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) { + Preconditions.checkNotNull(ctx); + Preconditions.checkNotNull(parentType); + final StmtContext parentContext = ctx.getParentContext(); + return parentContext != null ? parentType.equals(parentContext.getPublicDefinition()) : false; + } + + /** + * Validates the specified statement context with regards to if-feature and when statement on list keys. + * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses + * statement defined in a keyed list (a uses statement may add leaves into the list). + * + * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown. + * + * @param ctx statement context to be validated + */ + public static void validateIfFeatureAndWhenOnListKeys(final StmtContext ctx) { + Preconditions.checkNotNull(ctx); + + if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) { + return; + } + + final StmtContext listStmtCtx = ctx.getParentContext(); + final StmtContext, ?, ?> keyStmtCtx = + StmtContextUtils.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 -> { + if (isListKey(leafStmtCtx, keyStmtCtx)) { + disallowIfFeatureAndWhenOnListKeys(leafStmtCtx); + } + }); + } + } + + private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext ctx) { + return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) + && StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.LIST) + && StmtContextUtils.findFirstDeclaredSubstatement(ctx.getParentContext(), KeyStatement.class) != null; + } + + private static boolean isListKey(final StmtContext leafStmtCtx, + final StmtContext, ?, ?> keyStmtCtx) { + for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.getStatementArgument()) { + if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) { + return true; + } + } + + return false; + } + + private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext leafStmtCtx) { + leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> { + final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition(); + SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef) + || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(), + "%s statement is not allowed in %s leaf statement which is specified as a list key.", + statementDef.getStatementName(), leafStmtCtx.getStatementArgument()); + }); + } + + public static QName qnameFromArgument(StmtContext ctx, final String value) { + if (Strings.isNullOrEmpty(value)) { + return ctx.getPublicDefinition().getStatementName(); + } + + String prefix; + QNameModule qNameModule = null; + String localName = null; + + final String[] namesParts = value.split(":"); + switch (namesParts.length) { + case 1: + localName = namesParts[0]; + qNameModule = StmtContextUtils.getRootModuleQName(ctx); + break; + default: + prefix = namesParts[0]; + localName = namesParts[1]; + qNameModule = StmtContextUtils.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); + } + if (qNameModule == null + && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) { + ctx = ctx.getOriginalCtx().orElse(null); + qNameModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix); + } + break; + } + + qNameModule = InferenceException.throwIfNull(qNameModule, ctx.getStatementSourceReference(), + "Cannot resolve QNameModule for '%s'", value); + + final QNameModule resultQNameModule; + if (qNameModule.getRevision() == null) { + resultQNameModule = QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV) + .intern(); + } else { + resultQNameModule = qNameModule; + } + + return ctx.getFromNamespace(QNameCacheNamespace.class, QName.create(resultQNameModule, localName)); + } + + public static QNameModule getRootModuleQName(final StmtContext ctx) { + if (ctx == null) { + return null; + } + + final StmtContext rootCtx = ctx.getRoot(); + final QNameModule qNameModule; + + if (producesDeclared(rootCtx, ModuleStatement.class)) { + qNameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx); + } else if (producesDeclared(rootCtx, SubmoduleStatement.class)) { + final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(), + BelongsToStatement.class); + qNameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName); + } else { + qNameModule = null; + } + + Preconditions.checkArgument(qNameModule != null, "Failed to look up root QNameModule for %s", ctx); + if (qNameModule.getRevision() != null) { + return qNameModule; + } + + return QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV).intern(); + } + + public static QNameModule getModuleQNameByPrefix(final StmtContext ctx, final String prefix) { + final ModuleIdentifier modId = ctx.getRoot().getFromNamespace(ImpPrefixToModuleIdentifier.class, prefix); + final QNameModule qNameModule = ctx.getFromNamespace(ModuleIdentifierToModuleQName.class, modId); + + if (qNameModule == null && producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) { + final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix); + return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName); + } + return qNameModule; + } + + 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.getFormattedRevision()); + } + + // creates SourceIdentifier for a submodule + final Date revision = Optional.ofNullable(getLatestRevision(root.declaredSubstatements())) + .orElse(SimpleDateFormatUtil.DEFAULT_DATE_REV); + final String formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision); + return RevisionSourceIdentifier.create((String) root.getStatementArgument(), formattedRevision); + } + + public static Date getLatestRevision(final Iterable> subStmts) { + Date revision = null; + for (final StmtContext subStmt : subStmts) { + if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(RevisionStatement + .class)) { + if (revision == null && subStmt.getStatementArgument() != null) { + revision = (Date) subStmt.getStatementArgument(); + } else if (subStmt.getStatementArgument() != null && ((Date) subStmt.getStatementArgument()).compareTo + (revision) > 0) { + revision = (Date) subStmt.getStatementArgument(); + } + } + } + return revision; } }