Update DataTreeFactory
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextWriter.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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
16 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
17 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
18
19 final class StatementContextWriter implements StatementWriter {
20     private final ModelProcessingPhase phase;
21     private final SourceSpecificContext ctx;
22
23     private StatementContextBase<?, ?, ?> current;
24
25     StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
26         this.ctx = Preconditions.checkNotNull(ctx);
27         this.phase = Preconditions.checkNotNull(phase);
28     }
29
30     @Override
31     public void startStatement(final int childId, @Nonnull final QName name, final String argument,
32             @Nonnull final StatementSourceReference ref) {
33         current = Verify.verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
34     }
35
36     @Override
37     public void endStatement(@Nonnull final StatementSourceReference ref) {
38         Preconditions.checkState(current != null);
39         current.endDeclared(ref, phase);
40         StatementContextBase<?, ?, ?> parentContext = current.getParentContext();
41         while (parentContext != null && StatementSource.CONTEXT == parentContext.getStatementSource()) {
42             parentContext.endDeclared(ref, phase);
43             parentContext = parentContext.getParentContext();
44         }
45         current = parentContext;
46     }
47
48     @Nonnull
49     @Override
50     public ModelProcessingPhase getPhase() {
51         return phase;
52     }
53 }