home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / tooldocs / javadoc / source / SourcePath.java < prev   
Encoding:
Java Source  |  1999-09-19  |  3.3 KB  |  122 lines

  1. /*
  2.  * @(#)SourcePath.java    1.3 98/09/22
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.tools.doclets;
  16.  
  17. import java.io.File;
  18.  
  19. /**
  20.  * This class is used to represent a source path which can contain only
  21.  * directories no zip files. If a zip file is specified in the command line it 
  22.  * will not get reflected in the SourcePath.
  23.  *
  24.  * @author Atul M Dambalkar
  25.  */
  26. public
  27. class SourcePath {
  28.     private final char dirSeparator = File.pathSeparatorChar;
  29.  
  30.     private final String fileSeparator = File.separator;
  31.  
  32.     /**
  33.      * The original class path string
  34.      */
  35.     private String pathstr;
  36.  
  37.     /**
  38.      * List of source path entries. Each entry is a directory.
  39.      */
  40.     private File[] sourcePath;
  41.  
  42.  
  43.     /**
  44.      * Build a source path from the specified path string on the command line.
  45.      */
  46.     public SourcePath(String pathstr) {
  47.     init(pathstr);
  48.     }
  49.  
  50.     /**
  51.      * Build a default source path from the path strings specified by
  52.      * the properties env.class.path.
  53.      */
  54.     public SourcePath() {
  55.         init(System.getProperty("env.class.path"));
  56.     }
  57.  
  58.     /**
  59.      * Initialize the SourcePath File array, which will contain only the 
  60.      * directory names from the given path string.
  61.      *
  62.      * @param pathstr Path String.
  63.      */
  64.     private void init(String pathstr) {
  65.         if (pathstr == null ||  pathstr.length() == 0) {
  66.             pathstr = ".";
  67.         }
  68.  
  69.     int noOfFileSep = 0;
  70.         int index = 0;
  71.     this.pathstr = pathstr; // Save original class path string
  72.  
  73.     // Count the number of path separators
  74.     while ((index = pathstr.indexOf(dirSeparator, index)) != -1) {
  75.         noOfFileSep++;
  76.             index++;
  77.     }
  78.     // Build the source path
  79.     File[] tempPath = new File[noOfFileSep + 1];
  80.         int tempPathIndex = 0;
  81.     int len = pathstr.length();
  82.         int sepPos;
  83.     for (index = 0; index < len; index = sepPos + 1) {
  84.         sepPos = pathstr.indexOf(dirSeparator, index);
  85.             if (sepPos < 0) {
  86.                 sepPos = len;
  87.         }
  88.             File file = new File(pathstr.substring(index, sepPos));
  89.         if (file.isDirectory()) {
  90.                tempPath[tempPathIndex++] = file;
  91.             } // if it is really a file, ignore it.
  92.     }
  93.     sourcePath = new File[tempPathIndex];
  94.     System.arraycopy((Object)tempPath, 0, (Object)sourcePath, 
  95.                          0, tempPathIndex);
  96.     }
  97.  
  98.     /**
  99.      * Find the specified directory in the source path.
  100.      * 
  101.      * @param name Name of the directory to be searched for in the source path.
  102.      * @return File Return the directory if found else return null.
  103.      */
  104.     public File getDirectory(String name) {
  105.         for (int i = 0; i < sourcePath.length; i++) {
  106.             File directoryNeeded = new File(sourcePath[i], name);
  107.             if (directoryNeeded.isDirectory()) {
  108.                 return directoryNeeded;
  109.             }
  110.         }         
  111.     return null;
  112.     }
  113.  
  114.     /**
  115.      * Return original source path string.
  116.      */
  117.     public String toString() {
  118.     return pathstr;
  119.     }
  120. }
  121.  
  122.