import java.util.Map;
public class CalabashHelper {
private static Source run(final Log log, final String pipelineURI, final InputSource inputSource, final Map<String, Object> map) throws FileNotFoundException {
XProcMessageListener messageListener = log != null ? new MavenXProcMessageListener(log) : null;
Pipeline pipeline = new CalabashPipelineBuilder(false, true, messageListener).build(pipelineURI);
// <c:param-set xmlns:c="http://www.w3.org/ns/xproc-step">
// <c:param name="username" namespace="" value="user"/>
// <c:param name="host" namespace="" value="http://example.com/"/>
// <c:param name="password" namespace="" value="pass"/>
// </c:param-set>
StringBuffer strBuff = new StringBuffer("<c:param-set xmlns:c=\"http://www.w3.org/ns/xproc-step\">");
strBuff.append("");
if(null!=map){
for (Map.Entry<String, Object> entry : map.entrySet()) {
String rawValue;
if (entry.getValue() instanceof File) {
rawValue = ((File)entry.getValue()).toURI().toString();
} else if (entry.getValue() instanceof URI || entry.getValue() instanceof String) {
rawValue = entry.getValue().toString();
} else if (entry.getValue() instanceof URL) {
rawValue = ((URL)entry.getValue()).toExternalForm();
} else if (entry.getValue() != null) {
throw new UnsupportedOperationException(String.format("The map cannot contain values of type %s.", entry.getValue().getClass()));
} else {
// ignore nulls
continue;
}
strBuff
.append("<c:param name=\"")
.append(escapeXmlAttribute(entry.getKey()))
.append("\" namespace=\"\" value=\"")
.append(escapeXmlAttribute(rawValue))
.append("\"/>");
}
}
strBuff.append("</c:param-set>");
String params=strBuff.toString();
final InputStream paramsStream = new ByteArrayInputStream(params.getBytes());
//System.out.println("~!~!~!~!~!~!~!~!~!Sending: \n"+params+"\n");
List<PipelineInput<?>> pipelineInputs = new ArrayList<PipelineInput<?>>() {{
add(PipelineInput.port("source", inputSource));
add(PipelineInput.port("parameters", new InputSource(paramsStream)));
}};
pipeline.run(pipelineInputs);
List<Source> sources = pipeline.getResultPort("result"); // result of xinclude;
return sources.get(0);
}