1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.*; 23 import java.lang.reflect.*; 24 import java.util.*; 25 26 import org.apache.tools.ant.AntClassLoader; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.DirectoryScanner; 29 import org.apache.tools.ant.FileScanner; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.taskdefs.Copy; 32 import org.apache.tools.ant.taskdefs.Delete; 33 import org.apache.tools.ant.taskdefs.MatchingTask; 34 import org.apache.tools.ant.taskdefs.Mkdir; 35 import org.apache.tools.ant.types.FileSet; 36 import org.apache.tools.ant.types.Mapper; 37 import org.apache.tools.ant.types.Path; 38 39 43 48 public class JHIndexer extends MatchingTask { 49 50 private Path classpath; 51 private File db; 52 private File basedir; 53 private String locale; 54 private List<BrandedFileSet> brandings = new LinkedList<BrandedFileSet>(); 55 56 57 public Path createClasspath() { 58 if (classpath == null) { 61 classpath = new Path(getProject()); 62 } 63 return classpath.createPath(); 64 } 65 66 70 public void setDb (File db) { 71 this.db = db; 72 } 73 74 77 public void setBasedir (File basedir) { 78 this.basedir = basedir; 79 } 80 public void setLocale (String locale) { 81 this.locale = locale; 82 } 83 84 88 public static final class BrandedFileSet extends FileSet { 89 String branding; 90 public void setBranding(String b) { 91 this.branding = b; 92 } 93 } 94 95 152 public void addBrandedFileSet(BrandedFileSet s) { 153 brandings.add(s); 154 } 155 156 157 @Deprecated 158 public void setJhall(File f) { 159 log("The 'jhall' attribute to <jhindexer> is deprecated. Use a nested <classpath> instead.", Project.MSG_WARN); 160 createClasspath().setLocation(f); 161 } 162 163 166 public void setClassPath(Path cp) { 167 classpath = cp; 168 } 169 170 public void execute () throws BuildException { 171 if (classpath == null) throw new BuildException ("Must specify the classpath attribute to find jhall.jar or jsearch.jar"); 172 if (db == null) throw new BuildException ("Must specify the db attribute"); 173 if (basedir == null) throw new BuildException ("Must specify the basedir attribute"); 174 FileScanner scanner = getDirectoryScanner (basedir); 175 scanner.scan (); 176 String [] files = scanner.getIncludedFiles (); 177 if (basedir.exists () && db.exists ()) { 179 long lastModified = Long.MIN_VALUE; 180 FileScanner output = new DirectoryScanner (); 182 output.setBasedir (db); 183 output.scan (); 184 String [] outfiles = output.getIncludedFiles (); 185 if (outfiles.length > 0) { 186 for (int i = 0; i < outfiles.length; i++) { 187 long mod = new File (db, outfiles[i]).lastModified (); 188 if (mod > lastModified) { 189 lastModified = mod; 190 } 191 } 192 boolean ok = true; 194 for (int i = 0; i < files.length; i++) { 195 long mod = new File (basedir, files[i]).lastModified (); 196 if (mod > lastModified) { 197 ok = false; 198 break; 199 } 200 } 201 if (!brandings.isEmpty()) { 202 for(FileSet fs: brandings) { 204 FileScanner scanner2 = fs.getDirectoryScanner(getProject()); 205 String [] files2 = scanner2.getIncludedFiles(); 206 for (int i = 0; i < files2.length; i++) { 207 long mod = new File (basedir, files2[i]).lastModified (); 208 if (mod > lastModified) { 209 ok = false; 210 break; 211 } 212 } 213 } 214 } 215 if (ok) { 216 return; 218 } 219 } 220 } 221 Delete delete = (Delete) getProject().createTask("delete"); 222 delete.setDir (db); 223 delete.init (); 224 delete.setLocation(getLocation()); 225 delete.execute (); 226 Mkdir mkdir = (Mkdir) getProject().createTask("mkdir"); 227 mkdir.setDir (db); 228 mkdir.init (); 229 mkdir.setLocation(getLocation()); 230 mkdir.execute (); 231 String maxbranding = null; 232 if (!brandings.isEmpty()) { 233 File tmp = new File(System.getProperty("java.io.tmpdir"), "jhindexer-branding-merge"); 239 delete = (Delete) getProject().createTask("delete"); 240 delete.setDir(tmp); 241 delete.init(); 242 delete.setLocation(getLocation()); 243 delete.execute(); 244 tmp.mkdir(); 245 Copy copy = (Copy) getProject().createTask("copy"); 247 copy.setTodir(tmp); 248 copy.addFileset(fileset); 249 copy.init(); 250 copy.setLocation(getLocation()); 251 copy.execute(); 252 class BrandingLengthComparator implements Comparator<BrandedFileSet> { 255 public int compare(BrandedFileSet a, BrandedFileSet b) { 256 return a.branding.length() - b.branding.length(); 257 } 258 } 259 Collections.sort(brandings, new BrandingLengthComparator()); 260 for (BrandedFileSet s: brandings) { 261 if (maxbranding != null && !s.branding.startsWith(maxbranding + "_")) { 262 throw new BuildException("Illegal branding: " + s.branding, getLocation()); 263 } 264 maxbranding = s.branding; String [] suffixes = { 266 ".html", 267 ".htm", 268 ".xhtml", 269 }; 272 for (int i = 0; i < suffixes.length; i++) { 273 String suffix = suffixes[i]; 274 copy = (Copy) getProject().createTask("copy"); 275 copy.setTodir(tmp); 276 copy.setOverwrite(true); 277 copy.addFileset(s); 278 Mapper m = copy.createMapper(); 279 Mapper.MapperType mt = new Mapper.MapperType(); 280 mt.setValue("glob"); 281 m.setType(mt); 282 m.setFrom("*_" + s.branding + suffix); 283 m.setTo("*" + suffix); 284 copy.init(); 285 copy.setLocation(getLocation()); 286 copy.execute(); 287 if (locale != null) { 288 suffix = "_" + locale + suffix; 290 copy = (Copy) getProject().createTask("copy"); 291 copy.setTodir(tmp); 292 copy.setOverwrite(true); 293 copy.addFileset(s); 294 m = copy.createMapper(); 295 mt = new Mapper.MapperType(); 296 mt.setValue("glob"); 297 m.setType(mt); 298 m.setFrom("*_" + s.branding + suffix); 299 m.setTo("*" + suffix); 300 copy.init(); 301 copy.setLocation(getLocation()); 302 copy.execute(); 303 } 304 } 305 } 306 basedir = tmp; 308 FileSet tmpf = new FileSet(); 309 tmpf.setProject(getProject()); 310 tmpf.setDir(tmp); 311 files = tmpf.getDirectoryScanner(getProject()).getIncludedFiles(); 312 } 313 log ("Running JavaHelp search database indexer..."); 314 try { 315 File config = File.createTempFile ("jhindexer-config", ".txt"); 316 try { 317 OutputStream os = new FileOutputStream (config); 318 try { 319 PrintWriter pw = new PrintWriter (os); 320 pw.println ("IndexRemove " + basedir + File.separator); 321 String message = "Files to be indexed:"; 322 for (int i = 0; i < files.length; i++) { 323 String path = basedir + File.separator + files[i]; 326 pw.println ("File " + path); 327 message += "\n\t" + path; 328 } 329 log (message, Project.MSG_VERBOSE); 330 pw.flush (); 331 } finally { 332 os.close (); 333 } 334 AntClassLoader loader = new AntClassLoader(getProject(), classpath); 335 try { 336 Class <?> clazz = loader.loadClass("com.sun.java.help.search.Indexer"); 337 Method main = clazz.getMethod("main", String [].class); 338 List<String > args = Arrays.asList( 339 "-c", config.getAbsolutePath(), 340 "-db", db.getAbsolutePath() 341 ); 342 if (locale != null) { 343 args = new ArrayList<String >(args); args.add("-locale"); 345 args.add(locale); 346 } 347 main.invoke(null, new Object [] {args.toArray(new String [args.size()])}); 348 } catch (InvocationTargetException ite) { 349 throw new BuildException("Could not run indexer", ite.getTargetException(), getLocation()); 350 } catch (Exception e) { throw new BuildException("Could not run indexer", e, getLocation()); 352 } 353 } finally { 354 config.delete (); 355 } 356 } catch (IOException ioe) { 357 throw new BuildException("Could not make temporary config file", ioe, getLocation()); 358 } 359 if (maxbranding != null) { 360 String [] dbfiles = db.list(); 363 for (int i = 0; i < dbfiles.length; i++) { 364 String basename, ext; 365 int idx = dbfiles[i].lastIndexOf('.'); 366 if (idx != -1) { 367 basename = dbfiles[i].substring(0, idx); 368 ext = dbfiles[i].substring(idx); 369 } else { 370 basename = dbfiles[i]; 371 ext = ""; 372 } 373 File old = new File(db, dbfiles[i]); 374 File nue = new File(db, basename + "_" + maxbranding + ext); 375 log("Moving " + old + " to " + nue, Project.MSG_VERBOSE); 376 old.renameTo(nue); 377 } 378 } 379 } 380 381 } 382 | Popular Tags |