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.ImmutableSet;
13 import com.google.common.collect.ImmutableSet.Builder;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
23 public final class StmtContextUtils {
24 public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
26 private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
27 new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
29 public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
30 return input.buildDeclared();
34 private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
35 new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
37 public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
38 return input.buildEffective();
42 private StmtContextUtils() {
43 throw new UnsupportedOperationException("Utility class");
46 @SuppressWarnings("unchecked")
47 public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
48 return Function.class.cast(BUILD_DECLARED);
51 @SuppressWarnings("unchecked")
52 public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
53 return Function.class.cast(BUILD_EFFECTIVE);
56 @SuppressWarnings("unchecked")
57 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
58 final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
59 for (StmtContext<?, ?, ?> ctx : contexts) {
60 if (producesDeclared(ctx, declaredType)) {
61 return (AT) ctx.getStatementArgument();
67 @SuppressWarnings("unchecked")
68 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
69 final Class<DT> declaredType) {
70 return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
73 @SuppressWarnings("unchecked")
74 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
75 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
76 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
77 if (producesDeclared(subStmtContext,declaredType)) {
78 return (StmtContext<AT, ?, ?>) subStmtContext;
84 @SuppressWarnings("unchecked")
85 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
86 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
87 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
88 if (producesDeclared(subStmtContext,declaredType)) {
89 return (StmtContext<AT, ?, ?>) subStmtContext;
95 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
96 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
97 StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
98 return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
102 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
103 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
104 if (startIndex >= types.length) {
108 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
109 if (producesDeclared(subStmtContext,types[startIndex])) {
110 return startIndex + 1 == types.length ? subStmtContext
111 : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
117 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
118 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
119 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
120 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
121 return subStmtContext;
124 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
125 subStmtContext, declaredType, --sublevel);
126 if (result != null) {
135 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
136 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
137 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
138 if (producesDeclared(subStmtContext, declaredType)) {
139 return subStmtContext;
142 final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
143 if (result != null) {
151 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
152 final Class<? extends DeclaredStatement<?>> type) {
153 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
156 public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
157 StmtContext<?,?,?> current = stmtCtx;
158 while (!current.getParentContext().isRootContext()) {
159 current = current.getParentContext();
160 if (producesDeclared(current, UnknownStatementImpl.class)) {
168 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
169 return producesDeclared(stmtCtx, UnknownStatementImpl.class);
172 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
173 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
174 final QNameModule newQNameModule) {
176 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
177 boolean replaced = false;
178 for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
179 final QName qname = arg.getLastComponent();
180 if (!newQNameModule.equals(qname)) {
181 final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
182 QName.create(newQNameModule, qname.getLocalName()));
183 builder.add(SchemaNodeIdentifier.create(false, newQname));
190 // This makes sure we reuse the collection when a grouping is instantiated in the same module
191 return replaced ? builder.build() : keyStmtCtx.getStatementArgument();