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.stmt.test;
10 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.CONTAINER;
11 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.IMPORT;
12 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.MODULE;
13 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.NAMESPACE;
14 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.PREFIX;
15 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.REVISION;
16 import static org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping.REVISION_DATE;
17 import java.util.Arrays;
18 import java.util.List;
19 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
21 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
22 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
28 class TestStatementSource implements StatementStreamSource {
30 public static class ModuleEntry {
32 private final String name;
33 private final String revision;
35 public ModuleEntry(String name, String revision) {
38 this.revision = revision;
41 public String getName() {
46 public String getRevision() {
52 private static final String NS_PREFIX = "urn:org:opendaylight:yangtools:test:";
54 private final String name;
55 private final String revision;
56 private final List<ModuleEntry> imports;
57 private String container;
58 private StatementWriter writer;
59 private StatementSourceReference REF = new StatementSourceReference() {
62 public StatementSource getStatementSource() {
63 return StatementSource.DECLARATION;
67 public TestStatementSource(ModuleEntry module, ModuleEntry... imports) {
69 this.name = module.getName();
70 this.revision = module.getRevision();
71 this.imports = Arrays.asList(imports);
74 public void setContainer(String container) {
76 this.container = container;
80 public void writeFull(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes)
81 throws SourceException {
90 public void writeLinkage(StatementWriter writer, QNameToStatementDefinition stmtDef) throws SourceException {
96 public void writeLinkageAndStatementDefinitions(StatementWriter writer, QNameToStatementDefinition stmtDef,
97 PrefixToModule prefixes) throws SourceException {
104 protected void extensions() throws SourceException {
108 protected void body() throws SourceException {
110 if (container != null)
111 stmt(CONTAINER).arg(container).end();
114 TestStatementSource header() throws SourceException {
116 stmt(MODULE).arg(name);
118 stmt(NAMESPACE).arg(getNamespace()).end();
119 stmt(PREFIX).arg(name).end();
121 if (revision != null) {
122 stmt(REVISION).arg(revision).end();
125 for (ModuleEntry impEntry : imports) {
127 String imp = impEntry.getName();
128 String rev = impEntry.getRevision();
130 stmt(IMPORT).arg(imp);
132 stmt(PREFIX).arg(imp).end();
135 stmt(REVISION_DATE).arg(rev).end();
144 private String getNamespace() {
145 return NS_PREFIX + name;
148 protected TestStatementSource arg(String arg) throws SourceException {
149 writer.argumentValue(arg, REF);
153 protected TestStatementSource stmt(Rfc6020Mapping stmt) throws SourceException {
154 writer.startStatement(stmt.getStatementName(), REF);
158 protected TestStatementSource end() throws SourceException {
159 writer.endStatement(REF);