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