From 309a56223bbb3ef2698cb336a3e1c7c38ec288c3 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 21 Jun 2021 11:23:13 +0200 Subject: [PATCH] Introduce DocBook 4 converter. --- .../OSGI-INF/dbk4Converter.xml | 7 + publishing/org.argeo.publishing.ui/bnd.bnd | 3 +- .../org.argeo.publishing.ui/build.properties | 3 +- .../src/org/argeo/docbook/Dbk4Converter.java | 100 ++ .../src/org/argeo/docbook/db4-upgrade.xsl | 1398 +++++++++++++++++ 5 files changed, 1509 insertions(+), 2 deletions(-) create mode 100644 publishing/org.argeo.publishing.ui/OSGI-INF/dbk4Converter.xml create mode 100644 publishing/org.argeo.publishing.ui/src/org/argeo/docbook/Dbk4Converter.java create mode 100644 publishing/org.argeo.publishing.ui/src/org/argeo/docbook/db4-upgrade.xsl diff --git a/publishing/org.argeo.publishing.ui/OSGI-INF/dbk4Converter.xml b/publishing/org.argeo.publishing.ui/OSGI-INF/dbk4Converter.xml new file mode 100644 index 0000000..66526d9 --- /dev/null +++ b/publishing/org.argeo.publishing.ui/OSGI-INF/dbk4Converter.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/publishing/org.argeo.publishing.ui/bnd.bnd b/publishing/org.argeo.publishing.ui/bnd.bnd index 9704732..5485019 100644 --- a/publishing/org.argeo.publishing.ui/bnd.bnd +++ b/publishing/org.argeo.publishing.ui/bnd.bnd @@ -17,4 +17,5 @@ Service-Component:\ OSGI-INF/fontsServlet.xml,\ OSGI-INF/htmlServletContext.xml,\ OSGI-INF/dbkServlet.xml,\ -OSGI-INF/documentUiProvider.xml +OSGI-INF/documentUiProvider.xml,\ +OSGI-INF/dbk4Converter.xml diff --git a/publishing/org.argeo.publishing.ui/build.properties b/publishing/org.argeo.publishing.ui/build.properties index 2ef3d70..e97efd7 100644 --- a/publishing/org.argeo.publishing.ui/build.properties +++ b/publishing/org.argeo.publishing.ui/build.properties @@ -2,5 +2,6 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ OSGI-INF/,\ - OSGI-INF/documentUiProvider.xml + OSGI-INF/documentUiProvider.xml,\ + OSGI-INF/dbk4Converter.xml source.. = src/ diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/Dbk4Converter.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/Dbk4Converter.java new file mode 100644 index 0000000..916dddd --- /dev/null +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/Dbk4Converter.java @@ -0,0 +1,100 @@ +package org.argeo.docbook; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import javax.jcr.ImportUUIDBehavior; +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.apache.xalan.processor.TransformerFactoryImpl; +import org.argeo.jcr.JcrException; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** Convert from DocBook v4 to DocBook v5, using the official XSL. */ +public class Dbk4Converter { + private final Templates templates; + + public Dbk4Converter() { + try (InputStream in = getClass().getResourceAsStream("db4-upgrade.xsl")) { + Source xsl = new StreamSource(in); + TransformerFactory transformerFactory = new TransformerFactoryImpl(); + templates = transformerFactory.newTemplates(xsl); + } catch (IOException | TransformerConfigurationException e) { + throw new RuntimeException("Cannot initialise DocBook v4 converter", e); + } + } + + public void importXml(Node baseNode, InputStream in) throws IOException { + try (ByteArrayOutputStream out = new ByteArrayOutputStream();) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setXIncludeAware(true); + factory.setNamespaceAware(true); + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + Document doc = docBuilder.parse(new InputSource(in)); + Source xmlInput = new DOMSource(doc); + +// ContentHandler contentHandler = baseNode.getSession().getImportContentHandler(baseNode.getPath(), +// ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); + + Transformer transformer = templates.newTransformer(); + Result xmlOutput = new StreamResult(out); + transformer.transform(xmlInput, xmlOutput); + try (InputStream dbk5in = new ByteArrayInputStream(out.toByteArray())) { + baseNode.getSession().importXML(baseNode.getPath(), dbk5in, + ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); + } + } catch (RepositoryException e) { + throw new JcrException("Cannot import XML to " + baseNode, e); + } catch (TransformerException | SAXException | ParserConfigurationException e) { + throw new RuntimeException("Cannot import DocBook v4 to " + baseNode, e); + } + + } + + public static void main(String[] args) { + try { + + Source xsl = new StreamSource(new File("/usr/share/xml/docbook5/stylesheet/upgrade/db4-upgrade.xsl")); + TransformerFactory transformerFactory = new TransformerFactoryImpl(); + Templates templates = transformerFactory.newTemplates(xsl); + + File inputDir = new File(args[0]); + File outputDir = new File(args[1]); + + for (File inputFile : inputDir.listFiles()) { + Result xmlOutput = new StreamResult(new File(outputDir, inputFile.getName())); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setXIncludeAware(true); + factory.setNamespaceAware(true); + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + Document doc = docBuilder.parse(inputFile); + Source xmlInput = new DOMSource(doc); + Transformer transformer = templates.newTransformer(); + transformer.transform(xmlInput, xmlOutput); + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + +} diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/db4-upgrade.xsl b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/db4-upgrade.xsl new file mode 100644 index 0000000..00096be --- /dev/null +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/db4-upgrade.xsl @@ -0,0 +1,1398 @@ + + + + + + + + + + + + + + + + + UNKNOWN + + + + + + + + + + + + Converted by db4-upgrade version + + + + + + + + + + + + + + + + + + Check + + title. + + + + + + + + + + + + + + + Check + + : no title. + + + + + + + + + + + Check + + titleabbrev. + + + + + + + + + + + + + + + + + + + Check + + subtitle. + + + + + + + + + + + + + + + + + + + + + + + + + + + + Check + + title. + + + + + + + + + + + + + + + + + + + + + + Check + + titleabbrev. + + + + + + + + + + + + + + + + + + + Check + + subtitle. + + + + + + + + + + + + + + + + + + + + + + + + + + Discarding title from refentryinfo! + + + + + + + + Discarding titleabbrev from refentryinfo! + + + + + + + + Discarding subtitle from refentryinfo! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Dropping class attribute from productname + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Convert equation without title to informal equation. + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Check conversion of srccredit + (othercredit="srccredit"). + + + + + ??? + + + + + + + + + + + + + + comment + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Converting invpartnumber to biblioid otherclass="invpartnumber". + + + + + + + + + + + + + + Converting contractsponsor to othercredit="contractsponsor". + + + + + + + + + + + + + + + + + + + + + Converting contractnum to othercredit="contractnum". + + + + + ??? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Check conversion of collabname + (orgname role="collabname"). + + + + + + + + + + + + Discarding modespec ( + + ). + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Check conversion of contrib + (othercontrib="contrib"). + + + + ??? + + + + + + + + + + + + + + + + + + + + Converting ulink to link. + + + + + + + + + + + + + + Converting ulink to uri. + + + + + + + + + + + + + + + + + + Discarding linkmode on olink. + + + + + + + + + Converting olink targetdocent to targetdoc. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -01- + + + + + -02- + + + + + -03- + + + + + -04- + + + + + -05- + + + + + -06- + + + + + -07- + + + + + -08- + + + + + -09- + + + + + -10- + + + + + -11- + + + + + -12- + + + + + + + + + + + + -01- + + + + + -02- + + + + + -03- + + + + + -04- + + + + + -05- + + + + + -06- + + + + + -07- + + + + + -08- + + + + + -09- + + + + + -10- + + + + + -11- + + + + + -12- + + + + + + + + + + + + + + + + + + Converted + + into + + for + + + + + + + + + + + + + + Unparseable date: + + in + + (Using default: + + ) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Check abstract; moved into info correctly? + + + + + + + + + + + significance + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + beginpage pagenum= + + + Replacing beginpage with comment + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Discarding moreinfo on + + + + + + + + + + + + + + + + + + + + + Discarding float on + + + + + + + Adding floatstyle='normal' on + + + + + normal + + + + + + + Discarding float on + + + + + + + + Discarding float on + + + + + + + Adding floatstyle=' + + ' on + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Converting refmiscinfo@role=type to + @class=other,otherclass=type + + + other + type + + + + + + + + + + + + + + + + + 5.0 + + + + + + + + + 5.0 + + + + + + + + + + + + + + + + + + + ( + + ) + + + +
-- 2.30.2