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;
11 import java.io.IOException;
12 import java.io.Writer;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
21 * recursion. Provides the base API towards the writer, which is then specialized
24 abstract class JSONStreamWriterContext {
25 private final JSONStreamWriterContext parent;
26 private final boolean mandatory;
27 private final int depth;
28 private boolean emittedMyself = false;
29 private boolean haveChild = false;
32 * Construct a new context.
34 * @param parent Parent context, usually non-null.
35 * @param mandatory Mandatory flag. If set to true, the corresponding node
36 * will be emitted even if it has no children.
38 protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
39 this.mandatory = mandatory;
43 depth = parent.depth + 1;
50 * Write a child JSON node identifier, optionally prefixing it with the module name
51 * corresponding to its namespace.
53 * @param schema Schema context
54 * @param writer Output writer
55 * @param qname Namespace/name tuple
56 * @throws IOException when the writer reports it
58 final void writeChildJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
61 // Prepend module name if namespaces do not match
62 final URI ns = qname.getNamespace();
63 if (!ns.equals(getNamespace())) {
64 final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
65 Preconditions.checkArgument(module != null, "Could not find module for namespace {}", ns);
67 writer.append(module.getName());
71 writer.append(qname.getLocalName());
76 * Write our JSON node identifier, optionally prefixing it with the module name
77 * corresponding to its namespace.
79 * @param schema Schema context
80 * @param writer Output writer
81 * @param qname Namespace/name tuple
82 * @throws IOException when the writer reports it
84 protected final void writeMyJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
85 parent.writeChildJsonIdentifier(schema, writer, qname);
89 * Return the namespace associated with current node.
91 * @return Namespace as URI
93 protected abstract @Nonnull URI getNamespace();
96 * Emit the start of an element.
98 * @param schema Schema context
99 * @param writer Output writer
100 * @throws IOException
102 protected abstract void emitStart(final SchemaContext schema, final Writer writer) throws IOException;
105 * Emit the end of an element.
107 * @param schema Schema context
108 * @param writer Output writer
109 * @throws IOException
111 protected abstract void emitEnd(final Writer writer) throws IOException;
113 private final void emitMyself(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
114 if (!emittedMyself) {
115 if (parent != null) {
116 parent.emittingChild(schema, writer, indent);
119 emitStart(schema, writer);
120 emittedMyself = true;
125 * Invoked whenever a child node is being emitted. Checks whether this node has
126 * been emitted, and takes care of that if necessary. Also makes sure separator
127 * is emitted before a second and subsequent child.
129 * @param schema Schema context
130 * @param writer Output writer
131 * @param indent Indentation string
132 * @throws IOException when writer reports it
134 final void emittingChild(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
135 emitMyself(schema, writer, indent);
140 if (indent != null) {
143 for (int i = 0; i < depth; i++) {
144 writer.append(indent);
151 * Invoked by the writer when it is leaving this node. Checks whether this node
152 * needs to be emitted and takes of that if necessary.
154 * @param schema Schema context
155 * @param writer Output writer
156 * @param indent Indentation string
157 * @return Parent node context
158 * @throws IOException when writer reports it
159 * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
161 final JSONStreamWriterContext endNode(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
162 if (!emittedMyself && mandatory) {
163 emitMyself(schema, writer, indent);