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 com.google.common.base.Function;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Splitter;
13 import java.util.Collection;
14 import java.util.LinkedHashSet;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
24 public final class StmtContextUtils {
26 public static final char LIST_KEY_SEPARATOR = ' ';
27 private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
29 private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
30 new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
32 public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
33 return input.buildDeclared();
37 private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
38 new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
40 public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
41 return input.buildEffective();
45 public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
46 new Predicate<StmtContext<?,?,?>>() {
48 public boolean apply(final StmtContext<?, ?, ?> input) {
49 return input.isSupportedToBuildEffective();
53 private StmtContextUtils() {
54 throw new UnsupportedOperationException("Utility class");
57 @SuppressWarnings("unchecked")
58 public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
59 return Function.class.cast(BUILD_DECLARED);
62 @SuppressWarnings("unchecked")
63 public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
64 return Function.class.cast(BUILD_EFFECTIVE);
67 @SuppressWarnings("unchecked")
68 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
69 final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
70 for (StmtContext<?, ?, ?> ctx : contexts) {
71 if (producesDeclared(ctx, declaredType)) {
72 return (AT) ctx.getStatementArgument();
78 @SuppressWarnings("unchecked")
79 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
80 final Class<DT> declaredType) {
81 return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
84 @SuppressWarnings("unchecked")
85 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
86 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
87 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
88 if (producesDeclared(subStmtContext,declaredType)) {
89 return (StmtContext<AT, ?, ?>) subStmtContext;
95 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
96 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
97 if (startIndex >= types.length) {
101 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
102 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
103 if (producesDeclared(subStmtContext,types[startIndex])) {
104 if (startIndex + 1 == types.length) {
105 return subStmtContext;
107 return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
114 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
115 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
116 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
117 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
118 return subStmtContext;
121 StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
122 subStmtContext, declaredType, --sublevel);
123 if (result != null) {
132 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
133 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
134 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
135 if (producesDeclared(subStmtContext, declaredType)) {
136 return subStmtContext;
139 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
140 if (result != null) {
148 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
149 final Class<? extends DeclaredStatement<?>> type) {
150 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
153 public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
154 StmtContext<?,?,?> current = stmtCtx;
155 while(!current.getParentContext().isRootContext()) {
156 current = current.getParentContext();
157 if (producesDeclared(current, UnknownStatementImpl.class)) {
165 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
166 return producesDeclared(stmtCtx, UnknownStatementImpl.class);
169 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
170 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
171 final QNameModule newQNameModule) {
173 Set<SchemaNodeIdentifier> newKeys = new LinkedHashSet<>();
174 for (String keyToken : KEY_SPLITTER.split(keyStmtCtx.rawStatementArgument())) {
175 QName keyQName = QName.create(newQNameModule, keyToken);
176 SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier.create(false, keyQName);
177 newKeys.add(keyIdentifier);