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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
15 import java.util.Optional;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
19 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
22 final class StatementContextWriter implements StatementWriter {
23 private final ModelProcessingPhase phase;
24 private final SourceSpecificContext ctx;
26 private StatementContextBase<?, ?, ?> current;
28 StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
29 this.ctx = requireNonNull(ctx);
30 this.phase = requireNonNull(phase);
34 public Optional<? extends ResumedStatement> resumeStatement(final int childId) {
35 final Optional<StatementContextBase<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
36 existing.ifPresent(this::resumeStatement);
40 private void resumeStatement(final StatementContextBase<?, ?, ?> child) {
41 if (child.isFullyDefined()) {
42 child.walkChildren(phase);
43 child.endDeclared(phase);
50 public void storeStatement(final int expectedChildren, final boolean fullyDefined) {
51 checkState(current != null);
52 checkArgument(expectedChildren >= 0);
53 current.resizeSubstatements(expectedChildren);
56 current.setFullyDefined();
61 public void startStatement(final int childId, final QName name, final String argument,
62 final StatementSourceReference ref) {
63 final Optional<StatementContextBase<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
64 current = existing.isPresent() ? existing.get()
65 : verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
69 public void endStatement(final StatementSourceReference ref) {
70 checkState(current != null);
71 current.endDeclared(phase);
76 public ModelProcessingPhase getPhase() {
80 private void exitStatement() {
81 StatementContextBase<?, ?, ?> parentContext = current.getParentContext();
82 while (parentContext != null && StatementSource.CONTEXT == parentContext.getStatementSource()) {
83 parentContext.endDeclared(phase);
84 parentContext = parentContext.getParentContext();
86 current = parentContext;