2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
4 * Copyright (C) 2013 - Scilab Enterprises - Clement DAVID
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 package org.scilab.modules.helptools;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.FilenameFilter;
19 import java.util.ArrayList;
20 import java.util.jar.JarOutputStream;
21 import java.util.zip.ZipEntry;
23 import org.scilab.modules.commons.ScilabConstants;
25 import com.sun.java.help.search.Indexer;
27 public class JarOnlyConverter extends ContainerConverter {
29 private static final String SCI = ScilabConstants.SCI.getPath();
31 private static final String JAVAHELPSEARCH_DIR = "/JavaHelpSearch/";
32 private static final String COULD_NOT_FIND = "buildDoc: Could not find/access to ";
33 private static final String LEFT_PAR = " ( ";
34 private static final String RIGHT_PAR = " )";
35 private static final String JAR_EXT = ".jar";
36 private static final String SLASH = "/";
37 private static final int JAR_COMPRESSION_LEVEL = 9;
38 private static Indexer indexer = new Indexer();
40 private final boolean isToolbox;
41 private final String outImages;
43 public JarOnlyConverter(SciDocMain sciDocMain) {
44 super(sciDocMain.getOutputDirectory(), sciDocMain.getLanguage());
46 isToolbox = sciDocMain.isToolbox();
49 * Reuse the shared generated images directory from scilab
51 String images = sciDocMain.getOutputDirectory() + File.separator;
53 images = ScilabConstants.SCI.getPath() + "/modules/helptools/images";
54 File dir = new File(images);
63 * Embed the javahelp html files to a jar
66 public void convert() {
67 String outputJavaHelp = new String(outputDirectory + JAVAHELPSEARCH_DIR);
70 /* Purge the directory before launching the index */
71 /* because the JavaHelp Indexer failed when launched twice on the same directory */
72 Helpers.deleteDirectory(outputJavaHelp);
73 File directory = new File(outputJavaHelp);
75 String[] args = new String[] {"-db", outputJavaHelp + File.separator + "JavaHelpSearch", "-nostopwords", "."};
77 indexer.compile(args);
78 } catch (Exception e) {
79 System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
83 buildJar(outputDirectory, language);
87 * Embed the images files to another jar for non-toolboxes
90 public void install() {
92 * Toolboxes images are not in a separate jar.
98 JarOutputStream jarFile = null;
99 FileOutputStream fileOutputStream = null;
101 /* Stored into SCI/modules/helptools/jar */
102 String fileName = SCI + "/modules/helptools/jar" + SLASH + "scilab_images" + JAR_EXT;
104 fileOutputStream = new FileOutputStream(fileName);
105 jarFile = new JarOutputStream(fileOutputStream);
106 jarFile.setLevel(JAR_COMPRESSION_LEVEL);
107 } catch (java.io.FileNotFoundException e) {
108 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
109 } catch (java.io.IOException e) {
110 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
113 File[] allFiles = new File(outImages).listFiles();
114 for (int i = 0; i < allFiles.length; i++) {
116 File workingFile = allFiles[i];
117 FileInputStream fileInputStream = new FileInputStream(workingFile);
119 int length = (int) workingFile.length();
120 byte[] buffer = new byte[length];
122 fileInputStream.read(buffer, 0, length);
123 } catch (java.io.IOException e) {
124 System.err.println(COULD_NOT_FIND + workingFile + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
126 ZipEntry zipEntry = new ZipEntry(workingFile.getName());
127 jarFile.putNextEntry(zipEntry);
128 jarFile.write(buffer, 0, length);
129 fileInputStream.close();
130 } catch (java.io.IOException e) {
131 System.err.println("buildDoc: An error occurs while building the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
137 } catch (java.io.IOException e) {
138 System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
143 * Get the list of the files in a directory
144 * @param directory the directory where files have to be searched
145 * @param language String 'fr_FR'
146 * @return the list of the files found
148 private static ArrayList<File> buildFileList(File directory, String language) {
149 final String baseNameJar = Helpers.getBaseName(language) + JAR_EXT;
150 ArrayList<File> listFile = new ArrayList<File>();
152 File[] files = directory.listFiles(new FilenameFilter() {
153 public boolean accept(File dir, String name) {
154 return !name.equals(baseNameJar);
157 for (int i = 0; i < files.length; i++) {
158 if (files[i].isDirectory()) {
159 listFile.addAll(buildFileList(files[i], language));
161 listFile.add(files[i]);
168 * Private method which is trying to build the jar
170 * @param outputDirectory Where to build the jar file
171 * @param language In which language (for the file name)
172 * @return The result of the operation
174 static boolean buildJar(String outputDirectory, String language) {
175 String baseName = Helpers.getBaseName(language);
176 JarOutputStream jarFile = null;
177 FileOutputStream fileOutputStream = null;
179 /* Stored into SCI/modules/helptools/jar */
180 String fileName = outputDirectory + SLASH + baseName + JAR_EXT;
182 /* we do list of files before to create scilab_xx_XX_help.jar */
183 ArrayList<File> fileList = buildFileList(new File(outputDirectory), language);
186 fileOutputStream = new FileOutputStream(fileName);
187 jarFile = new JarOutputStream(fileOutputStream);
188 } catch (java.io.FileNotFoundException e) {
189 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
190 } catch (java.io.IOException e) {
191 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
194 jarFile.setLevel(JAR_COMPRESSION_LEVEL);
196 File[] allFiles = fileList.toArray(new File [fileList.size()]);
197 for (int i = 0; i < allFiles.length; i++) {
199 File workingFile = allFiles[i];
200 FileInputStream fileInputStream = new FileInputStream(workingFile);
202 int length = (int) workingFile.length();
203 byte[] buffer = new byte[length];
205 fileInputStream.read(buffer, 0, length);
206 } catch (java.io.IOException e) {
207 System.err.println(COULD_NOT_FIND + workingFile + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
209 String relativeFileName = null;
210 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
211 relativeFileName = baseName + SLASH + workingFile.getName();
213 relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
215 ZipEntry zipEntry = new ZipEntry(relativeFileName);
216 jarFile.putNextEntry(zipEntry);
218 jarFile.write(buffer, 0, length);
220 fileInputStream.close();
221 } catch (java.io.IOException e) {
222 System.err.println("buildDoc: An error occurs while building the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
228 } catch (java.io.IOException e) {
229 System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);