2 * Copyright (c) 2015 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/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Strings;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.Optional;
18 import java.util.function.Predicate;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.Revision;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
38 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
41 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
42 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
44 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 public final class StmtContextUtils {
47 private StmtContextUtils() {
51 @SuppressWarnings("unchecked")
52 public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(
53 final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
54 for (final StmtContext<?, ?, ?> ctx : contexts) {
55 if (producesDeclared(ctx, declaredType)) {
56 return (A) ctx.getStatementArgument();
62 @SuppressWarnings("unchecked")
63 public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
64 final Class<D> declaredType) {
65 return producesDeclared(ctx, declaredType) ? (A) ctx.getStatementArgument() : null;
68 public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
69 final StmtContext<?, ?, ?> ctx, final Class<D> declaredType) {
70 return firstAttributeOf(ctx.allSubstatements(), declaredType);
73 @SuppressWarnings("unchecked")
74 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstDeclaredSubstatement(
75 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
76 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
77 if (producesDeclared(subStmtContext, declaredType)) {
78 return (StmtContext<A, ?, ?>) subStmtContext;
85 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
86 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
87 if (startIndex >= types.length) {
91 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
92 if (producesDeclared(subStmtContext, types[startIndex])) {
93 return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
100 @SuppressWarnings("unchecked")
101 public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllDeclaredSubstatements(
102 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
103 final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
104 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
105 if (producesDeclared(subStmtContext, declaredType)) {
106 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
109 return listBuilder.build();
112 @SuppressWarnings("unchecked")
113 public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllEffectiveSubstatements(
114 final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
115 final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
116 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
117 if (producesDeclared(subStmtContext, type)) {
118 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
121 return listBuilder.build();
124 public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllSubstatements(
125 final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
126 final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
127 listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type));
128 listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type));
129 return listBuilder.build();
132 @SuppressWarnings("unchecked")
133 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
134 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
135 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
136 if (producesDeclared(subStmtContext, declaredType)) {
137 return (StmtContext<A, ?, ?>) subStmtContext;
144 * Searches for the first substatement of the specified type in the specified statement context.
145 * First, it tries to find the substatement in the effective substatements of the statement context.
146 * If it was not found, then it proceeds to search in the declared substatements. If it still was not found,
147 * the method returns null.
149 * @param stmtContext statement context to search in
150 * @param declaredType substatement type to search for
151 * @param <A> statement argument type
152 * @param <D> declared statement type
153 * @return statement context that was searched for or null if was not found
155 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstSubstatement(
156 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
157 final StmtContext<A, ?, ?> effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType);
158 return effectiveSubstatement != null ? effectiveSubstatement : findFirstDeclaredSubstatement(stmtContext,
162 public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
163 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType, int sublevel) {
164 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
165 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
166 return subStmtContext;
169 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(subStmtContext,
170 declaredType, --sublevel);
171 if (result != null) {
180 public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
181 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
182 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
183 if (producesDeclared(subStmtContext, declaredType)) {
184 return subStmtContext;
187 final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
188 if (result != null) {
196 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
197 final Class<? extends DeclaredStatement<?>> type) {
198 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
201 public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
202 StmtContext<?, ?, ?> current = stmtCtx;
203 while (current.coerceParentContext().getParentContext() != null) {
204 current = current.getParentContext();
205 if (isUnknownStatement(current)) {
214 * Returns true if supplied statement context represents unknown statement,
215 * otherwise returns false.
218 * statement context to be checked
219 * @return true if supplied statement context represents unknown statement,
221 * @throws NullPointerException
222 * if supplied statement context is null
224 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
225 return UnknownStatement.class
226 .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
230 * Returns true if supplied statement context represents unrecognized
231 * statement, otherwise returns false.
234 * statement context to be checked
235 * @return true if supplied statement context represents unrecognized
236 * statement, otherwise false
237 * @throws NullPointerException
238 * if supplied statement context is null
240 public static boolean isUnrecognizedStatement(final StmtContext<?, ?, ?> stmtCtx) {
241 return UnrecognizedStatement.class
242 .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
245 public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
246 final Set<QName> supportedFeatures) {
247 boolean isSupported = false;
248 boolean containsIfFeature = false;
249 for (final StmtContext<?, ?, ?> stmt : stmtContext.declaredSubstatements()) {
250 if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) {
251 containsIfFeature = true;
252 @SuppressWarnings("unchecked")
253 final Predicate<Set<QName>> argument = (Predicate<Set<QName>>) stmt.coerceStatementArgument();
254 if (argument.test(supportedFeatures)) {
263 return !containsIfFeature || isSupported;
267 * Checks whether statement context is a presence container or not.
271 * @return true if it is a presence container
273 public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
274 return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
278 * Checks whether statement context is a non-presence container or not.
282 * @return true if it is a non-presence container
284 public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
285 return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
288 private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
289 return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null;
293 * Checks whether statement context is a mandatory leaf, choice, anyxml,
294 * list or leaf-list according to RFC6020 or not.
298 * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
299 * according to RFC6020.
301 public static boolean isMandatoryNode(final StmtContext<?, ?, ?> stmtCtx) {
302 if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) {
305 switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) {
309 return Boolean.TRUE.equals(firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class));
312 final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class);
313 return minElements != null && minElements > 0;
320 * Checks whether a statement context is a statement of supplied statement
321 * definition and whether it is not mandatory leaf, choice, anyxml, list or
322 * leaf-list according to RFC6020.
327 * statement definition
328 * @return true if supplied statement context is a statement of supplied
329 * statement definition and if it is not mandatory leaf, choice,
330 * anyxml, list or leaf-list according to RFC6020
332 public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
333 final StatementDefinition stmtDef) {
334 return stmtCtx.getPublicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
338 * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
342 * StatementContext to be checked
343 * @param ancestorTypes
344 * collection of statement definitions
345 * @return true if at least one ancestor of a StatementContext matches one
346 * from collection of statement definitions, otherwise false.
348 public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> ctx,
349 final Collection<StatementDefinition> ancestorTypes) {
350 requireNonNull(ancestorTypes);
351 StmtContext<?, ?, ?> current = ctx.getParentContext();
352 while (current != null) {
353 if (ancestorTypes.contains(current.getPublicDefinition())) {
356 current = current.getParentContext();
362 * Checks whether all of StmtContext's ancestors of specified type have a child of specified type.
364 * @param ctx StmtContext to be checked
365 * @param ancestorType type of ancestor to search for
366 * @param ancestorChildType type of child to search for in the specified ancestor type
367 * @return true if all of StmtContext's ancestors of specified type have a child of specified type, otherwise false
369 public static <A, D extends DeclaredStatement<A>> boolean hasAncestorOfTypeWithChildOfType(
370 final StmtContext<?, ?, ?> ctx, final StatementDefinition ancestorType,
371 final StatementDefinition ancestorChildType) {
373 requireNonNull(ancestorType);
374 requireNonNull(ancestorChildType);
376 StmtContext<?, ?, ?> current = ctx.coerceParentContext();
377 StmtContext<?, ?, ?> parent = current.getParentContext();
378 while (parent != null) {
379 if (ancestorType.equals(current.getPublicDefinition())) {
380 @SuppressWarnings("unchecked")
381 final Class<D> ancestorChildTypeClass = (Class<D>) ancestorChildType.getDeclaredRepresentationClass();
382 if (findFirstSubstatement(current, ancestorChildTypeClass) == null) {
388 parent = current.getParentContext();
395 * Checks whether the parent of StmtContext is of specified type.
398 * StmtContext to be checked
400 * type of parent to check
401 * @return true if the parent of StmtContext is of specified type, otherwise false
403 public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
404 requireNonNull(parentType);
405 final StmtContext<?, ?, ?> parentContext = ctx.getParentContext();
406 return parentContext != null && parentType.equals(parentContext.getPublicDefinition());
410 * Validates the specified statement context with regards to if-feature and when statement on list keys.
411 * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses
412 * statement defined in a keyed list (a uses statement may add leaves into the list).
415 * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown.
417 * @param ctx statement context to be validated
419 public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) {
420 if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) {
424 final StmtContext<?, ?, ?> listStmtCtx = ctx.getParentContext();
425 final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx = findFirstDeclaredSubstatement(
426 listStmtCtx, KeyStatement.class);
428 if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) {
429 if (isListKey(ctx, keyStmtCtx)) {
430 disallowIfFeatureAndWhenOnListKeys(ctx);
432 } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) {
433 findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
434 if (isListKey(leafStmtCtx, keyStmtCtx)) {
435 disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
441 private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
442 return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
443 && findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null;
446 private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
447 final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx) {
448 for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.coerceStatementArgument()) {
449 if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) {
457 private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
458 leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
459 final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition();
460 SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
461 || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(),
462 "%s statement is not allowed in %s leaf statement which is specified as a list key.",
463 statementDef.getStatementName(), leafStmtCtx.getStatementArgument());
467 public static QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
468 if (Strings.isNullOrEmpty(value)) {
469 return ctx.getPublicDefinition().getStatementName();
473 QNameModule qnameModule = null;
474 String localName = null;
476 final String[] namesParts = value.split(":");
477 switch (namesParts.length) {
479 localName = namesParts[0];
480 qnameModule = getRootModuleQName(ctx);
483 prefix = namesParts[0];
484 localName = namesParts[1];
485 qnameModule = getModuleQNameByPrefix(ctx, prefix);
486 // in case of unknown statement argument, we're not going to parse it
487 if (qnameModule == null && isUnknownStatement(ctx)) {
489 qnameModule = getRootModuleQName(ctx);
491 if (qnameModule == null && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
492 ctx = ctx.getOriginalCtx().orElse(null);
493 qnameModule = getModuleQNameByPrefix(ctx, prefix);
497 return internedQName(ctx,
498 InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(),
499 "Cannot resolve QNameModule for '%s'", value), localName);
503 * Parse a YANG identifier string in context of a statement.
505 * @param ctx Statement context
506 * @param str String to be parsed
507 * @return An interned QName
508 * @throws NullPointerException if any of the arguments are null
509 * @throws SourceException if the string is not a valid YANG identifier
511 public static QName parseIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
512 SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
513 "Identifier may not be an empty string");
514 return internedQName(ctx, str);
517 public static QName parseNodeIdentifier(StmtContext<?, ?, ?> ctx, final String prefix,
518 final String localName) {
519 final QNameModule module = getModuleQNameByPrefix(ctx, prefix);
520 if (module != null) {
521 return internedQName(ctx, module, localName);
524 if (ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
525 final Optional<? extends StmtContext<?, ?, ?>> optOrigCtx = ctx.getOriginalCtx();
526 if (optOrigCtx.isPresent()) {
527 ctx = optOrigCtx.get();
528 final QNameModule origModule = getModuleQNameByPrefix(ctx, prefix);
529 if (origModule != null) {
530 return internedQName(ctx, origModule, localName);
535 throw new InferenceException(ctx.getStatementSourceReference(), "Cannot resolve QNameModule for '%s'", prefix);
539 * Parse a YANG node identifier string in context of a statement.
541 * @param ctx Statement context
542 * @param str String to be parsed
543 * @return An interned QName
544 * @throws NullPointerException if any of the arguments are null
545 * @throws SourceException if the string is not a valid YANG node identifier
547 public static QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
548 SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
549 "Node identifier may not be an empty string");
551 final int colon = str.indexOf(':');
553 return internedQName(ctx, str);
556 final String prefix = str.substring(0, colon);
557 SourceException.throwIf(prefix.isEmpty(), ctx.getStatementSourceReference(),
558 "String '%s' has an empty prefix", str);
559 final String localName = str.substring(colon + 1);
560 SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(),
561 "String '%s' has an empty identifier", str);
563 return parseNodeIdentifier(ctx, prefix, localName);
566 private static QName internedQName(final StmtContext<?, ?, ?> ctx, final String localName) {
567 return internedQName(ctx, getRootModuleQName(ctx), localName);
570 private static QName internedQName(final StmtContext<?, ?, ?> ctx, final QNameModule module,
571 final String localName) {
572 final QName template;
574 template = QName.create(module, localName);
575 } catch (IllegalArgumentException e) {
576 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName);
578 return template.intern();
581 public static QNameModule getRootModuleQName(final StmtContext<?, ?, ?> ctx) {
586 final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
587 final QNameModule qnameModule;
589 if (producesDeclared(rootCtx, ModuleStatement.class)) {
590 qnameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx);
591 } else if (producesDeclared(rootCtx, SubmoduleStatement.class)) {
592 final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(),
593 BelongsToStatement.class);
594 qnameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
599 checkArgument(qnameModule != null, "Failed to look up root QNameModule for %s", ctx);
603 public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
604 final StmtContext<?, ?, ?> importedModule = ctx.getRoot().getFromNamespace(ImportPrefixToModuleCtx.class,
606 final QNameModule qnameModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, importedModule);
607 if (qnameModule != null) {
611 if (producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) {
612 final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
613 return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
619 public static SourceIdentifier createSourceIdentifier(final StmtContext<?, ?, ?> root) {
620 final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
621 if (qNameModule != null) {
622 // creates SourceIdentifier for a module
623 return RevisionSourceIdentifier.create((String) root.getStatementArgument(), qNameModule.getRevision());
626 // creates SourceIdentifier for a submodule
627 final Optional<Revision> revision = getLatestRevision(root.declaredSubstatements());
628 return RevisionSourceIdentifier.create((String) root.getStatementArgument(), revision);
631 public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
632 Revision revision = null;
633 for (final StmtContext<?, ?, ?> subStmt : subStmts) {
634 if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(
635 RevisionStatement.class)) {
636 if (revision == null && subStmt.getStatementArgument() != null) {
637 revision = (Revision) subStmt.getStatementArgument();
639 final Revision subArg = (Revision) subStmt.getStatementArgument();
640 if (subArg != null && subArg.compareTo(revision) > 0) {
646 return Optional.ofNullable(revision);