mbeckerle commented on a change in pull request #436: URL: https://github.com/apache/incubator-daffodil/pull/436#discussion_r508045435
########## File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala ########## @@ -0,0 +1,192 @@ +/* + * 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.net.URI +import java.net.URISyntaxException + +import scala.util.Try + +import org.apache.daffodil.api.DFDL +import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException +import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException +import org.apache.daffodil.dpath.NodeInfo +import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument +import org.apache.daffodil.util.Maybe.One +import org.apache.daffodil.util.MaybeBoolean +import org.apache.daffodil.util.Misc +import org.apache.daffodil.xml.XMLUtils + +/** + * The SAXInfosetInputter consumes infosetEvent objects from the DaffodilInputContentHandler class + * and converts it to an event that the Dataprocessor unparse can use. This class contains two + * infosetEvent objects that contain the current event the unparse method is processing, and the + * next event to be processed after. + * + * This class together with the DaffodilInputContentHandler use coroutines to ensure that only one event, + * at a time, is passed between the two classes. The following is the general process: + * + * - the run method is called, with a StartDocument event already loaded on the inputter's queue. + * This is collected and stored in the nextEvent method, and the inputter's next method is called + * to populate the currentEvent and load the nextEvent + * - The dp.unparse method is called, and it calls hasNext to make sure an event exists to be + * processed and then queries the currentEvent, after it is done with the currentEvent, it calls + * inputter.next to get the next event, and that copies the nextEvent into the currentEvent and + * transfser control to the contentHandler to load the nextEvent + * - That continues until the currentEvent contains an EndDocument event, at which point, the + * nextEvent is clear, endDocumentReceived is set to true and hasNext is set to false + * - This ends the unparse process, and the unparseResult and/or any Errors are set on the event, + * and we call resumeFinal passing along that element, terminating this thread and resuming the + * contentHandler for the last time. + * + * @param inputContentHandler producer coroutine that sends infosetEvent to this class + * @param dp dataprocessor that we use to kickstart the unparse process and that consumes the + * currentEvent + * @param output outputChannel of choice where the unparsed data is stored + */ +class SAXInfosetInputter( + inputContentHandler: DFDL.DaffodilInputContentHandler, + dp: DFDL.DataProcessor, + output: DFDL.Output) + extends InfosetInputter with DFDL.ConsumerCoroutine { + // allows support for converting relative URIs in data to absolute URIs, this is mainly use + // for TDML as the tests allow relative URIs. This can be set to true by calling the + // inputContentHandler.enableInputterUriAbsolutization() + var enableUriAbsolutization: Boolean = false + + var endDocumentReceived = false + val currentEvent: DFDL.SaxInfosetEvent = new DFDL.SaxInfosetEvent + val nextEvent: DFDL.SaxInfosetEvent = new DFDL.SaxInfosetEvent + + override def getEventType(): InfosetInputterEventType = currentEvent.eventType.orNull + + override def getLocalName(): String = currentEvent.localName.orNull + + override def getNamespaceURI(): String = currentEvent.namespaceURI.orNull + + override def getSimpleText(primType: NodeInfo.Kind): String = { + val res = if (currentEvent.simpleText.isDefined) { + currentEvent.simpleText.get + } else ( + throw new NonTextFoundInSimpleContentException(getLocalName()) + ) + primType match { + case _: NodeInfo.String.Kind => + val remapped = XMLUtils.remapPUAToXMLIllegalCharacters(res) + remapped + case _: NodeInfo.AnyURI.Kind if enableUriAbsolutization && res.nonEmpty => + val absUri = convertRelativeURItoAbsolute(res) + absUri + case _ => + res + } + } + + override def isNilled(): MaybeBoolean = { + val _isNilled = if (currentEvent.nilValue.isDefined) { + val nilValue = currentEvent.nilValue.get + if (nilValue == "true" || nilValue == "1") { + MaybeBoolean(true) + } else if (nilValue == "false" || nilValue == "0") { + MaybeBoolean(false) + } else { + throw new InvalidInfosetException("xsi:nil property is not a valid boolean: '" + nilValue + + "' for element " + getLocalName()) + } + } else { + MaybeBoolean.Nope + } + _isNilled + } + + //called without changing any state Review comment: Well, every iterator I've ever written, the action all takes place in hasNext(). The logic is always: ``` def hasNext() = { if (isPreBuffered) true else { tryToFillPreBuffer() // does all the work, sets isPreBuffered boolean accordingly isPreBuffered } } def next() = if (hasNext) { val result = preBuffer clearPreBuffer() result } else { .... exception ... } ``` ---------------------------------------------------------------- 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]
