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 org.opendaylight.yangtools.yang.common.QName;
11 import com.google.common.base.Splitter;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
16 import java.util.HashSet;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
19 import com.google.common.base.Predicate;
20 import java.util.Collection;
21 import com.google.common.base.Function;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 public final class StmtContextUtils {
27 public static final char LIST_KEY_SEPARATOR = ' ';
28 private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
30 private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED = new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
32 public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
33 return input.buildDeclared();
37 private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE = new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
39 public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
40 return input.buildEffective();
44 public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate<StmtContext<?,?,?>>() {
46 public boolean apply(StmtContext<?, ?, ?> input) {
47 return input.isSupportedToBuildEffective();
51 private StmtContextUtils() {
52 throw new UnsupportedOperationException("Utility class");
55 @SuppressWarnings("unchecked")
56 public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
57 return Function.class.cast(BUILD_DECLARED);
60 @SuppressWarnings("unchecked")
61 public static final <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
62 return Function.class.cast(BUILD_EFFECTIVE);
65 @SuppressWarnings("unchecked")
66 public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
67 final Iterable<? extends StmtContext<?, ?, ?>> contexts,
68 final Class<DT> declaredType) {
69 for (StmtContext<?, ?, ?> ctx : contexts) {
70 if (producesDeclared(ctx, declaredType)) {
71 return (AT) ctx.getStatementArgument();
77 @SuppressWarnings("unchecked")
78 public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
79 final StmtContext<?, ?, ?> ctx, final Class<DT> declaredType) {
81 if (producesDeclared(ctx, declaredType)) {
82 return (AT) ctx.getStatementArgument();
88 @SuppressWarnings("unchecked")
89 public static final <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
90 StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType) {
91 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
92 .declaredSubstatements();
93 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
94 if (producesDeclared(subStmtContext,declaredType)) {
95 return (StmtContext<AT, ?, ?>) subStmtContext;
101 public static final StmtContext<?, ?, ?> findFirstDeclaredSubstatement(
102 final StmtContext<?, ?, ?> stmtContext, int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
104 if (startIndex >= types.length) {
108 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
109 .declaredSubstatements();
110 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
111 if (producesDeclared(subStmtContext,types[startIndex])) {
112 if (startIndex + 1 == types.length) {
113 return subStmtContext;
115 return findFirstDeclaredSubstatement(subStmtContext,
116 ++startIndex, types);
123 public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
124 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType,
126 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
127 .declaredSubstatements();
128 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
129 if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) {
130 return subStmtContext;
133 StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
134 subStmtContext, declaredType, --sublevel);
135 if (result != null) {
144 public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
145 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
147 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
148 .declaredSubstatements();
150 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
151 if (producesDeclared(subStmtContext,declaredType)) {
152 return subStmtContext;
154 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(
155 subStmtContext, declaredType);
156 if (result != null) {
165 public static final boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
166 final Class<? extends DeclaredStatement<?>> type) {
167 return type.isAssignableFrom(ctx.getPublicDefinition()
168 .getDeclaredRepresentationClass());
171 public static boolean isInExtensionBody(
172 StmtContext<?,?,?> stmtCtx) {
174 StmtContext<?,?,?> current = stmtCtx;
175 while(!current.getParentContext().isRootContext()) {
176 current = current.getParentContext();
177 if(producesDeclared(current, UnknownStatementImpl.class)) {
185 public static boolean isUnknownStatement(StmtContext<?, ?, ?> stmtCtx) {
186 if (producesDeclared(stmtCtx, UnknownStatementImpl.class)) {
193 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
194 QNameModule newQNameModule) {
196 List<String> keyTokens = KEY_SPLITTER.splitToList(keyStmtCtx.rawStatementArgument());
198 Set<SchemaNodeIdentifier> newKeys = new HashSet<>();
200 for (String keyToken : keyTokens) {
201 QName keyQName = QName.create(newQNameModule, keyToken);
202 SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier
203 .create(false, keyQName);
204 newKeys.add(keyIdentifier);