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, ?>> findAllDeclaredSubstatements(
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>> Collection<StmtContext<AT, DT, ?>> findAllEffectiveSubstatements(
99 final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
100 ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
101 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
102 if (producesDeclared(subStmtContext, type)) {
103 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
106 return listBuilder.build();
109 public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllSubstatements(
110 final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
111 ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
112 listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type));
113 listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type));
114 return listBuilder.build();
117 @SuppressWarnings("unchecked")
118 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
119 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
120 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
121 if (producesDeclared(subStmtContext,declaredType)) {
122 return (StmtContext<AT, ?, ?>) subStmtContext;
128 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
129 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
130 StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
131 return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
135 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
136 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
137 if (startIndex >= types.length) {
141 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
142 if (producesDeclared(subStmtContext,types[startIndex])) {
143 return startIndex + 1 == types.length ? subStmtContext
144 : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
150 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
151 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
152 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
153 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
154 return subStmtContext;
157 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
158 subStmtContext, declaredType, --sublevel);
159 if (result != null) {
168 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
169 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
170 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
171 if (producesDeclared(subStmtContext, declaredType)) {
172 return subStmtContext;
175 final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
176 if (result != null) {
184 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
185 final Class<? extends DeclaredStatement<?>> type) {
186 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
189 public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
190 StmtContext<?,?,?> current = stmtCtx;
191 while (!current.getParentContext().isRootContext()) {
192 current = current.getParentContext();
193 if (producesDeclared(current, UnknownStatementImpl.class)) {
201 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
202 return producesDeclared(stmtCtx, UnknownStatementImpl.class);
205 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
206 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
207 final QNameModule newQNameModule) {
209 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
210 boolean replaced = false;
211 for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
212 final QName qname = arg.getLastComponent();
213 if (!newQNameModule.equals(qname)) {
214 final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
215 QName.create(newQNameModule, qname.getLocalName()));
216 builder.add(SchemaNodeIdentifier.create(false, newQname));
223 // This makes sure we reuse the collection when a grouping is instantiated in the same module
224 return replaced ? builder.build() : keyStmtCtx.getStatementArgument();