import org.apache.commons.io.IOUtils;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
-import org.apache.xalan.processor.TransformerFactoryImpl;
-import org.argeo.api.cms.CmsTheme;
+import org.argeo.api.cms.CmsLog;
+import org.argeo.api.cms.ux.CmsTheme;
import org.argeo.app.docbook.DbkType;
-import org.argeo.app.docbook.DbkUtils;
+import org.argeo.app.jcr.docbook.DbkJcrUtils;
import org.argeo.cms.auth.RemoteAuthUtils;
import org.argeo.cms.servlet.ServletHttpRequest;
import org.argeo.jcr.Jcr;
import org.argeo.jcr.JcrUtils;
import org.w3c.dom.Document;
+import net.sf.saxon.BasicTransformerFactory;
+
/**
* 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 CmsLog log = CmsLog.getLog(DbkServlet.class);
+
private Repository repository;
private DocumentBuilderFactory documentBuilderFactory;
private Map<String, CmsTheme> themes = Collections.synchronizedMap(new HashMap<>());
+ private String xslBase;
+
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (node.hasNode(DbkType.article.get())) {
Node dbkNode = node.getNode(DbkType.article.get());
- if (DbkUtils.isDbk(dbkNode)) {
+ if (DbkJcrUtils.isDbk(dbkNode)) {
CmsTheme cmsTheme = null;
String themeId = req.getParameter("themeId");
if (themeId != null) {
Source xmlInput = new DOMSource(doc);
if (pdf) {
- //String baseUri = req.getRequestURI();
+ // String baseUri = req.getRequestURI();
FopFactory fopFactory = FopFactory.newInstance(URI.create(req.getRequestURL().toString()));
resp.setContentType("application/pdf");
- // DocBook to FO
- byte[] foBytes;
- try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
- Result xmlOutput = new StreamResult(out);
- Transformer docBookTransformer = docBoookFoTemplates.newTransformer();
- docBookTransformer.transform(xmlInput, xmlOutput);
- foBytes = out.toByteArray();
- }
-
- // FO to PDF
- try (InputStream foIn = new ByteArrayInputStream(foBytes)) {
- Fop fop = fopFactory.newFop("application/pdf", resp.getOutputStream());
- Transformer fopTransformer = transformerFactory.newTransformer(); // identity
- Source src = new StreamSource(foIn);
- Result fopResult = new SAXResult(fop.getDefaultHandler());
- fopTransformer.transform(src, fopResult);
- }
+// // DocBook to FO
+// byte[] foBytes;
+// try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
+// Result xmlOutput = new StreamResult(out);
+// Transformer docBookTransformer = docBoookFoTemplates.newTransformer();
+// docBookTransformer.transform(xmlInput, xmlOutput);
+// foBytes = out.toByteArray();
+// }
+//
+// // FO to PDF
+// try (InputStream foIn = new ByteArrayInputStream(foBytes)) {
+// Fop fop = fopFactory.newFop("application/pdf", resp.getOutputStream());
+// Transformer fopTransformer = transformerFactory.newTransformer(); // identity
+// Source src = new StreamSource(foIn);
+// Result fopResult = new SAXResult(fop.getDefaultHandler());
+// fopTransformer.transform(src, fopResult);
+// }
+//
+
+ Fop fop = fopFactory.newFop("application/pdf", resp.getOutputStream());
+ Transformer docBookTransformer = getDocBoookFoTemplates().newTransformer();
+ Result fopResult = new SAXResult(fop.getDefaultHandler());
+ docBookTransformer.transform(xmlInput, fopResult);
} else {
Result xmlOutput = new StreamResult(resp.getOutputStream());
resp.setContentType("text/html");
- Transformer docBookTransformer = docBoookHtmlTemplates.newTransformer();
+ Transformer docBookTransformer = getDocBoookHtmlTemplates().newTransformer();
// gather CSS
if (cmsTheme != null) {
public void init() throws ServletException {
// TODO improve configuration and provisioning of DocBook XSL
- String xslBase = System.getProperty("argeo.docbook.xsl");
+ xslBase = System.getProperty("argeo.docbook.xsl");
if (xslBase == null) {
// We need namespace aware XSL!
// Fedora (sudo dnf install docbook5-style-xsl)
if (!Files.exists(Paths.get(defaultXslBase))) {
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.");
+ log.error("System property argeo.docbook.xsl is not set and default location " + defaultXslBase
+ + " does not exist.");
}
}
xslBase = defaultXslBase;
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();
-
- String htmlXsl = xslBase + "/html/docbook.xsl";
- docBoookHtmlTemplates = createDocBookTemplates(htmlXsl);
- String foXsl = xslBase + "/fo/docbook.xsl";
- docBoookFoTemplates = createDocBookTemplates(foXsl);
}
protected Templates createDocBookTemplates(String xsl) {
try {
+ if (transformerFactory == null) {
+ transformerFactory = new BasicTransformerFactory();
+ // We must explicitly use the non-XSLTC transformer, as XSLTC is not working
+ // with DocBook stylesheets
+// transformerFactory = new TransformerFactoryImpl();
+ }
Source xslSource = new StreamSource(xsl);
Templates templates = transformerFactory.newTemplates(xslSource);
if (templates == null)
}
+ protected Templates getDocBoookHtmlTemplates() {
+ if (docBoookHtmlTemplates == null) {
+ String htmlXsl = xslBase + "/html/docbook.xsl";
+ docBoookHtmlTemplates = createDocBookTemplates(htmlXsl);
+ }
+ return docBoookHtmlTemplates;
+ }
+
+ protected Templates getDocBoookFoTemplates() {
+ if (docBoookFoTemplates == null) {
+ String foXsl = xslBase + "/fo/docbook.xsl";
+ docBoookFoTemplates = createDocBookTemplates(foXsl);
+ }
+ return docBoookFoTemplates;
+ }
+
public void setRepository(Repository repository) {
this.repository = repository;
}