001    package org.hackystat.sensor.ant.jdepend;
002    
003    import java.io.File;
004    import java.util.ArrayList;
005    import java.util.List;
006    
007    /**
008     * A utility for determining the file path associated with a package. 
009     * @author Philip Johnson
010     *
011     */
012    public class Package2Path {
013      /** Holds the list of sourcePaths. */
014      private List<String> sourcePaths = new ArrayList<String>();
015      /** The path separator character on this platform. */  
016      private char sep = System.getProperty("file.separator").charAt(0);
017      
018      /**
019       * Constructs a new Package2Path instance from a list of source file paths. 
020       * @param sourceFiles The source files from which the package paths are constructed.
021       */
022      public Package2Path(List<File> sourceFiles) {
023        for (File file : sourceFiles) {
024          String parentDir = file.getParent();
025          if (parentDir != null) {
026            sourcePaths.add(parentDir);
027          }
028        }
029      }
030      
031      /**
032       * Returns the source path associated with the package prefix, or null if not found. 
033       * @param packageString A string such as "foo.bar.baz"
034       * @return The corresponding path, such as "C:\foo\bar\baz", or null if it was not found.
035       */
036      public String getPath(String packageString) {
037        // first, convert the package string to a partial path.
038        String partialPath = packageString.replace('.', sep);
039        for (String path : sourcePaths) {
040          if (path.endsWith(partialPath)) {
041            return path;
042          }
043        }
044        return null;
045      }
046    }