diff --git a/build.gradle b/build.gradle index 2fbabe1..9ce3947 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java b/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java index 79ce4ef..989835e 100644 --- a/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java +++ b/src/main/java/com/elovirta/pdf/ant/StylesheetGeneratorTask.java @@ -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; @@ -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; @@ -80,7 +80,7 @@ public void execute() throws BuildException { } final XdmItem xdmItem = parseTemplate(); - getProject().log("Template:" + xdmItem, Project.MSG_INFO); + getProject().log("Template:" + xdmItem, Project.MSG_DEBUG); generate(xdmItem, "front-matter.xsl", "xsl/fo/front-matter.xsl", null); generate(xdmItem, "commons.xsl", "xsl/fo/commons.xsl", null); generate(xdmItem, "tables.xsl", "xsl/fo/tables.xsl", null); @@ -187,19 +187,21 @@ 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 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); } } @@ -211,33 +213,31 @@ private XdmItem parseYamlTemplate(final XdmItem base, final URI url) { 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); @@ -248,7 +248,10 @@ 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); } catch (SaxonApiException | IOException e) { throw new BuildException("Failed to convert YAML to JSON: " + e.getMessage(), e); } diff --git a/src/test/java/com/elovirta/pdf/ant/StylesheetGeneratorTaskTest.java b/src/test/java/com/elovirta/pdf/ant/StylesheetGeneratorTaskTest.java index 53550e3..492ffb9 100644 --- a/src/test/java/com/elovirta/pdf/ant/StylesheetGeneratorTaskTest.java +++ b/src/test/java/com/elovirta/pdf/ant/StylesheetGeneratorTaskTest.java @@ -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)); diff --git a/src/test/resources/com/elovirta/pdf/default.json b/src/test/resources/com/elovirta/pdf/default.json index ce30cb3..3456a45 100644 --- a/src/test/resources/com/elovirta/pdf/default.json +++ b/src/test/resources/com/elovirta/pdf/default.json @@ -1,7 +1,7 @@ { "style": { "h1": { - "font-weight": "bold", + "font-weight": "bold" } } } diff --git a/src/test/resources/exp/default.json b/src/test/resources/exp/default.json index e7bb2b7..aef845c 100644 --- a/src/test/resources/exp/default.json +++ b/src/test/resources/exp/default.json @@ -1,10 +1,4 @@ { - "style": { - "toc-1": { - "color": "blue" - }, - "toc-2": { - "color": "green" - } - } -} \ No newline at end of file + "style-topic-font-weight": "bold", + "style-topic-background-color": "pink" +} diff --git a/src/test/resources/src/default.json b/src/test/resources/src/default.json index 791dd7a..7ba4d51 100644 --- a/src/test/resources/src/default.json +++ b/src/test/resources/src/default.json @@ -5,4 +5,4 @@ "background-color": "pink" } } -} \ No newline at end of file +}