Permalink
Browse files

Added support for loading a Saxon configuration file (--saxon-configu…

…ration option, cc:saxon-configuration setting, and saxon-configuration system property)
  • Loading branch information...
ndw committed Dec 8, 2011
1 parent 4a3795a commit f72b949541c47092c1d88576d677ff59aaafa0cb
@@ -15,6 +15,8 @@
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.NamePool;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
@@ -62,6 +64,7 @@
public String saxonProcessor = "he";
public boolean schemaAware = false;
+ public String saxonConfigFile = null;
public Hashtable<String,String> nsBindings = new Hashtable<String,String> ();
public boolean debug = false;
public Hashtable<String,Vector<ReadablePipe>> inputs = new Hashtable<String,Vector<ReadablePipe>> ();
@@ -98,10 +101,12 @@
private boolean firstOutput = false;
public XProcConfiguration() {
+ init("he", false, null);
+/*
cfgProcessor = new Processor(false);
loadConfiguration();
- if (schemaAware || !"he".equals(saxonProcessor)) {
+ if (schemaAware || !"he".equals(saxonProcessor) || saxonConfigFile != null) {
// Bugger. We have to restart with a schema-aware processor
nsBindings.clear();
inputs.clear();
@@ -120,15 +125,20 @@ public XProcConfiguration() {
loadConfiguration();
}
+*/
}
// This constructor is historical, the (String, boolean) constructor is preferred
public XProcConfiguration(boolean schemaAware) {
- init("he", schemaAware);
+ init("he", schemaAware, null);
+ }
+
+ public XProcConfiguration(String saxoncfg) {
+ init(null, false, saxoncfg);
}
public XProcConfiguration(String proctype, boolean schemaAware) {
- init(proctype, schemaAware);
+ init(proctype, schemaAware, null);
}
public XProcConfiguration(Processor processor) {
@@ -143,23 +153,58 @@ public Processor getProcessor() {
return cfgProcessor;
}
- private void init(String proctype, boolean schemaAware) {
+ private void init(String proctype, boolean schemaAware, String saxoncfg) {
if (schemaAware) {
proctype = "ee";
}
+
+ createSaxonProcessor(proctype, schemaAware, saxoncfg);
+ loadConfiguration();
+
+ if (!(proctype == null || saxonProcessor.equals(proctype)) || schemaAware != this.schemaAware ||
+ (saxoncfg == null && saxonConfigFile != null)) {
+ // Drat. We have to restart to get the right configuration.
+ nsBindings.clear();
+ inputs.clear();
+ outputs.clear();
+ params.clear();
+ options.clear();
+ implementations.clear();
+ extensionFunctions.clear();
+
+ createSaxonProcessor(saxonProcessor, this.schemaAware, saxonConfigFile);
+ loadConfiguration();
+ }
+
+ // 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();
+ this.saxonProcessor = cfgProcessor.getUnderlyingConfiguration().softwareEdition.toLowerCase();
+ }
+
+ private void createSaxonProcessor(String proctype, boolean schemaAware, String saxoncfg) {
boolean licensed = schemaAware || !"he".equals(proctype);
- cfgProcessor = new Processor(licensed);
- boolean sa = cfgProcessor.isSchemaAware();
+ if (saxoncfg != null) {
+ try {
+ InputStream instream = new FileInputStream(saxoncfg);
+ SAXSource source = new SAXSource(new InputSource(instream));
+ cfgProcessor = new Processor(source);
+ } catch (FileNotFoundException e) {
+ throw new XProcException(e);
+ } catch (SaxonApiException e) {
+ throw new XProcException(e);
+ }
+ } else {
+ cfgProcessor = new Processor(licensed);
+ }
String actualtype = cfgProcessor.getUnderlyingConfiguration().softwareEdition;
if ((proctype != null) && !"he".equals(proctype) && (!actualtype.toLowerCase().equals(proctype))) {
System.err.println("Failed to obtain " + proctype.toUpperCase() + " processor; using " + actualtype + " instead.");
}
-
- loadConfiguration();
}
-
+
private void loadConfiguration() {
URI home = URIUtils.homeAsURI();
URI cwd = URIUtils.cwdAsURI();
@@ -212,6 +257,8 @@ private void loadConfiguration() {
throw new XProcException("Invalid Saxon processor specified in com.xmlcalabash.saxon-processor property.");
}
+ saxonConfigFile = System.getProperty("com.xmlcalabash.saxon-configuration", saxonConfigFile);
+
schemaAware = "true".equals(System.getProperty("com.xmlcalabash.schema-aware", ""+schemaAware));
debug = "true".equals(System.getProperty("com.xmlcalabash.debug", ""+debug));
extensionValues = "true".equals(System.getProperty("com.xmlcalabash.general-values", ""+extensionValues));
@@ -295,6 +342,8 @@ public void parse(XdmNode doc) {
parseImplementation(node);
} else if ("saxon-processor".equals(localName)) {
parseSaxonProcessor(node);
+ } else if ("saxon-configuration".equals(localName)) {
+ parseSaxonConfiguration(node);
} else if ("schema-aware".equals(localName)) {
parseSchemaAware(node);
} else if ("namespace-binding".equals(localName)) {
@@ -393,6 +442,11 @@ private void parseSaxonProcessor(XdmNode node) {
saxonProcessor = value;
}
+ private void parseSaxonConfiguration(XdmNode node) {
+ String value = node.getStringValue().trim();
+ saxonConfigFile = value;
+ }
+
private void parseSchemaAware(XdmNode node) {
String value = node.getStringValue().trim();
@@ -90,6 +90,15 @@ public void run(String[] args) throws SaxonApiException, IOException, URISyntaxE
usage();
}
+ if (cmd.saxonConfigFile != null) {
+ if (cmd.schemaAware) {
+ throw new XProcException("Specifying schema-aware processing is an error if you specify a Saxon configuration file.");
+ }
+ if (cmd.saxonProcessor != null) {
+ throw new XProcException("Specifying a processor type is an error if you specify a Saxon configuration file.");
+ }
+ }
+
try {
XProcConfiguration config = null;
@@ -100,7 +109,9 @@ public void run(String[] args) throws SaxonApiException, IOException, URISyntaxE
proc = "ee";
}
- if (proc != null) {
+ if (cmd.saxonConfigFile != null) {
+ config = new XProcConfiguration(cmd.saxonConfigFile);
+ } else if (proc != null) {
config = new XProcConfiguration(proc, cmd.schemaAware);
} else {
config = new XProcConfiguration();
@@ -27,6 +27,7 @@
public boolean debug = false;
public String saxonProcessor = null;
+ public String saxonConfigFile = null;
public boolean schemaAwareExplicit = false;
public boolean schemaAware = false;
@@ -75,6 +76,11 @@ public void parse(String[] args) {
continue;
}
+ if (arg.startsWith("--saxon-configuration")) {
+ saxonConfigFile = parseString(null, "saxon-configuration");
+ continue;
+ }
+
if (arg.startsWith("-a") || arg.startsWith("--schema-aware")) {
schemaAware = parseBoolean("a","schema-aware");
schemaAwareExplicit = true;
@@ -442,23 +448,26 @@ private boolean parseBoolean(String shortName, String longName) {
}
private String parseString(String shortName, String longName) {
- String sOpt = "-" + shortName;
- String lOpt = "--" + longName;
String value = null;
- if (arg.startsWith(sOpt)) {
- if (arg.equals(sOpt)) {
- value = args[++argpos];
- arg = null;
- argpos++;
- } else {
- value = arg.substring(2);
- arg = null;
- argpos++;
+ if (shortName != null) {
+ String sOpt = "-" + shortName;
+ if (arg.startsWith(sOpt)) {
+ if (arg.equals(sOpt)) {
+ value = args[++argpos];
+ arg = null;
+ argpos++;
+ } else {
+ value = arg.substring(2);
+ arg = null;
+ argpos++;
+ }
+ return value;
}
- return value;
}
+ String lOpt = "--" + longName;
+
if (arg.equals(lOpt)) {
value = args[++argpos];
arg = null;

0 comments on commit f72b949

Please sign in to comment.