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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.gson.stream.JsonWriter;
16 import java.io.IOException;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.XMLNamespace;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.Module;
26 * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter} recursion. Provides the base API
27 * towards the writer, which is then specialized by subclasses.
29 abstract class JSONStreamWriterContext {
30 private final JSONStreamWriterContext parent;
31 private final boolean mandatory;
32 private final int depth;
34 private boolean emittedMyself;
35 private boolean inChild;
38 * Construct a new context.
40 * @param parent Parent context, usually non-null.
41 * @param mandatory Mandatory flag. If set to true, the corresponding node
42 * will be emitted even if it has no children.
44 protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
45 this.mandatory = mandatory;
49 depth = parent.depth + 1;
56 * Write a child JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
58 * @param schema Schema context
59 * @param writer Output writer
60 * @param qname Namespace/name tuple
61 * @throws IOException when the writer reports it
63 final void writeChildJsonIdentifier(final EffectiveModelContext schema, final JsonWriter writer, final QName qname)
66 final StringBuilder sb = new StringBuilder();
67 // Prepend module name if namespaces do not match
68 final QNameModule module = qname.getModule();
69 if (!module.getNamespace().equals(getNamespace())) {
70 final Optional<String> modules = schema.findModule(module).map(Module::getName);
71 checkArgument(modules.isPresent(), "Could not find module for namespace {}", module);
72 sb.append(modules.get()).append(':');
74 sb.append(qname.getLocalName());
76 writer.name(sb.toString());
80 * Write our JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
82 * @param schema Schema context
83 * @param writer Output writer
84 * @param qname Namespace/name tuple
85 * @throws IOException when the writer reports it
87 protected final void writeMyJsonIdentifier(final EffectiveModelContext schema, final JsonWriter writer,
88 final QName qname) throws IOException {
89 parent.writeChildJsonIdentifier(schema, writer, qname);
93 * Return the namespace associated with current node.
95 * @return Namespace as URI
97 protected abstract @NonNull XMLNamespace getNamespace();
100 * Emit the start of an element.
102 * @param schema Schema context
103 * @param writer Output writer
104 * @throws IOException when the writer reports it
106 protected abstract void emitStart(EffectiveModelContext schema, JsonWriter writer) throws IOException;
109 * Emit the end of an element.
111 * @param schema Schema context
112 * @param writer Output writer
113 * @throws IOException when writer reports it
115 protected abstract void emitEnd(JsonWriter writer) throws IOException;
117 private void emitMyself(final EffectiveModelContext schema, final JsonWriter writer) throws IOException {
118 if (!emittedMyself) {
119 if (parent != null) {
120 parent.emitMyself(schema, writer);
123 emitStart(schema, writer);
124 emittedMyself = true;
129 * Invoked whenever a child node is being emitted. Checks whether this node has
130 * been emitted, and takes care of that if necessary. Also makes sure separator
131 * is emitted before a second and subsequent child.
133 * @param schema Schema context
134 * @param writer Output writer
135 * @throws IOException when writer reports it
137 final void emittingChild(final EffectiveModelContext schema, final JsonWriter writer) throws IOException {
138 checkState(!inChild, "Duplicate child encountered");
139 emitMyself(schema, writer);
144 * Invoked by the writer when it is leaving this node. Checks whether this node
145 * needs to be emitted and takes of that if necessary.
147 * @param schema Schema context
148 * @param writer Output writer
149 * @return Parent node context
150 * @throws IOException when writer reports it
151 * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
153 final JSONStreamWriterContext endNode(final EffectiveModelContext schema, final JsonWriter writer)
159 if (!emittedMyself && mandatory) {
160 emitMyself(schema, writer);
169 public final String toString() {
170 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
173 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {