11ac84f1d9471fa176629d45d53a2d2364957811
[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.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} recursion. Provides the base API
21  * towards the writer, which is then specialized by subclasses.
22  */
23 abstract class JSONStreamWriterContext {
24     private final JSONStreamWriterContext parent;
25     private final boolean mandatory;
26     private final int depth;
27     private boolean emittedMyself = false;
28
29     /**
30      * Construct a new context.
31      *
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.
35      */
36     protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
37         this.mandatory = mandatory;
38         this.parent = parent;
39
40         if (parent != null) {
41             depth = parent.depth + 1;
42         } else {
43             depth = 0;
44         }
45     }
46
47     /**
48      * Write a child JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
49      *
50      * @param schema Schema context
51      * @param writer Output writer
52      * @param qname Namespace/name tuple
53      * @throws IOException when the writer reports it
54      */
55     final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
56             throws IOException {
57
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);
64
65             sb.append(module.getName());
66             sb.append(':');
67         }
68         sb.append(qname.getLocalName());
69
70         writer.name(sb.toString());
71     }
72
73     /**
74      * Write our JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
75      *
76      * @param schema Schema context
77      * @param writer Output writer
78      * @param qname Namespace/name tuple
79      * @throws IOException when the writer reports it
80      */
81     protected final void writeMyJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
82             throws IOException {
83         parent.writeChildJsonIdentifier(schema, writer, qname);
84     }
85
86     /**
87      * Return the namespace associated with current node.
88      *
89      * @return Namespace as URI
90      */
91     protected abstract @Nonnull URI getNamespace();
92
93     /**
94      * Emit the start of an element.
95      *
96      * @param schema Schema context
97      * @param writer Output writer
98      * @throws IOException when the writer reports it
99      */
100     protected abstract void emitStart(SchemaContext schema, JsonWriter writer) throws IOException;
101
102     /**
103      * Emit the end of an element.
104      *
105      * @param schema Schema context
106      * @param writer Output writer
107      * @throws IOException when writer reports it
108      */
109     protected abstract void emitEnd(JsonWriter writer) throws IOException;
110
111     private void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
112         if (!emittedMyself) {
113             if (parent != null) {
114                 parent.emittingChild(schema, writer);
115             }
116
117             emitStart(schema, writer);
118             emittedMyself = true;
119         }
120     }
121
122     /**
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.
126      *
127      * @param schema Schema context
128      * @param writer Output writer
129      * @throws IOException when writer reports it
130      */
131     final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
132         emitMyself(schema, writer);
133     }
134
135     /**
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.
138      *
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)
144      */
145     final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
146         if (!emittedMyself && mandatory) {
147             emitMyself(schema, writer);
148         }
149
150         if (emittedMyself) {
151             emitEnd(writer);
152         }
153         return parent;
154     }
155 }