1a5181a5789f2e81982348fc263623e0f7a7e049
[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
12 import java.io.IOException;
13 import java.io.Writer;
14 import java.net.URI;
15
16 import javax.annotation.Nonnull;
17
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 /**
23  * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
24  * recursion. Provides the base API towards the writer, which is then specialized
25  * by subclasses.
26  */
27 abstract class JSONStreamWriterContext {
28     private final JSONStreamWriterContext parent;
29     private final boolean mandatory;
30     private final int depth;
31     private boolean emittedMyself = false;
32     private boolean haveChild = false;
33
34     /**
35      * Construct a new context.
36      *
37      * @param parent Parent context, usually non-null.
38      * @param mandatory Mandatory flag. If set to true, the corresponding node
39      *                  will be emitted even if it has no children.
40      */
41     protected JSONStreamWriterContext(final JSONStreamWriterContext parent, final boolean mandatory) {
42         this.mandatory = mandatory;
43         this.parent = parent;
44
45         if (parent != null) {
46             depth = parent.depth + 1;
47         } else {
48             depth = 0;
49         }
50     }
51
52     /**
53      * Write a JSON node identifier, optionally prefixing it with the module name
54      * corresponding to its namespace.
55      *
56      * @param schema Schema context
57      * @param writer Output writer
58      * @param qname Namespace/name tuple
59      * @throws IOException when the writer reports it
60      */
61     protected final void writeJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
62         writer.append('"');
63
64         // Prepend module name if namespaces do not match
65         final URI ns = qname.getNamespace();
66         if (!ns.equals(getNamespace())) {
67             final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
68             Preconditions.checkArgument(module != null, "Could not find module for namespace {}", ns);
69
70             writer.append(module.getName());
71             writer.append(':');
72         }
73
74         writer.append(qname.getLocalName());
75         writer.append("\":");
76     }
77
78     /**
79      * Return the namespace associated with current node.
80      *
81      * @return Namespace as URI
82      */
83     protected abstract @Nonnull URI getNamespace();
84
85     /**
86      * Emit the start of an element.
87      *
88      * @param schema Schema context
89      * @param writer Output writer
90      * @throws IOException
91      */
92     protected abstract void emitStart(final SchemaContext schema, final Writer writer) throws IOException;
93
94     /**
95      * Emit the end of an element.
96      *
97      * @param schema Schema context
98      * @param writer Output writer
99      * @throws IOException
100      */
101     protected abstract void emitEnd(final Writer writer) throws IOException;
102
103     private final void emitMyself(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
104         if (!emittedMyself) {
105             if (parent != null) {
106                 parent.emittingChild(schema, writer, indent);
107             }
108
109             emitStart(schema, writer);
110             emittedMyself = true;
111         }
112     }
113
114     /**
115      * Invoked whenever a child node is being emitted. Checks whether this node has
116      * been emitted, and takes care of that if necessary. Also makes sure separator
117      * is emitted before a second and subsequent child.
118      *
119      * @param schema Schema context
120      * @param writer Output writer
121      * @param indent Indentation string
122      * @throws IOException when writer reports it
123      */
124     final void emittingChild(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
125         emitMyself(schema, writer, indent);
126         if (haveChild) {
127             writer.append(',');
128         }
129
130         if (indent != null) {
131             writer.append('\n');
132
133             for (int i = 0; i < depth; i++) {
134                 writer.append(indent);
135             }
136         }
137         haveChild = true;
138     }
139
140     /**
141      * Invoked by the writer when it is leaving this node. Checks whether this node
142      * needs to be emitted and takes of that if necessary.
143      *
144      * @param schema Schema context
145      * @param writer Output writer
146      * @param indent Indentation string
147      * @return Parent node context
148      * @throws IOException when writer reports it
149      * @throws IllegalArgumentException if this node cannot be ended (e.g. root)
150      */
151     final JSONStreamWriterContext endNode(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
152         if (!emittedMyself && mandatory) {
153             emitMyself(schema, writer, indent);
154         }
155
156         if (emittedMyself) {
157             emitEnd(writer);
158         }
159         return parent;
160     }
161 }