Skip to content
This repository has been archived by the owner on Aug 8, 2020. It is now read-only.

Make Transform filter accept the file paths of the stylesheet #103

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion UsersGuide.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ generates output XML documents one per one input document and pass them to sinks
|=================
| Attribute | Description | Required

|style|The URI of the XSLT stylesheet. If not absolute, will be resolved by the base directory of the task.| Yes
|style|The URI or the file path of the XSLT stylesheet. If not absolute, will be resolved by the base directory of the task.| Yes

|force|Whether this filter proceed to process even if existing output files seem new enough. The descendants (sinks, sinks' sinks, and so on) can not override this to +no+.| No; defaults to +no+

Expand Down
41 changes: 27 additions & 14 deletions src/net/furfurylic/chionographis/Transform.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -33,6 +34,8 @@
import org.apache.tools.ant.BuildException;
import org.xml.sax.ContentHandler;

import net.furfurylic.chionographis.Logger.Level;

/**
* An <i>Transform</i> filter transforms each source document into an document
* styled by an XSLT stylesheet.
Expand Down Expand Up @@ -70,12 +73,13 @@ public final class Transform extends Sink implements Driver {
}

/**
* Sets the URI of the XSLT stylesheet. If the given string represents a relative URI,
* it is resolved by {@linkplain Chionographis#setBaseDir(String)
* the base directory of the task} to a file path.
* Sets the URI or the file path of the XSLT stylesheet.
* If the given string represents relative, it is resolved by
* {@linkplain Chionographis#setBaseDir(String) the base directory of the task}
* to a file path.
*
* @param style
* the URI of the XSLT stylesheet.
* the URI or the file path of the XSLT stylesheet.
*/
public void setStyle(String style) {
style_ = style;
Expand Down Expand Up @@ -173,8 +177,17 @@ public Output createOutput() {
void init(File baseDir, NamespaceContext namespaceContext, boolean force) {
paramMap_ = createParamMap(namespaceContext);

styleURI_ = URI.create(style_);
if (!styleURI_.isAbsolute()) {
if (style_ == null) {
sinks_.log(this, "\"style\" must be specified", Level.ERR);
throw new BuildException();
}
try {
// First we try as a URI
styleURI_ = new URI(style_);
} catch (URISyntaxException e) {
// Second we try as a file
}
if ((styleURI_ == null) || !styleURI_.isAbsolute()) {
styleURI_ = baseDir.toPath().resolve(style_).toUri();
}

Expand All @@ -186,10 +199,10 @@ void init(File baseDir, NamespaceContext namespaceContext, boolean force) {
private Map<String, Object> createParamMap(NamespaceContext namespaceContext) {
return params_.toMap(p -> p.yield(namespaceContext),
e -> sinks_.log(this,
"Adding a stylesheet parameter: " + e, Logger.Level.DEBUG),
"Adding a stylesheet parameter: " + e, Level.DEBUG),
k -> {
sinks_.log(this,
"Stylesheet parameter named " + k + " added twice", Logger.Level.ERR);
"Stylesheet parameter named " + k + " added twice", Level.ERR);
throw new ChionographisBuildException(true);
});
}
Expand Down Expand Up @@ -246,7 +259,7 @@ Result startOne(int origSrcIndex, String origSrcFileName,
r -> {
List<String> referredContents = Referral.extract(r.getNode(), referents);
sinks_.log(this, "Referred source data: "
+ String.join(", ", referredContents), Logger.Level.DEBUG);
+ String.join(", ", referredContents), Level.DEBUG);
Result openedResult =
sinks_.startOne(origSrcIndex, origSrcFileName,
lastModified, referredContents);
Expand Down Expand Up @@ -379,28 +392,28 @@ private Templates getCompiledStylesheet() {
u -> {},
u -> {
sinks_.log(Transform.this,
"Reusing compiled stylesheet: " + u.toString(), Logger.Level.DEBUG);
"Reusing compiled stylesheet: " + u.toString(), Level.DEBUG);
},
this::compileStylesheet);
}

private Templates compileStylesheet(URI styleURI) {
String styleSystemID = styleURI.toString();
sinks_.log(Transform.this,
"Compiling stylesheet: " + styleSystemID, Logger.Level.VERBOSE);
"Compiling stylesheet: " + styleSystemID, Level.VERBOSE);
synchronized (LOCK) {
tfac_ = (SAXTransformerFactory) TransformerFactory.newInstance();
if (usesCache_) {
resolver_ = new CachingResolver(
r -> sinks_.log(Transform.this, "Caching " + r, Logger.Level.DEBUG),
r -> sinks_.log(Transform.this, "Reusing " + r, Logger.Level.DEBUG));
r -> sinks_.log(Transform.this, "Caching " + r, Level.DEBUG),
r -> sinks_.log(Transform.this, "Reusing " + r, Level.DEBUG));
tfac_.setURIResolver(resolver_);
}
try {
return tfac_.newTemplates(new StreamSource(styleSystemID));
} catch (TransformerConfigurationException e) {
sinks_.log(Transform.this,
"Failed to compile stylesheet: " + styleSystemID, Logger.Level.ERR);
"Failed to compile stylesheet: " + styleSystemID, Level.ERR);
throw new FatalityException(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@

<!-- Shall be processed -->
<chionographis basedir="${test.prefix}" includes="input/input.xml" cache="no">
<transform style="output-${test.title}/flatten.xsl" force="yes" cache="no">
<transform style="${dir.output}/flatten.xsl" force="yes" cache="no">
<output dest="output-${test.title}/actual.txt"/>
</transform>
</chionographis>
Expand Down