olabusayoT commented on a change in pull request #436: URL: https://github.com/apache/incubator-daffodil/pull/436#discussion_r512207844
########## File path: daffodil-core/src/test/scala/org/apache/daffodil/processor/TestSAXUnparseAPI.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.processor + +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream + +import scala.xml.Elem + +import javax.xml.parsers.SAXParserFactory +import org.apache.daffodil.compiler.Compiler +import org.apache.daffodil.processors.DataProcessor +import org.apache.daffodil.util.SchemaUtils +import org.apache.daffodil.xml.XMLUtils +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Assert.fail +import org.junit.Test +import org.xml.sax.InputSource +import org.xml.sax.XMLReader + +object TestSAXUnparseAPI { + + lazy val testSchema: Elem = SchemaUtils.dfdlTestSchema( + <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>, + <dfdl:format ref="tns:GeneralFormat"/>, + <xs:element name="list" type="tns:example1"/> + <xs:complexType name="example1"> + <xs:sequence> + <xs:element name="w" type="xs:int" dfdl:length="1" dfdl:lengthKind="explicit" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + ) + lazy val testInfoset: Elem = + <list xmlns="http://example.com"><w>9</w><w>5</w><w>3</w><w>0</w></list> + lazy val testInfosetString: String = testInfoset.toString() + lazy val testData = "9530" + + lazy val dp: DataProcessor = testDataprocessor(testSchema) + lazy val xmlReader: XMLReader = SAXParserFactory.newInstance.newSAXParser.getXMLReader + + def testDataprocessor(testSchema: scala.xml.Elem): DataProcessor = { + val schemaCompiler = Compiler() + val pf = schemaCompiler.compileNode(testSchema) + if (pf.isError) { + val msgs = pf.getDiagnostics.map { _.getMessage() }.mkString("\n") + fail("pf compile errors: " + msgs) + } + pf.sset.root.erd.preSerialization // force evaluation of all compile-time constructs + val dp = pf.onPath("/").asInstanceOf[DataProcessor] + if (dp.isError) { + val msgs = dp.getDiagnostics.map { _.getMessage() }.mkString("\n") + fail("dp compile errors: " + msgs) + } + dp + } +} + +class TestSAXUnparseAPI { + import TestSAXUnparseAPI._ + + @Test def testUnparseContentHandler_unparse(): Unit = { + val bao = new ByteArrayOutputStream() + val wbc = java.nio.channels.Channels.newChannel(bao) + val unparseContentHandler = dp.newContentHandlerInstance(wbc) + xmlReader.setContentHandler(unparseContentHandler) + xmlReader.setFeature(XMLUtils.SAX_NAMESPACES_FEATURE, true) + xmlReader.setFeature(XMLUtils.SAX_NAMESPACE_PREFIXES_FEATURE, true) Review comment: You are right, XMLReaders are not threadsafe, and neither are SAXParserFactorys! Fixed ---------------------------------------------------------------- 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]
