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

  1. /*
  2.  * @(#)Standard.java    1.34 98/08/18
  3.  *
  4.  * Copyright 1997, 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.standard;
  16.  
  17. import com.sun.tools.doclets.*;
  18. import com.sun.javadoc.*;
  19. import java.util.*;
  20. import java.io.*;
  21.  
  22. /**
  23.  * The class with "start" method, calls individual Writers.
  24.  *
  25.  * @author Atul M Dambalkar
  26.  * @author Robert Field
  27.  */
  28. public class Standard {
  29.  
  30.     /**
  31.      * The "start" method as required by Javadoc.
  32.      *
  33.      * @param Root
  34.      * @see com.sun.javadoc.Root
  35.      * @return boolean
  36.      */
  37.     public static boolean start(RootDoc root) throws IOException {
  38.         try { 
  39.             configuration().setOptions(root);
  40.             (new Standard()).startGeneration(root);
  41.         } catch (DocletAbortException exc) {
  42.             exc.printStackTrace();
  43.             return false; // message has already been displayed
  44.         }
  45.         return true;
  46.     }
  47.         
  48.     /**
  49.      * Return the configuration instance. Create if it doesn't exist.
  50.      * Override this method to use a different
  51.      * configuration.
  52.      */
  53.     public static ConfigurationStandard configuration() {
  54.         if (HtmlDocWriter.configuration == null) {
  55.             HtmlDocWriter.configuration = new ConfigurationStandard();
  56.         }
  57.         return (ConfigurationStandard)HtmlDocWriter.configuration;
  58.     }
  59.  
  60.     /**
  61.      * Start the generation of files. Call generate methods in the individual
  62.      * writers, which will in turn genrate the documentation files. Call the
  63.      * TreeWriter generation first to ensure the Class Hierarchy is built
  64.      * first and then can be used in the later generation.
  65.      *
  66.      * For new format.
  67.      *
  68.      * @see com.sun.javadoc.RootDoc
  69.      */
  70.     protected void startGeneration(RootDoc root) throws DocletAbortException {
  71.         if (root.classes().length == 0) {
  72.             configuration().standardmessage.
  73.                         notice("doclet.No_Public_Classes_To_Document");
  74.             return;
  75.         }
  76.  
  77.         String configdestdir = configuration().destdirname;
  78.         String confighelpfile = configuration().helpfile;
  79.         String configstylefile = configuration().stylesheetfile;
  80.         boolean nodeprecated = configuration().nodeprecated;
  81.  
  82.         performCopy(configdestdir, confighelpfile);
  83.         performCopy(configdestdir, configstylefile);
  84.  
  85.         ClassTree classtree = new ClassTree(root, nodeprecated);
  86.  
  87.         // do early to reduce memory footprint
  88.         if (configuration().classuse) {
  89.             ClassUseMapper.generate(root, classtree);
  90.         }
  91.  
  92.         IndexBuilder indexbuilder = new IndexBuilder(root, nodeprecated);
  93.         PackageDoc[] packages = configuration().packages;
  94.  
  95.         if (configuration().createtree) {
  96.             TreeWriter.generate(classtree);
  97.         }
  98.  
  99.         if (configuration().createindex) {
  100.             if (configuration().splitindex) {
  101.                 SplitIndexWriter.generate(indexbuilder);
  102.             } else {
  103.                 SingleIndexWriter.generate(indexbuilder);
  104.             }
  105.         }
  106.  
  107.         if (!(configuration().nodeprecatedlist || nodeprecated)) { 
  108.             DeprecatedListWriter.generate(root);
  109.         }
  110.  
  111.         AllClassesFrameWriter.generate(new IndexBuilder(root, nodeprecated, 
  112.                                                         true));
  113.  
  114.         FrameOutputWriter.generate();
  115.  
  116.         PackagesFileWriter.generate();
  117.             
  118.         if (configuration().createoverview) {
  119.             PackageIndexWriter.generate(root);
  120.         }
  121.         
  122.         if (packages.length > 1) {
  123.             PackageIndexFrameWriter.generate();
  124.         }
  125.  
  126.         for(int i = 0; i < packages.length; i++) {
  127.             PackageDoc prev = (i == 0)? 
  128.                               null:
  129.                               packages[i-1];
  130.             PackageDoc packagedoc = packages[i];
  131.             PackageDoc next = (i+1 == packages.length)? 
  132.                               null:
  133.                               packages[i+1];
  134.             PackageWriter.generate(packages[i], prev, next);
  135.             PackageTreeWriter.generate(packages[i], prev, next, nodeprecated);
  136.             PackageFrameWriter.generate(packages[i]);
  137.         }
  138.  
  139.         generateClassFiles(root, classtree);
  140.  
  141.         SerializedFormWriter.generate(root);
  142.  
  143.         PackageListWriter.generate(root);
  144.  
  145.         HelpWriter.generate();
  146.         StylesheetWriter.generate();
  147.  
  148.     }
  149.  
  150.     protected void generateClassFiles(RootDoc root, ClassTree classtree) 
  151.                                throws DocletAbortException {
  152.         ClassDoc[] classes = root.specifiedClasses();
  153.         List incl = new ArrayList();
  154.         for (int i = 0; i < classes.length; i++) {
  155.             ClassDoc cd = classes[i];
  156.             if (cd.isIncluded()) {
  157.                 incl.add(cd);
  158.             }
  159.         }
  160.         ClassDoc[] inclClasses = new ClassDoc[incl.size()];
  161.         for (int i = 0; i < inclClasses.length; i++) {
  162.             inclClasses[i] = (ClassDoc)incl.get(i);
  163.         }
  164.         generateClassCycle(inclClasses, classtree, true);
  165.         PackageDoc[] packages = configuration().packages;
  166.         for (int i = 0; i < packages.length; i++) {
  167.             PackageDoc pkg = packages[i];
  168.             generateClassCycle(pkg.interfaces(), classtree, false);
  169.             generateClassCycle(pkg.ordinaryClasses(), classtree, false);
  170.             generateClassCycle(pkg.exceptions(), classtree, false);
  171.             generateClassCycle(pkg.errors(), classtree, false);
  172.         }
  173.     }
  174.  
  175.     protected String classFileName(ClassDoc cd) {
  176.         return cd.qualifiedName() + ".html";
  177.     }
  178.  
  179.     /**
  180.      * Instantiate ClassWriter for each Class within the ClassDoc[]
  181.      * passed to it and generate Documentation for that.
  182.      */
  183.     protected void generateClassCycle(ClassDoc[] arr, ClassTree classtree,
  184.                             boolean nopackage) throws DocletAbortException {
  185.         Arrays.sort(arr);
  186.         for(int i = 0; i < arr.length; i++) {
  187.             if (configuration().nodeprecated && 
  188.                      arr[i].tags("deprecated").length > 0) {
  189.                 continue;
  190.             }
  191.             ClassDoc prev = (i == 0)? 
  192.                             null:
  193.                             arr[i-1];
  194.             ClassDoc curr = arr[i];
  195.             ClassDoc next = (i+1 == arr.length)? 
  196.                             null:
  197.                             arr[i+1];
  198.  
  199.             ClassWriter.generate(curr, prev, next, classtree, nopackage);
  200.         }
  201.     }
  202.  
  203.     /**
  204.      * Check for doclet added options here. 
  205.      *
  206.      * @return number of arguments to option. Zero return means
  207.      * option not known.  Negative value means error occurred.
  208.      */
  209.     public static int optionLength(String option) {
  210.         return configuration().optionLength(option);
  211.     }
  212.  
  213.     /**
  214.      * Check that options have the correct arguments here. 
  215.      * <P>
  216.      * This method is not required and will default gracefully
  217.      * (to true) if absent.
  218.      * <P>
  219.      * Printing option related error messages (using the provided
  220.      * DocErrorReporter) is the responsibility of this method.
  221.      *
  222.      * @return true if the options are valid.
  223.      */
  224.     public static boolean validOptions(String options[][], 
  225.                                        DocErrorReporter reporter) 
  226.                                 throws IOException {
  227.         return configuration().validOptions(options, reporter);
  228.     }
  229.  
  230.     /**
  231.      * Copy source file to destination file.
  232.      *
  233.      * @throws SecurityException
  234.      * @throws IOException
  235.      */
  236.     public static void copyFile(File destfile, File srcfile)
  237.                          throws DocletAbortException, IOException {
  238.         byte[] bytearr = new byte[512];
  239.         int len = 0;
  240.         FileInputStream input = new FileInputStream(srcfile);
  241.         FileOutputStream output = new FileOutputStream(destfile);
  242.         try {
  243.             while ((len = input.read(bytearr)) != -1) {
  244.                 output.write(bytearr, 0, len);
  245.             }
  246.         } catch (FileNotFoundException exc) {
  247.             throw new DocletAbortException();
  248.         } catch (SecurityException exc) {
  249.             throw new DocletAbortException();
  250.         } finally {
  251.             input.close();
  252.             output.close();
  253.         }
  254.     }
  255.  
  256.     protected void performCopy(String configdestdir, String filename) 
  257.                         throws DocletAbortException {
  258.         try {
  259.             String destdir = (configdestdir.length() > 0)? 
  260.                              destdir = configdestdir + File.separatorChar: ""; 
  261.             if (filename.length() > 0) {
  262.                 File helpstylefile = new File(filename);
  263.                 String parent = helpstylefile.getParent();
  264.                 String helpstylefilename = (parent == null)? 
  265.                            filename:
  266.                            filename.substring(parent.length() + 1);
  267.                 File desthelpfile = new File(destdir + helpstylefilename); 
  268.                 if (!desthelpfile.getCanonicalPath().equals(
  269.                                   helpstylefile.getCanonicalPath())) {
  270.                     configuration().standardmessage.
  271.                                       notice("doclet.Copying_File_0_To_File_1",
  272.                            helpstylefile.toString(), desthelpfile.toString());
  273.                     copyFile(desthelpfile, helpstylefile);
  274.                 }
  275.             }
  276.         } catch (IOException exc) {
  277.             configuration().standardmessage.
  278.                error("doclet.perform_copy_exception_encountered", 
  279.                                                    exc.toString());
  280.             throw new DocletAbortException();
  281.         }
  282.     }
  283.         
  284.  
  285.