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 2022/01/05 19:05:08 UTC

[GitHub] [daffodil] tuxji commented on a change in pull request #721: Support --infoset null for CLI unparsing

tuxji commented on a change in pull request #721:
URL: https://github.com/apache/daffodil/pull/721#discussion_r779053191



##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/NullInfosetInputter.scala
##########
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.infoset
+
+import java.io.InputStream
+
+import scala.collection.mutable.ArrayBuffer
+
+import scala.xml.Elem
+import scala.xml.Node
+import scala.xml.SAXParser
+import scala.xml.Text
+import scala.xml.XML
+
+import org.apache.daffodil.dpath.NodeInfo
+import org.apache.daffodil.infoset.InfosetInputterEventType._
+import org.apache.daffodil.util.MaybeBoolean
+import org.apache.daffodil.xml.DaffodilSAXParserFactory
+import org.apache.daffodil.xml.XMLUtils
+
+object NullInfosetInputter {
+
+  case class Event(
+    eventType: InfosetInputterEventType,
+    localName: String = null,
+    namespaceURI: String = null,
+    simpleText: String = null,
+    isNilled: MaybeBoolean = MaybeBoolean.Nope,
+  )
+
+  def createEvents(is: InputStream): Array[Event] = {
+    val elem = {
+      val parser: SAXParser = {
+        val f = DaffodilSAXParserFactory()
+        f.setNamespaceAware(false)
+        val p = f.newSAXParser()
+        p
+      }
+      val node = XML.withSAXParser(parser).load(is)
+      val normalized = XMLUtils.normalize(node)
+      normalized
+    }
+    val events = ArrayBuffer[Event]()
+    events += Event(StartDocument)
+    nodeToEvents(elem, events)
+    events += Event(EndDocument)
+    events.toArray
+  }
+
+  /**
+   * Recursively walk a Scala XML Node, converting each Elem to an Event
+   * and appends it to the events array
+   */
+  private def nodeToEvents(node: Node, events: ArrayBuffer[Event]): Unit = {
+    node match {
+      case elem: Elem => {
+        val isSimple = elem.child.length == 0 || elem.child(0).isInstanceOf[Text]
+        val localName = elem.label
+        val namespaceURI = elem.namespace
+        val (simpleText, isNilled) = if (isSimple) {
+          val text = XMLUtils.remapPUAToXMLIllegalCharacters(elem.text)
+          val isNilled = elem.attribute(XMLUtils.XSI_NAMESPACE, "nil").map { attrs =>
+            val str = attrs.head.toString
+            val value = str == "true" || str == "1"
+            MaybeBoolean(value)
+          }.getOrElse(MaybeBoolean.Nope)
+          (text, isNilled)
+        } else {
+          (null, MaybeBoolean.Nope)
+        }
+
+        events += Event(StartElement, localName, namespaceURI, simpleText, isNilled)
+        if (!isSimple) elem.child.foreach { nodeToEvents(_, events) }
+        events += Event(EndElement, localName, namespaceURI)
+      }
+      case _ => // ignore

Review comment:
       This line (91) isn't covered by tests, so please expand the comment to explain why we have a case _ and what kind of Scala XML object this line might ignore.

##########
File path: daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala
##########
@@ -848,6 +846,9 @@ object Main {
         val tl = anyRef.asInstanceOf[ThreadLocal[org.w3c.dom.Document]]
         Left(new W3CDOMInfosetInputter(tl.get))
       }
+      case InfosetType.NULL => {
+        Left(new NullInfosetInputter(anyRef.asInstanceOf[Array[NullInfosetInputter.Event]]))

Review comment:
       Like the case above it, this case would be clearer if line 850 was more verbose like this:
   
   ```scala
           val events = anyRef.asInstanceOf[Array[NullInfosetInputter.Event]]
           Left(new NullInfosetInputter(events))
   ```
   
   On second thought, we should write a test to cover these lines - if we use only manual testing, these lines might break after a future change without us realizing it.

##########
File path: daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala
##########
@@ -815,6 +806,13 @@ object Main {
           }
         }
       }
+      case InfosetType.NULL => {
+        val is = data match {
+          case Left(bytes) => new ByteArrayInputStream(bytes)
+          case Right(is) => is
+        }
+        NullInfosetInputter.createEvents(is)

Review comment:
       Lines 811, 814, and 850 aren't covered by tests (weird that line 812 is ignored) and my initial impression at reading line 814 was that it was a side-effect call.  I saw createEvents' definition later and realized it returns an Array[Event], but line 814 would be clearer if it read like this:
   
   ```scala
           val events = NullInfosetInputter.createEvents(is)
           events
   ```
   
   Still, I won't quibble about these lines not being covered by tests.




-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

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