-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in feature/PI3-20-toevoegen-metadata-bestand-aan-ou (pull requ…
…est #5) Feature/PI3-20 toevoegen metadata bestand aan ou
- Loading branch information
Showing
40 changed files
with
559 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
src/main/java/nl/dedicon/pipeline/braille/calabash/impl/MetadataStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package nl.dedicon.pipeline.braille.calabash.impl; | ||
|
||
import com.xmlcalabash.core.XProcException; | ||
import com.xmlcalabash.core.XProcRuntime; | ||
import com.xmlcalabash.core.XProcStep; | ||
import com.xmlcalabash.io.ReadablePipe; | ||
import com.xmlcalabash.io.WritablePipe; | ||
import com.xmlcalabash.library.DefaultStep; | ||
import com.xmlcalabash.runtime.XAtomicStep; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.sf.saxon.s9api.QName; | ||
import net.sf.saxon.s9api.SaxonApiException; | ||
import net.sf.saxon.s9api.XQueryCompiler; | ||
import net.sf.saxon.s9api.XQueryEvaluator; | ||
import net.sf.saxon.s9api.XQueryExecutable; | ||
import net.sf.saxon.s9api.XdmAtomicValue; | ||
import net.sf.saxon.s9api.XdmNode; | ||
import net.sf.saxon.s9api.XdmValue; | ||
import org.daisy.braille.api.embosser.FileFormat; | ||
import org.daisy.common.xproc.calabash.XProcStepProvider; | ||
import static org.daisy.pipeline.braille.common.Provider.util.dispatch; | ||
import static org.daisy.pipeline.braille.common.Provider.util.memoize; | ||
import org.daisy.pipeline.braille.common.Provider.util.MemoizingProvider; | ||
import org.daisy.pipeline.braille.common.Query; | ||
import static org.daisy.pipeline.braille.common.Query.util.mutableQuery; | ||
import static org.daisy.pipeline.braille.common.Query.util.query; | ||
import org.daisy.pipeline.braille.pef.FileFormatProvider; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* XProc step for metadata | ||
* | ||
* @author Paul Rambags | ||
*/ | ||
public class MetadataStep extends DefaultStep { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MetadataStep.class); | ||
|
||
private static final QName _xquery = new QName("xquery"); | ||
private static final QName _identifier = new QName("identifier"); | ||
private static final QName _brf_file_extension = new QName("brf-file-extension"); | ||
private static final QName _brf_file_format = new QName("brf-file-format"); | ||
private static final QName _brf_name_pattern = new QName("brf-name-pattern"); | ||
private static final QName _brf_number_width = new QName("brf-number-width"); | ||
|
||
private final MemoizingProvider<Query,FileFormat> fileFormatProvider; | ||
|
||
private ReadablePipe source = null; | ||
private WritablePipe result = null; | ||
|
||
private MetadataStep(XProcRuntime runtime, XAtomicStep step, MemoizingProvider<Query,FileFormat> fileFormatProvider) { | ||
super(runtime, step); | ||
this.fileFormatProvider = fileFormatProvider; | ||
} | ||
|
||
@Override | ||
public void setInput(String port, ReadablePipe pipe) { | ||
source = pipe; | ||
} | ||
|
||
@Override | ||
public void setOutput(String port, WritablePipe pipe) { | ||
result = pipe; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
source.resetReader(); | ||
result.resetWriter(); | ||
} | ||
|
||
@Override | ||
public void run() throws SaxonApiException { | ||
super.run(); | ||
|
||
try { | ||
|
||
XdmNode pef = source.read(); | ||
|
||
String xquery = getOption(_xquery, ""); | ||
String identifier = getOption(_identifier, ""); | ||
String brfFileFormat = getOption(_brf_file_format, ""); | ||
String brfNamePattern = getOption(_brf_name_pattern, ""); | ||
int brfNumberWidth = getOption(_brf_number_width, 0); | ||
|
||
String brfFileExtension = getFileExtension(brfFileFormat); | ||
InputStream query = new URL(xquery).openConnection().getInputStream(); | ||
|
||
XQueryCompiler xqCompiler = runtime.getProcessor().newXQueryCompiler(); | ||
XQueryExecutable xqExecutable = xqCompiler.compile(query); | ||
XQueryEvaluator xqEvaluator = xqExecutable.load(); | ||
|
||
xqEvaluator.setSource(pef.asSource()); | ||
xqEvaluator.setExternalVariable(_identifier, new XdmAtomicValue(identifier)); | ||
xqEvaluator.setExternalVariable(_brf_name_pattern, new XdmAtomicValue(brfNamePattern)); | ||
xqEvaluator.setExternalVariable(_brf_number_width, new XdmAtomicValue(brfNumberWidth)); | ||
xqEvaluator.setExternalVariable(_brf_file_extension, new XdmAtomicValue(brfFileExtension)); | ||
|
||
XdmValue xqResult = xqEvaluator.evaluate(); | ||
|
||
// get the first node from the result | ||
XdmNode metadata = null; | ||
for (XdmValue xqValue : xqResult) { | ||
if (xqValue instanceof XdmNode) { | ||
metadata = (XdmNode)xqValue; | ||
break; | ||
} | ||
}; | ||
|
||
result.write(metadata); | ||
|
||
} catch (Exception e) { | ||
|
||
logger.error("dedicon:metadata failed", e); | ||
throw new XProcException(step.getNode(), e); | ||
|
||
} | ||
} | ||
|
||
private String getFileExtension (String fileFormatQuery) { | ||
Query.MutableQuery q = mutableQuery(query(fileFormatQuery)); | ||
Iterable<FileFormat> fileFormats = fileFormatProvider.get(q); | ||
String fileExtension = ""; | ||
for (FileFormat fileFormat : fileFormats) { | ||
fileExtension = fileFormat.getFileExtension(); | ||
break; | ||
} | ||
return fileExtension; | ||
} | ||
|
||
@Component( | ||
name = "dedicon:metadata", | ||
service = {XProcStepProvider.class}, | ||
property = {"type:String={http://www.dedicon.nl}metadata"} | ||
) | ||
public static class Provider implements XProcStepProvider { | ||
|
||
private List<FileFormatProvider> fileFormatProviders = new ArrayList<>(); | ||
private MemoizingProvider<Query,FileFormat> fileFormatProvider = memoize(dispatch(fileFormatProviders)); | ||
|
||
@Override | ||
public XProcStep newStep(XProcRuntime runtime, XAtomicStep step) { | ||
return new MetadataStep(runtime, step, fileFormatProvider); | ||
} | ||
|
||
@Reference( | ||
name = "FileFormatProvider", | ||
unbind = "unbindFileFormatProvider", | ||
service = FileFormatProvider.class, | ||
cardinality = ReferenceCardinality.MULTIPLE, | ||
policy = ReferencePolicy.DYNAMIC | ||
) | ||
protected void bindFileFormatProvider(FileFormatProvider provider) { | ||
fileFormatProviders.add(provider); | ||
} | ||
|
||
protected void unbindFileFormatProvider(FileFormatProvider provider) { | ||
fileFormatProviders.remove(provider); | ||
this.fileFormatProvider.invalidateCache(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<p:declare-step type="dedicon:metadata" | ||
xmlns:p="http://www.w3.org/ns/xproc" | ||
xmlns:px="http://www.daisy.org/ns/pipeline/xproc" | ||
xmlns:dedicon="http://www.dedicon.nl" | ||
version="1.0"> | ||
|
||
<p:input port="source" sequence="false" primary="true"/> | ||
<p:option name="xquery" required="true"/> | ||
<p:option name="identifier" required="false"/> | ||
<p:option name="brf-file-format" required="false"/> | ||
<p:option name="brf-name-pattern" required="false"/> | ||
<p:option name="brf-number-width" required="false"/> | ||
<p:output port="result" sequence="false" primary="true"/> | ||
|
||
</p:declare-step> |
Oops, something went wrong.