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 String fileName = outputDirectory + "/../../../jar/" + baseName + ".jar";
79 fileOutputStream = new FileOutputStream(fileName);
80 jarFile = new JarOutputStream(fileOutputStream);
82 } catch (java.io.FileNotFoundException e) {
83 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
84 } catch (java.io.IOException e) {
85 System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
88 jarFile.setLevel(compressionLevel);
89 ArrayList<File> fileList = BuildJavaHelp.buildFileList(new File(outputDirectory));
90 File []allFiles = fileList.toArray(new File [fileList.size()]);
91 for (int i = 0; i < allFiles.length; i++) {
93 File workingFile = allFiles[i];
94 FileInputStream fileInputStream = new FileInputStream(workingFile);
96 int length = (int) workingFile.length();
97 byte[] buffer = new byte[length];
99 fileInputStream.read(buffer, 0, length);
100 } catch (java.io.IOException e) {
101 System.err.println(COULD_NOT_FIND + workingFile + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
103 String relativeFileName = null;
104 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
105 relativeFileName = baseName + "/" + workingFile.getName();
107 relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
109 ZipEntry zipEntry = new ZipEntry(relativeFileName);
110 jarFile.putNextEntry(zipEntry);
112 jarFile.write(buffer, 0, length);
114 fileInputStream.close();
115 } catch (java.io.IOException e) {
116 System.err.println("buildDoc: An error occurs while building the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
122 } catch (java.io.IOException e) {
123 System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
129 * After the saxon process, create the Jar
131 * @param outputDirectory Where the files are available and
132 * @param language In which language (for the file name)
133 * @return The result of the process
135 public static boolean buildJavaHelp(String outputDirectory, String language) {
137 String outputJavaHelp = new String(outputDirectory + JAVAHELPSEARCH_DIR);
139 /* Purge the directory before launching the index */
140 /* because the JavaHelp Indexer failed when launched twice on the same directory */
141 Helpers.deleteDirectory(outputJavaHelp);
142 String[] args = new String[] {
144 outputJavaHelp, /* Where the Java Help Index should be created */
147 indexer.compile(args);
148 } catch (Exception e) {
149 System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
153 BuildJavaHelp.buildJar(outputDirectory, language);