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.verify;
13 import static com.google.common.base.Verify.verifyNotNull;
14 import static java.util.Objects.requireNonNull;
16 import java.util.Optional;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
23 final class StatementContextWriter implements StatementWriter {
24 private final ModelProcessingPhase phase;
25 private final SourceSpecificContext ctx;
27 private AbstractResumedStatement<?, ?, ?> current;
29 StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
30 this.ctx = requireNonNull(ctx);
31 this.phase = requireNonNull(phase);
35 public Optional<? extends ResumedStatement> resumeStatement(final int childId) {
36 final Optional<AbstractResumedStatement<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
37 existing.ifPresent(this::resumeStatement);
41 private void resumeStatement(final AbstractResumedStatement<?, ?, ?> child) {
42 if (child.isFullyDefined()) {
43 child.walkChildren(phase);
44 child.endDeclared(phase);
51 public void storeStatement(final int expectedChildren, final boolean fullyDefined) {
52 checkState(current != null);
53 checkArgument(expectedChildren >= 0);
54 current.resizeSubstatements(expectedChildren);
57 current.setFullyDefined();
62 public void startStatement(final int childId, final QName name, final String argument,
63 final StatementSourceReference ref) {
64 final Optional<AbstractResumedStatement<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
65 current = existing.isPresent() ? existing.get()
66 : verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
70 public void endStatement(final StatementSourceReference ref) {
71 checkState(current != null);
72 current.endDeclared(phase);
77 public ModelProcessingPhase getPhase() {
81 private void exitStatement() {
82 // TODO: AbstractResumedStatement should only ever have AbstractResumedStatement parents, which would:
83 // - remove the StatementSource check
84 // - allow endDeclared() to be moved to AbstractResumedStatement
85 // - remove the need for verify()
86 StatementContextBase<?, ?, ?> parentContext = current.getParentContext();
87 while (parentContext != null && StatementSource.CONTEXT == parentContext.getStatementSource()) {
88 parentContext.endDeclared(phase);
89 parentContext = parentContext.getParentContext();
91 if (parentContext != null) {
92 verify(parentContext instanceof AbstractResumedStatement, "Unexpected parent context %s", parentContext);
93 current = (AbstractResumedStatement<?, ?, ?>) parentContext;