Purge the directory before launching the index because the JavaHelp Indexer failed...
[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         
34         /**
35          * Default constructor (must no be used)
36          */
37         private BuildJavaHelp() {
38                 throw new UnsupportedOperationException();
39         }
40
41         /**
42          * Get the list of the files in a directory
43          * @param directory the directory where files have to be searched
44          * @return teh list of the files found
45          */
46         private static ArrayList<File> buildFileList(File directory) {
47                 ArrayList<File> listFile = new ArrayList<File>();
48
49                 File [] files = directory.listFiles();
50                 for (int i = 0; i < files.length; i++) {
51
52                         if (files[i].isDirectory()) {
53                                 listFile.addAll(buildFileList(files[i]));
54                         } else {
55                                 listFile.add(files[i]);
56                         }
57                 }
58
59                 return listFile;
60         }
61
62     /**
63      * Private method which is trying to build the jar
64      *
65      * @param outputDirectory Where to build the jar file
66      * @param language In which language (for the file name)
67      * @return The result of the operation
68          */
69         private static boolean buildJar(String outputDirectory, String language) {
70                 String baseName = Helpers.getBaseName(language);
71                 JarOutputStream jarFile = null;
72                 FileOutputStream fileOutputStream = null;
73                 final int compressionLevel = 5;
74                 String fileName = outputDirectory + "/../../../jar/" + baseName + ".jar";
75
76                 try {
77
78                         fileOutputStream = new FileOutputStream(fileName);
79                         jarFile = new JarOutputStream(fileOutputStream);
80
81                 } catch (java.io.FileNotFoundException e) {
82                         System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
83                 } catch (java.io.IOException e) {
84                         System.err.println(COULD_NOT_FIND + fileName + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
85                 }
86
87                 jarFile.setLevel(compressionLevel);
88                 ArrayList<File> fileList = BuildJavaHelp.buildFileList(new File(outputDirectory));
89                 File []allFiles = fileList.toArray(new File [fileList.size()]);
90                 for (int i = 0; i < allFiles.length; i++) {                     
91                         try {
92                                 File workingFile = allFiles[i];
93                                 FileInputStream fileInputStream = new FileInputStream(workingFile);
94  
95                                 int length = (int) workingFile.length();
96                                 byte[] buffer = new byte[length];
97                                 try {
98                                         fileInputStream.read(buffer, 0, length);
99                                 } catch (java.io.IOException e) {
100                                         System.err.println(COULD_NOT_FIND + workingFile + LEFT_PAR + e.getLocalizedMessage() + RIGHT_PAR);
101                                 }
102                                 String relativeFileName = null;
103                                 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
104                                         relativeFileName = baseName + "/" + workingFile.getName();
105                                 } else {
106                                         relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
107                                 }
108                                 ZipEntry zipEntry = new ZipEntry(relativeFileName);
109                                 jarFile.putNextEntry(zipEntry);
110
111                                 jarFile.write(buffer, 0, length);
112  
113                                 fileInputStream.close();
114                         } catch (java.io.IOException e) {
115                                 System.err.println("buildDoc: An error occurs while building the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
116                         }
117
118                 }
119                 try {
120                         jarFile.close();
121                 } catch (java.io.IOException e) {
122                         System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
123                 }
124                 return true;
125         }
126
127     /**
128      * After the saxon process, create the Jar 
129      *
130      * @param outputDirectory Where the files are available and 
131      * @param language In which language (for the file name)
132      * @return The result of the process
133          */
134         public static boolean buildJavaHelp(String outputDirectory, String language) {
135                 Indexer indexer = new Indexer();
136                 String outputJavaHelp = new String (outputDirectory + JAVAHELPSEARCH_DIR);
137                 try {
138                         Helpers.deleteDirectory(outputJavaHelp); /* Purge the directory before launching the index because the JavaHelp Indexer failed when launched twice on the same directory */
139                         String[] args = new String[] {
140                                 "-db",
141                                 outputJavaHelp, /* Where the Java Help Index should be created */
142                                 outputDirectory
143                         };
144                         indexer.compile(args);
145                 } catch (Exception e) {
146                         System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
147                         return false;
148                 }
149
150                 BuildJavaHelp.buildJar(outputDirectory, language);
151                         
152                 return true;
153         }
154
155 }