BUG-7983: unify JSONCodec and XmlCodec methods
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONNormalizedNodeStreamWriter.java
index 6b412d583d3c7675b81bc4ae89563b5d8809828d..c1b9c7181a6e96a1a585bf6b07ccc572a198d317 100644 (file)
@@ -7,11 +7,19 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
+import static org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.ANYXML_ARRAY_ELEMENT_ID;
+import static org.w3c.dom.Node.ELEMENT_NODE;
+import static org.w3c.dom.Node.TEXT_NODE;
+
 import com.google.common.base.Preconditions;
+import com.google.common.base.Throwables;
 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;
@@ -20,9 +28,10 @@ import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 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.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * This implementation will create JSON output as output stream.
@@ -30,119 +39,33 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
  * Values of leaf and leaf-list are NOT translated according to codecs.
  *
  */
-public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
+public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
     /**
      * 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;
 
+    @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 JsonWriter writer;
     private JSONStreamWriterContext context;
 
-    private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, JsonWriter JsonWriter, JSONStreamWriterRootContext rootContext) {
+    private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final JsonWriter JsonWriter, final JSONStreamWriterRootContext rootContext) {
         this.writer = Preconditions.checkNotNull(JsonWriter);
         this.codecs = Preconditions.checkNotNull(codecFactory);
         this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path);
         this.context = Preconditions.checkNotNull(rootContext);
     }
 
-    /**
-     * Create a new stream writer, which writes to the specified {@link Writer}.
-     *
-     * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
-     *
-     * @param schemaContext Schema context
-     * @param writer Output writer
-     * @return A stream writer instance
-     */
-    public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) {
-        return create(schemaContext, SchemaPath.ROOT, null, writer);
-    }
-
-    /**
-     * Create a new stream writer, which writes to the specified {@link Writer}.
-     *
-     * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
-     *
-     * @param schemaContext Schema context
-     * @param path Root schemapath
-     * @param writer Output writer
-     * @return A stream writer instance
-     */
-    public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) {
-        return create(schemaContext, path, null, writer);
-    }
-
-    /**
-     * Create a new stream writer, which writes to the specified {@link Writer}.
-     *
-     * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
-     *
-     * @param schemaContext Schema context
-     * @param path Root schemapath
-     * @param writer Output writer
-     * @param initialNs Initial namespace
-     * @return A stream writer instance
-     */
-    public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path,
-            final URI initialNs, final Writer writer) {
-        return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, JsonWriterFactory.createJsonWriter(writer));
-    }
-
-    /**
-     * Create a new stream writer, which writes to the specified output stream.
-     *
-     * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
-     *
-     * @param schemaContext Schema context
-     * @param writer Output writer
-     * @param indentSize indentation size
-     * @return A stream writer instance
-     */
-    public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer, final int indentSize) {
-        return createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,JsonWriterFactory.createJsonWriter(writer, indentSize));
-    }
-
-    /**
-     * Create a new stream writer, which writes to the specified output stream. The codec factory
-     * can be reused between multiple writers.
-     *
-     * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
-     *
-     * @param codecFactory JSON codec factory
-     * @param writer Output writer
-     * @param indentSize indentation size
-     * @return A stream writer instance
-     */
-    public static NormalizedNodeStreamWriter create(final JSONCodecFactory codecFactory, final Writer writer, final int indentSize) {
-        return createExclusiveWriter(codecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer,indentSize));
-    }
-
-    /**
-     * Create a new stream writer, which writes to the specified output stream.
-     *
-     * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
-     *
-     * @param schemaContext Schema context
-     * @param path Schema Path
-     * @param initialNs Initial namespace
-     * @param jsonWriter JsonWriter
-     * @return A stream writer instance
-     */
-    public static NormalizedNodeStreamWriter create(SchemaContext schemaContext, SchemaPath path, URI initialNs,
-            JsonWriter jsonWriter) {
-        return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, jsonWriter);
-    }
-
     /**
      * Create a new stream writer, which writes to the specified output stream.
      *
@@ -152,7 +75,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
      * top-level JSON element and ends it.
      *
      * This instance of writer can be used only to emit one top level element,
-     * therwise it will produce incorrect JSON.
+     * otherwise it will produce incorrect JSON.
      *
      * @param codecFactory JSON codec factory
      * @param path Schema Path
@@ -160,7 +83,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
      * @param jsonWriter JsonWriter
      * @return A stream writer instance
      */
-    public static NormalizedNodeStreamWriter createExclusiveWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
+    public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
         return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
     }
 
@@ -180,14 +103,14 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
      * @param jsonWriter JsonWriter
      * @return A stream writer instance
      */
-    public static NormalizedNodeStreamWriter createNestedWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
+    public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
         return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
     }
 
     @Override
     public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
         final LeafSchemaNode schema = tracker.leafNode(name);
-        final JSONCodec<Object> codec = codecs.codecFor(schema);
+        final JSONCodec<?> codec = codecs.codecFor(schema);
         context.emittingChild(codecs.getSchemaContext(), writer);
         context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
         writeValue(value, codec);
@@ -200,13 +123,19 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
     }
 
     @Override
-    public void leafSetEntryNode(final Object value) throws IOException {
-        final LeafListSchemaNode schema = tracker.leafSetEntryNode();
-        final JSONCodec<Object> codec = codecs.codecFor(schema);
+    public void leafSetEntryNode(final QName name, final Object value) throws IOException {
+        final LeafListSchemaNode schema = tracker.leafSetEntryNode(name);
+        final JSONCodec<?> codec = codecs.codecFor(schema);
         context.emittingChild(codecs.getSchemaContext(), writer);
         writeValue(value, codec);
     }
 
+    @Override
+    public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
+        tracker.startLeafSet(name);
+        context = new JSONStreamWriterListContext(context, name);
+    }
+
     /*
      * Warning suppressed due to static final constant which triggers a warning
      * for the call to schema.isPresenceContainer().
@@ -272,8 +201,14 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
 
         context.emittingChild(codecs.getSchemaContext(), writer);
         context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
-        // FIXME this kind of serialization is incorrect since the value for AnyXml is now a DOMSource
-        writer.value(String.valueOf(value));
+
+        writeAnyXmlValue((DOMSource) value);
+    }
+
+    @Override
+    public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
+        tracker.startYangModeledAnyXmlNode(name);
+        context = new JSONStreamWriterNamedObjectContext(context, name, true);
     }
 
     @Override
@@ -281,14 +216,115 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
         tracker.endNode();
         context = context.endNode(codecs.getSchemaContext(), writer);
 
-        if(context instanceof JSONStreamWriterRootContext) {
+        if (context instanceof JSONStreamWriterRootContext) {
             context.emitEnd(writer);
         }
     }
 
-    private void writeValue(Object value, JSONCodec<Object> codec)
+    @SuppressWarnings("unchecked")
+    private void writeValue(final Object value, final JSONCodec<?> codec)
             throws IOException {
-        codec.serializeToWriter(writer,value);
+        try {
+            ((JSONCodec<Object>) codec).writeValue(writer, value);
+        } catch (IOException e) {
+            throw e;
+        } catch (Exception e) {
+            Throwables.propagateIfPossible(e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    private void writeAnyXmlValue(final DOMSource anyXmlValue) throws IOException {
+        final Node documentNode = anyXmlValue.getNode();
+        final Node firstChild = documentNode.getFirstChild();
+        if (ELEMENT_NODE == firstChild.getNodeType() && !ANYXML_ARRAY_ELEMENT_ID.equals(firstChild.getNodeName())) {
+            writer.beginObject();
+            traverseAnyXmlValue(documentNode);
+            writer.endObject();
+        } else {
+            traverseAnyXmlValue(documentNode);
+        }
+    }
+
+    private void traverseAnyXmlValue(final Node node) throws IOException {
+        final NodeList children = node.getChildNodes();
+        boolean inArray = false;
+
+        for (int i = 0, length = children.getLength(); i < length; i++) {
+            final Node childNode = children.item(i);
+            boolean inObject = false;
+
+            if (ELEMENT_NODE == childNode.getNodeType()) {
+                final Node firstChild = childNode.getFirstChild();
+                // beginning of an array
+                if (ANYXML_ARRAY_ELEMENT_ID.equals(childNode.getNodeName()) && !inArray) {
+                    writer.beginArray();
+                    inArray = true;
+                    // object at the beginning of the array
+                    if (isJsonObjectInArray(childNode, firstChild)) {
+                        writer.beginObject();
+                        inObject = true;
+                    }
+                    // object in the array
+                } else if (isJsonObjectInArray(childNode, firstChild)) {
+                    writer.beginObject();
+                    inObject = true;
+                    // object
+                } else if (isJsonObject(firstChild)) {
+                    writer.name(childNode.getNodeName());
+                    writer.beginObject();
+                    inObject = true;
+                    // name
+                } else if (!inArray){
+                    writer.name(childNode.getNodeName());
+                }
+            }
+
+            // text value, i.e. a number, string, boolean or null
+            if (TEXT_NODE == childNode.getNodeType()) {
+                final String childNodeText = childNode.getNodeValue();
+                if (NUMBER_PATTERN.matcher(childNodeText).matches()) {
+                    writer.value(parseNumber(childNodeText));
+                } else if ("true".equals(childNodeText) || "false".equals(childNodeText)) {
+                    writer.value(Boolean.parseBoolean(childNodeText));
+                } else if ("null".equals(childNodeText)) {
+                    writer.nullValue();
+                } else {
+                    writer.value(childNodeText);
+                }
+
+                return;
+            }
+
+            traverseAnyXmlValue(childNode);
+
+            if (inObject) {
+                writer.endObject();
+            }
+        }
+
+        if (inArray) {
+            writer.endArray();
+        }
+    }
+
+    // json numbers are 64 bit wide floating point numbers - in java terms it is either long or double
+    private static Number parseNumber(final String numberText) {
+        if (NOT_DECIMAL_NUMBER_PATTERN.matcher(numberText).matches()) {
+            return Long.valueOf(numberText);
+        }
+
+        return Double.valueOf(numberText);
+    }
+
+    private static boolean isJsonObject(final Node firstChild) {
+        return !ANYXML_ARRAY_ELEMENT_ID.equals(firstChild.getNodeName()) && TEXT_NODE != firstChild.getNodeType();
+    }
+
+    private static boolean isJsonObjectInArray(final Node node, final Node firstChild) {
+        return ANYXML_ARRAY_ELEMENT_ID.equals(node.getNodeName())
+                && !ANYXML_ARRAY_ELEMENT_ID.equals(firstChild.getNodeName())
+                && TEXT_NODE != firstChild.getNodeType();
     }
 
     @Override
@@ -298,10 +334,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
 
     @Override
     public void close() throws IOException {
-        writer.flush();
+        flush();
         writer.close();
     }
-
-
-
 }