1 11 package org.eclipse.help.internal.base; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.util.Locale ; 18 import java.util.zip.ZipEntry ; 19 import java.util.zip.ZipOutputStream ; 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 29 public class IndexToolApplication implements IApplication { 30 31 34 public synchronized Object start(IApplicationContext context) throws Exception { 35 try { 36 String directory = System.getProperty("indexOutput"); if (directory == null || directory.length() == 0) { 38 throw new Exception (NLS.bind(HelpBaseResources.IndexToolApplication_propertyNotSet, "indexOutput")); } 40 String localeStr = System.getProperty("indexLocale"); if (localeStr == null || localeStr.length() < 2) { 42 throw new Exception (NLS.bind(HelpBaseResources.IndexToolApplication_propertyNotSet, "indexLocale")); } 44 Locale locale; 45 if (localeStr.length() >= 5) { 46 locale = new Locale (localeStr.substring(0, 2), localeStr.substring(3, 5)); 47 } 48 else { 49 locale = new Locale (localeStr.substring(0, 2), ""); } 51 preindex(directory, locale); 52 } 53 catch (Exception e) { 54 e.printStackTrace(); 55 HelpBasePlugin.logError("Preindexing failed.", e); } 57 return EXIT_OK; 58 } 59 60 63 public synchronized void stop() { 64 } 65 66 private void preindex(String outputDir, Locale locale) throws Exception { 67 File indexPath = new File (HelpBasePlugin.getConfigurationDirectory(), 68 "index/" + locale); 70 if (indexPath.exists()) { 72 delete(indexPath); 73 } 74 BaseHelpSystem.getLocalSearchManager().ensureIndexUpdated( 76 new NullProgressMonitor(), 77 BaseHelpSystem.getLocalSearchManager().getIndex(locale.toString())); 78 File d = new File (outputDir, 80 "nl" + File.separator + locale.getLanguage()); if (locale.getCountry().length() > 0) { 82 d = new File (d, locale.getCountry()); 83 } 84 if (!d.exists()) 85 d.mkdirs(); 86 ZipOutputStream zout = new ZipOutputStream (new FileOutputStream ( 87 new File (d, "doc_index.zip"))); try { 89 zipDirectory(indexPath, zout, null); 90 } finally { 91 zout.close(); 92 } 93 } 94 95 101 private static void delete(File file) throws IOException { 102 if (file.isDirectory()) { 103 File 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 ( 110 NLS.bind(HelpBaseResources.IndexToolApplication_cannotDelete, file.getAbsolutePath())); 111 } 112 } 113 114 125 private static void zipDirectory(File dir, ZipOutputStream zout, String base) 126 throws IOException { 127 byte buffer[] = new byte[8192]; 128 String [] files = dir.list(); 129 if (files == null || files.length == 0) 130 return; 131 for (int i = 0; i < files.length; i++) { 132 String path; 133 if (base == null) { 134 path = files[i]; 135 } else { 136 path = base + "/" + files[i]; } 138 File f = new File (dir, files[i]); 139 if (f.isDirectory()) 140 zipDirectory(f, zout, path); 141 else { 142 ZipEntry zentry = new ZipEntry (path); 143 zout.putNextEntry(zentry); 144 FileInputStream inputStream = new FileInputStream (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 |