|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.nbbuild;
import java.io.*;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.*;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Ant;
/** Generates a file with index of all files.
*
* @author Jaroslav Tulach
*/
public class JavadocIndex extends Task {
private File target;
private org.apache.tools.ant.types.FileSet set;
/** map of String(like name of package) -> List */
private Map classes = new HashMap (101);
/** The file to generate the index to.
*/
public void setTarget (File f) {
this.target = f;
}
/** List of indexes to search in.
*/
public void addPackagesList (org.apache.tools.ant.types.FileSet set)
throws BuildException {
if (this.set != null) {
throw new BuildException ("Package list can be associated only once");
}
this.set = set;
}
public void execute () throws org.apache.tools.ant.BuildException {
if (target == null) {
throw new BuildException ("Target must be set"); // NOI18N
}
if (set == null) {
throw new BuildException ("Set of files must be provided: " + set); // NOI18N
}
org.apache.tools.ant.DirectoryScanner scan = set.getDirectoryScanner (this.getProject ());
String[] files = scan.getIncludedFiles();
File bdir = scan.getBasedir();
for (int k=0; k XMLUtil([\\p{Alnum}\\.]*).*";
Pattern p = Pattern.compile (mask, Pattern.CASE_INSENSITIVE);
// group 1: relative URL to a class or interface
// group 2: interface or class string
// group 3: name of package
// group 4: name of class
int matches = 0;
for (;;) {
String line = is.readLine ();
if (line == null) break;
Matcher m = p.matcher (line);
if (m.matches ()) {
matches++;
log ("Accepted line: " + line, Project.MSG_DEBUG);
if (m.groupCount () != 4) {
StringBuffer sb = new StringBuffer ();
sb.append ("Line " + line + " has " + m.groupCount () + " groups and not four");
for (int i = 0; i <= m.groupCount (); i++) {
sb.append ("\n " + i + " grp: " + m.group (i));
}
throw new BuildException (sb.toString ());
}
Clazz c = new Clazz (
m.group (3),
m.group (4),
"interface".equals (m.group (2)),
urlPrefix + "/" + m.group (1)
);
if (c.name == null) throw new NullPointerException ("Null name for " + line + "\nclass: " + c);
if (c.name.length () == 0) throw new IllegalStateException ("Empty name for " + line + "\nclass: " + c);
log ("Adding class: " + c, Project.MSG_DEBUG);
List l = (List)classes.get (c.pkg);
if (l == null) {
l = new ArrayList ();
classes.put (c.pkg, l);
}
l.add (c);
} else {
log ("Refused line: " + line, Project.MSG_DEBUG);
}
}
if (matches == 0) {
throw new BuildException ("No classes defined in file: " + f);
}
} catch (java.io.IOException ex) {
throw new BuildException (ex);
}
}
private void printClasses (PrintStream ps) {
TreeSet allPkgs = new TreeSet (classes.keySet ());
Iterator it = allPkgs.iterator ();
while (it.hasNext ()) {
String pkg = (String)it.next ();
ps.println ("PKG " + pkg);
List list = (List)classes.get (pkg);
Collections.sort (list);
Iterator clss = list.iterator ();
while (clss.hasNext ()) {
Clazz c = (Clazz)clss.next ();
if (c.isInterface) {
ps.print ("INF");
} else {
ps.print ("CLS");
}
ps.print (" ");
ps.println (c.name);
ps.print ("URL ");
ps.println (c.url);
}
}
}
private void printClassesAsHtml (PrintStream ps) {
ps.println ("");
ps.println ("\nList of All Classes");
ps.println ();
TreeSet allPkgs = new TreeSet (classes.keySet ());
Iterator it = allPkgs.iterator ();
while (it.hasNext ()) {
String pkg = (String)it.next ();
ps.println ("" + pkg + "");
List list = (List)classes.get (pkg);
Collections.sort (list);
Iterator clss = list.iterator ();
while (clss.hasNext ()) {
Clazz c = (Clazz)clss.next ();
ps.print ("");
if (c.isInterface) {
ps.print ("");
}
ps.print (c.name);
if (c.isInterface) {
ps.print ("");
}
ps.println ("");
}
}
ps.println ("");
}
/** An information about one class in api */
private static final class Clazz extends Object
implements Comparable {
public final String pkg;
public final String name;
public final String url;
public final boolean isInterface;
public Clazz (String pkg, String name, boolean isInterface, String url) {
this.pkg = pkg;
this.name = name;
this.isInterface = isInterface;
this.url = url;
}
/** Compares based on class names */
public int compareTo (Object o) {
return name.compareTo (((Clazz)o).name);
}
public String toString () {
return "PKG: " + pkg + " NAME: " + name + " INTERFACE: " + isInterface + " url: " + url;
}
} // end of Clazz
}
|