olabusayoT commented on a change in pull request #436: URL: https://github.com/apache/incubator-daffodil/pull/436#discussion_r512007910
########## 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 Review comment: So unfortunately it appears to be implementation dependent when namespaces=false and namespace-prefixes=true (it is listed as 'unknown' behavior...which seems to me to be up for interpretation). In the true/true and true/false scenarios, they are explicitly not mixed. In the SAXParserFactory implementation we use for CLI and tests, it doesn't mix them in any scenarios, though. I just thought to play it safe since we are using a shared variable for both. Because if they are mixed, I think it would be duplicated information. There would be no reason to have it be separate information. ---------------------------------------------------------------- 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]
