* Use of helpers
Sylvestre Ledru [Thu, 14 Aug 2008 17:28:52 +0000 (17:28 +0000)]
* Export to PDF
* Some cleanup

scilab/modules/helptools/src/java/org/scilab/modules/helptools/BuildDocObject.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/BuildJavaHelp.java
scilab/modules/helptools/src/java/org/scilab/modules/helptools/BuildPDF.java [new file with mode: 0644]

index c251303..306a629 100644 (file)
@@ -24,7 +24,7 @@ public class BuildDocObject extends StyleSheet {
     /**
      * Creator ... creates the BuildDocObject object
         */
-       public BuildDocObject() {
+       public BuildDocObject() throws FileNotFoundException {
                super();
                this.docbookPath = System.getenv("DOCBOOK_ROOT");
                
@@ -33,6 +33,10 @@ public class BuildDocObject extends StyleSheet {
                                String SCI = System.getenv("SCI");
                                this.docbookPath = SCI + "/thirdparty/docbook";
                        }
+               }else{
+                       if (this.docbookPath == null) {
+                               throw new FileNotFoundException("Could not find variable DOCBOOK_ROOT defining Docbook root directory");                                
+                       }
                }
        }
 
@@ -84,7 +88,7 @@ public class BuildDocObject extends StyleSheet {
                // Need to work with a String instead of a enum since it needs
                // to be called from C/C++ and GIWS doesn't manage this type.
                // Can be CHM, HTML, PDF, JavaHelp, Postscript
-               if (format.equalsIgnoreCase("PDF")) {
+               if (format.equalsIgnoreCase("PDF") || format.equalsIgnoreCase("PS")) {
                        specificArgs.add("use.extensions=1");
                        specificArgs.add("graphicsize.extension=0");
                        specificArgs.add("paper.type=A4");
@@ -93,9 +97,10 @@ public class BuildDocObject extends StyleSheet {
                        specificArgs.add("section.autolabel=1");
                        specificArgs.add("variablelist.as.blocks=1");
                        specificArgs.add("shade.verbatim=1");
+                       this.styleDoc = docbookPath + "/fo/docbook.xsl";
+
                } 
-               if (format.equalsIgnoreCase("PS")) {
-               }
+
                if (format.equalsIgnoreCase("HTML")) {
                        specificArgs.add("use.id.as.filename=1");
                        specificArgs.add("html.stylesheet=html.css");
@@ -103,6 +108,8 @@ public class BuildDocObject extends StyleSheet {
                        specificArgs.add("graphicsize.extension=0");
                        specificArgs.add("toc.section.depth=3");
                        specificArgs.add("section.autolabel=1");
+                       this.styleDoc = docbookPath + "/html/chunk.xsl";
+
                }
                if (format.equalsIgnoreCase("JH") || format.equalsIgnoreCase("javaHelp")) {
                        // JavaHelp
@@ -120,8 +127,15 @@ public class BuildDocObject extends StyleSheet {
                String filename = (String) new File(masterXML).getName();
                /* Create the output file which will be created by copyconvert.run into the working directory  */
                File masterXMLTransformed = new File(this.outputDirectory + File.separator + filename.substring(0, filename.lastIndexOf(".")) + "-processed.xml");
-
-               this.copyFile(new File(styleSheet), new File(this.outputDirectory + File.separator + (String) new File(styleSheet).getName()));
+               String out=this.outputDirectory + File.separator + (String) new File(styleSheet).getName();
+               try {
+                       
+               Helpers.copyFile(new File(styleSheet), new File(out));
+               } catch(java.io.FileNotFoundException e) {
+                       System.err.println("Error while copying " + styleSheet + " to " + out + " : " + e.getMessage());                        
+               } catch (java.io.IOException e) {
+                       System.err.println("Error while copying " + styleSheet + " to " + out + " : " + e.getMessage());                        
+               }
         CopyConvert copyConvert = new CopyConvert();
         copyConvert.setVerbose(true);
         copyConvert.setPrintFormat(this.format);
@@ -144,27 +158,12 @@ public class BuildDocObject extends StyleSheet {
                if (this.format.equalsIgnoreCase("JH") || format.equalsIgnoreCase("javaHelp")) {
                        BuildJavaHelp.buildJavaHelp(this.outputDirectory, this.language);
                }
-       }
-
-
-       private void copyFile(File in, File out) {
-               try {
-                       FileInputStream fis = new FileInputStream(in);
-                       FileOutputStream fos = new FileOutputStream(out);
-                       byte[] buf = new byte[1024];
-                       int i = 0;
-                       while ((i = fis.read(buf)) != -1) {
-                               fos.write(buf, 0, i);
-                       }
-                       fis.close();
-                       fos.close();
-               } catch(java.io.FileNotFoundException e) {
-                       System.err.println("Error while copying " + in + " to " + out + " : " + e.getMessage());                        
-               } catch (java.io.IOException e) {
-                       System.err.println("Error while copying " + in + " to " + out + " : " + e.getMessage());                        
+               if (format.equalsIgnoreCase("PDF")) {
+                       BuildPDF.buildPDF(this.outputDirectory, this.language);
                }
        }
 
+
     /**
      * Launch the whole Saxon process 
      *
@@ -190,7 +189,13 @@ public class BuildDocObject extends StyleSheet {
                if (!new File(this.outputDirectory).isDirectory()) {
                        throw new FileNotFoundException("Could not find directory: " + this.outputDirectory);
                }
+               
                String sourceDocProcessed = this.preProcessMaster(sourceDoc, styleSheet);
+               if (format.equalsIgnoreCase("PDF")) {
+                       /* PDF takes others args */
+                       args.add("-o");
+                       args.add(Helpers.getTemporaryNameFo(outputDirectory));
+               }
 
                args.add(sourceDocProcessed);
                args.add(this.styleDoc);
@@ -200,8 +205,10 @@ public class BuildDocObject extends StyleSheet {
 
                doMain(args.toArray(new String [args.size()]), new StyleSheet(), "java com.icl.saxon.StyleSheet");
 
-               /* Delete the master temp file to avoid to be shipped with the rest */
-               new File(sourceDocProcessed).delete();
+               if (new File(sourceDocProcessed).isDirectory()) {
+                       /* Delete the master temp file to avoid to be shipped with the rest */
+                       new File(sourceDocProcessed).delete();
+               }
 
                this.postProcess();
 
@@ -213,11 +220,11 @@ public class BuildDocObject extends StyleSheet {
      * @param arg Useless arg
         */
        public static void main(String[] arg) {
-               BuildDocObject d = new BuildDocObject();
-               d.setOutputDirectory("/tmp/");
-               d.setExportFormat("JH");
-               d.setDocbookPath("/usr/share/xml/docbook/stylesheet/nwalsh/");
-               try {
+               try{
+                       BuildDocObject d = new BuildDocObject();
+                       d.setOutputDirectory("/tmp/");
+                       d.setExportFormat("PDF");
+                       d.setDocbookPath("/usr/share/xml/docbook/stylesheet/nwalsh/");
                        d.process("/home/sylvestre/dev/scilab5/modules/helptools/master_en_US_help.xml", "/home/sylvestre/dev/scilab5/modules/helptools/css/javahelp.css");
                } catch (FileNotFoundException e) {
                        System.err.println("Exception catched: " + e.getMessage());
index 5425105..cd8517a 100644 (file)
@@ -51,7 +51,7 @@ public class BuildJavaHelp {
      * @return The result of the operation
         */
        private static boolean buildJar(String outputDirectory, String language) {
-               String baseName = "scilab_" + language + "_help";
+               String baseName = Helpers.getBaseName(language);
                JarOutputStream jarFile = null;
                FileOutputStream fileOutputStream = null;
                final int compressionLevel = 5;
diff --git a/scilab/modules/helptools/src/java/org/scilab/modules/helptools/BuildPDF.java b/scilab/modules/helptools/src/java/org/scilab/modules/helptools/BuildPDF.java
new file mode 100644 (file)
index 0000000..eea52a0
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
+ *
+ *  This file must be used under the terms of the CeCILL.
+ *  This source file is licensed as described in the file COPYING, which
+ *  you should have received as part of this distribution.  The terms
+ *  are also available at
+ *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+package org.scilab.modules.helptools;
+import java.io.File;
+
+import java.io.OutputStream;
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
+//JAXP
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.Result;
+import javax.xml.transform.sax.SAXResult;
+
+import java.util.ArrayList;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.apps.FormattingResults;
+
+/**
+ * This class manages the build of the PDF file
+ */
+public class BuildPDF {
+
+
+    /**
+     * After the saxon process, create the PDF thanks to fop 
+     *
+     * @param outputDirectory Where the files are available and 
+     * @param language In which language (for the file name)
+     * @return The result of the process
+        */
+       public static boolean buildPDF(String outputDirectory, String language) {
+
+               String baseName=Helpers.getBaseName(language);
+               String fileName = outputDirectory + "/" + baseName + ".pdf";
+               try {
+                       FopFactory fopFactory = FopFactory.newInstance();
+                       // Step 3: Construct fop with desired output format
+                       OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
+                       Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
+
+                       // Step 4: Setup JAXP using identity transformer
+                       TransformerFactory factory = TransformerFactory.newInstance();
+                       Transformer transformer = factory.newTransformer(); // identity transformer
+                       // Step 5: Setup input and output for XSLT transformation 
+                       // Setup input stream
+                       Source src = new StreamSource(new File(Helpers.getTemporaryNameFo(outputDirectory)));
+
+                       // Resulting SAX events (the generated FO) must be piped through to FOP
+                       Result res = new SAXResult(fop.getDefaultHandler());
+            
+                       // Step 6: Start XSLT transformation and FOP processing
+                       transformer.transform(src, res);
+            FormattingResults foResults = fop.getResults();
+            System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
+
+                       //Clean-up
+                       out.close();
+               }catch(Exception e){
+
+               }
+                       
+               return true;
+       }
+
+}