Checkstyle
[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 teh list of the files found
46          */
47         private static ArrayList<File> buildFileList(File directory) {
48                 ArrayList<File> listFile = new ArrayList<File>();
49
50                 File [] files = directory.listFiles();
51                 for (int i = 0; i < files.length; i++) {
52
53                         if (files[i].isDirectory()) {
54                                 listFile.addAll(buildFileList(files[i]));
55                         } else {
56                                 listFile.add(files[i]);
57                         }
58                 }
59
60                 return listFile;
61         }
62
63     /**
64      * Private method which is trying to build the jar
65      *
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
69          */
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";
76
77                 try {
78
79                         fileOutputStream = new FileOutputStream(fileName);
80                         jarFile = new JarOutputStream(fileOutputStream);
81
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);
86                 }
87
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++) {                     
92                         try {
93                                 File workingFile = allFiles[i];
94                                 FileInputStream fileInputStream = new FileInputStream(workingFile);
95  
96                                 int length = (int) workingFile.length();
97                                 byte[] buffer = new byte[length];
98                                 try {
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);
102                                 }
103                                 String relativeFileName = null;
104                                 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
105                                         relativeFileName = baseName + "/" + workingFile.getName();
106                                 } else {
107                                         relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
108                                 }
109                                 ZipEntry zipEntry = new ZipEntry(relativeFileName);
110                                 jarFile.putNextEntry(zipEntry);
111
112                                 jarFile.write(buffer, 0, length);
113  
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);
117                         }
118
119                 }
120                 try {
121                         jarFile.close();
122                 } catch (java.io.IOException e) {
123                         System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
124                 }
125                 return true;
126         }
127
128     /**
129      * After the saxon process, create the Jar 
130      *
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
134          */
135         public static boolean buildJavaHelp(String outputDirectory, String language) {
136                 
137                 String outputJavaHelp = new String(outputDirectory + JAVAHELPSEARCH_DIR);
138                 try {
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[] {
143                                 "-db",
144                                 outputJavaHelp, /* Where the Java Help Index should be created */
145                                 outputDirectory
146                         };
147                         indexer.compile(args);
148                 } catch (Exception e) {
149                         System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
150                         return false;
151                 }
152
153                 BuildJavaHelp.buildJar(outputDirectory, language);
154                         
155                 return true;
156         }
157
158 }