Merge "Added tests for yang.model.util"
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONNormalizedNodeStreamWriter.java
index b03771d4c49698c676e1feb2f332a6c4257a29fc..c1a4c31d43f1c1b21146fd1dd87f84ff2ae556d9 100644 (file)
@@ -10,13 +10,11 @@ package org.opendaylight.yangtools.yang.data.codec.gson;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.gson.stream.JsonWriter;
-
 import java.io.IOException;
 import java.io.Writer;
 import java.net.URI;
 import java.util.ArrayDeque;
 import java.util.Deque;
-
 import org.opendaylight.yangtools.concepts.Codec;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -28,6 +26,7 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
  * This implementation will create JSON output as output stream.
@@ -97,6 +96,22 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
         this.tracker = SchemaTracker.create(schemaContext);
     }
 
+    private JSONNormalizedNodeStreamWriter(final SchemaContext schemaContext, final SchemaPath path,
+            final Writer writer, final int indentSize) {
+        this.schemaContext = Preconditions.checkNotNull(schemaContext);
+        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 = CodecFactory.create(schemaContext);
+        this.tracker = SchemaTracker.create(schemaContext,path);
+    }
+
     /**
      * Create a new stream writer, which writes to the specified {@link Writer}.
      *
@@ -108,6 +123,17 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
         return new JSONNormalizedNodeStreamWriter(schemaContext, writer, 0);
     }
 
+    /**
+     * Create a new stream writer, which writes to the specified {@link Writer}.
+     *
+     * @param schemaContext Schema context
+     * @param writer Output writer
+     * @return A stream writer instance
+     */
+    public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, SchemaPath path,final Writer writer) {
+        return new JSONNormalizedNodeStreamWriter(schemaContext, path, writer, 0);
+    }
+
     /**
      * Create a new stream writer, which writes to the specified output stream.
      *
@@ -200,9 +226,10 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite
     public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
             throws IOException {
         tracker.startListItem(identifier);
-
-        stack.push(new TypeInfo(NodeType.OBJECT, identifier.getNodeType().getNamespace()));
         separateElementFromPreviousElement();
+        stack.push(new TypeInfo(NodeType.OBJECT, identifier.getNodeType().getNamespace()));
+
+
         writeStartObject();
         indentRight();
     }