You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by ol...@apache.org on 2023/07/13 17:40:39 UTC

[daffodil] branch main updated: Implement Minimal Option for XMLTextInfosetOutputter

This is an automated email from the ASF dual-hosted git repository.

olabusayo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git


The following commit(s) were added to refs/heads/main by this push:
     new a23ffc799 Implement Minimal Option for XMLTextInfosetOutputter
a23ffc799 is described below

commit a23ffc7997b535be333dd2ac95dc6fa54c972d79
Author: olabusayoT <50...@users.noreply.github.com>
AuthorDate: Tue Jul 11 19:10:57 2023 -0400

    Implement Minimal Option for XMLTextInfosetOutputter
    
    - exclude xml slug and prefix namespace bindings
    
    DAFFODIL-2732
---
 .../runtime1/debugger/InteractiveDebugger.scala    |  2 +-
 .../runtime1/infoset/XMLTextInfosetOutputter.scala | 36 ++++++++++++++--------
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/debugger/InteractiveDebugger.scala b/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/debugger/InteractiveDebugger.scala
index 0935a94c8..bff7156b1 100644
--- a/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/debugger/InteractiveDebugger.scala
+++ b/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/debugger/InteractiveDebugger.scala
@@ -470,7 +470,7 @@ class InteractiveDebugger(
 
   private def infosetToString(ie: InfosetElement): String = {
     val bos = new java.io.ByteArrayOutputStream()
-    val xml = new XMLTextInfosetOutputter(bos, true)
+    val xml = new XMLTextInfosetOutputter(bos, pretty = true, minimal = true)
     val iw = InfosetWalker(
       ie.asInstanceOf[DIElement],
       xml,
diff --git a/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/infoset/XMLTextInfosetOutputter.scala b/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/infoset/XMLTextInfosetOutputter.scala
index e8a0524d0..e51caea73 100644
--- a/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/infoset/XMLTextInfosetOutputter.scala
+++ b/daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/infoset/XMLTextInfosetOutputter.scala
@@ -28,16 +28,18 @@ import org.apache.daffodil.runtime1.dpath.NodeInfo
 /**
  * Writes the infoset to a java.io.Writer as XML text.
  *
- * @param writer The writer to write the XML text to
- * @param pretty Whether or to enable pretty printing. Set to true, XML
- *               elements are indented and newlines are inserted.
+ * @param writer             The writer to write the XML text to
+ * @param pretty             Whether or to enable pretty printing. Set to true, XML
+ *                           elements are indented and newlines are inserted.
  * @param xmlTextEscapeStyle Determine whether to wrap values of elements of type
- *        xs:string in CDATA tags in order to preserve whitespace.
+ *                           xs:string in CDATA tags in order to preserve whitespace.
+ * @param minimal            Determine whether to exclude xml slug and prefix bindings
  */
 class XMLTextInfosetOutputter private (
   writer: java.io.Writer,
   pretty: Boolean,
   xmlTextEscapeStyle: XMLTextEscapeStyle.Value,
+  minimal: Boolean,
 ) extends InfosetOutputter
   with Indentable
   with XMLInfosetOutputter {
@@ -46,8 +48,14 @@ class XMLTextInfosetOutputter private (
     os: java.io.OutputStream,
     pretty: Boolean,
     xmlTextEscapeStyle: XMLTextEscapeStyle.Value = XMLTextEscapeStyle.Standard,
+    minimal: Boolean = false,
   ) = {
-    this(new java.io.OutputStreamWriter(os, StandardCharsets.UTF_8), pretty, xmlTextEscapeStyle)
+    this(
+      new java.io.OutputStreamWriter(os, StandardCharsets.UTF_8),
+      pretty,
+      xmlTextEscapeStyle,
+      minimal,
+    )
   }
 
   private val sb = new StringBuilder()
@@ -87,12 +95,14 @@ class XMLTextInfosetOutputter private (
 
     outputTagName(elem)
 
-    val nsbStart = elem.erd.minimizedScope
-    val nsbEnd = if (elem.isRoot) scala.xml.TopScope else elem.diParent.erd.minimizedScope
-    if (nsbStart != nsbEnd) {
-      sb.setLength(0) // reset the stringbuilder
-      nsbStart.buildString(sb, nsbEnd)
-      writer.write(sb.toString)
+    if (!minimal) {
+      val nsbStart = elem.erd.minimizedScope
+      val nsbEnd = if (elem.isRoot) scala.xml.TopScope else elem.diParent.erd.minimizedScope
+      if (nsbStart != nsbEnd) {
+        sb.setLength(0) // reset the stringbuilder
+        nsbStart.buildString(sb, nsbEnd)
+        writer.write(sb.toString)
+      }
     }
 
     if (isNilled(elem)) {
@@ -233,7 +243,9 @@ class XMLTextInfosetOutputter private (
   }
 
   override def startDocument(): Unit = {
-    writer.write("""<?xml version="1.0" encoding="UTF-8"?>""")
+    if (!minimal) {
+      writer.write("""<?xml version="1.0" encoding="UTF-8"?>""")
+    }
   }
 
   override def endDocument(): Unit = {