*/
package org.opendaylight.yangtools.yang.parser.spi.meta;
+import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.opendaylight.yangtools.yang.common.QNameModule;
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.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
private static boolean checkFeatureSupport(final StmtContext.Mutable<?, ?, ?> stmtContext,
final Predicate<QName> isFeatureSupported) {
- final Collection<StatementContextBase<?, ?, ?>> substatements = stmtContext.declaredSubstatements();
-
boolean isSupported = false;
boolean containsIfFeature = false;
- for (final StatementContextBase<?, ?, ?> stmt : substatements) {
- if (stmt.getPublicDefinition().equals(YangStmtMapping.IF_FEATURE)) {
+ for (final StatementContextBase<?, ?, ?> stmt : stmtContext.declaredSubstatements()) {
+ if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) {
containsIfFeature = true;
if (isFeatureSupported.test((QName) stmt.getStatementArgument())) {
isSupported = true;
return false;
}
}
+
+ /**
+ * 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<StatementDefinition> 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 <AT, DT extends DeclaredStatement<AT>> boolean hasAncestorOfTypeWithChildOfType(final StmtContext<?, ?, ?> ctx,
+ final StatementDefinition ancestorType, final StatementDefinition ancestorChildType) {
+ Preconditions.checkNotNull(ctx);
+ Preconditions.checkNotNull(ancestorType);
+ Preconditions.checkNotNull(ancestorChildType);
+ StmtContext<?, ?, ?> current = ctx.getParentContext();
+ while (!(current instanceof RootStatementContext)) {
+ if (ancestorType.equals(current.getPublicDefinition())) {
+ @SuppressWarnings("unchecked")
+ final Class<DT> ancestorChildTypeClass = (Class<DT>) ancestorChildType.getDeclaredRepresentationClass();
+ if (findFirstSubstatement(current, ancestorChildTypeClass) == null) {
+ return false;
+ }
+ }
+ current = 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;
+ }
}