}
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(runtime.getStaticBaseURI());
tree.addStartElement(p_declare_step);
tree.addAttribute(new QName("version"), "1.0");
tree.startContent();
tree.addStartElement(p_input);
tree.addAttribute(new QName("port"), "source");
tree.addAttribute(new QName("sequence"), "true");
tree.startContent();
tree.addEndElement();
tree.addStartElement(p_input);
tree.addAttribute(new QName("port"), "parameters");
tree.addAttribute(new QName("kind"), "parameter");
tree.startContent();
tree.addEndElement();
// This is a hack too. If there are no outputs, fake one.
// Implicit pipelines default to having a single primary output port names "result"
if (outputs.size() == 0) {
outputs.put("result", new Output("-"));
}
String lastStepName = "cmdlineStep" + steps.size();
for (String port : outputs.keySet()) {
if (port == null) {
port = "result";
}
tree.addStartElement(p_output);
tree.addAttribute(new QName("port"), port);
tree.startContent();
tree.addStartElement(p_pipe);
tree.addAttribute(new QName("step"), lastStepName);
tree.addAttribute(new QName("port"), port);
tree.startContent();
tree.addEndElement();
tree.addEndElement();
}
for (Input library : libraries) {
switch (library.getKind()) {
case URI:
tree.addStartElement(p_import);
tree.addAttribute(new QName("href"), library.getUri());
tree.startContent();
tree.addEndElement();
break;
case INPUT_STREAM:
InputStream libraryInputStream = library.getInputStream();
FileOutputStream fileOutputStream = null;
try {
File tempLibrary = createTempFile("calabashLibrary", null);
tempLibrary.deleteOnExit();
fileOutputStream = new FileOutputStream(tempLibrary);
fileOutputStream.getChannel().transferFrom(newChannel(libraryInputStream), 0, MAX_VALUE);
tree.addStartElement(p_import);
tree.addAttribute(new QName("href"), tempLibrary.toURI().toASCIIString());
tree.startContent();
tree.addEndElement();
} finally {
Closer.close(fileOutputStream);
libraryInputStream.close();
}
break;
default:
throw new UnsupportedOperationException(format("Unsupported library kind '%s'", library.getKind()));
}
}
int stepNum = 0;
for (StepArgs step : steps) {
stepNum ++;
tree.addStartElement(step.stepName);
tree.addAttribute(new QName("name"), "cmdlineStep" + stepNum);
for (QName optname : step.options.keySet()) {
tree.addAttribute(optname, step.options.get(optname));
}
tree.startContent();
for (String port : step.inputs.keySet()) {
tree.addStartElement(p_input);
tree.addAttribute(new QName("port"), (port == null) ? "source" : port);
tree.startContent();
for (Input input : step.inputs.get(port)) {
QName qname = (input.getType() == DATA) ? p_data : p_document;
switch (input.getKind()) {
case URI:
String uri = input.getUri();
if ("p:empty".equals(uri)) {
tree.addStartElement(p_empty);
tree.startContent();
tree.addEndElement();
} else {
tree.addStartElement(qname);
tree.addAttribute(new QName("href"), uri);
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
}
tree.startContent();
tree.addEndElement();
}
break;
case INPUT_STREAM:
InputStream inputStream = input.getInputStream();
if (System.in.equals(inputStream)) {
tree.addStartElement(qname);
tree.addAttribute(new QName("href"), "-");
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
}
tree.startContent();
tree.addEndElement();
} else {
FileOutputStream fileOutputStream = null;
try {
File tempInput = createTempFile("calabashInput", null);
tempInput.deleteOnExit();
fileOutputStream = new FileOutputStream(tempInput);
fileOutputStream.getChannel().transferFrom(newChannel(inputStream), 0, MAX_VALUE);
tree.addStartElement(qname);
tree.addAttribute(new QName("href"), tempInput.toURI().toASCIIString());
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
}
tree.startContent();
tree.addEndElement();
} finally {
Closer.close(fileOutputStream);
inputStream.close();
}
}
break;
default:
throw new UnsupportedOperationException(format("Unsupported input kind '%s'", input.getKind()));
}
}
tree.addEndElement();
}
for (String port : step.params.keySet()) {
for (QName pname : step.params.get(port).keySet()) {
String value = step.params.get(port).get(pname);
// Double single quotes to escape them between the enclosing single quotes
value = "'" + value.replace("'", "''") + "'";
tree.addStartElement(p_with_param);
if (!"*".equals(port)) {
tree.addAttribute(new QName("port"), port);
}
if (!pname.getPrefix().isEmpty() || !pname.getNamespaceURI().isEmpty()) {
tree.addNamespace(pname.getPrefix(), pname.getNamespaceURI());
}
tree.addAttribute(new QName("name"), pname.toString());
tree.addAttribute(new QName("select"), value);
tree.startContent();
tree.addEndElement();
}
}