Bug 3874: Support of yang modeled AnyXML - XML deserialization
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
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;
23
24 public final class StmtContextUtils {
25     public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
26
27     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
28             new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
29         @Override
30         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
31             return input.buildDeclared();
32         }
33     };
34
35     private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
36             new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
37         @Override
38         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
39             return input.buildEffective();
40         }
41     };
42
43     private StmtContextUtils() {
44         throw new UnsupportedOperationException("Utility class");
45     }
46
47     @SuppressWarnings("unchecked")
48     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
49         return Function.class.cast(BUILD_DECLARED);
50     }
51
52     @SuppressWarnings("unchecked")
53     public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
54         return Function.class.cast(BUILD_EFFECTIVE);
55     }
56
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();
63             }
64         }
65         return null;
66     }
67
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;
72     }
73
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;
80             }
81         }
82         return null;
83     }
84
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);
92             }
93         }
94         return listBuilder.build();
95     }
96
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;
103             }
104         }
105         return null;
106     }
107
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);
112     }
113
114     @SafeVarargs
115     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
116             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
117         if (startIndex >= types.length) {
118             return null;
119         }
120
121         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
122             if (producesDeclared(subStmtContext,types[startIndex])) {
123                 return startIndex + 1 == types.length ? subStmtContext
124                         : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
125             }
126         }
127         return null;
128     }
129
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;
135             }
136             if (sublevel > 1) {
137                 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
138                     subStmtContext, declaredType, --sublevel);
139                 if (result != null) {
140                     return result;
141                 }
142             }
143         }
144
145         return null;
146     }
147
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;
153             }
154
155             final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
156             if (result != null) {
157                 return result;
158             }
159         }
160
161         return null;
162     }
163
164     public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
165             final Class<? extends DeclaredStatement<?>> type) {
166         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
167     }
168
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)) {
174                 return true;
175             }
176         }
177
178         return false;
179     }
180
181     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
182         return producesDeclared(stmtCtx, UnknownStatementImpl.class);
183     }
184
185     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
186             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
187             final QNameModule newQNameModule) {
188
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));
197                 replaced = true;
198             } else {
199                 builder.add(arg);
200             }
201         }
202
203         // This makes sure we reuse the collection when a grouping is instantiated in the same module
204         return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
205     }
206 }