Make xmltoformat works with toolboxes
[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                 /* Stored into SCI/modules/helptools/jar */
76                 String fileName = outputDirectory + "/" + baseName + ".jar";
77
78                 try {
79
80                         fileOutputStream = new FileOutputStream(fileName);
81                         jarFile = new JarOutputStream(fileOutputStream);
82
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);
87                 }
88
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++) {                     
93                         try {
94                                 File workingFile = allFiles[i];
95                                 FileInputStream fileInputStream = new FileInputStream(workingFile);
96  
97                                 int length = (int) workingFile.length();
98                                 byte[] buffer = new byte[length];
99                                 try {
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);
103                                 }
104                                 String relativeFileName = null;
105                                 if (workingFile.getPath().indexOf("JavaHelpSearch") == -1) {
106                                         relativeFileName = baseName + "/" + workingFile.getName();
107                                 } else {
108                                         relativeFileName = baseName + JAVAHELPSEARCH_DIR + workingFile.getName();
109                                 }
110                                 ZipEntry zipEntry = new ZipEntry(relativeFileName);
111                                 jarFile.putNextEntry(zipEntry);
112
113                                 jarFile.write(buffer, 0, length);
114  
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);
118                         }
119
120                 }
121                 try {
122                         jarFile.close();
123                 } catch (java.io.IOException e) {
124                         System.err.println("buildDoc: An error occurs while closing the JavaHelp ( " + e.getLocalizedMessage() + RIGHT_PAR);
125                 }
126                 return true;
127         }
128
129     /**
130      * After the saxon process, create the Jar 
131      *
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
135          */
136         public static String buildJavaHelp(String outputDirectory, String language) {
137         
138                 String outputJavaHelp = new String(outputDirectory + JAVAHELPSEARCH_DIR);
139         
140         try {
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);
145                         directory.mkdirs();
146
147                         String[] args = new String[] {"."};
148
149                         indexer.compile(args);
150                 } catch (Exception e) {
151                         System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
152                         return null;
153                 }
154
155                 BuildJavaHelp.buildJar(outputDirectory, language);
156                         
157                 return outputDirectory;
158         }
159
160 }