Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / DeclaredEffectiveStatementBase.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.rfc6020.effective;
9
10 import com.google.common.base.Verify;
11 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
12 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
13 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
15 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
16
17 public abstract class DeclaredEffectiveStatementBase<A, D extends DeclaredStatement<A>> extends
18         EffectiveStatementBase<A, D> {
19
20     private final StatementSource statementSource;
21     private final A argument;
22     private final D declaredInstance;
23
24     public DeclaredEffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
25         this(ctx, true);
26     }
27
28     /**
29      * Constructor.
30      *
31      * @param ctx
32      *            context of statement.
33      * @param buildUnknownSubstatements
34      *            if it is false, the unknown substatements are omitted from
35      *            build of effective substatements till the call of either
36      *            effectiveSubstatements or getOmittedUnknownSubstatements
37      *            method of EffectiveStatementBase class. The main purpose of
38      *            this is to allow the build of recursive extension definitions.
39      */
40     protected DeclaredEffectiveStatementBase(StmtContext<A, D, ?> ctx, final boolean buildUnknownSubstatements) {
41         super(ctx, buildUnknownSubstatements);
42
43         this.argument = ctx.getStatementArgument();
44         this.statementSource = ctx.getStatementSource();
45
46         /*
47          * Share original instance of declared statement between all effective
48          * statements which have been copied or derived from this original
49          * declared statement.
50          */
51         StatementContextBase<A, D, ?> originalCtx = (StatementContextBase<A, D, ?>) ctx.getOriginalCtx();
52         if (originalCtx != null) {
53             ctx = originalCtx;
54         }
55         declaredInstance = Verify.verifyNotNull(ctx.buildDeclared(), "Statement %s failed to build declared statement",
56                 ctx);
57     }
58
59     @Override
60     public final StatementDefinition statementDefinition() {
61         return declaredInstance.statementDefinition();
62     }
63
64     @Override
65     public A argument() {
66         return argument;
67     }
68
69     @Override
70     public final StatementSource getStatementSource() {
71         return statementSource;
72     }
73
74     @Override
75     public final D getDeclared() {
76         return declaredInstance;
77     }
78 }