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.stmt.reactor;
10 import java.util.List;
11 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.GroupingUtils;
12 import org.opendaylight.yangtools.yang.common.QNameModule;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import com.google.common.base.Preconditions;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 class SubstatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
23 extends StatementContextBase<A, D, E> {
25 private final StatementContextBase<?, ?, ?> parent;
26 private final A argument;
28 SubstatementContext(StatementContextBase<?, ?, ?> parent,
29 ContextBuilder<A, D, E> builder) throws SourceException {
31 this.parent = Preconditions.checkNotNull(parent,
32 "Parent must not be null");
33 this.argument = builder.getDefinition().parseArgumentValue(this,
34 builder.getRawArgument());
37 SubstatementContext(SubstatementContext<A, D, E> original,
38 QNameModule newQNameModule,
39 StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy)
40 throws SourceException {
42 this.parent = newParent;
44 if (newQNameModule != null && original.argument instanceof QName) {
45 QName originalQName = (QName) original.argument;
46 this.argument = (A) QName.create(newQNameModule,
47 originalQName.getLocalName());
49 this.argument = original.argument;
52 copyDeclaredStmts(original, newQNameModule, typeOfCopy);
54 copyEffectiveStmts(original, newQNameModule, typeOfCopy);
57 private void copyDeclaredStmts(SubstatementContext<A, D, E> original,
58 QNameModule newQNameModule, TypeOfCopy typeOfCopy)
59 throws SourceException {
60 Collection<? extends StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original
61 .declaredSubstatements();
62 for (StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
63 if (GroupingUtils.needToCopyByUses(stmtContext)) {
64 StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(
65 newQNameModule, this, typeOfCopy);
66 this.addEffectiveSubstatement(copy);
67 } else if (GroupingUtils.isReusedByUses(stmtContext)) {
68 this.addEffectiveSubstatement(stmtContext);
73 private void copyEffectiveStmts(SubstatementContext<A, D, E> original,
74 QNameModule newQNameModule, TypeOfCopy typeOfCopy)
75 throws SourceException {
76 Collection<? extends StatementContextBase<?, ?, ?>> originalEffectiveSubstatements = original
77 .effectiveSubstatements();
78 for (StatementContextBase<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
79 if (GroupingUtils.needToCopyByUses(stmtContext)) {
80 StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(
81 newQNameModule, this, typeOfCopy);
82 this.addEffectiveSubstatement(copy);
83 } else if (GroupingUtils.isReusedByUses(stmtContext)) {
84 this.addEffectiveSubstatement(stmtContext);
90 public StatementContextBase<?, ?, ?> getParentContext() {
95 public NamespaceStorageNode getParentNamespaceStorage() {
100 public Registry getBehaviourRegistry() {
101 return parent.getBehaviourRegistry();
105 public RootStatementContext<?, ?, ?> getRoot() {
106 return parent.getRoot();
110 public A getStatementArgument() {
115 public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule,
116 StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy)
117 throws SourceException {
118 SubstatementContext<A, D, E> copy = new SubstatementContext<>(this,
119 newQNameModule, newParent, typeOfCopy);
120 copy.setTypeOfCopy(typeOfCopy);
121 copy.setOriginalCtx(this);
126 public List<Object> getArgumentsFromRoot() {
127 List<Object> argumentsFromRoot = parent.getArgumentsFromRoot();
128 argumentsFromRoot.add(argument);
129 return argumentsFromRoot;
133 public boolean isRootContext() {