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 com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
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;
31 * Construct a new context.
33 * @param parent Parent context, usually non-null.
34 * @param mandatory Mandatory flag. If set to true, the corresponding node
35 * will be emitted even if it has no children.
37 protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
38 this.mandatory = mandatory;
42 depth = parent.depth + 1;
49 * Write a child JSON node identifier, optionally prefixing it with the module name
50 * corresponding to its namespace.
52 * @param schema Schema context
53 * @param writer Output writer
54 * @param qname Namespace/name tuple
55 * @throws IOException when the writer reports it
57 final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname) throws IOException {
59 final StringBuilder sb = new StringBuilder();
60 // Prepend module name if namespaces do not match
61 final URI ns = qname.getNamespace();
62 if (!ns.equals(getNamespace())) {
63 final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
64 Preconditions.checkArgument(module != null, "Could not find module for namespace {}", ns);
66 sb.append(module.getName());
69 sb.append(qname.getLocalName());
71 writer.name(sb.toString());
75 * Write our JSON node identifier, optionally prefixing it with the module name
76 * corresponding to its namespace.
78 * @param schema Schema context
79 * @param writer Output writer
80 * @param qname Namespace/name tuple
81 * @throws IOException when the writer reports it
83 protected final void writeMyJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname) throws IOException {
84 parent.writeChildJsonIdentifier(schema, writer, qname);
88 * Return the namespace associated with current node.
90 * @return Namespace as URI
92 protected abstract @Nonnull URI getNamespace();
95 * Emit the start of an element.
97 * @param schema Schema context
98 * @param writer Output writer
101 protected abstract void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException;
104 * Emit the end of an element.
106 * @param schema Schema context
107 * @param writer Output writer
108 * @throws IOException
110 protected abstract void emitEnd(final JsonWriter writer) throws IOException;
112 private void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
113 if (!emittedMyself) {
114 if (parent != null) {
115 parent.emittingChild(schema, writer);
118 emitStart(schema, writer);
119 emittedMyself = true;
124 * Invoked whenever a child node is being emitted. Checks whether this node has
125 * been emitted, and takes care of that if necessary. Also makes sure separator
126 * is emitted before a second and subsequent child.
128 * @param schema Schema context
129 * @param writer Output writer
130 * @throws IOException when writer reports it
132 final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
133 emitMyself(schema, writer);
137 * Invoked by the writer when it is leaving this node. Checks whether this node
138 * needs to be emitted and takes of that if necessary.
140 * @param schema Schema context
141 * @param writer Output writer
142 * @return Parent node context
143 * @throws IOException when writer reports it
144 * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
146 final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
147 if (!emittedMyself && mandatory) {
148 emitMyself(schema, writer);