Working DocBook servlet.
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 15 Jun 2021 09:47:52 +0000 (11:47 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 15 Jun 2021 09:47:52 +0000 (11:47 +0200)
dep/org.argeo.suite.dep.ui.rap/pom.xml
lib/pom.xml
publishing/org.argeo.publishing.ui/bnd.bnd
publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java

index 20b5f1b06eb109459fca3a7932092844615525f0..d94c60991219b26ab27c8a011f8bdc7369c10f96 100644 (file)
                        <groupId>org.argeo.tp.javax</groupId>
                        <artifactId>javax.xml.bind</artifactId>
                </dependency>
-               <dependency>
-                       <groupId>org.argeo.tp.apache</groupId>
-                       <artifactId>org.apache.xerces</artifactId>
-               </dependency>
 
                <!-- Jackson JSON processor -->
                <dependency>
index 81d3d89020defd42827f4f5bd8e535ea6a446ba4..c5392901669f772e58f6f4169a0fee24799747fe 100644 (file)
                        <version>2.3.1-SNAPSHOT</version>
                        <scope>provided</scope>
                        <exclusions>
-                               <exclusion>
-                                       <groupId>org.argeo.tp.apache</groupId>
-                                       <artifactId>org.apache.xerces</artifactId>
-                               </exclusion>
                                <!-- Avoid slf4j implementations lurking in the classpath. -->
                                <exclusion>
                                        <groupId>org.argeo.tp.sdk</groupId>
index 30b13d813f7d42b68d382e5fc78320830df62dd1..9704732251184f79246a0a2558ca7326b3effc1c 100644 (file)
@@ -7,6 +7,7 @@ org.argeo.api,\
 org.eclipse.swt,\
 org.eclipse.jface.viewers,\
 org.osgi.framework,\
+org.apache.xml.serializer,\
 *
 
 Provide-Capability:\
index fe4ca32af7c03aef6d312b819844b7920821d7f2..4e86f21a0067a1315fafc41d86e024ce46d3c5a3 100644 (file)
@@ -2,11 +2,12 @@ package org.argeo.publishing.servlet;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
@@ -27,24 +28,29 @@ import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
-import org.apache.xalan.xsltc.trax.SmartTransformerFactoryImpl;
+import org.apache.xalan.processor.TransformerFactoryImpl;
 import org.argeo.cms.servlet.ServletAuthUtils;
 import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrException;
 import org.w3c.dom.Document;
 
+/**
+ * A servlet transforming a dbk:* JCR node into HTML, using the DocBook XSL.
+ */
 public class DbkServlet extends HttpServlet {
        private static final long serialVersionUID = 6906020513498289335L;
 
        private Repository repository;
 
-       private DocumentBuilderFactory factory;
+       private DocumentBuilderFactory documentBuilderFactory;
        private TransformerFactory transformerFactory;
        private Templates docBoookTemplates;
 
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-               resp.setContentType("text/html; charset=utf-8");
+               // TODO customise DocBook so that it outputs UTF-8
+               // see http://www.sagehill.net/docbookxsl/OutputEncoding.html
+               resp.setContentType("text/html; charset=ISO-8859-1");
 
                Session session = null;
                try {
@@ -55,59 +61,32 @@ public class DbkServlet extends HttpServlet {
                                pathInfo = pathInfo.substring(1);
                        String path = URLDecoder.decode(pathInfo, StandardCharsets.UTF_8);
 
+                       // TODO optimise with pipes, SAX, etc. ?
+
                        byte[] arr;
                        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                                session.exportDocumentView(path, out, true, false);
                                arr = out.toByteArray();
-                               System.out.println(new String(arr, StandardCharsets.UTF_8));
+//                             System.out.println(new String(arr, StandardCharsets.UTF_8));
                        } catch (RepositoryException e) {
                                throw new JcrException(e);
                        }
 
                        try (InputStream in = new ByteArrayInputStream(arr);
-                                       ByteArrayOutputStream out = new ByteArrayOutputStream();) {
-//                     Source xsl = new StreamSource(new File("/usr/share/sgml/docbook/xsl-stylesheets/xhtml/docbook.xsl"));
-//                     Source xsl = new StreamSource(
-//                                     Files.newBufferedReader(Paths.get("/home/mbaudier/Downloads/docbook-xsl-1.79.2/xhtml/docbook.xsl"),
-//                                                     StandardCharsets.US_ASCII),
-//                                     "file:///home/mbaudier/Downloads/docbook-xsl-1.79.2/xhtml/docbook.xsl");
-                               Source xsl = new StreamSource(
-                                               new File(System.getProperty("user.home") + "/Downloads/docbook-xsl-1.79.2/html/docbook.xsl"));
-//                             if (docBoookTemplates == null) {
-//                                     try {
-//                                             docBoookTemplates = transformerFactory.newTemplates(xsl);
-//                                     } catch (TransformerConfigurationException e) {
-//                                             throw new ServletException("Cannot instantiate XSL " + xsl, e);
-//                                     }
-//                             }
-
-//                     Source xmlInput = new StreamSource(new File("/home/mbaudier/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"));
-//                     Source xmlInput = new StreamSource(in);
-                               Result xmlOutput = new StreamResult(out);
-
-//                             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-////                   DocumentBuilderFactory factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
-//                             factory.setXIncludeAware(true);
-//                             factory.setNamespaceAware(true);
-                               DocumentBuilder docBuilder = factory.newDocumentBuilder();
-//                     Document doc = docBuilder
-//                                     .parse(new File("/home/mbaudier/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"));
-                               Document doc = docBuilder.parse(in);
-                               Source xmlInput = new DOMSource(doc);
+//                                     ByteArrayOutputStream out = new ByteArrayOutputStream();
+                       ) {
 
-//                     SAXParserFactory factory = SAXParserFactory.newInstance();
-//                     SAXParser saxParser = factory.newSAXParser();
-//                     Source xmlInput = new SAXSource(saxParser.getXMLReader(),
-//                                     new InputSource(new FileReader(new File("/home/mbaudier/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"),StandardCharsets.UTF_8)));
+                               Result xmlOutput = new StreamResult(resp.getOutputStream());
 
-//                     Source xmlInput = new StreamSource(
-//                                     new File("/home/mbaudier/dev/workspaces/argeo-suite/xslt-test/input.xml"));
-//                     Result xmlOutput = new StreamResult(new File("output.html"));
+                               DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
+//                             Document doc = docBuilder.parse(new File(
+//                                             System.getProperty("user.home") + "/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"));
+                               Document doc = docBuilder.parse(in);
+                               Source xmlInput = new DOMSource(doc);
 
-//                     TransformerFactory transformerFactory = new org.apache.xalan.processor.TransformerFactoryImpl();
-                               Transformer transformer = transformerFactory.newTransformer(xsl);
+                               Transformer transformer = docBoookTemplates.newTransformer();
                                transformer.transform(xmlInput, xmlOutput);
-                               resp.getOutputStream().write(out.toByteArray());
+//                             resp.getOutputStream().write(out.toByteArray());
                        } catch (Exception e) {
                                throw new ServletException("Cannot transform " + path, e);
                        }
@@ -118,10 +97,36 @@ public class DbkServlet extends HttpServlet {
 
        @Override
        public void init() throws ServletException {
-               factory = DocumentBuilderFactory.newInstance();
-               factory.setXIncludeAware(true);
-               factory.setNamespaceAware(true);
-               transformerFactory = new SmartTransformerFactoryImpl();
+
+               // TODO improve configuration and provisioning of DocBook XSL
+               String xslBase = System.getProperty("argeo.docbook.xsl");
+               if (xslBase == null) {
+                       String defaultXslBase = "/opt/docbook-xsl";
+                       if (!Files.exists(Paths.get(defaultXslBase))) {
+                               throw new ServletException("System property argeo.docbook.xsl is not set and default location "
+                                               + defaultXslBase + " does not exist.");
+                       } else {
+                               xslBase = defaultXslBase;
+                       }
+               }
+               String xsl = xslBase + "/html/docbook.xsl";
+
+               documentBuilderFactory = DocumentBuilderFactory.newInstance();
+               documentBuilderFactory.setXIncludeAware(true);
+               documentBuilderFactory.setNamespaceAware(true);
+
+               // We must explicitly use the non-XSLTC transformer, as XSLTC is not working
+               // with DocBook stylesheets
+               transformerFactory = new TransformerFactoryImpl();
+
+               Source xslSource = new StreamSource(xsl);
+               try {
+                       docBoookTemplates = transformerFactory.newTemplates(xslSource);
+                       if (docBoookTemplates == null)
+                               throw new ServletException("Could not instantiate XSL " + xsl);
+               } catch (TransformerConfigurationException e) {
+                       throw new ServletException("Cannot instantiate XSL " + xsl, e);
+               }
        }
 
        public void setRepository(Repository repository) {