Skip to content

Commit

Permalink
Merged in feature/PI3-20-toevoegen-metadata-bestand-aan-ou (pull requ…
Browse files Browse the repository at this point in the history
…est #5)

Feature/PI3-20 toevoegen metadata bestand aan ou
  • Loading branch information
PaulRambags committed Oct 6, 2017
2 parents 77e931e + 560ccba commit d130bd0
Show file tree
Hide file tree
Showing 40 changed files with 559 additions and 49 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.daisy.pipeline.modules.braille</groupId>
<artifactId>braille-modules-parent</artifactId>
<version>1.10.1</version>
<version>1.10.2-SNAPSHOT</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -133,6 +133,7 @@
<configuration>
<instructions>
<_dsannotations>
nl.dedicon.pipeline.braille.calabash.impl.MetadataStep$Provider,
nl.dedicon.pipeline.braille.calabash.impl.SymbolsListStep$Provider,
nl.dedicon.pipeline.braille.impl.DediconTranslator$Provider,
nl.dedicon.pipeline.braille.impl.TablePath
Expand Down
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.runtime.XAtomicStep;
import java.io.IOException;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import nl.dedicon.pipeline.braille.model.Symbol;
import nl.dedicon.pipeline.braille.symbolslist.SymbolsReplacer;
import nl.dedicon.pipeline.braille.symbolslist.Utils;
import org.daisy.common.xproc.calabash.XProcStepProvider;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
* XProc step for the symbols list
Expand Down Expand Up @@ -70,8 +65,6 @@ public void run() throws SaxonApiException {

XdmNode book = source.read();

info(book, "Including symbols list");

String symbolsCode = getOption(_symbols_code, "");
String header = getOption(_symbols_list_header, "");

Expand Down
73 changes: 62 additions & 11 deletions src/main/resources/xml/xproc/dtbook-to-pef.xpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<p:option name="stylesheet" select="'http://www.dedicon.nl/pipeline/modules/braille/default.scss'"/>
<p:option name="include-preview" select="'true'"/>
<p:option name="include-brf" select="'true'"/>
<p:option name="include-metadata" required="false" px:type="boolean" select="'true'">
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h2 px:role="name">Include metadata file</h2>
</p:documentation>
</p:option>
<p:option name="include-obfl" select="'false'"/>
<p:option name="ascii-file-format" select="'(table:&quot;com_braillo.BrailloTableProvider.TableType.BRAILLO_6DOT_031_01&quot;)(line-breaks:dos)(pad:both)(charset:&quot;IBM00858&quot;)(file-extension:&quot;.brl&quot;)'"/>
<p:option name="ascii-table"/>
Expand Down Expand Up @@ -179,6 +184,7 @@
ascii-table
include-brf
include-preview
include-obfl
pef-output-dir
brf-output-dir
preview-output-dir
Expand Down Expand Up @@ -246,6 +252,7 @@
<dedicon:src-backslash-fix/>
<p:choose>
<p:when test="$include-symbols-list='true'">
<px:message message="Including symbols list"/>
<dedicon:symbols-list>
<p:with-option name="symbols-code" select="$symbols-code"/>
<p:with-option name="symbols-list-header" select="$symbols-list-header"/>
Expand All @@ -272,25 +279,34 @@
<!-- DTBOOK TO PEF -->
<!-- ============= -->
<px:dtbook-to-pef.convert default-stylesheet="http://www.daisy.org/pipeline/modules/braille/dtbook-to-pef/css/default.css"
transform="(formatter:dotify)(translator:dedicon)">
transform="(formatter:dotify)(translator:dedicon)"
name="convert">
<p:with-option name="temp-dir" select="string(/c:result)">
<p:pipe step="temp-dir" port="result"/>
</p:with-option>
<p:with-option name="stylesheet" select="$stylesheet"/>
<p:with-option name="include-obfl" select="$include-obfl"/>
<p:input port="parameters">
<p:pipe port="result" step="input-options"/>
</p:input>
</px:dtbook-to-pef.convert>

<!-- ========= -->
<!-- STORE PEF -->
<!-- ========= -->
<!-- ===== -->
<!-- STORE -->
<!-- ===== -->
<p:group>
<p:variable name="name" select="if (//dtb:meta[@name='dc:Identifier']/@content)
then //dtb:meta[@name='dc:Identifier']/@content
else replace(p:base-uri(/),'^.*/([^/]*)\.[^/\.]*$','$1')">
<p:variable name="identifier" select="//dtb:meta[@name='dc:Identifier']/@content">
<p:pipe step="main" port="source"/>
</p:variable>
<p:variable name="name" select="if ($identifier) then $identifier
else replace(p:base-uri(/),'^.*/([^/]*)\.[^/\.]*$','$1')">
<p:pipe step="main" port="source"/>
</p:variable>
<p:variable name="brf-file-format" select="$ascii-file-format"/>
<p:variable name="brf-name-pattern" select="concat('p',$name,'_{}')"/>
<p:variable name="brf-number-width" select="3"/>

<!-- Store PEF, BRLs -->
<pef:store>
<p:with-option name="href" select="concat($pef-output-dir,'/',$name,'.pef')"/>
<p:with-option name="preview-href" select="if ($include-preview='true' and $preview-output-dir!='')
Expand All @@ -309,10 +325,45 @@
else ''
else ''
"/>
<p:with-option name="brf-file-format" select="$ascii-file-format"/>
<p:with-option name="brf-name-pattern" select="concat('p',$name,'_{}')"/>
<p:with-option name="brf-number-width" select="'3'"/>
<p:with-option name="brf-file-format" select="$brf-file-format"/>
<p:with-option name="brf-name-pattern" select="$brf-name-pattern"/>
<p:with-option name="brf-number-width" select="$brf-number-width"/>
</pef:store>

<!-- Store METADATA -->
<p:choose>
<p:when test="$include-metadata='true'">
<px:message message="Storing metadata"/>
<dedicon:metadata>
<p:input port="source">
<p:pipe port="result" step="convert"/>
</p:input>
<p:with-option name="xquery" select="resolve-uri('../../xquery/metadata.xql')"/>
<p:with-option name="identifier" select="$identifier"/>
<p:with-option name="brf-file-format" select="$brf-file-format"/>
<p:with-option name="brf-name-pattern" select="$brf-name-pattern"/>
<p:with-option name="brf-number-width" select="$brf-number-width"/>
</dedicon:metadata>
<p:store>
<p:with-option name="href" select="concat($pef-output-dir, '/m', $name, '_001.xml')"/>
</p:store>
</p:when>
<p:otherwise>
<px:message message="Skipping metadata"/>
<p:sink/>
</p:otherwise>
</p:choose>

<!-- Store OBFL -->
<p:for-each>
<p:iteration-source>
<p:pipe step="convert" port="obfl"/>
</p:iteration-source>
<px:message message="Storing OBFL"/>
<p:store>
<p:with-option name="href" select="concat($pef-output-dir,'/',$name,'.obfl')"/>
</p:store>
</p:for-each>
</p:group>

</p:declare-step>
1 change: 1 addition & 0 deletions src/main/resources/xml/xproc/library.xpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<p:library xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0">

<p:import href="metadata.xpl"/>
<p:import href="symbols-list.xpl"/>
<p:import href="pre-processing.xpl"/>
<p:import href="src-backslash-fix.xpl"/>
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/xml/xproc/metadata.xpl
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>
Loading

0 comments on commit d130bd0

Please sign in to comment.