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);