You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by ja...@apache.org on 2019/04/24 13:40:35 UTC

[incubator-daffodil] branch master updated: Output empty elements with self closing tags

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

jadams pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-daffodil.git


The following commit(s) were added to refs/heads/master by this push:
     new aae5c12  Output empty elements with self closing tags
aae5c12 is described below

commit aae5c1233dd38d574a347b14750d6d9c31574fa1
Author: Josh Adams <ja...@tresys.com>
AuthorDate: Mon Apr 15 10:44:45 2019 -0400

    Output empty elements with self closing tags
    
    Before empty comlex elements would have to be represented with
    <elem></elem>, which could cause some problems with non-schema aware XML
    parsers. Now empty complex elements will show up as <elem /> in the
    infoset.
    
    DAFFODIL-2064
---
 .../org/apache/daffodil/infoset/InfosetImpl.scala  |  3 +++
 .../daffodil/infoset/XMLTextInfosetOutputter.scala | 27 ++++++++++++++--------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/InfosetImpl.scala b/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/InfosetImpl.scala
index df45c27..8d20eea 100644
--- a/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/InfosetImpl.scala
+++ b/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/InfosetImpl.scala
@@ -1337,6 +1337,8 @@ sealed class DIComplex(override val erd: ElementRuntimeData, val tunable: Daffod
 
   override def children = childNodes.toStream
 
+  var hasVisibleChildren = false
+
   final def getChild(erd: ElementRuntimeData): InfosetElement = {
     getChild(erd.dpathElementCompileInfo)
   }
@@ -1381,6 +1383,7 @@ sealed class DIComplex(override val erd: ElementRuntimeData, val tunable: Daffod
   }
 
   override def addChild(e: InfosetElement): Unit = {
+    if (!e.isHidden && !hasVisibleChildren) hasVisibleChildren = true
     if (e.runtimeData.isArray) {
       val childERD = e.runtimeData
       if (childNodes.isEmpty || childNodes.last.erd != childERD) {
diff --git a/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/XMLTextInfosetOutputter.scala b/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/XMLTextInfosetOutputter.scala
index db685a4..a2dec72 100644
--- a/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/XMLTextInfosetOutputter.scala
+++ b/daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/XMLTextInfosetOutputter.scala
@@ -41,7 +41,7 @@ class XMLTextInfosetOutputter(writer: java.io.Writer, pretty: Boolean = true)
     if (prefix == null || prefix == "") elem.erd.name else prefix + ":" + elem.erd.name
   }
 
-  private def outputStartTag(elem: DIElement, name: String): Unit = {
+  private def outputStartTag(elem: DIElement, name: String, isEmpty: Boolean=false): Unit = {
     writer.write("<")
 
     writer.write(name)
@@ -58,7 +58,11 @@ class XMLTextInfosetOutputter(writer: java.io.Writer, pretty: Boolean = true)
       writer.write(" xsi:nil=\"true\"")
     }
 
-    writer.write(">")
+    if (isEmpty) {
+      writer.write("/>")
+    } else {
+      writer.write(">")
+    }
   }
 
   private def outputEndTag(elem: DIElement, name: String): Unit = {
@@ -97,19 +101,24 @@ class XMLTextInfosetOutputter(writer: java.io.Writer, pretty: Boolean = true)
   override def startComplex(complex: DIComplex): Boolean = {
     val name = getTagName(complex)
     if (pretty) outputIndentation(writer)
-    outputStartTag(complex, name)
+    outputStartTag(complex, name, !complex.hasVisibleChildren)
     incrementIndentation()
     if (pretty) writer.write(System.lineSeparator())
     true
   }
 
   override def endComplex(complex: DIComplex): Boolean = {
-    val name = getTagName(complex)
-    decrementIndentation()
-    if (pretty) outputIndentation(writer)
-    outputEndTag(complex, name)
-    if (pretty) writer.write(System.lineSeparator())
-    true
+    if (!complex.hasVisibleChildren) {
+      // Empty complex elements should have an inline closing tag
+      true
+    } else {
+      val name = getTagName(complex)
+      decrementIndentation()
+      if (pretty) outputIndentation(writer)
+      outputEndTag(complex, name)
+      if (pretty) writer.write(System.lineSeparator())
+      true
+    }
   }
 
   override def startArray(array: DIArray): Boolean = {