2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 package org.scilab.modules.helptools;
14 import com.sun.java.help.search.Indexer; /* jhall (Java Help) */
17 import java.io.FileOutputStream;
18 import java.io.FileInputStream;
19 import java.util.zip.ZipEntry;
20 import java.util.jar.JarOutputStream;
22 import java.util.ArrayList;
25 * This class manages the build of the Java Help
27 public final class BuildJavaHelp {
29 private static final String JAVAHELPSEARCH_DIR = "/JavaHelpSearch/";
30 private static final String COULD_NOT_FIND = "buildDoc: Could not find/access to ";
31 private static final String LEFT_PAR = " ( ";
32 private static final String RIGHT_PAR = " )";
33 private static Indexer indexer = new Indexer();
36 * Default constructor (must no be used)
38 private BuildJavaHelp() {
39 throw new UnsupportedOperationException();
43 * Get the list of the files in a directory
44 * @param directory the directory where files have to be searched
45 * @return teh list of the files found
47 private static ArrayList<File> buildFileList(File directory) {
48 ArrayList<File> listFile = new ArrayList<File>();
50 File [] files = directory.listFiles();
51 for (int i = 0; i < files.length; i++) {
53 if (files[i].isDirectory()) {
54 listFile.addAll(buildFileList(files[i]));
56 listFile.add(files[i]);
64 * Private method which is trying to build the jar
66 * @param outputDirectory Where to build the jar file
67 * @param language In which language (for the file name)
68 * @return The result of the operation
70 private static boolean buildJar(String outputDirectory, String language) {
71 String baseName = Helpers.getBaseName(language);
72 JarOutputStream jarFile = null;
73 FileOutputStream fileOutputStream = null;
74 final int compressionLevel = 5;
75 /* Stored into SCI/modules/helptools/jar */
76 String fileName = outputDirectory + "/../../jar/" + baseName + ".jar";
80 fileOutputStream = new FileOutputStream(fileName);
81 jarFile = new JarOutputStream(fileOutputStream);
83 } catch (java.io.FileNotFoundException e) {
84 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
85 } catch (java.io.IOException e) {
86 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
89 jarFile.setLevel(compressionLevel);
90 ArrayList<File> fileList = BuildJavaHelp.buildFileList(new File(outputDirectory));
91 File []allFiles = fileList.toArray(new File [fileList.size()]);
92 for (int i = 0; i < allFiles.length; i++) {
94 File workingFile = allFiles[i];
95 FileInputStream fileInputStream = new FileInputStream(workingFile);
97 int length = (int) workingFile.length();
98 byte[] buffer = new byte[length];
100 fileInputStream.read(buffer, 0, length);
101 } catch (java.io.IOException e) {
102 System.err.println(COULD_NOT_FIND + workingFile + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
104 String relativeFileName = null;
105 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
106 relativeFileName = baseName + "/" + workingFile.getName();
108 relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
110 ZipEntry zipEntry = new ZipEntry(relativeFileName);
111 jarFile.putNextEntry(zipEntry);
113 jarFile.write(buffer, 0, length);
115 fileInputStream.close();
116 } catch (java.io.IOException e) {
117 System.err.println("buildDoc: An error occurs while building the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
123 } catch (java.io.IOException e) {
124 System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
130 * After the saxon process, create the Jar
132 * @param outputDirectory Where the files are available and
133 * @param language In which language (for the file name)
134 * @return The result of the process
136 public static String buildJavaHelp(String outputDirectory, String language) {
138 String outputJavaHelp = new String(outputDirectory + JAVAHELPSEARCH_DIR);
141 /* Purge the directory before launching the index */
142 /* because the JavaHelp Indexer failed when launched twice on the same directory */
143 Helpers.deleteDirectory(outputJavaHelp);
144 File directory = new File(outputJavaHelp);
147 String[] args = new String[] {"."};
149 indexer.compile(args);
150 } catch (Exception e) {
151 System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
155 BuildJavaHelp.buildJar(outputDirectory, language);
157 return outputDirectory;