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;
12 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.GroupingUtils;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import com.google.common.base.Preconditions;
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.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 class SubstatementContext<A,D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
24 extends StatementContextBase<A,D,E> {
26 private final StatementContextBase<?,?,?> parent;
27 private final A argument;
29 SubstatementContext(StatementContextBase<?,?,?> parent,ContextBuilder<A,D,E> builder) throws SourceException {
31 this.parent = Preconditions.checkNotNull(parent, "Parent must not be null");
32 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
35 SubstatementContext(SubstatementContext<A,D,E> original, QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent) throws SourceException {
37 this.parent = newParent;
39 if(newQNameModule != null && original.argument instanceof QName) {
40 QName originalQName = (QName) original.argument;
41 this.argument = (A) QName.create(newQNameModule,originalQName.getLocalName());
43 this.argument = original.argument;
46 copyDeclaredStmts(original, newQNameModule);
48 copyEffectiveStmts(original, newQNameModule);
51 private void copyDeclaredStmts(SubstatementContext<A, D, E> original,
52 QNameModule newQNameModule) throws SourceException {
53 Collection<? extends StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original
54 .declaredSubstatements();
55 for (StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
56 if (GroupingUtils.needToCopyByUses(stmtContext)) {
57 StatementContextBase<?, ?, ?> copy = stmtContext
58 .createCopy(newQNameModule,this);
59 this.addEffectiveSubstatement(copy);
60 } else if (GroupingUtils.isReusedByUses(stmtContext)) {
61 this.addEffectiveSubstatement(stmtContext);
66 private void copyEffectiveStmts(SubstatementContext<A, D, E> original,
67 QNameModule newQNameModule) throws SourceException {
68 Collection<? extends StatementContextBase<?, ?, ?>> originalEffectiveSubstatements = original
69 .effectiveSubstatements();
70 for (StatementContextBase<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
71 if (GroupingUtils.needToCopyByUses(stmtContext)) {
72 StatementContextBase<?, ?, ?> copy = stmtContext
73 .createCopy(newQNameModule,this);
74 this.addEffectiveSubstatement(copy);
75 } else if (GroupingUtils.isReusedByUses(stmtContext)) {
76 this.addEffectiveSubstatement(stmtContext);
82 public StatementContextBase<?,?,?> getParentContext() {
87 public NamespaceStorageNode getParentNamespaceStorage() {
92 public Registry getBehaviourRegistry() {
93 return parent.getBehaviourRegistry();
97 public RootStatementContext<?, ?, ?> getRoot() {
98 return parent.getRoot();
103 public A getStatementArgument() {
108 public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent) throws SourceException {
109 return new SubstatementContext<>(this,newQNameModule, newParent);
113 public List<Object> getArgumentsFromRoot() {
114 List<Object> argumentsFromRoot = parent.getArgumentsFromRoot();
115 argumentsFromRoot.add(argument);
116 return argumentsFromRoot;