Skip to content

Commit

Permalink
Fix base URL in extends
Browse files Browse the repository at this point in the history
  • Loading branch information
jelovirt committed Dec 21, 2024
1 parent 9515a66 commit 2e8da8d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.13.0'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.32'
implementation group: "org.dita-ot", name: "dost", version: "[4.1,)"
implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '10.6'
implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '12.4'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.2'
testImplementation 'org.skyscreamer:jsonassert:1.5.0'
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.annotations.VisibleForTesting;
import net.sf.saxon.s9api.*;
import net.sf.saxon.tree.wrapper.RebasedDocument;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
Expand All @@ -12,8 +13,7 @@

import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.util.HashMap;
Expand Down Expand Up @@ -187,57 +187,60 @@ private XdmItem resolveVariables(final XdmItem base) {
}

private XdmItem parseJsonTemplate() {
try {
new JsonBuilder(xmlUtils.getProcessor().getUnderlyingConfiguration());
final XdmItem theme = xpathCompiler.evaluateSingle("json-doc(.)", new XdmAtomicValue(template.toURI()));
try (var in = new FileReader(template)){
final XdmValue theme = xmlUtils.getProcessor().newJsonBuilder().parseJson(in);
// TODO: reuse compiled stylesheet
final XsltExecutable executable = compiler.compile(resolver.resolve("classpath:/com/elovirta/pdf/merge.xsl", null));
final Xslt30Transformer transformer = executable.load30();
final Map<QName, XdmItem> parameters = singletonMap(
QName.fromClarkName("{}base-url"), new XdmAtomicValue(template.toURI())
);
transformer.setStylesheetParameters(parameters);
transformer.setGlobalContextItem(theme);
transformer.setGlobalContextItem(theme.itemAt(0));
return transformer.applyTemplates(theme).itemAt(0);
} catch (TransformerException | SaxonApiException e) {
throw new BuildException(String.format("Failed to parse template %s", template), e);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private XdmItem parseYamlTemplate(final XdmItem base, final URI url) {
try {
final XdmItem extendsValue = xpathCompiler.evaluateSingle(". ?extends", base);
if (extendsValue != null && extendsValue.getStringValue().equals(DEFAULT_EXTENDS)) {
// final XdmItem extendsRes = parseYaml(DEFAULT_EXTENDS_URI);
final XPathSelector selector = xpathCompiler.compile("json-doc(.)").load();
selector.setURIResolver(resolver);
selector.setContextItem(new XdmAtomicValue(DEFAULT_EXTENDS_URI));
final XdmItem extendsRes = selector.evaluateSingle();
// final XdmItem extendsRes = xpathCompiler.evaluateSingle("json-doc(.)", new XdmAtomicValue(DEFAULT_EXTENDS_URI));

return transformer.callFunction(QName.fromClarkName("{x}merge"), new XdmValue[]{
normalize(extendsRes),
normalize(base)
normalize(extendsRes, DEFAULT_EXTENDS_URI),
normalize(base, url)
}).itemAt(0);
} else if (extendsValue != null) {
final URI extendsUri = url.resolve(extendsValue.getStringValue());
final XdmItem extendsRes = parseYamlTemplate(parseYaml(extendsUri), url);

return transformer.callFunction(QName.fromClarkName("{x}merge"), new XdmValue[]{
extendsRes, normalize(base)
extendsRes, normalize(base, url)
}).itemAt(0);
} else {
return normalize(base).itemAt(0);
return normalize(base, url).itemAt(0);
}
} catch (SaxonApiException e) {
throw new BuildException(String.format("Failed to parse template %s", template), e);
}
}

private XdmItem normalize(XdmItem in) {
private XdmItem normalize(XdmItem in, URI base) {
try {
return transformer.callFunction(QName.fromClarkName("{x}normalize"), new XdmValue[]{
transformer.callFunction(QName.fromClarkName("{x}flatten"), new XdmValue[]{
in
}), XdmEmptySequence.getInstance(), new XdmAtomicValue(DEFAULT_EXTENDS_URI)
}), XdmEmptySequence.getInstance(), new XdmAtomicValue(base)
}).itemAt(0);
} catch (SaxonApiException e) {
throw new RuntimeException(e);
Expand All @@ -248,7 +251,11 @@ private XdmItem parseYaml(final URI abs) {
try {
final Object yaml = yamlReader.readValue(abs.toURL(), Object.class);
final String json = jsonWriter.writeValueAsString(yaml);
return xpathCompiler.evaluateSingle("parse-json(.)", new XdmAtomicValue(json));

final JsonBuilder builder = xmlUtils.getProcessor().newJsonBuilder();
final XdmValue xdmItems = builder.parseJson(json);
return xdmItems.itemAt(0);
// return xpathCompiler.evaluateSingle("parse-json(.)", new XdmAtomicValue(json));
} catch (SaxonApiException | IOException e) {
throw new BuildException("Failed to convert YAML to JSON: " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class StylesheetGeneratorTaskTest {
@BeforeEach
public void setUp() {
xmlUtils = new XMLUtils();
xmlUtils.getProcessor().getUnderlyingConfiguration().setURIResolver(resolver);
xpathCompiler = xmlUtils.getProcessor().newXPathCompiler();
final Project project = new Project();
xmlUtils.setLogger(new DITAOTAntLogger(project));
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/com/elovirta/pdf/default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"style": {
"h1": {
"font-weight": "bold",
"font-weight": "bold"
}
}
}
12 changes: 3 additions & 9 deletions src/test/resources/exp/default.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"style": {
"toc-1": {
"color": "blue"
},
"toc-2": {
"color": "green"
}
}
}
"style-topic-font-weight": "bold",
"style-topic-background-color": "pink"
}
2 changes: 1 addition & 1 deletion src/test/resources/src/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"background-color": "pink"
}
}
}
}

0 comments on commit 2e8da8d

Please sign in to comment.