olabusayoT commented on a change in pull request #436:
URL: https://github.com/apache/incubator-daffodil/pull/436#discussion_r511948390



##########
File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala
##########
@@ -0,0 +1,309 @@
+/*
+ * 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.processors
+
+import scala.util.Try
+import scala.xml.NamespaceBinding
+
+import javax.xml.XMLConstants
+import org.apache.daffodil.api.DFDL
+import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
+import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
+import org.apache.daffodil.infoset.IllegalContentWhereEventExpected
+import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
+import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
+import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
+import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetInputter
+import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.Maybe.Nope
+import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Misc
+import org.xml.sax.Attributes
+import org.xml.sax.Locator
+
+/**
+ * DaffodilUnparseContentHandler produces InfosetEvent objects for the 
SAXInfosetInputter to
+ * consume and convert to a event that the Dataprocessor unparse can use. The 
infosetEvent object
+ * is built from information that is passed to the ContentHandler from an 
XMLReader parser. In
+ * order to receive the uri and prefix information from the XMLReader, the 
following features
+ * must be set to true on whatever XMLReader is used: 
http://xml.org/sax/features/namespaces and
+ * http://xml.org/sax/features/namespace-prefixes
+ *
+ * This class, together with the SAXInfosetInputter, uses coroutines to ensure 
that only one event,
+ * at a time, is passed between the two classes. The following is the general 
process:
+ *
+ * - an external call is made to parse an XML Documents
+ * - this class receives a StartDocument call, which is the first infosetEvent 
that is sent to
+ * the SAXInfosetInputter. That event is put on the inputter's queue, this 
thread is paused, and
+ * that inputter's thread is run
+ * - when the SAXInfosetInputter is done processing an event and is ready for 
a new event, it
+ * sends the completed event via the coroutine system, and loads it on the 
contentHandler's
+ * queue, which restarts this thread and pauses that one. In the expected 
case, the events will
+ * contain no new information, until the unparse is completed.
+ * -  this process continues until the EndDocument method is called. Once that 
infosetEvent is
+ * sent to the inputter, it signals the end of events coming from the 
contentHandler. This
+ * ends the unparseProcess and returns the event with the unparseResult and/or 
any error
+ * information
+ *
+ * @param dp dataprocessor object that will be used to call the parse
+ * @param output outputChannel of choice where the unparsed data is stored
+ */
+class DaffodilUnparseContentHandler(
+  dp: DFDL.DataProcessor,
+  output: DFDL.Output)
+  extends DFDL.DaffodilUnparseContentHandler {
+  private lazy val inputter = new SAXInfosetInputter(this, dp, output)
+  private var unparseResult: DFDL.UnparseResult = _
+  private lazy val infosetEvent: DFDL.SaxInfosetEvent = new 
DFDL.SaxInfosetEvent
+  private lazy val characterData = new StringBuilder
+  private var prefixMapping: NamespaceBinding = _
+  private lazy val prefixMappingTrackingStack = new MStackOf[NamespaceBinding]
+  private var contentHandlerPrefixMappingUsed = false
+
+  /**
+   * returns null in the case of an DaffodilUnhandledSAXException
+   */
+  def getUnparseResult: DFDL.UnparseResult = unparseResult
+
+  def enableInputterResolutionOfRelativeInfosetBlobURIs(): Unit = 
inputter.enableResolutionOfRelativeInfosetBlobURIs()
+
+  override def setDocumentLocator(locator: Locator): Unit = {
+    // do nothing
+  }
+
+  override def startDocument(): Unit = {
+    infosetEvent.eventType = One(StartDocument)
+    sendToInputter()
+  }
+
+  override def endDocument(): Unit = {
+    infosetEvent.eventType = One(EndDocument)
+    sendToInputter()
+  }
+
+  override def startPrefixMapping(prefix: String, uri: String): Unit = {
+    contentHandlerPrefixMappingUsed = true
+    val pre = if (prefix == "") null else prefix
+    prefixMapping = NamespaceBinding(pre, uri, prefixMapping)
+  }
+
+  /**
+   * XMLReader does not guarantee the order of the prefixes called for this 
function, but it does
+   * guarantee that this method is called after its corresponding endElement, 
which means we can
+   * can just take off the top mappings, because the element that might have 
cared about the order
+   * is already done using the prefixMappings
+   */
+  override def endPrefixMapping(prefix: String): Unit = {
+    prefixMapping = if (prefixMapping == null) prefixMapping else 
prefixMapping.parent
+  }
+
+  /**
+   * Uses Attributes, which is passed in to the startElement callback, to 
extract prefix mappings and
+   * populate the global prefixMapping
+   */
+  def mapPrefixMappingFromAttributesImpl(atts:Attributes): Unit = {
+    var i = 0
+    while (i < atts.getLength) {
+      val qName = atts.getQName(i)
+      if (qName.startsWith("xmlns")) {
+        val uri =  atts.getValue(i)
+        val prefix = if(qName.contains(":")) {
+          val pref = qName.split(":").last
+          pref
+        } else {
+         null // NamespaceBinding does not allow blanks so return null instead
+        }
+        prefixMapping = NamespaceBinding(prefix, uri, prefixMapping)
+      }
+      i += 1
+    }
+  }
+
+  override def startElement(uri: String, localName: String, qName: String, 
atts: Attributes): Unit = {
+    // we need to check if the characters data is all whitespace, if it is we 
drop the whitespace
+    // data, if it is not, it is an error as starting a new element with 
actual characterData means
+    // we haven't hit an endElement yet, which means we're in a complexElement 
and a complexElement
+    // cannot have character content
+    if (characterData.nonEmpty && !Misc.isAllWhitespace(characterData)) {
+      throw new IllegalContentWhereEventExpected("Non-whitespace characters in 
complex " +
+        "Element: " + characterData.toString
+      )
+    } else {
+      // reset since it was whitespace only
+      characterData.setLength(0)
+    }
+
+    if (!contentHandlerPrefixMappingUsed) {
+      // always pushes but doesn't always add a mapping since atts can be empty
+      prefixMappingTrackingStack.push(prefixMapping)
+      mapPrefixMappingFromAttributesImpl(atts)
+    }
+
+    if (!infosetEvent.isEmpty && infosetEvent.localName.isDefined) {
+      // we started another element while we were in the process of building a 
startElement
+      // this means the first element was complex and we are ready for the 
inputter queue
+      sendToInputter()
+    }
+    // use Attributes to determine xsi:nil value
+    val nilIn = atts.getIndex(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, 
"nil")
+    infosetEvent.nilValue = if (nilIn >= 0) {
+      val nilValue = atts.getValue(nilIn)
+      One(nilValue)
+    } else {
+      Nope
+    }
+    // set localName and namespaceURI
+    lazy val qNameArr = qName.split(":")
+    infosetEvent.localName =
+      if (localName.nonEmpty) {
+        One(localName)
+      } else if (qNameArr.length > 1) {
+        One(qNameArr.last)
+      } else if (qNameArr.nonEmpty) {
+        One(qNameArr.head)
+      } else {

Review comment:
       Ah you're right, it was a left over from when I was using options, I was 
returning either lift(1) or lift(0) then since the former case represented when 
there was a prefix and a localName and the latter, when there is only a 
localName, but it can be simplified to .nonEmpty and .last




----------------------------------------------------------------
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:
[email protected]


Reply via email to