More feedback
[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 org.scilab.modules.localization.Messages;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.FilenameFilter;
21 import java.io.FileInputStream;
22 import java.util.zip.ZipEntry;
23 import java.util.jar.JarOutputStream;
24
25
26 /**
27  * This class manages the build of the Java Help
28  */
29 public class BuildJavaHelp {
30
31     /**
32      * Private method which is trying to build the jar
33      *
34      * @param outputDirectory Where to build the jar file
35      * @param language In which language (for the file name)
36      * @return The result of the operation
37          */
38         private static boolean buildJar(String outputDirectory, String language) {
39                 String baseName = "scilab_" + language + "_help";
40                 JarOutputStream jarFile = null;
41                 FileOutputStream fileOutputStream = null;
42                 final int compressionLevel = 5;
43                 String fileName=outputDirectory + "/../../../jar/" + baseName + ".jar";
44
45                 try {
46
47                         fileOutputStream = new FileOutputStream(fileName);
48                         jarFile = new JarOutputStream(fileOutputStream);
49
50                 } catch (java.io.FileNotFoundException e) {
51                         System.err.println("buildDoc: Could not find/access to " +fileName +" ( "+e.getLocalizedMessage() +" )");
52                 } catch (java.io.IOException e) {
53                         System.err.println("buildDoc: Could not find/access to " +fileName +" ( "+e.getLocalizedMessage() +" )");
54                 }
55
56                 jarFile.setLevel(compressionLevel);
57
58                 /* Defines the filter to know what we want to ship in the jar */
59                 FilenameFilter filter = new FilenameFilter() {
60                                 public boolean accept(File dir, String name) {
61                                         return name.endsWith(".html") || name.endsWith(".xml") || name.endsWith(".jhm") || name.endsWith(".hs");
62                                 }
63                         };
64
65         File currentDir = new File(outputDirectory);
66                 File []allFiles = currentDir.listFiles(filter);
67                 for (int i = 0; i < allFiles.length; i++) {                     
68                         try {
69                                 FileInputStream fileInputStream = new FileInputStream(allFiles[i]);
70  
71                                 int length = (int) allFiles[i].length();
72                                 byte[] buffer = new byte[length];
73
74                                 try {
75                                         fileInputStream.read(buffer, 0, length);
76                                 } catch (java.io.IOException e) {
77                                         System.err.println("buildDoc: Could not find/access to " +allFiles[i] +" ( "+e.getLocalizedMessage() +" )");
78                                 }
79  
80                                 ZipEntry zipEntry = new ZipEntry(baseName + "/" + allFiles[i].getName());
81                                 jarFile.putNextEntry(zipEntry);
82
83                                 jarFile.write(buffer, 0, length);
84  
85                                 fileInputStream.close();
86                         } catch (java.io.IOException e) {
87                                 System.err.println("buildDoc: An error occurs while building the JavaHelp ( "+e.getLocalizedMessage() +" )");
88                         }
89
90                 }
91                 try {
92                         jarFile.close();
93                 } catch (java.io.IOException e) {
94                         System.err.println("buildDoc: An error occurs while closing the JavaHelp ( "+e.getLocalizedMessage() +" )");
95                 }
96                 return true;
97         }
98
99     /**
100      * After the saxon process, create the Jar 
101      *
102      * @param outputDirectory Where the files are available and 
103      * @param language In which language (for the file name)
104      * @return The result of the process
105          */
106         public static boolean buildJavaHelp(String outputDirectory, String language) {
107                 Indexer indexer = new Indexer();
108
109                 try {
110                         String[] args = new String[] {
111                                 outputDirectory
112                         };
113                         indexer.compile(args);
114                 } catch (Exception e) {
115                         System.err.println("buildDoc: Error building search index: " + e.getLocalizedMessage());
116                         return false;
117                 }
118
119                 BuildJavaHelp.buildJar(outputDirectory, language);
120                         
121                 return true;
122         }
123
124 }