You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2021/05/27 18:31:20 UTC

[GitHub] [daffodil] mbeckerle commented on a change in pull request #576: Enhance XML Loader to tolerate whitespace

mbeckerle commented on a change in pull request #576:
URL: https://github.com/apache/daffodil/pull/576#discussion_r640872213



##########
File path: daffodil-lib/src/main/scala/org/apache/daffodil/xml/DaffodilConstructingLoader.scala
##########
@@ -288,10 +288,77 @@ class DaffodilConstructingLoader private[xml] (uri: URI,
     text(pos, s)
   }
 
+  /**
+   * Same CRLF/CR => LF processing as text gets.
+   */
   override def comment(pos: Int, s: String): Comment = {
     Comment(text(pos, s).text)
   }
 
+  /**
+   * Same CRLF/CR => LF processing as text gets.
+   */
+  override def procInstr(pos: Int, target: String, txt: String) =
+    ProcInstr(target, text(pos, txt).text)
+
+  /**
+   * Override of document to make it tolerant of the start of the file
+   * being whitespace instead of a "<" character
+   */
+  override def document(): Document = {
+
+    if ('<' == ch) super.document()
+    else {
+      // File did not start with "<".
+      // We want to tolerate whitespace and comments prior to the
+      // document element.
+
+      doc = new Document()
+
+      nextch()
+      var children: NodeSeq = null
+
+      val ts = new NodeBuffer()
+      ts &+ content(TopScope)
+      children = NodeSeq.fromSeq(ts)
+
+      var isErr = false
+      var elemCount = 0
+      var theNode: Node = null
+      for (c <- children) c match {
+        case _: ProcInstr => // skip
+        case _: Comment => // skip
+        case _: EntityRef => {
+          reportSyntaxError("no entity references allowed here")
+          isErr = true
+        }
+        case s: SpecialNode => {
+          val txt = s.toString.trim()
+          if (txt.length > 0) {
+            reportSyntaxError("non-empty text nodes not allowed: '" + txt + "'.")
+            isErr = true

Review comment:
       Their code was pretty weak. I changed it to give more precise diagnostics. 

##########
File path: daffodil-lib/src/main/scala/org/apache/daffodil/xml/DaffodilConstructingLoader.scala
##########
@@ -288,10 +288,77 @@ class DaffodilConstructingLoader private[xml] (uri: URI,
     text(pos, s)
   }
 
+  /**
+   * Same CRLF/CR => LF processing as text gets.
+   */
   override def comment(pos: Int, s: String): Comment = {
     Comment(text(pos, s).text)
   }
 
+  /**
+   * Same CRLF/CR => LF processing as text gets.
+   */
+  override def procInstr(pos: Int, target: String, txt: String) =
+    ProcInstr(target, text(pos, txt).text)
+
+  /**
+   * Override of document to make it tolerant of the start of the file
+   * being whitespace instead of a "<" character
+   */
+  override def document(): Document = {
+
+    if ('<' == ch) super.document()
+    else {
+      // File did not start with "<".
+      // We want to tolerate whitespace and comments prior to the
+      // document element.
+
+      doc = new Document()
+
+      nextch()
+      var children: NodeSeq = null
+
+      val ts = new NodeBuffer()
+      ts &+ content(TopScope)
+      children = NodeSeq.fromSeq(ts)
+
+      var isErr = false
+      var elemCount = 0
+      var theNode: Node = null
+      for (c <- children) c match {
+        case _: ProcInstr => // skip
+        case _: Comment => // skip
+        case _: EntityRef => {
+          reportSyntaxError("no entity references allowed here")
+          isErr = true
+        }
+        case s: SpecialNode => {
+          val txt = s.toString.trim()
+          if (txt.length > 0) {
+            reportSyntaxError("non-empty text nodes not allowed: '" + txt + "'.")
+            isErr = true
+          }
+        }
+        case m: Elem =>

Review comment:
       The "node" has to be a single document element, so they were just not using as precise a type as they could have. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org