org.daisy.common.xproc.calabash.impl.CalabashXProcPipeline Maven / Gradle / Ivy
The newest version!
package org.daisy.common.xproc.calabash.impl;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.xmlcalabash.core.XProcConfiguration;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcMessageListener;
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.model.SequenceType;
import com.xmlcalabash.runtime.XPipeline;
import com.xmlcalabash.util.XProcSystemPropertySet;
import net.sf.saxon.dom.NodeOverNodeInfo;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmValue;
import org.daisy.common.properties.Properties;
import org.daisy.common.saxon.SaxonHelper;
import org.daisy.common.saxon.SaxonInputValue;
import org.daisy.common.xproc.XProcError;
import org.daisy.common.xproc.XProcErrorException;
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.CalabashXProcError;
import org.daisy.common.xproc.calabash.XProcConfigurationFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
/**
* Calabash piplines allow to define and run xproc pipelines using calabash. The
* pipelines supplied by this class are reusable.
*/
public class CalabashXProcPipeline implements XProcPipeline {
private static final Logger logger = LoggerFactory.getLogger(CalabashXProcPipeline.class);
/** 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;
private final boolean AUTO_NAME_STEPS = Boolean.parseBoolean(
Properties.getProperty("org.daisy.pipeline.calabash.autonamesteps", "false"));
/** 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 pipeline = PipelineInstance.newInstance(uri,
configFactory.newConfiguration(null, null),
uriResolver,
entityResolver);
DeclareStep declaration = pipeline.xpipe.getDeclareStep();
// input and parameter ports
for (Input input : declaration.inputs()) {
if (!input.getParameterInput()) {
builder.withPort(
XProcPortInfo.newInputPort(
input.getPort(), input.getSequence(),
input.getBinding().isEmpty(), 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()) {
SequenceType sequenceType = option.getSequenceType();
if (sequenceType == null) sequenceType = SequenceType.XS_STRING;
builder.withOption(new CalabashXProcOptionInfo(option, sequenceType));
}
pipeline.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) {
this.uri = uri;
this.configFactory = configFactory;
this.uriResolver = uriResolver;
this.entityResolver = entityResolver;
}
/*
* (non-Javadoc)
*
* @see org.daisy.common.xproc.XProcPipeline#getInfo()
*/
@Override
public XProcPipelineInfo getInfo() {
return info.get();
}
@Override
public XProcResult run(XProcInput data) throws XProcErrorException {
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, Map properties) throws XProcErrorException {
MessageListenerImpl messageListener = monitor != null
? new MessageListenerImpl(monitor.getMessageAppender(), AUTO_NAME_STEPS)
: null;
try {
PipelineInstance pipeline = PipelineInstance.newInstance(uri,
configFactory.newConfiguration(monitor, properties),
uriResolver,
entityResolver);
if (messageListener != null)
((XProcMessageListenerAggregator)pipeline.xpipe.getStep().getXProc().getMessageListener())
.add(messageListener);
if (properties != null)
// for p:system-property() function
pipeline.runtime.addSystemPropertySet(
new XProcSystemPropertySet() {
@Override
public String systemProperty(XProcRuntime runtime, net.sf.saxon.s9api.QName propertyName) throws XProcException {
if ("http://www.daisy.org/ns/pipeline/data".equals(propertyName.getNamespaceURI())) {
String p = propertyName.getLocalName();
String v = properties.get(p);
if (v == null) // if property not settable
v = Properties.getProperty(p);
return v;
} else
return null; }});
// bind inputs
for (String name : pipeline.xpipe.getInputs()) {
boolean cleared = false;
for (Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy