KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > IndexToolApplication


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.zip.ZipEntry JavaDoc;
19 import java.util.zip.ZipOutputStream JavaDoc;
20
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.equinox.app.IApplication;
23 import org.eclipse.equinox.app.IApplicationContext;
24 import org.eclipse.osgi.util.NLS;
25
26 /**
27  * application org.eclipse.help.indexTool
28  */

29 public class IndexToolApplication implements IApplication {
30     
31     /* (non-Javadoc)
32      * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
33      */

34     public synchronized Object JavaDoc start(IApplicationContext context) throws Exception JavaDoc {
35         try {
36             String JavaDoc directory = System.getProperty("indexOutput"); //$NON-NLS-1$
37
if (directory == null || directory.length() == 0) {
38                 throw new Exception JavaDoc(NLS.bind(HelpBaseResources.IndexToolApplication_propertyNotSet, "indexOutput")); //$NON-NLS-1$
39
}
40             String JavaDoc localeStr = System.getProperty("indexLocale"); //$NON-NLS-1$
41
if (localeStr == null || localeStr.length() < 2) {
42                 throw new Exception JavaDoc(NLS.bind(HelpBaseResources.IndexToolApplication_propertyNotSet, "indexLocale")); //$NON-NLS-1$
43
}
44             Locale JavaDoc locale;
45             if (localeStr.length() >= 5) {
46                 locale = new Locale JavaDoc(localeStr.substring(0, 2), localeStr.substring(3, 5));
47             }
48             else {
49                 locale = new Locale JavaDoc(localeStr.substring(0, 2), ""); //$NON-NLS-1$
50
}
51             preindex(directory, locale);
52         }
53         catch (Exception JavaDoc e) {
54             e.printStackTrace();
55             HelpBasePlugin.logError("Preindexing failed.", e); //$NON-NLS-1$
56
}
57         return EXIT_OK;
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.equinox.app.IApplication#stop()
62      */

63     public synchronized void stop() {
64     }
65     
66     private void preindex(String JavaDoc outputDir, Locale JavaDoc locale) throws Exception JavaDoc {
67         File JavaDoc indexPath = new File JavaDoc(HelpBasePlugin.getConfigurationDirectory(),
68                 "index/" + locale); //$NON-NLS-1$
69

70         // clean
71
if (indexPath.exists()) {
72             delete(indexPath);
73         }
74         // index
75
BaseHelpSystem.getLocalSearchManager().ensureIndexUpdated(
76                 new NullProgressMonitor(),
77                 BaseHelpSystem.getLocalSearchManager().getIndex(locale.toString()));
78         // zip up
79
File JavaDoc d = new File JavaDoc(outputDir,
80                 "nl" + File.separator + locale.getLanguage()); //$NON-NLS-1$
81
if (locale.getCountry().length() > 0) {
82             d = new File JavaDoc(d, locale.getCountry());
83         }
84         if (!d.exists())
85             d.mkdirs();
86         ZipOutputStream JavaDoc zout = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(
87                 new File JavaDoc(d, "doc_index.zip"))); //$NON-NLS-1$
88
try {
89             zipDirectory(indexPath, zout, null);
90         } finally {
91             zout.close();
92         }
93     }
94
95     /**
96      * Recursively deletes directory and files.
97      *
98      * @param file
99      * @throws IOException
100      */

101     private static void delete(File JavaDoc file) throws IOException JavaDoc {
102         if (file.isDirectory()) {
103             File JavaDoc files[] = file.listFiles();
104             for (int i = 0; i < files.length; i++) {
105                 delete(files[i]);
106             }
107         }
108         if (!file.delete()) {
109             throw new IOException JavaDoc(
110                     NLS.bind(HelpBaseResources.IndexToolApplication_cannotDelete, file.getAbsolutePath()));
111         }
112     }
113
114     /**
115      * Adds files in a directory to a zip stream
116      *
117      * @param dir
118      * directory with files to zip
119      * @param zout
120      * ZipOutputStream
121      * @param base
122      * directory prefix for file entries inside the zip or null
123      * @throws IOException
124      */

125     private static void zipDirectory(File JavaDoc dir, ZipOutputStream JavaDoc zout, String JavaDoc base)
126             throws IOException JavaDoc {
127         byte buffer[] = new byte[8192];
128         String JavaDoc[] files = dir.list();
129         if (files == null || files.length == 0)
130             return;
131         for (int i = 0; i < files.length; i++) {
132             String JavaDoc path;
133             if (base == null) {
134                 path = files[i];
135             } else {
136                 path = base + "/" + files[i]; //$NON-NLS-1$
137
}
138             File JavaDoc f = new File JavaDoc(dir, files[i]);
139             if (f.isDirectory())
140                 zipDirectory(f, zout, path);
141             else {
142                 ZipEntry JavaDoc zentry = new ZipEntry JavaDoc(path);
143                 zout.putNextEntry(zentry);
144                 FileInputStream JavaDoc inputStream = new FileInputStream JavaDoc(f);
145                 int len;
146                 while ((len = inputStream.read(buffer)) != -1)
147                     zout.write(buffer, 0, len);
148                 inputStream.close();
149                 zout.flush();
150                 zout.closeEntry();
151             }
152         }
153     }
154 }
155
Popular Tags