2fe7f01138a3bdc72e3f7b136faea0a3bf55d8fe
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.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.stmt.reactor;
9
10 import java.util.List;
11
12 import java.util.LinkedList;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import java.util.Collection;
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;
21
22 class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
23         extends StatementContextBase<A, D, E> {
24
25     private final SourceSpecificContext sourceContext;
26     private final A argument;
27
28     RootStatementContext(ContextBuilder<A, D, E> builder,
29             SourceSpecificContext sourceContext) throws SourceException {
30         super(builder);
31         this.sourceContext = sourceContext;
32         this.argument = builder.getDefinition().parseArgumentValue(this,
33                 builder.getRawArgument());
34     }
35
36     RootStatementContext(RootStatementContext<A, D, E> original,
37             QNameModule newQNameModule) throws SourceException {
38         super(original);
39
40         sourceContext = original.sourceContext;
41         this.argument = original.argument;
42
43         copyDeclaredStmts(original, newQNameModule);
44
45         copyEffectiveStmts(original, newQNameModule);
46
47     }
48
49     private void copyDeclaredStmts(RootStatementContext<A, D, E> original,
50             QNameModule newQNameModule) throws SourceException {
51         Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original
52                 .declaredSubstatements();
53         for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
54             this.addEffectiveSubstatement(stmtContext
55                     .createCopy(newQNameModule,this));
56         }
57     }
58
59     private void copyEffectiveStmts(RootStatementContext<A, D, E> original,
60             QNameModule newQNameModule) throws SourceException {
61         Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original
62                 .effectiveSubstatements();
63         for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
64             this.addEffectiveSubstatement(stmtContext
65                     .createCopy(newQNameModule,this));
66         }
67     }
68
69     @Override
70     public StatementContextBase<?, ?, ?> getParentContext() {
71         return null;
72     }
73
74     @Override
75     public NamespaceStorageNode getParentNamespaceStorage() {
76         return sourceContext;
77     }
78
79     @Override
80     public Registry getBehaviourRegistry() {
81         return sourceContext;
82     }
83
84     @Override
85     public RootStatementContext<?, ?, ?> getRoot() {
86         return this;
87     }
88
89     SourceSpecificContext getSourceContext() {
90         return sourceContext;
91     }
92
93     @Override
94     public A getStatementArgument() {
95         return argument;
96     }
97
98     @Override
99     public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent)
100             throws SourceException {
101
102         StatementContextBase<A, D, E> copy = new RootStatementContext<A, D, E>(
103                 this, newQNameModule);
104         return copy;
105     }
106
107     @Override
108     public List<Object> getArgumentsFromRoot() {
109         List<Object> argumentList = new LinkedList<Object>();
110         argumentList.add(argument);
111         return argumentList;
112     }
113
114 }