* bug 5249 fixed - Using the help or apropos functions, some words (stop word)
[scilab.git] / scilab / modules / helptools / src / java / org / scilab / modules / helptools / BuildJavaHelp.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
4  *
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
10  *
11  */
12 package org.scilab.modules.helptools;
13
14 import com.sun.java.help.search.Indexer; /* jhall (Java Help) */
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.FileInputStream;
19 import java.util.zip.ZipEntry;
20 import java.util.jar.JarOutputStream;
21
22 import java.util.ArrayList;
23
24 /**
25  * This class manages the build of the Java Help
26  */
27 public final class BuildJavaHelp {
28         
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();
34         
35         /**
36          * Default constructor (must no be used)
37          */
38         private BuildJavaHelp() {
39                 throw new UnsupportedOperationException();
40         }
41
42         /**
43          * Get the list of the files in a directory
44          * @param directory the directory where files have to be searched
45          * @return the list of the files found
46          */
47         private static ArrayList<File> buildFileList(File directory,  String language) {
48             String baseNameJar = Helpers.getBaseName(language) + ".jar";
49                 ArrayList<File> listFile = new ArrayList<File>();
50                 
51                 File [] files = directory.listFiles();
52                 for (int i = 0; i < files.length; i++) {
53                         if (files[i].isDirectory()) {
54                                 listFile.addAll(buildFileList(files[i], language));
55                         } else {
56                             /* bug 4407 */
57                             /* we do not add scilab_xx_XX_help.jar file to the list */
58                             if (files[i].compareTo(new File(baseNameJar)) != 0) {
59                                 listFile.add(files[i]);
60                                 }
61                         }
62                 }
63                 return listFile;
64         }
65
66     /**
67      * Private method which is trying to build the jar
68      *
69      * @param outputDirectory Where to build the jar file
70      * @param language In which language (for the file name)
71      * @return The result of the operation
72          */
73         private static boolean buildJar(String outputDirectory, String language) {
74                 String baseName = Helpers.getBaseName(language);
75                 JarOutputStream jarFile = null;
76                 FileOutputStream fileOutputStream = null;
77                 final int compressionLevel = 5;
78                 /* Stored into SCI/modules/helptools/jar */
79                 String fileName = outputDirectory + "/" + baseName + ".jar";
80                 
81                 /* bug 4407 */
82                 /* we do list of files before to create scilab_xx_XX_help.jar */
83                 ArrayList<File> fileList = BuildJavaHelp.buildFileList(new File(outputDirectory), language);
84
85                 try {
86
87                         fileOutputStream = new FileOutputStream(fileName);
88                         jarFile = new JarOutputStream(fileOutputStream);
89
90                 } catch (java.io.FileNotFoundException e) {
91                         System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
92                 } catch (java.io.IOException e) {
93                         System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
94                 }
95
96                 jarFile.setLevel(compressionLevel);
97                 
98                 File []allFiles = fileList.toArray(new File [fileList.size()]);
99                 for (int i = 0; i < allFiles.length; i++) {                     
100                         try {
101                                 File workingFile = allFiles[i];
102                                 FileInputStream fileInputStream = new FileInputStream(workingFile);
103  
104                                 int length = (int) workingFile.length();
105                                 byte[] buffer = new byte[length];
106                                 try {
107                                         fileInputStream.read(buffer, 0, length);
108                                 } catch (java.io.IOException e) {
109                                         System.err.println(COULD_NOT_FIND + workingFile + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
110                                 }
111                                 String relativeFileName = null;
112                                 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
113                                         relativeFileName = baseName + "/" + workingFile.getName();
114                                 } else {
115                                         relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
116                                 }
117                                 ZipEntry zipEntry = new ZipEntry(relativeFileName);
118                                 jarFile.putNextEntry(zipEntry);
119
120                                 jarFile.write(buffer, 0, length);
121  
122                                 fileInputStream.close();
123                         } catch (java.io.IOException e) {
124                                 System.err.println("buildDoc: An error occurs while building the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
125                         }
126
127                 }
128                 try {
129                         jarFile.close();
130                 } catch (java.io.IOException e) {
131                         System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
132                 }
133                 return true;
134         }
135
136     /**
137      * After the saxon process, create the Jar 
138      *
139      * @param outputDirectory Where the files are available and 
140      * @param language In which language (for the file name)
141      * @return The result of the process
142          */
143         public static String buildJavaHelp(String outputDirectory, String language) {
144         
145                 String outputJavaHelp = new String(outputDirectory + JAVAHELPSEARCH_DIR);
146         
147         try {
148                         /* Purge the directory before launching the index */
149                         /* because the JavaHelp Indexer failed when launched twice on the same directory */
150                         Helpers.deleteDirectory(outputJavaHelp); 
151                         File directory = new File(outputJavaHelp);
152                         directory.mkdirs();
153
154                         String[] args = new String[] {"-nostopwords","."};
155
156                         indexer.compile(args);
157                 } catch (Exception e) {
158                         System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
159                         return null;
160                 }
161
162                 BuildJavaHelp.buildJar(outputDirectory, language);
163                         
164                 return outputDirectory;
165         }
166
167 }