jvanzyl     2002/07/14 15:35:17

  Added:       src/plugins-build/dependency/src/java/org/apache/maven/dependency
                        BytecodeProjectResolver.java
                        DependencyResolver.java PackageProjectMap.java
                        ProjectResolver.java
  Log:
  o Moving to plugin.
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-maven/src/plugins-build/dependency/src/java/org/apache/maven/dependency/BytecodeProjectResolver.java
  
  Index: BytecodeProjectResolver.java
  ===================================================================
  package org.apache.maven.dependency;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  
  import java.util.Iterator;
  import java.util.List;
  import java.util.LinkedList;
  import java.util.jar.JarFile;
  import java.util.jar.JarEntry;
  import java.util.StringTokenizer;
  import java.util.Properties;
  import java.util.HashMap;
  
  import org.apache.maven.MavenUtils;
  import org.apache.maven.util.JarUtil;
  import org.apache.maven.importscrubber.ClassParserWrapper;
  import org.apache.maven.importscrubber.TreeMapListener;
  
  /**
   * <code>BytecodeProjectResolver</code> is an executor that will process
   * a directory looking for JAR and class files and turn the set
   * class references it finds into a set of project references
   * which can be used to create an accurate set of dependencies
   * in a project descriptor.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version 
   * $Id: BytecodeProjectResolver.java,v 1.1 2002/07/14 22:35:17 jvanzyl Exp $
   */
  public class BytecodeProjectResolver
  {
      /**
       * Base directory in which JAR files and class files will
       * be searched for or just a JAR file.
       */
      private File base;
      
      /**
       * Listener that registers class references as they are found by the 
       * {@link ClassParserWrapper}
       */
      private TreeMapListener treeMapListener;
      
      /**
       * Package references that are collected by mapping the class references
       * to package references.
       */
      private List packageReferences;
      
      /**
       * Packages to exclude from the final list of packages.
       */
      private List packageExcludes;
  
      /**
       * Package -> Project map.
       */
      private File mapFile;
  
      /**
       * Project references.
       */
      private List projectReferences;
  
      /**
       * Default constructor.
       */
      public BytecodeProjectResolver()
      {
          packageReferences = new LinkedList();
          packageExcludes = new LinkedList();
          treeMapListener = new TreeMapListener();
          projectReferences = new LinkedList();
      }
      
      /**
       * Set the packages to exclude from the final list found
       * by the resolution process.
       *
       * @param packageExcludesList comma separate list of the packages to exclude
       *   from the final list of packages.
       */
      public void setPackageExcludes(String packageExcludesList)
      {
          StringTokenizer st = new StringTokenizer(packageExcludesList, ",");
          while (st.hasMoreTokens())
          {
              String packageExclude = st.nextToken();
              packageExcludes.add(packageExclude);
          }
      }
  
      /**
       * Set the directory in which JARs and class files are
       * searched for.
       *
       * @param base Directory in which JARs and class files are searched for.
       */
      public void setBase(File base)
      {
          this.base = base;
      }        
  
      /**
       * Set the location of the package -> project map.
       *
       * @param mapFile Location of the package -> project map.
       */
      public void setMapFile(File mapFile)
      {
          this.mapFile = mapFile;
      }        
  
      /**
       * Get the package referenced determined by the resolution
       * process.
       *
       * @return List The list of packages references found.
       */
      public List getPackageReferences()
      {
          return packageReferences;
      }        
  
      /**
       * Get the projects referenced determined by the resolution
       * process.
       *
       * @return List The list of project references found.
       */
      public List getProjectReferences()
      {
          return projectReferences;
      }        
  
      /**
       * Resolve the package dependencies from the set of JARs
       * and classes used as the input.
       */
      public void resolveDependencies()
      {
          // Collect all the JAR and class file that we can find
          // in the base or just process the JAR.
          
          if (base.isDirectory())
          {
              String[] files = 
                  MavenUtils.getFiles(base.getAbsolutePath(), "*.jar,*.class");
          
              for (int i = 0; i < files.length; i++)
              {
                  String file = files[i];
              
                  if (file.endsWith("jar"))
                  {
                      processJAR(file);
                  }
                  else
                  {
                      processClassReference(file);
                  }
              }
          }
          else
          {
              processJAR(base.getAbsolutePath());
          }
          
          Properties map = new Properties();
          
          try
          {
              map.load(new FileInputStream(mapFile));
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
  
          HashMap projects = new HashMap();
  
          List packageReferences = getPackageReferences();
          for (Iterator i = packageReferences.iterator(); i.hasNext();)
          {
              String classReference = (String) i.next();
              
              for (Iterator j = map.keySet().iterator(); j.hasNext();)
              {
                  String packageKey = (String) j.next();
                  
                  if (classReference.indexOf(packageKey) == 0)
                  {
                      Object o = map.get(packageKey);
                      projects.put(o, o);
                  }
              }
          }            
          
          projectReferences.addAll(projects.values());
      }
      
      /**
       * Process an individual JAR file looking package
       * dependencies.
       * @param file the jar file to process
       */
      public void processJAR(String file)
      {
          JarFile jarFile = null;
          
          try
          {   
              jarFile = new JarFile(new File(file));
              List classEntries = JarUtil.getClassEntries(jarFile);
              
              treeMapListener.getTreeMap().clear();
              for (Iterator i = classEntries.iterator(); i.hasNext();)
              {
                  JarEntry jarEntry = (JarEntry) i.next();
                  InputStream classInputStream = jarFile.getInputStream(jarEntry);
                  ClassParserWrapper.parse(classInputStream, treeMapListener);
              }                
  
              jarFile.close();
              
              for (Iterator i = treeMapListener.getTreeMap().keySet().iterator();
                  i.hasNext();)
              {
                  String classReference = (String) i.next();
                  processClassReference(classReference);
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
      
      /**
       * Process an individual class file.
       *
       * @param classReference An individual class reference as a string.
       */
      private void processClassReference(String classReference)
      {
          // Don't care about inner classes.
          if (classReference.indexOf("$") < 0
              && classReference.startsWith("[") == false)
          {
              boolean includePackage = true;
                      
              for (Iterator j = packageExcludes.iterator(); j.hasNext();)
              {
                  String packageExclude = (String) j.next();
                  if (classReference.startsWith(packageExclude))
                  {
                      includePackage = false;
                      break;
                  }
              }
                      
              if (includePackage)
              {
                  packageReferences.add(
                      treeMapListener.getTreeMap().get(classReference));
              }                        
          }                    
      }    
      
      /**
       * Used for testing only.
       * @param args command line parameters
       * @throws Exception when an error occurs
       */
      public static void main(String[] args)
          throws Exception
      {
          ProjectResolver bdr = new ProjectResolver();
          bdr.setBase(new File(args[0]));
          bdr.setPackageExcludes(args[1]);
          bdr.setMapFile(new File(args[2]));
          bdr.resolveDependencies();
          
          List projectReferences = bdr.getProjectReferences();
          for (Iterator i = projectReferences.iterator(); i.hasNext();)
          {
              System.out.println(i.next());
          } 
      }
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/plugins-build/dependency/src/java/org/apache/maven/dependency/DependencyResolver.java
  
  Index: DependencyResolver.java
  ===================================================================
  package org.apache.maven.dependency;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  
  import java.util.List;
  import java.util.Iterator;
  import java.util.ArrayList;
  
  import org.apache.maven.MavenUtils;
  import org.apache.maven.project.Dependency;
  import org.apache.maven.project.Project;
  
  import org.apache.commons.graph.domain.dependency.DependencyGraph;
  
  public class DependencyResolver
  {
      /**
       * Dependency graph
       */
      private DependencyGraph dependencyGraph;
      
      private String projectsDirectory;
      private List projects;
      
      public DependencyResolver()
      {
          dependencyGraph = new DependencyGraph();
          projects = new ArrayList();
      }
  
      public void setProjectsDirectory(String projectsDirectory)
      {
          this.projectsDirectory = projectsDirectory;
      }        
  
      public void compute()
          throws Exception
      {
      }            
      
      public List getSortedDependencies(String projectId)
          throws Exception
      {
          String[] projectFiles = MavenUtils.getFiles(projectsDirectory, 
              "**/project.xml");
          
          for (int i = 0; i < projectFiles.length; i++)
          {
              Project project = MavenUtils.getProject(new File(projectFiles[i]));
              projects.add(project);
              List dependencies = project.getDependencies();
              
              List deps = new ArrayList();
              for (Iterator j = project.getDependencies().iterator(); 
                  j.hasNext();)
              {
                  Dependency dep = (Dependency) j.next();
                  deps.add(dep.getId());
              }
  
              dependencyGraph.addDependencies(project.getId(), deps);
          }
          
          return dependencyGraph.getSortedDependencies(projectId);
      }
      
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/plugins-build/dependency/src/java/org/apache/maven/dependency/PackageProjectMap.java
  
  Index: PackageProjectMap.java
  ===================================================================
  package org.apache.maven.dependency;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.util.Properties;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import org.apache.maven.MavenUtils;
  import org.apache.maven.project.Project;
  
  /**
   * Task for creating the package -> project map so that after
   * determining which classes are used in a project we can map
   * these classes to a set of projects which contain these
   * references.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: PackageProjectMap.java,v 1.1 2002/07/14 22:35:17 jvanzyl Exp $
   */
  public class PackageProjectMap
  {
      /** 
       * Log. 
       */
      private static final Log log = LogFactory.getLog(PackageProjectMap.class);
  
      /**
       * The directory where all the Maven descriptors are stored.
       */
      private File descriptorDir;
  
      /**
       * Where to write the resultant package -> project map.
       */
      private File map;
  
      /**
       * Set the directory where all the Maven descriptors are stored.
       *
       * @param descriptorDir The directory where all the Maven descriptors are
       *      stored.
       */
      public void setDescriptorDir(File descriptorDir)
      {
          this.descriptorDir = descriptorDir;
      }
  
      /**
       * Get the directory where all the Maven descriptors are stored.
       *
       * @return The directory where all the Maven descriptors are stored.
       */
      public File getDescriptorDir()
      {
          return descriptorDir;
      }
  
      /**
       * Where to write the resultant package -> project map
       *
       * @param map The file where the package -> project map is stored
       */
      public void setMap(File map)
      {
          this.map = map;
      }
  
      /**
       * Get where to write the resultant package -> project map
       *
       * @return The file to write the resultant package -> project map to
       */
      public File getMap()
      {
          return map;
      }
  
      /**
       * Execute the the overall maven opertion.
       *
       * @throws Exception when an error occurs
       */
      public void execute()
          throws Exception
      {
          if (descriptorDir == null)
          {
              throw new Exception("descriptorDir attribute must be specified!");
          }            
  
          if (map == null)
          {
              throw new Exception("map attribute must be specified!");
          }            
          
          String[] projects = 
              MavenUtils.getFiles(descriptorDir.getAbsolutePath(), "**/*.xml");
  
          log.debug("projects: " + projects.length);
  
          Properties m = new Properties();
          
          for (int i = 0; i < projects.length; i++)
          {
              Project p = MavenUtils.getProject(projects[i]);
              String projectPackage = p.getPackage();
              String projectId = p.getId();
              
              // Some of the projects may not have packages.
              if (projectPackage != null
                  && projectPackage.trim().length() > 0
                  && projectId != null)
              {
                  m.put(projectPackage, projectId);
              }                
          }
          
          FileOutputStream fos = null;
          try
          {
              fos = new FileOutputStream(map);
              m.store(fos, "Package -> Project Map");
          }
          finally
          {
              if (fos != null)
              {
                  fos.close();
              }
          }
      }
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/plugins-build/dependency/src/java/org/apache/maven/dependency/ProjectResolver.java
  
  Index: ProjectResolver.java
  ===================================================================
  package org.apache.maven.dependency;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.PrintStream;
  import java.util.Iterator;
  import java.util.List;
  import java.util.LinkedList;
  import java.util.StringTokenizer;
  import java.util.Properties;
  import java.util.HashMap;
  
  import org.apache.maven.MavenUtils;
  import org.apache.maven.java.SourceTool;
  import org.apache.maven.java.ImportStatementVisitor;
  
  /**
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: ProjectResolver.java,v 1.1 2002/07/14 22:35:17 jvanzyl Exp $
   */
  public class ProjectResolver
  {
      /**
       * Base directory in which JAR files and class files will be searched for or
       * just a JAR file.
       */
      private File base;
      /**
       * Package references that are collected by mapping the class references to
       * package references.
       */
      private List packageReferences;
      /** Packages to exclude from the final list of packages */
      private List packageExcludes;
      /** Package -> Project map */
      private File mapFile;
      /** Project references */
      private List projectReferences;
      /** used to process source code with a visitor */
      private SourceTool st;
      /** visitor to process import statements */
      private ImportStatementVisitor isv;
  
      /**
       * Default constructor.
       */
      public ProjectResolver()
      {
          st = new SourceTool();
          isv = new ImportStatementVisitor();
          st.setVisitor(isv);
  
          packageReferences = new LinkedList();
          packageExcludes = new LinkedList();
          projectReferences = new LinkedList();
      }
  
      /**
       * Set the packages to exclude from the final list found by the resolution
       * process.
       *
       * @param packageExcludesList comma separate list of the packages to
       *      exclude from the final list of packages.
       */
      public void setPackageExcludes(String packageExcludesList)
      {
          StringTokenizer st = new StringTokenizer(packageExcludesList, ",");
          while (st.hasMoreTokens())
          {
              String packageExclude = st.nextToken();
              packageExcludes.add(packageExclude);
          }
      }
  
      /**
       * Set the directory in which JARs and class files are searched for.
       *
       * @param base directory in which JARs and class files are searched for.
       */
      public void setBase(File base)
      {
          this.base = base;
      }
  
      public File getBase()
      {
          return base;
      }        
  
      /**
       * Set the location of the package -> project map.
       *
       * @param mapFile Location of the package -> project map.
       */
      public void setMapFile(File mapFile)
      {
          this.mapFile = mapFile;
      }
  
      /**
       * Get the package referenced determined by the resolution process.
       *
       * @return List The list of packages references found.
       */
      public List getPackageReferences()
      {
          return packageReferences;
      }
  
      /**
       * Get the projects referenced determined by the resolution process.
       *
       * @return List The list of project references found.
       */
      public List getProjectReferences()
      {
          return projectReferences;
      }
  
      /**
       * Resolve the package dependencies from the set of JARs and classes used as
       * the input.
       * @throws Exception when any error occurs
       */
      public void resolveDependencies()
          throws Exception
      {
          if (base.isDirectory())
          {
              String[] files =
                  MavenUtils.getFiles(base.getAbsolutePath(), "**/*.java");
              
              for (int i = 0; i < files.length; i++)
              {
                  String file = files[i];
                  
                  try
                  {
                      st.parse(new FileInputStream(file));
                  }
                  catch (Exception e)
                  {
                      // FIXME: Why is this ignored?
                  }
              }
          }
  
          for (Iterator j = isv.getImportStatements().iterator(); j.hasNext();)
          {   
              String classReference = (String) j.next();
              processClassReference(classReference);
          }
  
  
          Properties map = new Properties();
  
          try
          {
              map.load(new FileInputStream(mapFile));
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
  
          HashMap projects = new HashMap();
  
          List packageReferences = getPackageReferences();
          for (Iterator i = packageReferences.iterator(); i.hasNext();)
          {
              String classReference = (String) i.next();
  
              for (Iterator j = map.keySet().iterator(); j.hasNext();)
              {
                  String packageKey = (String) j.next();
  
                  if (classReference.indexOf(packageKey) == 0)
                  {
                      Object o = map.get(packageKey);
                      projects.put(o, o);
                  }
              }
          }
  
          projectReferences.addAll(projects.values());
      }
  
      /**
       * Process an individual class file.
       *
       * @param classReference An individual class reference in the form of a
       *      string.
       */
      private void processClassReference(String classReference)
      {
          boolean includePackage = true;
  
          for (Iterator j = packageExcludes.iterator(); j.hasNext();)
          {
              String packageExclude = (String) j.next();
              if (classReference.startsWith(packageExclude))
              {
                  includePackage = false;
                  break;
              }
          }
  
          if (includePackage)
          {
              packageReferences.add(classReference);
          }
      }
  
      public void execute()
          throws Exception
      {
          try
          {
              resolveDependencies();
  
              List projectReferences = getProjectReferences();
              
              File f = new File(getBase(), "deps.xml");
              PrintStream ps = new PrintStream(new FileOutputStream(
                  new File(getBase(), "deps.xml")));
              
              ps.println("<project>");
              for (Iterator i = projectReferences.iterator(); i.hasNext();)
              {
                  String projectReference = (String) i.next();            
              
                  ps.println("  <dependency>");
                  ps.println("    <id>" + projectReference + "</id>");
                  ps.println("    <version>???</version>");
                  ps.println("  </dependency>");
              }
  
              ps.println("</project>");
              ps.flush();
              ps.close();
              
          }
          catch (Exception e)
          {
              System.out.println(e);
          }            
          
      }
  }
  
  
  

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

Reply via email to