Permalink
Browse files

Fix issue #224 by adding --serialize command-line parameter

  • Loading branch information...
ndw committed Sep 22, 2015
1 parent 9835600 commit 60fecce800016f91d81174e4da9039924a737198
@@ -375,8 +375,6 @@ boolean run(UserArgs userArgs, XProcConfiguration config) throws SaxonApiExcepti
if (serial == null) {
// Use the configuration options
- // FIXME: should each of these be considered separately?
- // FIXME: should there be command-line options to override these settings?
serial = new Serialization(runtime, pipeline.getNode()); // The node's a hack
for (String name : config.serializationOptions.keySet()) {
String value = config.serializationOptions.get(name);
@@ -400,6 +398,37 @@ boolean run(UserArgs userArgs, XProcConfiguration config) throws SaxonApiExcepti
}
}
+ // Command line values override pipeline or configuration specified values
+ for (String name: new String[] {
+ "byte-order-mark", "escape-uri-attributes", "include-content-type",
+ "indent", "omit-xml-declaration", "undeclare-prefixes", "method",
+ "doctype-public", "doctype-system", "encoding", "media-type",
+ "normalization-form", "standalone", "version" }) {
+ String value = userArgs.getSerializationParameter(port, name);
+ if (value == null) {
+ value = userArgs.getSerializationParameter(name);
+ if (value == null) {
+ continue;
+ }
+ }
+
+ if ("byte-order-mark".equals(name)) serial.setByteOrderMark("true".equals(value));
+ if ("escape-uri-attributes".equals(name)) serial.setEscapeURIAttributes("true".equals(value));
+ if ("include-content-type".equals(name)) serial.setIncludeContentType("true".equals(value));
+ if ("indent".equals(name)) serial.setIndent("true".equals(value));
+ if ("omit-xml-declaration".equals(name)) serial.setOmitXMLDeclaration("true".equals(value));
+ if ("undeclare-prefixes".equals(name)) serial.setUndeclarePrefixes("true".equals(value));
+ if ("method".equals(name)) serial.setMethod(new QName("", value));
+ // N.B. cdata-section-elements isn't allowed
+ if ("doctype-public".equals(name)) serial.setDoctypePublic(value);
+ if ("doctype-system".equals(name)) serial.setDoctypeSystem(value);
+ if ("encoding".equals(name)) serial.setEncoding(value);
+ if ("media-type".equals(name)) serial.setMediaType(value);
+ if ("normalization-form".equals(name)) serial.setNormalizationForm(value);
+ if ("standalone".equals(name)) serial.setStandalone(value);
+ if ("version".equals(name)) serial.setVersion(value);
+ }
+
// I wonder if there's a better way...
WritableDocument wd = null;
if (output == null) {
@@ -165,6 +165,27 @@ public UserArgs parse(String[] args) {
continue;
}
+ if (arg.startsWith("--serialize")) {
+ String ser = parseString(null, "serialize");
+ String port = null;
+ String param = null;
+ String value = null;
+ int pos = ser.indexOf('=');
+ if (pos < 0) {
+ throw new XProcException("Invalid serialization parameter: " + ser);
+ }
+ param = ser.substring(0,pos);
+ value = ser.substring(pos+1);
+ pos = param.indexOf(':');
+ if (pos >= 0) {
+ port = param.substring(0, pos);
+ param = param.substring(pos+1);
+ }
+
+ userArgs.setSerializationParameter(port, param, value);
+ continue;
+ }
+
if (arg.startsWith("-")) {
throw new XProcException("Unrecognized option: '" + arg + "'.");
}
@@ -92,6 +92,7 @@
protected String jsonFlavor = null;
protected Integer piperackPort = null;
protected Integer piperackExpires = null;
+ protected Map<String,String> serParams = new HashMap<String, String> ();
public void setDebug(boolean debug) {
this.debug = debug;
@@ -428,6 +429,46 @@ public void setJsonFlavor(String jsonFlavor) {
}
}
+ public void setSerializationParameter(String port, String param, String value) {
+ if (port == null) {
+ port = "*";
+ }
+ if (param.equals("byte-order-mark")
+ || param.equals("escape-uri-attributes")
+ || param.equals("include-content-type")
+ || param.equals("indent")
+ || param.equals("omit-xml-declaration")
+ || param.equals("undeclare-prefixes")
+ || param.equals("method")
+ || param.equals("doctype-public")
+ || param.equals("doctype-system")
+ || param.equals("encoding")
+ || param.equals("media-type")
+ || param.equals("normalization-form")
+ || param.equals("standalone")
+ || param.equals("version")) {
+ serParams.put(port + ":" + param, value);
+ } else {
+ throw new XProcException("Unsupported or unrecognized serialization parameter: " + param);
+ }
+ }
+
+ public String getSerializationParameter(String port, String param) {
+ if (serParams.containsKey(port + ":" + param)) {
+ return serParams.get(port + ":" + param);
+ } else {
+ return null;
+ }
+ }
+
+ public String getSerializationParameter(String param) {
+ if (serParams.containsKey("*:" + param)) {
+ return serParams.get("*:" + param);
+ } else {
+ return null;
+ }
+ }
+
/**
* This method does some sanity checks and should be called at the
* beginning of every public method that has a return value to make

0 comments on commit 60fecce

Please sign in to comment.