leosutic    2003/08/14 15:11:16

  Modified:    attributes/compiler/src/java/org/apache/avalon/attributes/compiler
                        AttributeCompiler.java
  Added:       attributes/compiler/src/java/org/apache/avalon/attributes/compiler
                        AttributeIndexer.java
  Log:
  Added support for building indexes of which classes have which attributes.
  This will speed up the query "Which classes have attribute X" significantly.
  
  Revision  Changes    Path
  1.3       +20 -7     
avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeCompiler.java
  
  Index: AttributeCompiler.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeCompiler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AttributeCompiler.java    10 Aug 2003 21:47:44 -0000      1.2
  +++ AttributeCompiler.java    14 Aug 2003 22:11:16 -0000      1.3
  @@ -44,16 +44,21 @@
       }
       
       protected void addExpressions (Collection tags, PrintWriter pw, String 
collectionName, String fileName) {
  +        fileName = fileName.replace ('\\', '/');
           Iterator iter = tags.iterator ();
           while (iter.hasNext ()) {
               XTag tag = (XTag) iter.next ();
               
  -            String expression = tag.getName () + " " + tag.getValue ();
  -            if (!expression.endsWith (")")) {
  -                expression = expression + "()";
  -            }
  -            
  -            if (Character.isUpperCase (expression.charAt (0))) {
  +            if (isAttribute (tag)) {
  +                String expression = tag.getName () + " " + tag.getValue ();
  +                if (expression.startsWith ("@")) {
  +                    expression = expression.substring (1);
  +                }
  +                
  +                if (!expression.endsWith (")")) {
  +                    expression = expression + "()";
  +                }
  +                
                   pw.println ("        " + collectionName + ".add (\n" +
                       "new " + expression + " // " + fileName + ":" + 
tag.getLineNumber () + "\n" +
                       ");");
  @@ -229,9 +234,17 @@
           return false;
       }
   
  +    protected boolean isClassName (String name) {
  +        StringTokenizer tok = new StringTokenizer (name, ".");
  +        String lastToken = null;
  +        while (tok.hasMoreTokens ()) {
  +            lastToken = tok.nextToken ();
  +        }
  +        return Character.isUpperCase (lastToken.charAt (0));
  +    }
       
       protected boolean isAttribute (XTag tag) {
  -        return Character.isUpperCase (tag.getName ().charAt (0));
  +        return tag.getName ().charAt (0) == '@' || isClassName (tag.getName ());
       }
       
       protected void start() throws BuildException {
  
  
  
  1.1                  
avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeIndexer.java
  
  Index: AttributeIndexer.java
  ===================================================================
  package org.apache.avalon.attributes.compiler;
  
  import java.io.*;
  import java.net.URL;
  import java.net.URLClassLoader;
  import org.apache.avalon.attributes.AttributeRepositoryClass;
  import org.apache.avalon.attributes.Attributes;
  import org.apache.avalon.attributes.Indexed;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.types.FileSet;
  
  import xjavadoc.*;
  import xjavadoc.ant.*;
  import java.util.*;
  
  /**
   * Ant task to compile attributes.
   */
  public class AttributeIndexer extends Task {
      
      private final ArrayList fileSets = new ArrayList ();
      private File destFile;
      private HashMap attributes = new HashMap ();
      
      public AttributeIndexer () {
      }
      
      protected void addAttribute (String className, String attributeName) {
          HashSet thisAttr = (HashSet) attributes.get (attributeName);
          if (thisAttr == null) {
              thisAttr = new HashSet ();
              attributes.put (attributeName, thisAttr);
          }
          thisAttr.add (className);
      }
      
      public void addFileset (FileSet set) {
          fileSets.add (set);
      }
      
      
      public void setDestfile (File destFile) {
          this.destFile = destFile;
      }
      
      private static final String SUFFIX = "$__org_apache_avalon_Attributes";
      private static final String CLASS_SUFFIX = SUFFIX + ".class";
      private static final String SOURCE_SUFFIX = SUFFIX + ".java";
      
      public void execute () throws BuildException {
          try {
              
              ArrayList urls = new ArrayList ();
              Iterator iter = fileSets.iterator ();
              while (iter.hasNext ()) {
                  FileSet fs = (FileSet) iter.next ();
                  urls.add (fs.getDir (project).toURL ());
              }
              
              ClassLoader cl = new URLClassLoader ((URL[]) urls.toArray (new URL[0]), 
this.getClass ().getClassLoader ());
              
              iter = fileSets.iterator ();
              while (iter.hasNext ()) {
                  FileSet fs = (FileSet) iter.next ();
                  File fromDir = fs.getDir(project);
                  
                  String[] srcFiles = 
fs.getDirectoryScanner(project).getIncludedFiles();
                  
                  for (int i = 0; i < srcFiles.length; i++) {
                      String className = srcFiles[i].replace ('/', '.').replace ('\\', 
'.');
                      if (className.endsWith (CLASS_SUFFIX)) {
                          String baseClassName = className.substring (0, 
className.length () - CLASS_SUFFIX.length ());
                          className = className.substring (0, className.length () - 6);
                          Class repoClass = cl.loadClass (className);
                          AttributeRepositoryClass repo = (AttributeRepositoryClass) 
repoClass.newInstance ();
                          Collection classAttrs = repo.getClassAttributes ();
                          
                          Collection indexedAttrs = new HashSet ();
                          Iterator inner = classAttrs.iterator ();
                          while (inner.hasNext ()) {
                              indexedAttrs.add (inner.next ().getClass ());
                          }
                          
                          indexedAttrs = Attributes.getClassesWithAttributeType 
(indexedAttrs, Indexed.class);
                          
                          inner = indexedAttrs.iterator ();
                          while (inner.hasNext ()) {
                              addAttribute (baseClassName, ((Class) inner.next 
()).getName ());
                          }
                      }
                  }
              }   
              
              destFile.getParentFile ().mkdirs ();
              PrintWriter pw = new PrintWriter (new FileWriter (destFile));
              try {
                  Iterator attrs = attributes.keySet ().iterator ();
                  while (attrs.hasNext ()) {
                      String attrName = (String) attrs.next ();
                      pw.println ("Attribute: " + attrName);
                      Iterator classes = ((Collection) attributes.get 
(attrName)).iterator ();
                      while (classes.hasNext ()) {
                          pw.println ("Class: " + classes.next ());
                      }
                      pw.println ();
                  }
              } finally {
                  pw.close ();
              }
          } catch (Exception e) {
              e.printStackTrace ();
              throw new BuildException (e.toString ());
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to