From 0f03059c394b830d2f2891916d783e389a92925e Mon Sep 17 00:00:00 2001 From: Norman Walsh Date: Thu, 14 May 2015 18:37:10 -0500 Subject: [PATCH] Fix issue #208 by refactoring the initialization code; fix issue #209 by moving the property changes to apply only to processors constructed by XML Calabash --- .../xmlcalabash/core/XProcConfiguration.java | 33 +++++++---- .../com/xmlcalabash/drivers/EmbeddedTest.java | 59 +++++++++++++++++++ 2 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/xmlcalabash/drivers/EmbeddedTest.java diff --git a/src/main/java/com/xmlcalabash/core/XProcConfiguration.java b/src/main/java/com/xmlcalabash/core/XProcConfiguration.java index 0d6e4743..3c30619c 100644 --- a/src/main/java/com/xmlcalabash/core/XProcConfiguration.java +++ b/src/main/java/com/xmlcalabash/core/XProcConfiguration.java @@ -117,37 +117,44 @@ public class XProcConfiguration { private boolean firstOutput = false; public XProcConfiguration() { - init("he", false, null); + logger = LoggerFactory.getLogger(this.getClass()); + initSaxonProcessor("he", false, null); } // This constructor is historical, the (String, boolean) constructor is preferred public XProcConfiguration(boolean schemaAware) { - init("he", schemaAware, null); + logger = LoggerFactory.getLogger(this.getClass()); + initSaxonProcessor("he", schemaAware, null); + init(); } public XProcConfiguration(Input saxoncfg) { - init(null, false, saxoncfg); + logger = LoggerFactory.getLogger(this.getClass()); + initSaxonProcessor(null, false, saxoncfg); + init(); } public XProcConfiguration(String proctype, boolean schemaAware) { - init(proctype, schemaAware, null); + logger = LoggerFactory.getLogger(this.getClass()); + initSaxonProcessor(proctype, schemaAware, null); + init(); } public XProcConfiguration(Processor processor) { + logger = LoggerFactory.getLogger(this.getClass()); cfgProcessor = processor; loadConfiguration(); if (schemaAware != processor.isSchemaAware()) { throw new XProcException("Schema awareness in configuration conflicts with specified processor."); } + init(); } public Processor getProcessor() { return cfgProcessor; } - private void init(String proctype, boolean schemaAware, Input saxoncfg) { - logger = LoggerFactory.getLogger(this.getClass()); - + private void initSaxonProcessor(String proctype, boolean schemaAware, Input saxoncfg) { if (schemaAware) { proctype = "ee"; } @@ -186,7 +193,13 @@ private void init(String proctype, boolean schemaAware, Input saxoncfg) { this.schemaAware = cfgProcessor.isSchemaAware(); saxonProcessor = Configuration.softwareEdition.toLowerCase(); } + } + private void init() { + // If we got a schema aware processor, make sure it's reflected in our config + // FIXME: are there other things that should be reflected this way? + this.schemaAware = cfgProcessor.isSchemaAware(); + saxonProcessor = Configuration.softwareEdition.toLowerCase(); findStepClasses(); findExtensionFunctions(); } @@ -222,6 +235,9 @@ private void createSaxonProcessor(String proctype, boolean schemaAware, Input sa cfgProcessor = new Processor(licensed); } + cfgProcessor.getUnderlyingConfiguration().setStripsAllWhiteSpace(false); + cfgProcessor.getUnderlyingConfiguration().setStripsWhiteSpace(Whitespace.NONE); + String actualtype = Configuration.softwareEdition; if ((proctype != null) && !"he".equals(proctype) && (!actualtype.toLowerCase().equals(proctype))) { System.err.println("Failed to obtain " + proctype.toUpperCase() + " processor; using " + actualtype + " instead."); @@ -275,9 +291,6 @@ private void loadConfiguration() { URI cwd = URIUtils.cwdAsURI(); URI puri = home; - cfgProcessor.getUnderlyingConfiguration().setStripsAllWhiteSpace(false); - cfgProcessor.getUnderlyingConfiguration().setStripsWhiteSpace(Whitespace.NONE); - String cfg = System.getProperty("com.xmlcalabash.config.global"); try { InputStream instream = null; diff --git a/src/test/java/com/xmlcalabash/drivers/EmbeddedTest.java b/src/test/java/com/xmlcalabash/drivers/EmbeddedTest.java new file mode 100644 index 00000000..36b28633 --- /dev/null +++ b/src/test/java/com/xmlcalabash/drivers/EmbeddedTest.java @@ -0,0 +1,59 @@ +package com.xmlcalabash.drivers; + +import com.xmlcalabash.core.XProcConfiguration; +import com.xmlcalabash.core.XProcRuntime; +import com.xmlcalabash.runtime.XPipeline; +import net.sf.saxon.Configuration; +import net.sf.saxon.s9api.DocumentBuilder; +import net.sf.saxon.s9api.Processor; +import net.sf.saxon.s9api.SaxonApiException; +import net.sf.saxon.s9api.XdmNode; +import org.xml.sax.InputSource; + +import javax.xml.transform.sax.SAXSource; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; + +/** + * Created by ndw on 5/14/15. + * + * This class attempts to test how XML Calabash performs when it's embedded in other applications + */ +public class EmbeddedTest { + String pipeline_xml = "\n" + + "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "\n" + + "\n" + + "\n"; + + public static void main(String[] args) throws SaxonApiException, IOException, URISyntaxException { + EmbeddedTest test = new EmbeddedTest(); + test.run(); + } + + public void run() throws SaxonApiException { + Processor saxon = new Processor(false); + XProcConfiguration config = new XProcConfiguration(saxon); + XProcRuntime runtime = new XProcRuntime(config); + + InputStream stream = new ByteArrayInputStream(pipeline_xml.getBytes()); + DocumentBuilder builder = saxon.newDocumentBuilder(); + XdmNode pipeline_doc = builder.build(new SAXSource(new InputSource(stream))); + + XPipeline pipeline = runtime.use(pipeline_doc); + pipeline.run(); + } + +}