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.HashSet;
15 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
26 public final class StmtContextUtils {
28 public static final char LIST_KEY_SEPARATOR = ' ';
29 private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
31 private static final Function<StmtContext<?, ?, ?>, DeclaredStatement<?>> BUILD_DECLARED = new Function<StmtContext<?, ?, ?>, DeclaredStatement<?>>() {
33 public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
34 return input.buildDeclared();
38 private static final Function<StmtContext<?, ?, ?>, EffectiveStatement<?, ?>> BUILD_EFFECTIVE = 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 = new Predicate<StmtContext<?, ?, ?>>() {
47 public boolean apply(StmtContext<?, ?, ?> input) {
48 return input.isSupportedToBuildEffective();
52 private StmtContextUtils() {
53 throw new UnsupportedOperationException("Utility class");
56 @SuppressWarnings("unchecked")
57 public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
58 return Function.class.cast(BUILD_DECLARED);
61 @SuppressWarnings("unchecked")
62 public static final <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
63 return Function.class.cast(BUILD_EFFECTIVE);
66 @SuppressWarnings("unchecked")
67 public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
68 final Iterable<? extends StmtContext<?, ?, ?>> contexts, 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(final StmtContext<?, ?, ?> ctx,
79 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.declaredSubstatements();
92 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
93 if (producesDeclared(subStmtContext, declaredType)) {
94 return (StmtContext<AT, ?, ?>) subStmtContext;
100 public static final StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
101 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
103 if (startIndex >= types.length) {
107 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
108 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
109 if (producesDeclared(subStmtContext, types[startIndex])) {
110 if (startIndex + 1 == types.length) {
111 return subStmtContext;
113 return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
120 public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
121 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
122 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
123 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
124 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
125 return subStmtContext;
128 StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType,
130 if (result != null) {
139 public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
140 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
142 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
144 for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
145 if (producesDeclared(subStmtContext, declaredType)) {
146 return subStmtContext;
148 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
149 if (result != null) {
158 public static final boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
159 final Class<? extends DeclaredStatement<?>> type) {
160 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
163 public static boolean isInExtensionBody(StmtContext<?, ?, ?> stmtCtx) {
165 StmtContext<?, ?, ?> current = stmtCtx;
166 while (!current.getParentContext().isRootContext()) {
167 current = current.getParentContext();
168 if (producesDeclared(current, UnknownStatementImpl.class)) {
176 public static boolean isUnknownStatement(StmtContext<?, ?, ?> stmtCtx) {
177 return producesDeclared(stmtCtx, UnknownStatementImpl.class);
180 public static Set<TypeOfCopy> getCopyTypesFromOriginal(StmtContext<?, ?, ?> ctx) {
182 Set<TypeOfCopy> copyTypesFromOriginal = new HashSet<>();
183 StmtContext<?, ?, ?> current = ctx;
185 while (current.getOriginalCtx() != null) {
186 copyTypesFromOriginal.add(current.getTypeOfCopy());
187 current = current.getOriginalCtx();
190 return copyTypesFromOriginal;
193 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
194 StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx, 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.create(false, keyQName);
203 newKeys.add(keyIdentifier);