PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/l10ntools/source/help/HelpIndexerTool.java

https://bitbucket.org/mst/ooo340
Java | 369 lines | 293 code | 40 blank | 36 comment | 66 complexity | e27f0ebe8b87ae0521ac63639a7f043c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * Copyright 2000, 2010 Oracle and/or its affiliates.
  6. *
  7. * OpenOffice.org - a multi-platform office productivity suite
  8. *
  9. * This file is part of OpenOffice.org.
  10. *
  11. * OpenOffice.org is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License version 3
  13. * only, as published by the Free Software Foundation.
  14. *
  15. * OpenOffice.org is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License version 3 for more details
  19. * (a copy is included in the LICENSE file that accompanied this code).
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * version 3 along with OpenOffice.org. If not, see
  23. * <http://www.openoffice.org/license.html>
  24. * for a copy of the LGPLv3 License.
  25. *
  26. ************************************************************************/
  27. package com.sun.star.help;
  28. import java.io.FileInputStream;
  29. import java.io.FileOutputStream;
  30. import java.util.Arrays;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.zip.ZipEntry;
  34. import java.util.zip.ZipOutputStream;
  35. import java.util.zip.CRC32;
  36. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  37. import org.apache.lucene.analysis.cjk.CJKAnalyzer;
  38. import org.apache.lucene.analysis.Analyzer;
  39. import org.apache.lucene.index.IndexWriter;
  40. import java.io.File;
  41. import java.io.FileNotFoundException;
  42. import java.io.IOException;
  43. import java.util.Date;
  44. public class HelpIndexerTool
  45. {
  46. public HelpIndexerTool()
  47. {
  48. }
  49. /**
  50. * @param args the command line arguments
  51. */
  52. public static void main( String[] args )
  53. {
  54. boolean bExtensionMode = false;
  55. mainImpl( args, bExtensionMode );
  56. }
  57. public static void mainImpl( String[] args, boolean bExtensionMode )
  58. {
  59. String aDirToZipStr = "";
  60. String aSrcDirStr = "";
  61. String aLanguageStr = "";
  62. String aModule = "";
  63. String aTargetZipFileStr = "";
  64. String aCfsName = "";
  65. // Scan arguments
  66. boolean bLang = false;
  67. boolean bMod = false;
  68. boolean bZipDir = false;
  69. boolean bSrcDir = false;
  70. boolean bOutput = false;
  71. boolean bCfsName = false;
  72. int nArgCount = args.length;
  73. for( int i = 0 ; i < nArgCount ; i++ )
  74. {
  75. if( "-lang".equals(args[i]) )
  76. {
  77. if( i + 1 < nArgCount )
  78. {
  79. aLanguageStr = args[i + 1];
  80. bLang = true;
  81. }
  82. i++;
  83. }
  84. else if( "-mod".equals(args[i]) )
  85. {
  86. if( i + 1 < nArgCount )
  87. {
  88. aModule = args[i + 1];
  89. bMod = true;
  90. }
  91. i++;
  92. }
  93. else if( "-zipdir".equals(args[i]) )
  94. {
  95. if( i + 1 < nArgCount )
  96. {
  97. aDirToZipStr = args[i + 1];
  98. bZipDir = true;
  99. }
  100. i++;
  101. }
  102. else if( "-srcdir".equals(args[i]) )
  103. {
  104. if( i + 1 < nArgCount )
  105. {
  106. aSrcDirStr = args[i + 1];
  107. bSrcDir = true;
  108. }
  109. i++;
  110. }
  111. else if( "-o".equals(args[i]) )
  112. {
  113. if( i + 1 < nArgCount )
  114. {
  115. aTargetZipFileStr = args[i + 1];
  116. bOutput = true;
  117. }
  118. i++;
  119. }
  120. else if( "-checkcfsname".equals(args[i]) )
  121. {
  122. if( i + 1 < nArgCount )
  123. {
  124. aCfsName = args[i + 1] + ".cfs";
  125. bCfsName = true;
  126. }
  127. i++;
  128. }
  129. }
  130. if( !bLang || !bMod || !bZipDir || (!bOutput && !bExtensionMode) )
  131. {
  132. if( bExtensionMode )
  133. return;
  134. System.out.println("Usage: HelpIndexer -lang ISOLangCode -mod HelpModule -zipdir TempZipDir -o OutputZipFile");
  135. System.exit( -1 );
  136. }
  137. String aIndexDirName = aModule + ".idxl";
  138. File aIndexDir = new File( aDirToZipStr + File.separator + aIndexDirName );
  139. if( !bSrcDir )
  140. aSrcDirStr = aDirToZipStr;
  141. File aCaptionFilesDir = new File( aSrcDirStr + File.separator + "caption" );
  142. File aContentFilesDir = new File( aSrcDirStr + File.separator + "content" );
  143. try
  144. {
  145. Date start = new Date();
  146. Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer() : (Analyzer)new StandardAnalyzer();
  147. IndexWriter writer = new IndexWriter( aIndexDir, analyzer, true );
  148. if( !bExtensionMode )
  149. System.out.println( "Lucene: Indexing to directory '" + aIndexDir + "'..." );
  150. int nRet = indexDocs( writer, aModule, bExtensionMode, aCaptionFilesDir, aContentFilesDir );
  151. if( nRet != -1 )
  152. {
  153. if( !bExtensionMode )
  154. {
  155. System.out.println();
  156. System.out.println( "Optimizing ..." );
  157. }
  158. writer.optimize();
  159. }
  160. writer.close();
  161. boolean bCfsFileOk = true;
  162. if( bCfsName && !bExtensionMode && nRet != -1 )
  163. {
  164. String aCompleteCfsFileName = aDirToZipStr + File.separator + aIndexDirName + File.separator + aCfsName;
  165. File aCfsFile = new File( aCompleteCfsFileName );
  166. bCfsFileOk = aCfsFile.exists();
  167. System.out.println( "Checking cfs file " + aCfsName+ ": " + (bCfsFileOk ? "Found" : "Not found") );
  168. }
  169. if( bExtensionMode )
  170. {
  171. if( !bSrcDir )
  172. {
  173. deleteRecursively( aCaptionFilesDir );
  174. deleteRecursively( aContentFilesDir );
  175. }
  176. }
  177. else
  178. {
  179. if( nRet == -1 )
  180. deleteRecursively( aIndexDir );
  181. if( bCfsFileOk )
  182. System.out.println( "Zipping ..." );
  183. File aDirToZipFile = new File( aDirToZipStr );
  184. createZipFile( aDirToZipFile, aTargetZipFileStr );
  185. deleteRecursively( aDirToZipFile );
  186. }
  187. if( !bCfsFileOk )
  188. {
  189. System.out.println( "cfs file check failed, terminating..." );
  190. System.exit( -1 );
  191. }
  192. Date end = new Date();
  193. if( !bExtensionMode )
  194. System.out.println(end.getTime() - start.getTime() + " total milliseconds");
  195. }
  196. catch (IOException e)
  197. {
  198. if( bExtensionMode )
  199. return;
  200. System.out.println(" caught a " + e.getClass() +
  201. "\n with message: " + e.getMessage());
  202. System.exit( -1 );
  203. }
  204. }
  205. private static int indexDocs(IndexWriter writer, String aModule, boolean bExtensionMode,
  206. File aCaptionFilesDir, File aContentFilesDir) throws IOException
  207. {
  208. if( !aCaptionFilesDir.canRead() || !aCaptionFilesDir.isDirectory() )
  209. {
  210. if( !bExtensionMode )
  211. System.out.println( "Not found: " + aCaptionFilesDir );
  212. return -1;
  213. }
  214. if( !aContentFilesDir.canRead() || !aContentFilesDir.isDirectory() )
  215. {
  216. if( !bExtensionMode )
  217. System.out.println( "Not found: " + aContentFilesDir );
  218. return -1;
  219. }
  220. String[] aCaptionFiles = aCaptionFilesDir.list();
  221. List aCaptionFilesList = Arrays.asList( aCaptionFiles );
  222. HashSet aCaptionFilesHashSet = new HashSet( aCaptionFilesList );
  223. String[] aContentFiles = aContentFilesDir.list();
  224. List aContentFilesList = Arrays.asList( aContentFiles );
  225. HashSet aContentFilesHashSet = new HashSet( aContentFilesList );
  226. // Loop over caption files and find corresponding content file
  227. if( !bExtensionMode )
  228. System.out.println( "Indexing, adding files" );
  229. int nCaptionFilesLen = aCaptionFiles.length;
  230. for( int i = 0 ; i < nCaptionFilesLen ; i++ )
  231. {
  232. String aCaptionFileStr = aCaptionFiles[i];
  233. File aCaptionFile = new File( aCaptionFilesDir, aCaptionFileStr );
  234. File aContentFile = null;
  235. if( aContentFilesHashSet.contains( aCaptionFileStr ) )
  236. aContentFile = new File( aContentFilesDir, aCaptionFileStr );
  237. if( !bExtensionMode )
  238. System.out.print( "." );
  239. writer.addDocument( HelpFileDocument.Document( aModule, aCaptionFile, aContentFile ) );
  240. }
  241. // Loop over content files to find remaining files not mapped to caption files
  242. int nContentFilesLen = aContentFiles.length;
  243. for( int i = 0 ; i < nContentFilesLen ; i++ )
  244. {
  245. String aContentFileStr = aContentFiles[i];
  246. if( !aCaptionFilesHashSet.contains( aContentFileStr ) )
  247. {
  248. // Not already handled in caption files loop
  249. File aCaptionFile = null;
  250. File aContentFile = new File( aContentFilesDir, aContentFileStr );
  251. if( !bExtensionMode )
  252. System.out.print( "." );
  253. writer.addDocument( HelpFileDocument.Document( aModule, aCaptionFile, aContentFile ) );
  254. }
  255. }
  256. return 0;
  257. }
  258. public static void createZipFile( File aDirToZip, String aTargetZipFileStr )
  259. throws FileNotFoundException, IOException
  260. {
  261. FileOutputStream fos = new FileOutputStream( aTargetZipFileStr );
  262. ZipOutputStream zos = new ZipOutputStream( fos );
  263. File[] aChildrenFiles = aDirToZip.listFiles();
  264. int nFileCount = aChildrenFiles.length;
  265. for( int i = 0 ; i < nFileCount ; i++ )
  266. addToZipRecursively( zos, aChildrenFiles[i], null );
  267. zos.close();
  268. }
  269. public static void addToZipRecursively( ZipOutputStream zos, File aFile, String aBasePath )
  270. throws FileNotFoundException, IOException
  271. {
  272. if( aFile.isDirectory() )
  273. {
  274. String aDirName = aFile.getName();
  275. if( aDirName.equalsIgnoreCase( "caption" ) || aDirName.equalsIgnoreCase( "content" ) )
  276. return;
  277. File[] aChildrenFiles = aFile.listFiles();
  278. String aNewBasePath = "";
  279. if( aBasePath != null )
  280. aNewBasePath += aBasePath + File.separator;
  281. aNewBasePath += aDirName;
  282. int nFileCount = aChildrenFiles.length;
  283. for( int i = 0 ; i < nFileCount ; i++ )
  284. addToZipRecursively( zos, aChildrenFiles[i], aNewBasePath );
  285. return;
  286. }
  287. // No directory
  288. // read contents of file we are going to put in the zip
  289. int fileLength = (int) aFile.length();
  290. FileInputStream fis = new FileInputStream( aFile );
  291. byte[] wholeFile = new byte[fileLength];
  292. int bytesRead = fis.read( wholeFile, 0, fileLength );
  293. fis.close();
  294. String aFileName = aFile.getName();
  295. String aEntryName = "";
  296. if( aBasePath != null )
  297. aEntryName += aBasePath + "/";
  298. aEntryName += aFileName;
  299. ZipEntry aZipEntry = new ZipEntry( aEntryName );
  300. aZipEntry.setTime( aFile.lastModified() );
  301. aZipEntry.setSize( fileLength );
  302. int nMethod = ( aFileName.toLowerCase().endsWith( ".jar" ) )
  303. ? ZipEntry.STORED : ZipEntry.DEFLATED;
  304. aZipEntry.setMethod( nMethod );
  305. CRC32 tempCRC = new CRC32();
  306. tempCRC.update( wholeFile, 0, wholeFile.length );
  307. aZipEntry.setCrc( tempCRC.getValue() );
  308. // write the contents into the zip element
  309. zos.putNextEntry( aZipEntry );
  310. zos.write( wholeFile, 0, fileLength );
  311. zos.closeEntry();
  312. }
  313. static public boolean deleteRecursively( File aFile )
  314. {
  315. if( aFile.isDirectory() )
  316. {
  317. File[] aChildrenFiles = aFile.listFiles();
  318. int nFileCount = aChildrenFiles.length;
  319. for( int i = 0 ; i < nFileCount ; i++ )
  320. {
  321. File aChildrenFile = aChildrenFiles[i];
  322. boolean bSuccess = deleteRecursively( aChildrenFile );
  323. if( !bSuccess )
  324. return false;
  325. }
  326. }
  327. return aFile.delete();
  328. }
  329. }