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.Splitter;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSet.Builder;
15 import java.util.Collection;
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 {
25 public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
27 private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
28 new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
30 public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
31 return input.buildDeclared();
35 private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
36 new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
38 public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
39 return input.buildEffective();
43 private StmtContextUtils() {
44 throw new UnsupportedOperationException("Utility class");
47 @SuppressWarnings("unchecked")
48 public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
49 return Function.class.cast(BUILD_DECLARED);
52 @SuppressWarnings("unchecked")
53 public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
54 return Function.class.cast(BUILD_EFFECTIVE);
57 @SuppressWarnings("unchecked")
58 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
59 final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
60 for (StmtContext<?, ?, ?> ctx : contexts) {
61 if (producesDeclared(ctx, declaredType)) {
62 return (AT) ctx.getStatementArgument();
68 @SuppressWarnings("unchecked")
69 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
70 final Class<DT> declaredType) {
71 return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
74 @SuppressWarnings("unchecked")
75 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
76 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
77 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
78 if (producesDeclared(subStmtContext,declaredType)) {
79 return (StmtContext<AT, ?, ?>) subStmtContext;
85 @SuppressWarnings("unchecked")
86 public static <AT,DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllDeclaredSubstatement(
87 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
88 ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
89 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
90 if (producesDeclared(subStmtContext,declaredType)) {
91 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
94 return listBuilder.build();
97 @SuppressWarnings("unchecked")
98 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
99 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
100 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
101 if (producesDeclared(subStmtContext,declaredType)) {
102 return (StmtContext<AT, ?, ?>) subStmtContext;
108 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
109 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
110 StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
111 return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
115 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
116 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
117 if (startIndex >= types.length) {
121 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
122 if (producesDeclared(subStmtContext,types[startIndex])) {
123 return startIndex + 1 == types.length ? subStmtContext
124 : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
130 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
131 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
132 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
133 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
134 return subStmtContext;
137 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
138 subStmtContext, declaredType, --sublevel);
139 if (result != null) {
148 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
149 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
150 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
151 if (producesDeclared(subStmtContext, declaredType)) {
152 return subStmtContext;
155 final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
156 if (result != null) {
164 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
165 final Class<? extends DeclaredStatement<?>> type) {
166 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
169 public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
170 StmtContext<?,?,?> current = stmtCtx;
171 while (!current.getParentContext().isRootContext()) {
172 current = current.getParentContext();
173 if (producesDeclared(current, UnknownStatementImpl.class)) {
181 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
182 return producesDeclared(stmtCtx, UnknownStatementImpl.class);
185 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
186 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
187 final QNameModule newQNameModule) {
189 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
190 boolean replaced = false;
191 for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
192 final QName qname = arg.getLastComponent();
193 if (!newQNameModule.equals(qname)) {
194 final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
195 QName.create(newQNameModule, qname.getLocalName()));
196 builder.add(SchemaNodeIdentifier.create(false, newQname));
203 // This makes sure we reuse the collection when a grouping is instantiated in the same module
204 return replaced ? builder.build() : keyStmtCtx.getStatementArgument();