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



##########
File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilInputContentHandler.scala
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.Maybe.Nope
+import org.apache.daffodil.util.Maybe.One
+import org.xml.sax.Attributes
+import org.xml.sax.Locator
+
+/**
+ * DaffodilInputContentHandler 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 DaffodilInputContentHandler(
+  dp: DFDL.DataProcessor,
+  output: DFDL.Output)
+  extends DFDL.DaffodilInputContentHandler {
+  private val inputter = new SAXInfosetInputter(this, dp, output)
+  private var unparseResult: DFDL.UnparseResult = _
+  private val infosetEvent: DFDL.SaxInfosetEvent = new DFDL.SaxInfosetEvent
+  private val characterData = new StringBuilder()
+  private var prefixMapping: NamespaceBinding = _
+
+  def getUnparseResult: DFDL.UnparseResult = unparseResult
+
+  def enableInputterUriAbsolutization(): Unit = 
inputter.enableUriAbsolutization = true
+
+  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 = {
+    val pre = if (prefix == "") null else prefix
+    prefixMapping = NamespaceBinding(pre, uri, prefixMapping)
+  }
+
+  override def endPrefixMapping(prefix: String): Unit = {
+    // do nothing

Review comment:
       I think we need to do something here. If startPrefixMapping is adding a 
new mapping endPrefixMapping needs to remove that, i.e.
   ```sclaa
   prefixMapping = prefixMapping.parent
   ```
   Note that according to SAX the order of endPrefixMapping doesn't necessarily 
match the order of startPrefixMapping. So using the ``prefix`` variable to 
check we're removing the right thing would require looking up the namespace 
mapping and removing it. And since NamespaceMappings are immutable, it makes it 
hard to remove a previous one. So I'd just recommend we remove the top, and as 
long as the XMLReader is doing the right thing we'll remove the right number of 
mappings.




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