Re: [docbook-apps] Embedding DocBook → PD F transformation in a Java web app
Paul Hoadley <[email protected]> Tue, 1 Aug 2023 13:14:43 +0930
| Newsgroups | gmane.text.docbook.apps |
|---|---|
| Message-ID | <[email protected]> |
--Apple-Mail=_8C12A8A2-1EEF-4502-A31D-EA3F25CDF36F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 [Some of this exchange unintentionally fell off-list.] On 29 Jul 2023, at 22:37, Norm Tovey-Walsh <[email protected]> wrote: > So what=E2=80=99s happened here is that classpath:/xsl/juno-driver.xsl = has > succeeded and now it=E2=80=99s trying to import docbook.xsl. = Unfortunately, the > java.net class doesn=E2=80=99t like classpath:, so the attempt to = resolve > =E2=80=9Cdocbook.xsl=E2=80=9D agains the base URI fails. >=20 > That said, I=E2=80=99m a little surprised that the XML Resolver = doesn=E2=80=99t do the > right thing for you in this case. They=E2=80=99re not found in the = catalog, I > suppose. I think this was my blunder. At one point I was doing this: factory.setURIResolver(new StandardURIResolver()); Although that class claims to support classpath: URIs, it's not an = org.xmlresolver class. Omitting that line gets me further. (What gets = used by default, XMLCatalogResolver?) > If you use the full, absolute URI for the base stylesheet and setup a > catalog to map that to the classpath: URI, I think it=E2=80=99ll work. > On 30 Jul 2023, at 15:50, Norm Tovey-Walsh <[email protected]> wrote: >=20 >> Sounds promising... could you give me a little more hand-holding = here? What would the full, >> absolute URI look like here, and how do I set up a catalog and = provide that to the machinery? >=20 > Sure. If you look in, for example, the xslTNG jar file, you=E2=80=99ll = find a > catalog in xmlresolver/catalog.xml that does the mapping for xslTNG. = If > you check out the repo, you=E2=80=99ll find targets in build.gradle = that > construct it from sources. It=E2=80=99s a bit fussy because I=E2=80=99m = mapping a couple > off different flavors for some resources. >=20 > If you=E2=80=99re using the latest XML Resolver (which is what = you=E2=80=99ll get with > Saxon 11+), then constructing a corresponding catalog for the URIs of > your stylesheets should =E2=80=9Cjust work=E2=80=9D. The resolver will = look for > xmlresolver/catalog.xml files in jars on the classpath. After a lot of trial and error (quick aside: I cannot, for the life of = me, get any SLF4J logging output from Saxon or org.xmlresolver classes), = if I do this: private Document transformDocument(Document document) throws = TransformerException, FileNotFoundException { DOMResult result =3D new DOMResult(); TransformerFactory factory =3D TransformerFactory.newInstance(); InputStream is =3D = XmlTest.class.getResourceAsStream("/xsl/juno-driver.xsl"); Source source =3D new StreamSource(is, = "file:/xsl/juno-driver.xsl"); Transformer transformer =3D factory.newTransformer(source); transformer.transform(new DOMSource(document), result); return (Document) result.getNode(); } with this catalog.xml: <?xml version=3D"1.0" encoding=3D"utf-8"?> <catalog xmlns=3D"urn:oasis:names:tc:entity:xmlns:xml:catalog"> <uri name=3D"file:/xsl/juno-driver.xsl" uri=3D"classpath:/xsl/juno-driver.xsl" /> <uri name=3D"file:/xsl/header-footer.xsl" uri=3D"classpath:/xsl/header-footer.xsl" /> <uri name=3D"file:/xsl/table.xsl" uri=3D"classpath:/xsl/table.xsl" /> <uri name=3D"file:/xsl/titlepage.xsl" uri=3D"classpath:/xsl/titlepage.xsl" /> <uri name=3D"file:/xsl/docbook-xsl-1.79.2/fo/docbook.xsl" uri=3D"classpath:/xsl/docbook-xsl-1.79.2/fo/docbook.xsl" /> <uri name=3D"file:/xsl/docbook-xsl-1.79.2/VERSION.xsl" uri=3D"classpath:/xsl/docbook-xsl-1.79.2/VERSION.xsl" /> <uri name=3D"file:/xsl/docbook-xsl-1.79.2/fo/param.xsl" uri=3D"classpath:/xsl/docbook-xsl-1.79.2/fo/param.xsl" /> </catalog> I get as far as the catalog goes. That is, at this point it fails with: Error=20 XTSE0165: I/O error reported by XML parser processing file:/xsl/docbook-xsl-1.79.2/lib/lib.xsl: = /xsl/docbook-xsl-1.79.2/lib/lib.xsl (No such file or directory) javax.xml.transform.TransformerConfigurationException: = net.sf.saxon.s9api.SaxonApiException: I/O error reported by XML parser = processing file:/xsl/docbook-xsl-1.79.2/lib/lib.xsl Which seems pretty reasonable, because that's the next included file in = fo/docbook.xsl and it's not in the catalog. While I suspect this will work eventually, it seems more than slightly = bogus. The 'name' attributes in the catalog.xml don't seem to matter = until I get down to the DocBook files, at which point it seems to be = imperative that they're 'file:' URIs. Why am I setting the system ID on = that first StreamSource using a 'file:' URI? Is this all working as = expected, and I just need to finish the catalog? (And if anyone can tell me how to turn on SLF4J logging output, I = suspect that would be a big help!) --=20 Paul Hoadley https://logicsquad.net/ https://www.linkedin.com/company/logic-squad/ --Apple-Mail=_8C12A8A2-1EEF-4502-A31D-EA3F25CDF36F Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; = charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;" class=3D"">[Some= of this exchange unintentionally fell off-list.]<div class=3D""><br = class=3D""></div><div class=3D"">On 29 Jul 2023, at 22:37, Norm = Tovey-Walsh <<a href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>> wrote:<div class=3D""><br = class=3D""></div><div class=3D""><div><blockquote type=3D"cite" = class=3D"">So what=E2=80=99s happened here is that = classpath:/xsl/juno-driver.xsl has<br class=3D"">succeeded and now = it=E2=80=99s trying to import docbook.xsl. Unfortunately, the<br = class=3D""><a href=3D"http://java.net" class=3D"">java.net</a> class = doesn=E2=80=99t like classpath:, so the attempt to resolve<br = class=3D"">=E2=80=9Cdocbook.xsl=E2=80=9D agains the base URI fails.<br = class=3D""><br class=3D"">That said, I=E2=80=99m a little surprised that = the XML Resolver doesn=E2=80=99t do the<br class=3D"">right thing for = you in this case. They=E2=80=99re not found in the catalog, I<br = class=3D"">suppose.<br class=3D""></blockquote><div><br = class=3D""></div><div>I think this was my blunder. At one point I was = doing this:</div><div><br class=3D""></div><div><span = style=3D"font-family: Menlo; font-size: small;" = class=3D"">factory.setURIResolver(new = StandardURIResolver());</span></div><div><br = class=3D""></div><div>Although that class claims to support classpath: = URIs, it's not an org.xmlresolver class. Omitting that line gets me = further. (What gets used by default, XMLCatalogResolver?)</div><div><br = class=3D""></div><blockquote type=3D"cite" class=3D"">If you use the = full, absolute URI for the base stylesheet and setup a<br = class=3D"">catalog to map that to the classpath: URI, I think it=E2=80=99l= l work.<br class=3D""></blockquote><div class=3D""><br = class=3D""></div></div></div><div><blockquote type=3D"cite" = class=3D""><div class=3D"">On 30 Jul 2023, at 15:50, Norm Tovey-Walsh = <<a href=3D"mailto:[email protected]" class=3D"">[email protected]</a>> = wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D""><div = class=3D""><blockquote type=3D"cite" class=3D"">Sounds promising... = could you give me a little more hand-holding here? What would the = full,<br class=3D"">absolute URI look like here, and how do I set up a = catalog and provide that to the machinery?<br class=3D""></blockquote><br = class=3D"">Sure. If you look in, for example, the xslTNG jar file, = you=E2=80=99ll find a<br class=3D"">catalog in xmlresolver/catalog.xml = that does the mapping for xslTNG. If<br class=3D"">you check out the = repo, you=E2=80=99ll find targets in build.gradle that<br = class=3D"">construct it from sources. It=E2=80=99s a bit fussy because = I=E2=80=99m mapping a couple<br class=3D"">off different flavors for = some resources.<br class=3D""><br class=3D"">If you=E2=80=99re using the = latest XML Resolver (which is what you=E2=80=99ll get with<br = class=3D"">Saxon 11+), then constructing a corresponding catalog for the = URIs of<br class=3D"">your stylesheets should =E2=80=9Cjust work=E2=80=9D.= The resolver will look for<br class=3D"">xmlresolver/catalog.xml files = in jars on the classpath.<br class=3D""></div></div></blockquote><div><br = class=3D""></div><div>After a lot of trial and error (quick aside: I = cannot, for the life of me, get any SLF4J logging output from Saxon or = org.xmlresolver classes), if I do this:</div><div><br = class=3D""></div><div><div><font face=3D"Menlo" class=3D""><span = style=3D"font-style: normal; font-size: 12px;" class=3D"">private = Document transformDocument(Document document) throws = TransformerException, FileNotFoundException = {</span></font></div><div><font face=3D"Menlo" class=3D""><span = style=3D"font-style: normal; font-size: 12px;" class=3D""><span = class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>DOMResult = result =3D new DOMResult();</span></font></div><div><font face=3D"Menlo" = class=3D""><span style=3D"font-style: normal; font-size: 12px;" = class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre"> = </span>TransformerFactory factory =3D = TransformerFactory.newInstance();</span></font></div><div><font = face=3D"Menlo" class=3D""><span style=3D"font-style: normal; font-size: = 12px;" class=3D""><span class=3D"Apple-tab-span" = style=3D"white-space:pre"> </span>InputStream is =3D = XmlTest.class.getResourceAsStream("/xsl/juno-driver.xsl");</span></font></= div><div><font face=3D"Menlo" class=3D""><span style=3D"font-style: = normal; font-size: 12px;" class=3D""><span class=3D"Apple-tab-span" = style=3D"white-space:pre"> </span>Source source =3D new = StreamSource(is, = "file:/xsl/juno-driver.xsl");</span></font></div><div><font face=3D"Menlo"= class=3D""><span style=3D"font-style: normal; font-size: 12px;" = class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre"> = </span>Transformer transformer =3D = factory.newTransformer(source);</span></font></div><div><font = face=3D"Menlo" class=3D""><span style=3D"font-style: normal; font-size: = 12px;" class=3D""><span class=3D"Apple-tab-span" = style=3D"white-space:pre"> </span>transformer.transform(new = DOMSource(document), result);</span></font></div><div><font face=3D"Menlo"= class=3D""><span style=3D"font-style: normal; font-size: 12px;" = class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre"> = </span>return (Document) result.getNode();</span></font></div><div><font = face=3D"Menlo" class=3D""><span style=3D"font-style: normal; font-size: = 12px;" class=3D"">}</span></font></div><div class=3D""><br = class=3D""></div></div><div>with this catalog.xml:</div><div><br = class=3D""></div><div><div><span style=3D"font-style: normal; font-size: = 12px;" class=3D""><font face=3D"Menlo" class=3D""><?xml version=3D"1.0"= encoding=3D"utf-8"?></font></span></div><div><span = style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""><catalog = xmlns=3D"urn:oasis:names:tc:entity:xmlns:xml:catalog"></font></span></d= iv><div><span style=3D"font-style: normal; font-size: 12px;" = class=3D""><font face=3D"Menlo" class=3D""> <uri = name=3D"file:/xsl/juno-driver.xsl"</font></span></div><div><span = style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/juno-driver.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" class=3D""> = <uri = name=3D"file:/xsl/header-footer.xsl"</font></span></div><div><span = style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/header-footer.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" class=3D""> = <uri name=3D"file:/xsl/table.xsl"</font></span></div><div><span = style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/table.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" class=3D""> = <uri = name=3D"file:/xsl/titlepage.xsl"</font></span></div><div><span = style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/titlepage.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" class=3D""> = <uri = name=3D"file:/xsl/docbook-xsl-1.79.2/fo/docbook.xsl"</font></span></div><d= iv><span style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/docbook-xsl-1.79.2/fo/docbook.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" class=3D""> = <uri = name=3D"file:/xsl/docbook-xsl-1.79.2/VERSION.xsl"</font></span></div><div>= <span style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/docbook-xsl-1.79.2/VERSION.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" class=3D""> = <uri = name=3D"file:/xsl/docbook-xsl-1.79.2/fo/param.xsl"</font></span></div><div= ><span style=3D"font-style: normal; font-size: 12px;" class=3D""><font = face=3D"Menlo" class=3D""> = uri=3D"classpath:/xsl/docbook-xsl-1.79.2/fo/param.xsl" = /></font></span></div><div><span style=3D"font-style: normal; = font-size: 12px;" class=3D""><font face=3D"Menlo" = class=3D""></catalog></font></span></div><div><br = class=3D""></div><div>I get as far as the catalog goes. That is, at this = point it fails with:</div><div><br class=3D""></div><div><div = style=3D"margin: 0px; font-stretch: normal; font-size: 12px; = line-height: normal; font-family: Menlo; color: rgb(255, 0, 0);" = class=3D"">Error </div><div style=3D"margin: 0px; font-stretch: = normal; font-size: 12px; line-height: normal; font-family: Menlo; color: = rgb(255, 0, 0);" class=3D""> XTSE0165: I/O error reported by XML = parser processing</div><div style=3D"margin: 0px; font-stretch: normal; = font-size: 12px; line-height: normal; font-family: Menlo; color: = rgb(255, 0, 0);" class=3D""> = file:/xsl/docbook-xsl-1.79.2/lib/lib.xsl: = /xsl/docbook-xsl-1.79.2/lib/lib.xsl (No such</div><div style=3D"margin: = 0px; font-stretch: normal; font-size: 12px; line-height: normal; = font-family: Menlo; color: rgb(255, 0, 0);" class=3D""> file or = directory)</div><div style=3D"margin: 0px; font-stretch: normal; = font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, = 104, 218);" class=3D""><span style=3D"text-decoration: underline" = class=3D"">javax.xml.transform.TransformerConfigurationException</span><sp= an style=3D"color: #ff0000" class=3D"">: </span><span = style=3D"text-decoration: underline" = class=3D"">net.sf.saxon.s9api.SaxonApiException</span><span = style=3D"color: #ff0000" class=3D"">: I/O error reported by XML parser = processing = file:/xsl/docbook-xsl-1.79.2/lib/lib.xsl</span></div></div></div></div><di= v class=3D""><br class=3D""></div><div class=3D"">Which seems pretty = reasonable, because that's the next included file in fo/docbook.xsl and = it's not in the catalog.</div><div class=3D""><br class=3D""></div><div = class=3D"">While I suspect this will work eventually, it seems more than = slightly bogus. The 'name' attributes in the catalog.xml don't seem to = matter until I get down to the DocBook files, at which point it seems to = be imperative that they're 'file:' URIs. Why am I setting the system ID = on that first StreamSource using a 'file:' URI? Is this all working as = expected, and I just need to finish the catalog?</div><div class=3D""><br = class=3D""></div><div class=3D"">(And if anyone can tell me how to turn = on SLF4J logging output, I suspect that would be a big help!)</div><div = class=3D""><br class=3D""></div><br class=3D""><div class=3D""> <div style=3D"color: rgb(0, 0, 0); letter-spacing: normal; text-align: = start; text-indent: 0px; text-transform: none; white-space: normal; = word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: = break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" = class=3D""><div style=3D"color: rgb(0, 0, 0); letter-spacing: normal; = text-align: start; text-indent: 0px; text-transform: none; white-space: = normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: = break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" = class=3D""><span class=3D"Apple-style-span" style=3D"border-collapse: = separate; font-variant-ligatures: normal; font-variant-position: normal; = font-variant-numeric: normal; font-variant-alternates: normal; = font-variant-east-asian: normal; font-weight: normal; line-height: = normal; border-spacing: 0px; -webkit-text-decorations-in-effect: = none;"><div style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; = line-break: after-white-space;" class=3D"">-- <br class=3D"">Paul = Hoadley<br class=3D""><a href=3D"https://logicsquad.net/" = class=3D"">https://logicsquad.net/</a><br = class=3D"">https://www.linkedin.com/company/logic-squad/</div><div = style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; line-break: = after-white-space;" class=3D""><br = class=3D""></div></span></div></div></div></div></body></html>= --Apple-Mail=_8C12A8A2-1EEF-4502-A31D-EA3F25CDF36F--