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

  1. /*
  2.  * @(#)DirectoryManager.java    1.11 98/09/22
  3.  *
  4.  * Copyright 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 com.sun.javadoc.*;
  18. import java.io.*;
  19. import java.lang.*;
  20.  
  21.  
  22. /** 
  23.  * Handle the directory creations and the path string generations.
  24.  *
  25.  * @since JDK1.2
  26.  * @author Atul M Dambalkar
  27.  */
  28. public class DirectoryManager {
  29.   
  30.     /**
  31.      * The file separator string, "/", used in the formation of the URL path.
  32.      */
  33.     public static final String urlfileseparator = "/";
  34.  
  35.     /**
  36.      * The actual platform dependent file separator.
  37.      * 
  38.      * @see java.io.File.separator
  39.      */
  40.     public static final String fileseparator = File.separator;
  41.  
  42.     /**
  43.      * Given a PackageDoc, return its URL path string.
  44.      * 
  45.      * @param pd PackageDoc
  46.      * @see #getPath(String)
  47.      */
  48.     public static String createPathString(PackageDoc pd) {
  49.         if (pd == null) {
  50.             return "";
  51.         }
  52.         return getPath(pd.name());
  53.     }
  54.  
  55.     /**
  56.      * Given a ClassDoc, return its URL path string.
  57.      * 
  58.      * @param cd ClassDoc
  59.      * @see #getPath(String)
  60.      */
  61.     public static String createPathString(ClassDoc cd) {
  62.         if (cd == null) {
  63.             return "";
  64.         }
  65.         PackageDoc pd = cd.containingPackage();
  66.         return (pd == null)? "": getPath(pd.name());
  67.     }
  68.  
  69.     /**
  70.      * Given a PackageDoc, return the directory name. If name of the package
  71.      * is "java.lang" then path will be "java/lang". If name of the package is
  72.      * "no_interior_dot" then path will be just "no_interior_dot". The file 
  73.      * separator used is a platform dependent file separator.
  74.      *
  75.      * @param pd PackageDoc 
  76.      * @return String The directory path for the package.
  77.      */
  78.     public static String getDirectoryPath(PackageDoc pd) {
  79.         if (pd == null) {
  80.             return "";
  81.         }
  82.         String name = pd.name();
  83.         StringBuffer pathstr = new StringBuffer();
  84.         for (int i = 0; i < name.length(); i++) {
  85.             char ch = name.charAt(i);
  86.             if (ch == '.') {
  87.                 pathstr.append(fileseparator);
  88.             } else {
  89.                 pathstr.append(ch);
  90.             }
  91.         }
  92.         return pathstr.toString();
  93.     }
  94.  
  95.     /**
  96.      * Given any string, return the URL path string. For example, if the string
  97.      * is "com.sun.javadoc" then the URL path string will be "com/sun/javadoc"
  98.      * The URL path separator "/"(which is not platform specific) is used to 
  99.      * separate the directory names.
  100.      *
  101.      * @param name String.
  102.      * @return String URL path string.
  103.      */
  104.     public static String getPath(String name) {
  105.         if (name == null || name.length() == 0) {
  106.             return "";
  107.         }
  108.         StringBuffer pathstr = new StringBuffer();
  109.         for (int i = 0; i < name.length(); i++) {
  110.             char ch = name.charAt(i);
  111.             if (ch == '.') {
  112.                 pathstr.append(urlfileseparator);
  113.             } else {
  114.                 pathstr.append(ch);
  115.             }
  116.         }
  117.         return pathstr.toString();
  118.     }
  119.  
  120.     /**
  121.      * Given two strings/(package names) return the relative path from one 
  122.      * string to another. For example, if the parameter string "from" is 
  123.      * "java.lang" and parameter string "to" is "java.applet" the method will 
  124.      * return string "../../java/applet". The relative path returned is URL 
  125.      * specific, and hence it uses "/" as file separator.
  126.      * 
  127.      * @param from "from" this directory(package) location.
  128.      * @param to "to" this directory(package) location.
  129.      * @return String relative path from "from" to "to".
  130.      * @see #getRelativePath(String)
  131.      * @see #getPath(String)
  132.      */
  133.     public static String getRelativePath(String from, String to) {
  134.         StringBuffer pathstr = new StringBuffer();
  135.         pathstr.append(getRelativePath(from));
  136.         pathstr.append(getPath(to));
  137.         pathstr.append(urlfileseparator);
  138.         return pathstr.toString();
  139.     }
  140.  
  141.     /**
  142.      * Given string(package name), return relative path string. For example, 
  143.      * if the string "from" is "java.lang" the method will return "../../" 
  144.      * relative path, till the directory, in which is output is getting 
  145.      * generated. The returned string is URL specific. String from is nothing 
  146.      * but the directory location 
  147.      *
  148.      * @param from "from" this directory(package) location.
  149.      * @return String relative path from "from".
  150.      * @see #getRelativePath(String, String)
  151.      */
  152.     public static String getRelativePath(String from) {
  153.         if (from == null || from.length() == 0) {
  154.             return "";
  155.         }
  156.         StringBuffer pathstr = new StringBuffer();
  157.         for (int i = 0; i < from.length(); i++) {
  158.             char ch = from.charAt(i);
  159.             if (ch == '.') {
  160.                 pathstr.append(".." + urlfileseparator);
  161.             }     
  162.         }
  163.         pathstr.append(".." + urlfileseparator);
  164.         return pathstr.toString();
  165.     }
  166.  
  167.     /**
  168.      * Given a URL path string, this will return the reverse path. For 
  169.      * example, if the URL path string is "java/lang" the method will return 
  170.      * URL specific string "../".
  171.      */
  172.     public static String getBackPath(String path) {
  173.         if (path == null || path.length() == 0) {
  174.             return "";
  175.         }
  176.         StringBuffer backpath = new StringBuffer();
  177.         for (int i = 0; i < path.length(); i++) {
  178.             char ch = path.charAt(i);
  179.             if (ch == '/') {
  180.                 backpath.append("..");
  181.                 backpath.append(urlfileseparator);
  182.             }       // there is always a trailing fileseparator
  183.         }
  184.         return backpath.toString();
  185.     }
  186.  
  187.     /**
  188.      * Given a path string create all the directories in the path. For example,
  189.      * if the path string is "java/applet", the method will create directory 
  190.      * "java" and then "java/applet" if they don't exist. The file separator 
  191.      * string "/" is platform dependent system property.
  192.      * 
  193.      * @param path Directory path string.
  194.      */
  195.     public static void createDirectory(String path) {
  196.         if (path == null || path.length() == 0) {
  197.             return;
  198.         }
  199.         File dir = new File(path);
  200.         try {
  201.             if (dir.exists()) {
  202.                 return;
  203.             } else {
  204.                 if (dir.mkdirs()) {
  205.                     return;
  206.                 } else {
  207.                     HtmlDocWriter.configuration.message.error(
  208.                                   "doclet.Unable_to_create_directory_0", path);
  209.                     throw new DocletAbortException();
  210.                 }
  211.             }
  212.         } catch (SecurityException exc) {
  213.             exc.printStackTrace();
  214.             System.exit(1);
  215.         } catch (DocletAbortException exc) {
  216.             exc.printStackTrace();
  217.             System.exit(1);
  218.         } 
  219.     }
  220.  
  221.     /**
  222.      * Given a package name and a file name, return the full path to that file.
  223.      * For example, if PackageDoc passed is for "java.lang" and the filename 
  224.      * passed is "package-summary.html", then the string returned is
  225.      * "java/lang/package-summary.html".
  226.      * 
  227.      * @param pd         PackageDoc.
  228.      * @param filename   File name to be appended to the path of the package.
  229.      */
  230.     public static String getPathToPackage(PackageDoc pd, String filename) {
  231.         StringBuffer buf = new StringBuffer();  
  232.         String pathstr = createPathString(pd);
  233.         if (pathstr.length() > 0) {
  234.             buf.append(pathstr);
  235.             buf.append("/");
  236.         }
  237.         buf.append(filename);
  238.         return buf.toString();
  239.     }
  240.  
  241.     /**
  242.      * Given a class name return the full path to the class file.
  243.      * For example, if ClassDoc passed is for "java.lang.Object" then the 
  244.      * string returned is "java/lang/Object.html".
  245.      * 
  246.      * @param cd   ClassDoc.
  247.      */
  248.     public static String getPathToClass(ClassDoc cd) {
  249.         return getPathToPackage(cd.containingPackage(), cd.name() + ".html");
  250.     }
  251.  
  252. }
  253.