if (userArgs.isShowVersion()) {
XProcConfiguration.showVersion(runtime);
}
XPipeline pipeline = null;
if (userArgs.getPipeline() != null) {
pipeline = runtime.load(userArgs.getPipeline());
} else if (userArgs.hasImplicitPipeline()) {
XdmNode implicitPipeline = userArgs.getImplicitPipeline(runtime);
if (debug) {
System.err.println("Implicit pipeline:");
Serializer serializer = new Serializer();
serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
serializer.setOutputStream(System.err);
S9apiUtils.serialize(runtime, implicitPipeline, serializer);
}
pipeline = runtime.use(implicitPipeline);
} else if (config.pipeline != null) {
XdmNode doc = config.pipeline.read();
pipeline = runtime.use(doc);
} else {
throw new UnsupportedOperationException("Either a pipeline or libraries and / or steps must be given");
}
// Process parameters from the configuration...
for (String port : config.params.keySet()) {
Map<QName, String> parameters = config.params.get(port);
setParametersOnPipeline(pipeline, port, parameters);
}
// Now process parameters from the command line...
for (String port : userArgs.getParameterPorts()) {
Map<QName, String> parameters = userArgs.getParameters(port);
setParametersOnPipeline(pipeline, port, parameters);
}
Set<String> ports = pipeline.getInputs();
Set<String> userArgsInputPorts = userArgs.getInputPorts();
Set<String> cfgInputPorts = config.inputs.keySet();
Set<String> allPorts = new HashSet<String>();
allPorts.addAll(userArgsInputPorts);
allPorts.addAll(cfgInputPorts);
// map a given input without port specification to the primary non-parameter input implicitly
for (String port : ports) {
if (!allPorts.contains(port) && allPorts.contains(null)
&& pipeline.getDeclareStep().getInput(port).getPrimary()
&& !pipeline.getDeclareStep().getInput(port).getParameterInput()) {
if (userArgsInputPorts.contains(null)) {
userArgs.setDefaultInputPort(port);
allPorts.remove(null);
allPorts.add(port);
}
break;
}
}
for (String port : allPorts) {
if (!ports.contains(port)) {
throw new XProcException("There is a binding for the port '" + port + "' but the pipeline declares no such port.");
}
pipeline.clearInputs(port);
if (userArgsInputPorts.contains(port)) {
XdmNode doc = null;
for (Input input : userArgs.getInputs(port)) {
switch (input.getType()) {
case XML:
switch (input.getKind()) {
case URI:
String uri = input.getUri();
if ("-".equals(uri)) {
doc = runtime.parse(new InputSource(System.in));
} else {
doc = runtime.parse(new InputSource(uri));
}
break;
case INPUT_STREAM:
InputStream inputStream = input.getInputStream();
try {
doc = runtime.parse(new InputSource(inputStream));
} finally {
Closer.close(inputStream);
}
break;
default:
throw new UnsupportedOperationException(format("Unsupported input kind '%s'", input.getKind()));
}
break;
case DATA:
ReadableData rd;
switch (input.getKind()) {
case URI:
rd = new ReadableData(runtime, c_data, input.getUri(), input.getContentType());
doc = rd.read();
break;
case INPUT_STREAM:
InputStream inputStream = input.getInputStream();
try {
rd = new ReadableData(runtime, c_data, inputStream, input.getContentType());
doc = rd.read();
} finally {
Closer.close(inputStream);
}
break;
default:
throw new UnsupportedOperationException(format("Unsupported input kind '%s'", input.getKind()));
}
break;
default:
throw new UnsupportedOperationException(format("Unsupported input type '%s'", input.getType()));
}
pipeline.writeTo(port, doc);
}
} else {
for (ReadablePipe pipe : config.inputs.get(port)) {
XdmNode doc = pipe.read();
pipeline.writeTo(port, doc);
}
}
}
// Implicit binding for stdin?
String implicitPort = null;
for (String port : ports) {
if (!allPorts.contains(port)) {
if (pipeline.getDeclareStep().getInput(port).getPrimary()
&& !pipeline.getDeclareStep().getInput(port).getParameterInput()) {
implicitPort = port;
}
}
}
if (implicitPort != null && !pipeline.hasReadablePipes(implicitPort)) {
XdmNode doc = runtime.parse(new InputSource(System.in));
pipeline.writeTo(implicitPort, doc);
}
Map<String, Output> portOutputs = new HashMap<String, Output>();
Map<String, Output> userArgsOutputs = userArgs.getOutputs();
for (String port : pipeline.getOutputs()) {
// Bind to "-" implicitly
Output output = null;
if (userArgsOutputs.containsKey(port)) {
output = userArgsOutputs.get(port);
} else if (config.outputs.containsKey(port)) {
output = new Output(config.outputs.get(port));
} else if (userArgsOutputs.containsKey(null)
&& pipeline.getDeclareStep().getOutput(port).getPrimary()) {
// Bind unnamed port to primary output port
output = userArgsOutputs.get(null);
}
// Look for explicit binding to "-"
if ((output != null) && (output.getKind() == Kind.URI) && "-".equals(output.getUri())) {
output = null;
}
portOutputs.put(port, output);
}
for (QName optname : config.options.keySet()) {
RuntimeValue value = new RuntimeValue(config.options.get(optname), null, null);
pipeline.passOption(optname, value);
}
for (QName optname : userArgs.getOptionNames()) {
RuntimeValue value = new RuntimeValue(userArgs.getOption(optname), null, null);
pipeline.passOption(optname, value);
}
pipeline.run();
for (String port : pipeline.getOutputs()) {
Output output;
if (portOutputs.containsKey(port)) {
output = portOutputs.get(port);
} else {
// You didn't bind it, and it isn't going to stdout, so it's going into the bit bucket.
continue;
}
if ((output == null) || ((output.getKind() == OUTPUT_STREAM) && System.out.equals(output.getOutputStream()))) {
logger.trace("Copy output from " + port + " to stdout");
} else {
switch (output.getKind()) {
case URI:
logger.trace("Copy output from " + port + " to " + output.getUri());
break;
case OUTPUT_STREAM:
String outputStreamClassName = output.getOutputStream().getClass().getName();
logger.trace("Copy output from " + port + " to " + outputStreamClassName + " stream");
break;
default:
throw new UnsupportedOperationException(format("Unsupported output kind '%s'", output.getKind()));
}
}
Serialization serial = pipeline.getSerialization(port);
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);
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));
// FIXME: if ("cdata-section-elements".equals(name)) serial.setCdataSectionElements();
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) {
wd = new WritableDocument(runtime, null, serial);
} else {
switch (output.getKind()) {
case URI:
URI furi = new URI(output.getUri());
String filename = furi.getPath();
FileOutputStream outfile = new FileOutputStream(filename);
wd = new WritableDocument(runtime, filename, serial, outfile);
break;
case OUTPUT_STREAM:
OutputStream outputStream = output.getOutputStream();
wd = new WritableDocument(runtime, null, serial, outputStream);
break;
default:
throw new UnsupportedOperationException(format("Unsupported output kind '%s'", output.getKind()));
}
}
try {
ReadablePipe rpipe = pipeline.readFrom(port);
while (rpipe.moreDocuments()) {
wd.write(rpipe.read());
}
} finally {
if (output != null) {