Merge "Introduced skeletons of Contributor and User guide."
[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 com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.net.URI;
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;
19
20 /**
21  * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
22  * recursion. Provides the base API towards the writer, which is then specialized
23  * by subclasses.
24  */
25 abstract class JSONStreamWriterContext {
26     private final JSONStreamWriterContext parent;
27     private final boolean mandatory;
28     private final int depth;
29     private boolean emittedMyself = 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 JsonWriter writer, final QName qname) throws IOException {
59
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);
66
67             strWriter.append(module.getName());
68             strWriter.append(':');
69         }
70         strWriter.append(qname.getLocalName());
71
72         writer.name(strWriter.toString());
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 JsonWriter 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 JsonWriter 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 JsonWriter writer) throws IOException;
112
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);
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      * @throws IOException when writer reports it
132      */
133     final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
134         emitMyself(schema, writer);
135     }
136
137     /**
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.
140      *
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)
146      */
147     final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
148         if (!emittedMyself && mandatory) {
149             emitMyself(schema, writer);
150         }
151
152         if (emittedMyself) {
153             emitEnd(writer);
154         }
155         return parent;
156     }
157
158 }