/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.parser.spi.meta; import org.opendaylight.yangtools.yang.common.QName; import com.google.common.base.Splitter; import java.util.List; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement; import java.util.HashSet; import java.util.Set; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl; import com.google.common.base.Predicate; import java.util.Collection; import com.google.common.base.Function; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; public final class StmtContextUtils { public static final char LIST_KEY_SEPARATOR = ' '; private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults(); private static final Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { @Override public DeclaredStatement apply(final StmtContext input) { return input.buildDeclared(); } }; private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { @Override public EffectiveStatement apply(final StmtContext input) { return input.buildEffective(); } }; public static final Predicate> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate>() { @Override public boolean apply(StmtContext input) { return input.isSupportedToBuildEffective(); } }; private StmtContextUtils() { throw new UnsupportedOperationException("Utility class"); } @SuppressWarnings("unchecked") public static > Function, D> buildDeclared() { return Function.class.cast(BUILD_DECLARED); } @SuppressWarnings("unchecked") public static final > Function, E> buildEffective() { return Function.class.cast(BUILD_EFFECTIVE); } @SuppressWarnings("unchecked") public static final > AT firstAttributeOf( final Iterable> contexts, final Class
declaredType) { for (StmtContext ctx : contexts) { if (producesDeclared(ctx, declaredType)) { return (AT) ctx.getStatementArgument(); } } return null; } @SuppressWarnings("unchecked") public static final > AT firstAttributeOf( final StmtContext ctx, final Class
declaredType) { if (producesDeclared(ctx, declaredType)) { return (AT) ctx.getStatementArgument(); } return null; } @SuppressWarnings("unchecked") public static final > StmtContext findFirstDeclaredSubstatement( StmtContext stmtContext, Class
declaredType) { Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (producesDeclared(subStmtContext,declaredType)) { return (StmtContext) subStmtContext; } } return null; } public static final 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); } } } return null; } public static final
> StmtContext findFirstDeclaredSubstatementOnSublevel( final StmtContext stmtContext, final Class
declaredType, int sublevel) { Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } else { if (sublevel > 1) { StmtContext result = findFirstDeclaredSubstatementOnSublevel( subStmtContext, declaredType, --sublevel); if (result != null) { return result; } } } } return null; } public static final
> StmtContext findDeepFirstDeclaredSubstatement( final StmtContext stmtContext, final Class
declaredType) { Collection> declaredSubstatements = stmtContext .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { if (producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } else { StmtContext result = findDeepFirstDeclaredSubstatement( subStmtContext, declaredType); if (result != null) { return result; } } } return null; } public static final boolean producesDeclared(final StmtContext ctx, final Class> type) { return type.isAssignableFrom(ctx.getPublicDefinition() .getDeclaredRepresentationClass()); } public static boolean isInExtensionBody( StmtContext stmtCtx) { StmtContext current = stmtCtx; while(!current.getParentContext().isRootContext()) { current = current.getParentContext(); if(producesDeclared(current, UnknownStatementImpl.class)) { return true; } } return false; } public static boolean isUnknownStatement(StmtContext stmtCtx) { if (producesDeclared(stmtCtx, UnknownStatementImpl.class)) { return true; } else { return false; } } public static Collection replaceModuleQNameForKey(StmtContext, KeyStatement, ?> keyStmtCtx, QNameModule newQNameModule) { List keyTokens = KEY_SPLITTER.splitToList(keyStmtCtx.rawStatementArgument()); Set newKeys = new HashSet<>(); for (String keyToken : keyTokens) { QName keyQName = QName.create(newQNameModule, keyToken); SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier .create(false, keyQName); newKeys.add(keyIdentifier); } return newKeys; } }