2 * Copyright (c) 2014 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.data.codec.gson;
10 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.io.Writer;
16 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
24 * recursion. Provides the base API towards the writer, which is then specialized
27 abstract class JSONStreamWriterContext {
28 private final JSONStreamWriterContext parent;
29 private final boolean mandatory;
30 private final int depth;
31 private boolean emittedMyself = false;
32 private boolean haveChild = false;
35 * Construct a new context.
37 * @param parent Parent context, usually non-null.
38 * @param mandatory Mandatory flag. If set to true, the corresponding node
39 * will be emitted even if it has no children.
41 protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
42 this.mandatory = mandatory;
46 depth = parent.depth + 1;
53 * Write a JSON node identifier, optionally prefixing it with the module name
54 * corresponding to its namespace.
56 * @param schema Schema context
57 * @param writer Output writer
58 * @param qname Namespace/name tuple
59 * @throws IOException when the writer reports it
61 protected final void writeJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
64 // Prepend module name if namespaces do not match
65 final URI ns = qname.getNamespace();
66 if (!ns.equals(getNamespace())) {
67 final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
68 Preconditions.checkArgument(module != null, "Could not find module for namespace {}", ns);
70 writer.append(module.getName());
74 writer.append(qname.getLocalName());
79 * Return the namespace associated with current node.
81 * @return Namespace as URI
83 protected abstract @Nonnull URI getNamespace();
86 * Emit the start of an element.
88 * @param schema Schema context
89 * @param writer Output writer
92 protected abstract void emitStart(final SchemaContext schema, final Writer writer) throws IOException;
95 * Emit the end of an element.
97 * @param schema Schema context
98 * @param writer Output writer
101 protected abstract void emitEnd(final Writer writer) throws IOException;
103 private final void emitMyself(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
104 if (!emittedMyself) {
105 if (parent != null) {
106 parent.emittingChild(schema, writer, indent);
109 emitStart(schema, writer);
110 emittedMyself = true;
115 * Invoked whenever a child node is being emitted. Checks whether this node has
116 * been emitted, and takes care of that if necessary. Also makes sure separator
117 * is emitted before a second and subsequent child.
119 * @param schema Schema context
120 * @param writer Output writer
121 * @param indent Indentation string
122 * @throws IOException when writer reports it
124 final void emittingChild(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
125 emitMyself(schema, writer, indent);
130 if (indent != null) {
133 for (int i = 0; i < depth; i++) {
134 writer.append(indent);
141 * Invoked by the writer when it is leaving this node. Checks whether this node
142 * needs to be emitted and takes of that if necessary.
144 * @param schema Schema context
145 * @param writer Output writer
146 * @param indent Indentation string
147 * @return Parent node context
148 * @throws IOException when writer reports it
149 * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
151 final JSONStreamWriterContext endNode(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
152 if (!emittedMyself && mandatory) {
153 emitMyself(schema, writer, indent);