r665 - in branches/jrst-docutils-jython/jrst/src: main/java/org/nuiton/jrst test/java/org/nuiton/jrst/bugs test/resources/bugs
Author: jpages Date: 2012-05-16 14:57:18 +0200 (Wed, 16 May 2012) New Revision: 665 Url: http://nuiton.org/repositories/revision/jrst/665 Log: Remplacement dans JRST de FileImputStream par ByteArrayOutputStream pour la g?\195?\169n?\195?\169ration du xml. Modified: branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/bugs/TitlesTest.java branches/jrst-docutils-jython/jrst/src/test/resources/bugs/testNoContentSubtitles.rst Modified: branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java =================================================================== --- branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-05-16 09:23:38 UTC (rev 664) +++ branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-05-16 12:57:18 UTC (rev 665) @@ -46,7 +46,8 @@ import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; import org.dom4j.Document; -import org.dom4j.io.SAXReader; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; import org.nuiton.i18n.I18n; import org.nuiton.i18n.init.ClassPathI18nInitializer; import org.nuiton.jrst.convertisor.DocUtils2RST; @@ -56,6 +57,8 @@ import org.nuiton.util.Resource; import org.nuiton.util.StringUtil; import org.python.util.PythonInterpreter; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; /** * FIXME: 'JRST --help' doesn't work, but 'JRST --help toto' work :( FIXME: @@ -73,6 +76,12 @@ public static final String UTF_8 = "UTF-8"; + public static final String DOCUTILS_LAUNCHER = "/__run__.py"; + + public static final String IMPORT_SCRIPT = "import __run__"; + + public static final String WINDOWS_NAME = "win"; + public enum Overwrite { NEVER, IFNEWER, ALLTIME } @@ -422,27 +431,31 @@ } public static Document generateXML(File in) throws Exception { - // RST to XML transformation via Docutils and a temporary file - File out = File.createTempFile("result", ".xml"); - out = generateDocument(TYPE_XML, in, out); + // RST to XML transformation in a StringWriter + ByteArrayOutputStream out = null; + Document doc = null; - SAXReader saxReader = new SAXReader(); - saxReader.setEncoding(UTF_8); - return saxReader.read(out); + try { + out = new ByteArrayOutputStream(); + doc = generateDocument(TYPE_XML, in, out); + } finally { + if ( out != null) { + out.close(); + } + } + + return doc; } - public static File generateDocument(String outputType, File in, File out) throws Exception { - // Name of the main Python script - final String runner = "__run__"; - + public static Document generateDocument(String outputType, File in, ByteArrayOutputStream out) throws Exception { /* Transformation of the __run__ URL into a path that python will use For example the URL is : jar:file:/home/user/.m2/repository/org/nuiton/jrst/docutils/1.6-SNAPSHOT/docutils-1.6-SNAPSHOT.jar!/__run__.py and it will become : /home/user/.m2/repository/org/nuiton/jrst/docutils/1.6-SNAPSHOT/docutils-1.6-SNAPSHOT.jar/ */ - URL resource = JRST.class.getResource("/__run__.py"); + URL resource = JRST.class.getResource(DOCUTILS_LAUNCHER); String docutilsPath = resource.getPath() .replaceAll("__run__.py", ""); docutilsPath = docutilsPath.replaceAll("!", ""); @@ -450,35 +463,37 @@ // Import of the main script to use docutils ( __run__ ) PythonInterpreter interp = new PythonInterpreter(); - String commandImport = "import __run__"; + String commandImport = IMPORT_SCRIPT; interp.exec(commandImport); // If the OS is windows, escapes the backslashs in the filepath String filePath = in.getAbsolutePath(); String property = System.getProperty("os.name").toLowerCase(); - if (property.contains("win")) { + if (property.contains(WINDOWS_NAME)) { filePath = filePath.replaceAll("\\\\", "\\\\\\\\"); } // Execution of the docutils script to transform rst to xml String commandExec = String.format("__run__.exec_docutils('%s', '%s', '%s')", docutilsPath, outputType, filePath); - FileOutputStream fileOutputStream = null; + + // Sets an output stream in the python interpreter and executes the code + interp.setOut(out); + interp.exec(commandExec); + + // Cleans the python interpreter to avoid problems if they are multiple execution of this method + interp.cleanup(); + + String xmlString = new String(out.toByteArray()); + + Document doc = null; try { - // Sets an output stream to build the output file - fileOutputStream = new FileOutputStream(out); - interp.setOut(fileOutputStream); - interp.exec(commandExec); - - // Cleans the python interpreter to avoid problems if they are multiple execution of this method - interp.cleanup(); - } finally { - if (fileOutputStream != null) { - fileOutputStream.close(); - } + doc = DocumentHelper.parseText(xmlString); + } catch (DocumentException e) { + log.error("Erreur lors de la lecture du xml", e); } - return out; + return doc; } Modified: branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/bugs/TitlesTest.java =================================================================== --- branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/bugs/TitlesTest.java 2012-05-16 09:23:38 UTC (rev 664) +++ branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/bugs/TitlesTest.java 2012-05-16 12:57:18 UTC (rev 665) @@ -8,16 +8,16 @@ * Copyright (C) 2011 Codelutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% @@ -35,10 +35,10 @@ /** * Test concernant les titres en général. - * + * * @author chatellier * @version $Revision$ - * + * * Last update : $Date$ * By : $Author$ */ Modified: branches/jrst-docutils-jython/jrst/src/test/resources/bugs/testNoContentSubtitles.rst =================================================================== --- branches/jrst-docutils-jython/jrst/src/test/resources/bugs/testNoContentSubtitles.rst 2012-05-16 09:23:38 UTC (rev 664) +++ branches/jrst-docutils-jython/jrst/src/test/resources/bugs/testNoContentSubtitles.rst 2012-05-16 12:57:18 UTC (rev 665) @@ -22,6 +22,7 @@ .. * <http://www.gnu.org/licenses/lgpl-3.0.html>. .. * #L% .. - + Lancement =========
participants (1)
-
jpages@users.nuiton.org