public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) { StmtContext<?, ?, ?> current = stmtCtx; while (current.coerceParentContext().getParentContext() != null) { current = current.getParentContext(); if (isUnknownStatement(current)) { return true; } } return false; }
@Override public final GroupingStatement createDeclared(final StmtContext<QName, GroupingStatement, ?> ctx) { // Shadowing check: make sure grandparent does not see a conflicting definition. This is required to ensure // that a grouping in child scope does not shadow a grouping in parent scope which occurs later in the text. final StmtContext<?, ?, ?> parent = ctx.getParentContext(); if (parent != null) { final StmtContext<?, ?, ?> grandParent = parent.getParentContext(); if (grandParent != null) { checkConflict(grandParent, ctx); } } return new GroupingStatementImpl(ctx); }
@Override public TypedefStatement createDeclared(final StmtContext<QName, TypedefStatement, ?> ctx) { // Shadowing check: make sure grandparent does not see a conflicting definition. This is required to ensure // that a typedef in child scope does not shadow a typedef in parent scope which occurs later in the text. final StmtContext<?, ?, ?> parent = ctx.getParentContext(); if (parent != null) { final StmtContext<?, ?, ?> grandParent = parent.getParentContext(); if (grandParent != null) { checkConflict(grandParent, ctx); } } return new TypedefStatementImpl(ctx); }
/** * 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) { requireNonNull(parentType); final StmtContext<?, ?, ?> parentContext = ctx.getParentContext(); return parentContext != null ? parentType.equals(parentContext.getPublicDefinition()) : false; }
/** * 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 * @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<StatementDefinition> ancestorTypes) { requireNonNull(ancestorTypes); StmtContext<?, ?, ?> current = ctx.getParentContext(); while (current != null) { if (ancestorTypes.contains(current.getPublicDefinition())) { return true; } current = current.getParentContext(); } return false; }
private static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) { // Augment is in uses - we need to augment instantiated nodes in parent. final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext(); if (YangStmtMapping.USES == parent.getPublicDefinition()) { return parent.getParentContext(); } return parent; }
/** * Return the parent statement context, forcing a VerifyException if this is the root statement. * * @return context of parent of statement * @throws VerifyException if this statement is the root statement */ default @NonNull StmtContext<?, ?, ?> coerceParentContext() { return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this); }
/** * 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 <A, D extends DeclaredStatement<A>> boolean hasAncestorOfTypeWithChildOfType( final StmtContext<?, ?, ?> ctx, final StatementDefinition ancestorType, final StatementDefinition ancestorChildType) { requireNonNull(ctx); requireNonNull(ancestorType); requireNonNull(ancestorChildType); StmtContext<?, ?, ?> current = ctx.coerceParentContext(); StmtContext<?, ?, ?> parent = current.getParentContext(); while (parent != null) { if (ancestorType.equals(current.getPublicDefinition())) { @SuppressWarnings("unchecked") final Class<D> ancestorChildTypeClass = (Class<D>) ancestorChildType.getDeclaredRepresentationClass(); if (findFirstSubstatement(current, ancestorChildTypeClass) == null) { return false; } } current = parent; parent = current.getParentContext(); } return true; }
private static QNameModule getNewQNameModule(final StmtContext<?, ?, ?> targetCtx, final StmtContext<?, ?, ?> stmtContext) { if (targetCtx.getParentContext() == null) { return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx); } if (targetCtx.getPublicDefinition() == YangStmtMapping.AUGMENT) { return StmtContextUtils.getRootModuleQName(targetCtx); } final Object targetStmtArgument = targetCtx.getStatementArgument(); final Object sourceStmtArgument = stmtContext.getStatementArgument(); if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) { return ((QName) targetStmtArgument).getModule(); } return null; } }
/** * 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). * * <p> * 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) { if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) { return; } final StmtContext<?, ?, ?> listStmtCtx = ctx.getParentContext(); final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> 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); } }); } }