Skip to content

Commit

Permalink
Fix issue #208 by refactoring the initialization code; fix issue #209
Browse files Browse the repository at this point in the history
…by moving the property changes to apply only to processors constructed by XML Calabash
  • Loading branch information
ndw committed May 14, 2015
1 parent 4f0b073 commit 0f03059
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/main/java/com/xmlcalabash/core/XProcConfiguration.java
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions 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 = "<p:declare-step xmlns:p=\"http://www.w3.org/ns/xproc\" version=\"1.0\"\n" +
" xmlns:c=\"http://www.w3.org/ns/xproc-step\"\n" +
" xmlns:cx=\"http://xmlcalabash.com/ns/extensions\"\n" +
" xmlns:exf=\"http://exproc.org/standard/functions\"\n" +
" exclude-inline-prefixes=\"cx exf\"\n" +
" name=\"main\">\n" +
"<p:output port=\"result\"/>\n" +
"\n" +
"<p:identity>\n" +
" <p:input port=\"source\">\n" +
" <p:inline><doc/></p:inline>\n" +
" </p:input>\n" +
"</p:identity>\n" +
"\n" +
"</p:declare-step>\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();
}

}

0 comments on commit 0f03059

Please sign in to comment.