org.daisy.common.xproc.calabash.impl.CalabashXProcPipeline Maven / Gradle / Ivy
package org.daisy.common.xproc.calabash.impl;
import java.net.URI;
import java.util.Properties;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import org.daisy.common.xproc.XProcInput;
import org.daisy.common.xproc.XProcMonitor;
import org.daisy.common.xproc.XProcOptionInfo;
import org.daisy.common.xproc.XProcPipeline;
import org.daisy.common.xproc.XProcPipelineInfo;
import org.daisy.common.xproc.XProcPortInfo;
import org.daisy.common.xproc.XProcResult;
import org.daisy.common.xproc.calabash.XProcConfigurationFactory;
import org.daisy.pipeline.event.EventBusProvider;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.xmlcalabash.core.XProcConfiguration;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.model.DeclareStep;
import com.xmlcalabash.model.Input;
import com.xmlcalabash.model.Option;
import com.xmlcalabash.model.Output;
import com.xmlcalabash.model.RuntimeValue;
import com.xmlcalabash.runtime.XPipeline;
/**
* Calabash piplines allow to define and run xproc pipelines using calabash. The
* pipelines supplied by this class are reusable.
*/
public class CalabashXProcPipeline implements XProcPipeline {
/** The uri. */
private final URI uri;
/** The config factory. */
private final XProcConfigurationFactory configFactory;
/** The uri resolver. */
private final URIResolver uriResolver;
/** The entity resolver. */
private final EntityResolver entityResolver;
/** The message listener factory. */
private final EventBusProvider eventBusProvider;
/**
* The pipeline supplier returns a ready-to-go pipeline instance based on
* the XProcPipeline object
*/
private final Supplier pipelineSupplier = new Supplier() {
/**
* configures the clone of the pipeline instance setting all the objects
* present in the XProcPipeline object
*/
@Override
public PipelineInstance get() {
XProcConfiguration config = configFactory.newConfiguration();
XProcRuntime runtime = new XProcRuntime(config);
runtime.setMessageListener(new slf4jXProcMessageListener());
if (uriResolver != null) {
runtime.setURIResolver(uriResolver);
}
if (entityResolver != null) {
runtime.setEntityResolver(entityResolver);
}
XProcMessageListenerAggregator listeners = new XProcMessageListenerAggregator();
listeners.add(new slf4jXProcMessageListener());
// TODO: get rid of asAccessor as from now on it will be available
// from the job monitor
// listeners.addAsAccessor(new MessageListenerWrapper(
// messageListenerFactory.createMessageListener()));
runtime.setMessageListener(listeners);
XPipeline xpipeline = null;
try {
xpipeline = runtime.load(new com.xmlcalabash.util.Input(uri.toString()));
} catch (SaxonApiException e) {
throw new RuntimeException(e.getMessage(), e);
}
return new PipelineInstance(xpipeline, config,runtime);
}
};
/** Suplies the current Pipeline info for this pipeline object */
private final Supplier info = Suppliers
.memoize(new Supplier() {
@Override
public XProcPipelineInfo get() {
XProcPipelineInfo.Builder builder = new XProcPipelineInfo.Builder();
builder.withURI(uri);
PipelineInstance instance=pipelineSupplier.get();
DeclareStep declaration = instance.xpipe.getDeclareStep();
// input and parameter ports
for (Input input : declaration.inputs()) {
if (!input.getParameterInput()) {
builder.withPort(XProcPortInfo.newInputPort(
input.getPort(), input.getSequence(),
input.getPrimary()));
} else {
builder.withPort(XProcPortInfo.newParameterPort(
input.getPort(), input.getPrimary()));
}
}
// output ports
for (Output output : declaration.outputs()) {
builder.withPort(XProcPortInfo.newOutputPort(
output.getPort(), output.getSequence(),
output.getPrimary()));
}
// options
for (Option option : declaration.options()) {
builder.withOption(new XProcOptionInfo(new QName(option
.getName().getNamespaceURI(), option.getName()
.getLocalName(), option.getName().getPrefix()),
option.getRequired(), option.getSelect()));
}
instance.runtime.close();
return builder.build();
}
});
/**
* Instantiates a new calabash x proc pipeline.
*
* @param uri
* the uri to load the xpl file
* @param configFactory
* the configuration factory
* @param uriResolver
* the uri resolver
* @param entityResolver
* the entity resolver
* @param messageListenerFactory
* the message listener factory used to process pipeline
* execution related messages
*/
public CalabashXProcPipeline(URI uri,
XProcConfigurationFactory configFactory, URIResolver uriResolver,
EntityResolver entityResolver,
EventBusProvider eventBusProvider) {
this.uri = uri;
this.configFactory = configFactory;
this.uriResolver = uriResolver;
this.entityResolver = entityResolver;
this.eventBusProvider=eventBusProvider;
}
/*
* (non-Javadoc)
*
* @see org.daisy.common.xproc.XProcPipeline#getInfo()
*/
@Override
public XProcPipelineInfo getInfo() {
return info.get();
}
@Override
public XProcResult run(XProcInput data) {
return run(data, null,null);
}
/*
* (non-Javadoc)
*
* @see
* org.daisy.common.xproc.XProcPipeline#run(org.daisy.common.xproc.XProcInput
* )
*/
@Override
public XProcResult run(XProcInput data, XProcMonitor monitor,Properties props) {
PipelineInstance pipeline = pipelineSupplier.get();
// monitor.setMessageAccessor(pipeline.messageAccessor);
((XProcMessageListenerAggregator) pipeline.xpipe.getStep().getXProc()
.getMessageListener()).add(new EventBusMessageListener(eventBusProvider,props));
// bind inputs
for (String name : pipeline.xpipe.getInputs()) {
for (Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy