X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-codec-gson%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fgson%2FJSONNormalizedNodeStreamWriter.java;h=ab6d05de89f8a0b477f54d5ee2caa424c2ec7d20;hb=aa37f2a86bfb4d04a99f78b1b0d892b3c0b76caf;hp=643e9de9ad041f9b2c4c3c02bf6e3d3317061295;hpb=68f459e972f360597301b929b5943082ba9e9730;p=yangtools.git diff --git a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java index 643e9de9ad..ab6d05de89 100644 --- a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java +++ b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java @@ -7,260 +7,448 @@ */ package org.opendaylight.yangtools.yang.data.codec.gson; -import com.google.common.base.CharMatcher; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.gson.stream.JsonWriter; +import static java.util.Objects.requireNonNull; +import static org.w3c.dom.Node.ELEMENT_NODE; +import static org.w3c.dom.Node.TEXT_NODE; +import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.io.Writer; import java.net.URI; - +import java.util.regex.Pattern; +import javax.annotation.RegEx; +import javax.xml.transform.dom.DOMSource; +import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker; -import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; /** * This implementation will create JSON output as output stream. * + *
* Values of leaf and leaf-list are NOT translated according to codecs. - * - * FIXME: rewrite this in terms of {@link JsonWriter}. */ -public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter { +public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter { + private static final class Exclusive extends JSONNormalizedNodeStreamWriter { + Exclusive(final JSONCodecFactory codecFactory, final SchemaTracker tracker, final JsonWriter writer, + final JSONStreamWriterRootContext rootContext) { + super(codecFactory, tracker, writer, rootContext); + } + + @Override + public void close() throws IOException { + flush(); + closeWriter(); + } + } + + private static final class Nested extends JSONNormalizedNodeStreamWriter { + Nested(final JSONCodecFactory codecFactory, final SchemaTracker tracker, final JsonWriter writer, + final JSONStreamWriterRootContext rootContext) { + super(codecFactory, tracker, writer, rootContext); + } + + @Override + public void close() throws IOException { + flush(); + // The caller "owns" the writer, let them close it + } + } + /** * RFC6020 deviation: we are not required to emit empty containers unless they * are marked as 'presence'. */ private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true; - /** - * Matcher used to check if a string needs to be escaped. - */ - private static final CharMatcher QUOTES_OR_BACKSLASH = CharMatcher.anyOf("\\\""); + @RegEx + private static final String NUMBER_STRING = "-?\\d+(\\.\\d+)?"; + private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_STRING); + + @RegEx + private static final String NOT_DECIMAL_NUMBER_STRING = "-?\\d+"; + private static final Pattern NOT_DECIMAL_NUMBER_PATTERN = Pattern.compile(NOT_DECIMAL_NUMBER_STRING); private final SchemaTracker tracker; private final JSONCodecFactory codecs; - private final Writer writer; - private final String indent; + private final JsonWriter writer; private JSONStreamWriterContext context; - private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, - final Writer writer, final URI initialNs, final int indentSize) { - this.writer = Preconditions.checkNotNull(writer); - - Preconditions.checkArgument(indentSize >= 0, "Indent size must be non-negative"); - if (indentSize != 0) { - indent = Strings.repeat(" ", indentSize); - } else { - indent = null; - } - this.codecs = Preconditions.checkNotNull(codecFactory); - this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path); - this.context = new JSONStreamWriterRootContext(initialNs); + JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaTracker tracker, + final JsonWriter writer, final JSONStreamWriterRootContext rootContext) { + this.writer = requireNonNull(writer); + this.codecs = requireNonNull(codecFactory); + this.tracker = requireNonNull(tracker); + this.context = requireNonNull(rootContext); } /** - * Create a new stream writer, which writes to the specified {@link Writer}. + * Create a new stream writer, which writes to the specified output stream. * - * @param schemaContext Schema context - * @param writer Output writer - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) { - return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, writer, null, 0); - } - - /** - * Create a new stream writer, which writes to the specified {@link Writer}. + *
+ * The codec factory can be reused between multiple writers. + * + *
+ * Returned writer is exclusive user of JsonWriter, which means it will start + * top-level JSON element and ends it. + * + *
+ * This instance of writer can be used only to emit one top level element, + * otherwise it will produce incorrect JSON. Closing this instance will close + * the writer too. * - * @param schemaContext Schema context - * @param path Root schemapath - * @param writer Output writer + * @param codecFactory JSON codec factory + * @param path Schema Path + * @param initialNs Initial namespace + * @param jsonWriter JsonWriter * @return A stream writer instance */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) { - return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), path, writer, null, 0); + public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory, + final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) { + return new Exclusive(codecFactory, SchemaTracker.create(codecFactory.getSchemaContext(), path), jsonWriter, + new JSONStreamWriterExclusiveRootContext(initialNs)); } /** - * Create a new stream writer, which writes to the specified {@link Writer}. + * Create a new stream writer, which writes to the specified output stream. + * + *
+ * The codec factory can be reused between multiple writers. * - * @param schemaContext Schema context - * @param path Root schemapath - * @param writer Output writer + *
+ * Returned writer is exclusive user of JsonWriter, which means it will start + * top-level JSON element and ends it. + * + *
+ * This instance of writer can be used only to emit one top level element, + * otherwise it will produce incorrect JSON. Closing this instance will close + * the writer too. + * + * @param codecFactory JSON codec factory + * @param rootNode Root node * @param initialNs Initial namespace + * @param jsonWriter JsonWriter * @return A stream writer instance */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, - final URI initialNs, final Writer writer) { - return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), path, writer, initialNs, 0); + public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory, + final DataNodeContainer rootNode, final URI initialNs, final JsonWriter jsonWriter) { + return new Exclusive(codecFactory, SchemaTracker.create(rootNode), jsonWriter, + new JSONStreamWriterExclusiveRootContext(initialNs)); } /** * Create a new stream writer, which writes to the specified output stream. * - * @param schemaContext Schema context - * @param writer Output writer - * @param indentSize indentation size + *
+ * The codec factory can be reused between multiple writers. + * + *
+ * Returned writer can be used emit multiple top level element, + * but does not start / close parent JSON object, which must be done + * by user providing {@code jsonWriter} instance in order for + * JSON to be valid. Closing this instance will not + * close the wrapped writer; the caller must take care of that. + * + * @param codecFactory JSON codec factory + * @param path Schema Path + * @param initialNs Initial namespace + * @param jsonWriter JsonWriter * @return A stream writer instance */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer, final int indentSize) { - return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, writer, null, indentSize); + public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory, + final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) { + return new Nested(codecFactory, SchemaTracker.create(codecFactory.getSchemaContext(), path), jsonWriter, + new JSONStreamWriterSharedRootContext(initialNs)); } /** - * Create a new stream writer, which writes to the specified output stream. The codec factory - * can be reused between multiple writers. + * Create a new stream writer, which writes to the specified output stream. * - * @param codecFactor JSON codec factory - * @param writer Output writer - * @param indentSize indentation size + *
+ * The codec factory can be reused between multiple writers. + * + *
+ * Returned writer can be used emit multiple top level element, + * but does not start / close parent JSON object, which must be done + * by user providing {@code jsonWriter} instance in order for + * JSON to be valid. Closing this instance will not + * close the wrapped writer; the caller must take care of that. + * + * @param codecFactory JSON codec factory + * @param rootNode Root node + * @param initialNs Initial namespace + * @param jsonWriter JsonWriter * @return A stream writer instance */ - public static NormalizedNodeStreamWriter create(final JSONCodecFactory codecFactory, final Writer writer, final int indentSize) { - return new JSONNormalizedNodeStreamWriter(codecFactory, SchemaPath.ROOT, writer, null, indentSize); + public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory, + final DataNodeContainer rootNode, final URI initialNs, final JsonWriter jsonWriter) { + return new Nested(codecFactory, SchemaTracker.create(rootNode), jsonWriter, + new JSONStreamWriterSharedRootContext(initialNs)); } @Override - public void leafNode(final NodeIdentifier name, final Object value) throws IOException { + public final void leafNode(final NodeIdentifier name, final Object value) throws IOException { final LeafSchemaNode schema = tracker.leafNode(name); - final JSONCodec