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} recursion. Provides the base API
21 * towards the writer, which is then specialized by subclasses.
23 abstract class JSONStreamWriterContext {
24 private final JSONStreamWriterContext parent;
25 private final boolean mandatory;
26 private final int depth;
27 private boolean emittedMyself = false;
30 * Construct a new context.
32 * @param parent Parent context, usually non-null.
33 * @param mandatory Mandatory flag. If set to true, the corresponding node
34 * will be emitted even if it has no children.
36 protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
37 this.mandatory = mandatory;
41 depth = parent.depth + 1;
48 * Write a child JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
50 * @param schema Schema context
51 * @param writer Output writer
52 * @param qname Namespace/name tuple
53 * @throws IOException when the writer reports it
55 final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
58 final StringBuilder sb = new StringBuilder();
59 // Prepend module name if namespaces do not match
60 final URI ns = qname.getNamespace();
61 if (!ns.equals(getNamespace())) {
62 final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
63 Preconditions.checkArgument(module != null, "Could not find module for namespace {}", ns);
65 sb.append(module.getName());
68 sb.append(qname.getLocalName());
70 writer.name(sb.toString());
74 * Write our JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
76 * @param schema Schema context
77 * @param writer Output writer
78 * @param qname Namespace/name tuple
79 * @throws IOException when the writer reports it
81 protected final void writeMyJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
83 parent.writeChildJsonIdentifier(schema, writer, qname);
87 * Return the namespace associated with current node.
89 * @return Namespace as URI
91 protected abstract @Nonnull URI getNamespace();
94 * Emit the start of an element.
96 * @param schema Schema context
97 * @param writer Output writer
98 * @throws IOException when the writer reports it
100 protected abstract void emitStart(SchemaContext schema, JsonWriter writer) throws IOException;
103 * Emit the end of an element.
105 * @param schema Schema context
106 * @param writer Output writer
107 * @throws IOException when writer reports it
109 protected abstract void emitEnd(JsonWriter writer) throws IOException;
111 private void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
112 if (!emittedMyself) {
113 if (parent != null) {
114 parent.emittingChild(schema, writer);
117 emitStart(schema, writer);
118 emittedMyself = true;
123 * Invoked whenever a child node is being emitted. Checks whether this node has
124 * been emitted, and takes care of that if necessary. Also makes sure separator
125 * is emitted before a second and subsequent child.
127 * @param schema Schema context
128 * @param writer Output writer
129 * @throws IOException when writer reports it
131 final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
132 emitMyself(schema, writer);
136 * Invoked by the writer when it is leaving this node. Checks whether this node
137 * needs to be emitted and takes of that if necessary.
139 * @param schema Schema context
140 * @param writer Output writer
141 * @return Parent node context
142 * @throws IOException when writer reports it
143 * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
145 final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
146 if (!emittedMyself && mandatory) {
147 emitMyself(schema, writer);