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.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.gson.stream.JsonWriter;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.NoSuchElementException;
19 import java.util.regex.Pattern;
20 import javax.xml.transform.dom.DOMSource;
21 import org.checkerframework.checker.regex.qual.Regex;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.common.MountPointLabel;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedAnydata;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.MountPointExtension;
32 import org.opendaylight.yangtools.yang.data.util.NormalizedNodeStreamWriterStack;
33 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
38 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
41 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
42 import org.w3c.dom.Element;
43 import org.w3c.dom.Node;
44 import org.w3c.dom.NodeList;
45 import org.w3c.dom.Text;
48 * This implementation will create JSON output as output stream.
51 * Values of leaf and leaf-list are NOT translated according to codecs.
53 public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter, MountPointExtension {
54 private static final class Exclusive extends JSONNormalizedNodeStreamWriter {
55 Exclusive(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
56 final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
57 super(codecFactory, tracker, writer, rootContext);
61 public void close() throws IOException {
67 private static final class Nested extends JSONNormalizedNodeStreamWriter {
68 Nested(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
69 final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
70 super(codecFactory, tracker, writer, rootContext);
74 public void close() throws IOException {
76 // The caller "owns" the writer, let them close it
81 * RFC6020 deviation: we are not required to emit empty containers unless they
82 * are marked as 'presence'.
84 private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
87 private static final String NUMBER_STRING = "-?\\d+(\\.\\d+)?";
88 private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_STRING);
91 private static final String NOT_DECIMAL_NUMBER_STRING = "-?\\d+";
92 private static final Pattern NOT_DECIMAL_NUMBER_PATTERN = Pattern.compile(NOT_DECIMAL_NUMBER_STRING);
94 private final NormalizedNodeStreamWriterStack tracker;
95 private final JSONCodecFactory codecs;
96 private final JsonWriter writer;
97 private JSONStreamWriterContext context;
99 JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
100 final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
101 this.writer = requireNonNull(writer);
102 codecs = requireNonNull(codecFactory);
103 this.tracker = requireNonNull(tracker);
104 context = requireNonNull(rootContext);
108 * Create a new stream writer, which writes to the specified output stream.
111 * The codec factory can be reused between multiple writers.
114 * Returned writer is exclusive user of JsonWriter, which means it will start
115 * top-level JSON element and ends it.
118 * This instance of writer can be used only to emit one top level element,
119 * otherwise it will produce incorrect JSON. Closing this instance will close
122 * @param codecFactory JSON codec factory
123 * @param jsonWriter JsonWriter
124 * @return A stream writer instance
126 public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
127 final JsonWriter jsonWriter) {
128 return createExclusiveWriter(codecFactory, jsonWriter, null);
132 * Create a new stream writer, which writes to the specified output stream.
135 * The codec factory can be reused between multiple writers.
138 * Returned writer is exclusive user of JsonWriter, which means it will start
139 * top-level JSON element and ends it.
142 * This instance of writer can be used only to emit one top level element,
143 * otherwise it will produce incorrect JSON. Closing this instance will close
146 * @param codecFactory JSON codec factory
147 * @param jsonWriter JsonWriter
148 * @param initialNs Initial namespace, can be null
149 * @return A stream writer instance
151 public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
152 final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
153 return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()),
154 jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
158 * Create a new stream writer, which writes to the specified output stream.
161 * The codec factory can be reused between multiple writers.
164 * Returned writer is exclusive user of JsonWriter, which means it will start
165 * top-level JSON element and ends it.
168 * This instance of writer can be used only to emit one top level element,
169 * otherwise it will produce incorrect JSON. Closing this instance will close
172 * @param codecFactory JSON codec factory
173 * @param rootNode Root node inference
174 * @param initialNs Initial namespace
175 * @param jsonWriter JsonWriter
176 * @return A stream writer instance
178 public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
179 final EffectiveStatementInference rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
180 return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(rootNode), jsonWriter,
181 new JSONStreamWriterExclusiveRootContext(initialNs));
185 * Create a new stream writer, which writes to the specified output stream.
188 * The codec factory can be reused between multiple writers.
191 * Returned writer is exclusive user of JsonWriter, which means it will start
192 * top-level JSON element and ends it.
195 * This instance of writer can be used only to emit one top level element,
196 * otherwise it will produce incorrect JSON. Closing this instance will close
199 * @param codecFactory JSON codec factory
200 * @param path Schema Path
201 * @param initialNs Initial namespace
202 * @param jsonWriter JsonWriter
203 * @return A stream writer instance
205 public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
206 final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
207 return new Exclusive(codecFactory,
208 NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
209 new JSONStreamWriterExclusiveRootContext(initialNs));
213 * Create a new stream writer, which writes to the specified output stream.
216 * The codec factory can be reused between multiple writers.
219 * Returned writer can be used emit multiple top level element,
220 * but does not start / close parent JSON object, which must be done
221 * by user providing {@code jsonWriter} instance in order for
222 * JSON to be valid. Closing this instance <strong>will not</strong>
223 * close the wrapped writer; the caller must take care of that.
225 * @param codecFactory JSON codec factory
226 * @param jsonWriter JsonWriter
227 * @return A stream writer instance
229 public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
230 final JsonWriter jsonWriter) {
231 return createNestedWriter(codecFactory, jsonWriter, null);
235 * Create a new stream writer, which writes to the specified output stream.
238 * The codec factory can be reused between multiple writers.
241 * Returned writer can be used emit multiple top level element,
242 * but does not start / close parent JSON object, which must be done
243 * by user providing {@code jsonWriter} instance in order for
244 * JSON to be valid. Closing this instance <strong>will not</strong>
245 * close the wrapped writer; the caller must take care of that.
247 * @param codecFactory JSON codec factory
248 * @param initialNs Initial namespace
249 * @param jsonWriter JsonWriter
250 * @return A stream writer instance
252 public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
253 final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
254 return new Nested(codecFactory, NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()),
255 jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
259 * Create a new stream writer, which writes to the specified output stream.
262 * The codec factory can be reused between multiple writers.
265 * Returned writer can be used emit multiple top level element,
266 * but does not start / close parent JSON object, which must be done
267 * by user providing {@code jsonWriter} instance in order for
268 * JSON to be valid. Closing this instance <strong>will not</strong>
269 * close the wrapped writer; the caller must take care of that.
271 * @param codecFactory JSON codec factory
272 * @param path Schema Path
273 * @param initialNs Initial namespace
274 * @param jsonWriter JsonWriter
275 * @return A stream writer instance
277 public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
278 final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
279 return new Nested(codecFactory,
280 NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
281 new JSONStreamWriterSharedRootContext(initialNs));
285 * Create a new stream writer, which writes to the specified output stream.
288 * The codec factory can be reused between multiple writers.
291 * Returned writer can be used emit multiple top level element,
292 * but does not start / close parent JSON object, which must be done
293 * by user providing {@code jsonWriter} instance in order for
294 * JSON to be valid. Closing this instance <strong>will not</strong>
295 * close the wrapped writer; the caller must take care of that.
297 * @param codecFactory JSON codec factory
298 * @param rootNode Root node inference
299 * @param initialNs Initial namespace
300 * @param jsonWriter JsonWriter
301 * @return A stream writer instance
303 public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
304 final EffectiveStatementInference rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
305 return new Nested(codecFactory, NormalizedNodeStreamWriterStack.of(rootNode), jsonWriter,
306 new JSONStreamWriterSharedRootContext(initialNs));
310 public Collection<? extends Extension> supportedExtensions() {
311 return List.of(this);
315 public void startLeafNode(final NodeIdentifier name) throws IOException {
316 tracker.startLeafNode(name);
317 context.emittingChild(codecs.getEffectiveModelContext(), writer);
318 context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
322 public final void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
323 tracker.startLeafSet(name);
324 context = new JSONStreamWriterListContext(context, name);
328 public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
329 tracker.startLeafSetEntryNode(name);
330 context.emittingChild(codecs.getEffectiveModelContext(), writer);
334 public final void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
335 tracker.startLeafSet(name);
336 context = new JSONStreamWriterListContext(context, name);
340 * Warning suppressed due to static final constant which triggers a warning
341 * for the call to schema.isPresenceContainer().
344 public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
345 final SchemaNode schema = tracker.startContainerNode(name);
346 final boolean isPresence = schema instanceof ContainerSchemaNode container
347 ? container.isPresenceContainer() : DEFAULT_EMIT_EMPTY_CONTAINERS;
348 context = new JSONStreamWriterNamedObjectContext(context, name, isPresence);
352 public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
353 tracker.startList(name);
354 context = new JSONStreamWriterListContext(context, name);
358 public final void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
359 tracker.startListItem(name);
360 context = new JSONStreamWriterObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
364 public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
365 tracker.startList(name);
366 context = new JSONStreamWriterListContext(context, name);
370 public final void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
372 tracker.startListItem(identifier);
373 context = new JSONStreamWriterObjectContext(context, identifier, DEFAULT_EMIT_EMPTY_CONTAINERS);
377 public final void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
378 tracker.startList(name);
379 context = new JSONStreamWriterListContext(context, name);
383 public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
384 tracker.startChoiceNode(name);
385 context = new JSONStreamWriterInvisibleContext(context);
389 public final boolean startAnydataNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
390 if (NormalizedAnydata.class.isAssignableFrom(objectModel)) {
391 tracker.startAnydataNode(name);
392 context.emittingChild(codecs.getEffectiveModelContext(), writer);
393 context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
401 public final NormalizedNodeStreamWriter startMountPoint(final MountPointLabel label,
402 final MountPointContext mountCtx) throws IOException {
403 final EffectiveModelContext ctx = mountCtx.getEffectiveModelContext();
404 return new Nested(codecs.rebaseTo(ctx), NormalizedNodeStreamWriterStack.of(ctx), writer,
405 new JSONStreamWriterSharedRootContext(context.getNamespace()));
409 public final boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
410 if (DOMSource.class.isAssignableFrom(objectModel)) {
411 tracker.startAnyxmlNode(name);
412 context.emittingChild(codecs.getEffectiveModelContext(), writer);
413 context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
420 public final void endNode() throws IOException {
422 context = context.endNode(codecs.getEffectiveModelContext(), writer);
426 public final void flush() throws IOException {
430 final void closeWriter() throws IOException {
431 if (!(context instanceof JSONStreamWriterRootContext)) {
432 throw new IOException("Unexpected root context " + context);
435 context.endNode(codecs.getEffectiveModelContext(), writer);
440 public void scalarValue(final Object value) throws IOException {
441 final Object current = tracker.getParent();
442 if (current instanceof TypedDataSchemaNode typed) {
443 writeValue(value, codecs.codecFor(typed, tracker));
444 } else if (current instanceof AnydataSchemaNode) {
445 writeAnydataValue(value);
447 throw new IllegalStateException(String.format("Cannot emit scalar %s for %s", value, current));
452 public void domSourceValue(final DOMSource value) throws IOException {
453 final Object current = tracker.getParent();
454 checkState(current instanceof AnyxmlSchemaNode, "Cannot emit DOMSource %s for %s", value, current);
455 // FIXME: should have a codec based on this :)
456 writeAnyXmlValue(value);
459 @SuppressWarnings("unchecked")
460 private void writeValue(final Object value, final JSONCodec<?> codec) throws IOException {
461 ((JSONCodec<Object>) codec).writeValue(writer, value);
464 private void writeAnydataValue(final Object value) throws IOException {
465 if (value instanceof NormalizedAnydata normalized) {
466 writeNormalizedAnydata(normalized);
468 throw new IllegalStateException("Unexpected anydata value " + value);
472 private void writeNormalizedAnydata(final NormalizedAnydata anydata) throws IOException {
473 // Adjust state to point to parent node and ensure it can handle data tree nodes
474 final SchemaInferenceStack.Inference inference;
476 final SchemaInferenceStack stack = SchemaInferenceStack.ofInference(anydata.getInference());
477 stack.exitToDataTree();
478 inference = stack.toInference();
479 } catch (IllegalArgumentException | IllegalStateException | NoSuchElementException e) {
480 throw new IOException("Cannot emit " + anydata, e);
483 anydata.writeTo(JSONNormalizedNodeStreamWriter.createNestedWriter(
484 codecs.rebaseTo(inference.getEffectiveModelContext()), inference, context.getNamespace(), writer));
487 private void writeAnyXmlValue(final DOMSource anyXmlValue) throws IOException {
488 writeXmlNode(anyXmlValue.getNode());
491 private void writeXmlNode(final Node node) throws IOException {
492 if (isArrayElement(node)) {
493 writeArrayContent(node);
496 final Element firstChildElement = getFirstChildElement(node);
497 if (firstChildElement == null) {
500 writeObjectContent(firstChildElement);
504 private void writeArrayContent(final Node node) throws IOException {
510 private void writeObjectContent(final Element firstChildElement) throws IOException {
511 writer.beginObject();
512 writeObject(firstChildElement);
516 private static boolean isArrayElement(final Node node) {
517 if (Node.ELEMENT_NODE == node.getNodeType()) {
518 final String nodeName = node.getNodeName();
519 for (Node nextNode = node.getNextSibling(); nextNode != null; nextNode = nextNode.getNextSibling()) {
520 if (Node.ELEMENT_NODE == nextNode.getNodeType() && nodeName.equals(nextNode.getNodeName())) {
528 private void handleArray(final Node node) throws IOException {
529 final Element parentNode = (Element)node.getParentNode();
530 final NodeList elementsList = parentNode.getElementsByTagName(node.getNodeName());
531 for (int i = 0, length = elementsList.getLength(); i < length; i++) {
532 final Node arrayElement = elementsList.item(i);
533 final Element parent = (Element)arrayElement.getParentNode();
534 if (parentNode.isSameNode(parent)) {
535 final Element firstChildElement = getFirstChildElement(arrayElement);
536 if (firstChildElement != null) {
537 writeObjectContent(firstChildElement);
540 writeXmlValue(arrayElement);
546 private void writeObject(Node node) throws IOException {
547 final var previousNodeNames = new HashSet<String>();
548 while (node != null) {
549 if (Node.ELEMENT_NODE == node.getNodeType()) {
550 final var nodeName = node.getNodeName();
551 if (previousNodeNames.add(nodeName)) {
552 writer.name(nodeName);
556 node = node.getNextSibling();
560 private void writeXmlValue(final Node node) throws IOException {
561 Text firstChild = getFirstChildText(node);
562 String childNodeText = firstChild != null ? firstChild.getWholeText() : "";
563 childNodeText = childNodeText != null ? childNodeText.trim() : "";
565 if (NUMBER_PATTERN.matcher(childNodeText).matches()) {
566 writer.value(parseNumber(childNodeText));
569 switch (childNodeText) {
570 case "null" -> writer.nullValue();
571 case "false" -> writer.value(false);
572 case "true" -> writer.value(true);
573 default -> writer.value(childNodeText);
577 private static Element getFirstChildElement(final Node node) {
578 final NodeList children = node.getChildNodes();
579 for (int i = 0, length = children.getLength(); i < length; i++) {
580 final Node childNode = children.item(i);
581 if (Node.ELEMENT_NODE == childNode.getNodeType()) {
582 return (Element) childNode;
588 private static Text getFirstChildText(final Node node) {
589 final NodeList children = node.getChildNodes();
590 for (int i = 0, length = children.getLength(); i < length; i++) {
591 final Node childNode = children.item(i);
592 if (Node.TEXT_NODE == childNode.getNodeType()) {
593 return (Text) childNode;
599 // json numbers are 64 bit wide floating point numbers - in java terms it is either long or double
600 private static Number parseNumber(final String numberText) {
601 if (NOT_DECIMAL_NUMBER_PATTERN.matcher(numberText).matches()) {
602 return Long.valueOf(numberText);
605 return Double.valueOf(numberText);