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;
13 import java.io.StringWriter;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
22 * recursion. Provides the base API towards the writer, which is then specialized
25 abstract class JSONStreamWriterContext {
26 private final JSONStreamWriterContext parent;
27 private final boolean mandatory;
28 private final int depth;
29 private boolean emittedMyself = 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 JsonWriter writer, final QName qname) throws IOException {
60 StringWriter strWriter = new StringWriter();
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 strWriter.append(module.getName());
68 strWriter.append(':');
70 strWriter.append(qname.getLocalName());
72 writer.name(strWriter.toString());
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 JsonWriter 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 JsonWriter 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 JsonWriter writer) throws IOException;
113 private final void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
114 if (!emittedMyself) {
115 if (parent != null) {
116 parent.emittingChild(schema, writer);
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 * @throws IOException when writer reports it
133 final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
134 emitMyself(schema, writer);
138 * Invoked by the writer when it is leaving this node. Checks whether this node
139 * needs to be emitted and takes of that if necessary.
141 * @param schema Schema context
142 * @param writer Output writer
143 * @return Parent node context
144 * @throws IOException when writer reports it
145 * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
147 final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
148 if (!emittedMyself && mandatory) {
149 emitMyself(schema, writer);