Bug 1761: Fixed missing module name prefix for augmentations.
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONStreamWriterContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.io.Writer;
13 import java.net.URI;
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;
18
19 /**
20  * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
21  * recursion. Provides the base API towards the writer, which is then specialized
22  * by subclasses.
23  */
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;
30
31     /**
32      * Construct a new context.
33      *
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.
37      */
38     protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
39         this.mandatory = mandatory;
40         this.parent = parent;
41
42         if (parent != null) {
43             depth = parent.depth + 1;
44         } else {
45             depth = 0;
46         }
47     }
48
49     /**
50      * Write a child JSON node identifier, optionally prefixing it with the module name
51      * corresponding to its namespace.
52      *
53      * @param schema Schema context
54      * @param writer Output writer
55      * @param qname Namespace/name tuple
56      * @throws IOException when the writer reports it
57      */
58     final void writeChildJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
59         writer.append('"');
60
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);
66
67             writer.append(module.getName());
68             writer.append(':');
69         }
70
71         writer.append(qname.getLocalName());
72         writer.append("\":");
73     }
74
75     /**
76      * Write our JSON node identifier, optionally prefixing it with the module name
77      * corresponding to its namespace.
78      *
79      * @param schema Schema context
80      * @param writer Output writer
81      * @param qname Namespace/name tuple
82      * @throws IOException when the writer reports it
83      */
84     protected final void writeMyJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
85         parent.writeChildJsonIdentifier(schema, writer, qname);
86     }
87
88     /**
89      * Return the namespace associated with current node.
90      *
91      * @return Namespace as URI
92      */
93     protected abstract @Nonnull URI getNamespace();
94
95     /**
96      * Emit the start of an element.
97      *
98      * @param schema Schema context
99      * @param writer Output writer
100      * @throws IOException
101      */
102     protected abstract void emitStart(final SchemaContext schema, final Writer writer) throws IOException;
103
104     /**
105      * Emit the end of an element.
106      *
107      * @param schema Schema context
108      * @param writer Output writer
109      * @throws IOException
110      */
111     protected abstract void emitEnd(final Writer writer) throws IOException;
112
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);
117             }
118
119             emitStart(schema, writer);
120             emittedMyself = true;
121         }
122     }
123
124     /**
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.
128      *
129      * @param schema Schema context
130      * @param writer Output writer
131      * @param indent Indentation string
132      * @throws IOException when writer reports it
133      */
134     final void emittingChild(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
135         emitMyself(schema, writer, indent);
136         if (haveChild) {
137             writer.append(',');
138         }
139
140         if (indent != null) {
141             writer.append('\n');
142
143             for (int i = 0; i < depth; i++) {
144                 writer.append(indent);
145             }
146         }
147         haveChild = true;
148     }
149
150     /**
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.
153      *
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)
160      */
161     final JSONStreamWriterContext endNode(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
162         if (!emittedMyself && mandatory) {
163             emitMyself(schema, writer, indent);
164         }
165
166         if (emittedMyself) {
167             emitEnd(writer);
168         }
169         return parent;
170     }
171
172 }