src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/AbstractBuilder.java

Wed, 31 Oct 2012 13:48:15 -0700

author
jjg
date
Wed, 31 Oct 2012 13:48:15 -0700
changeset 1383
b980e8e6aabf
parent 1359
25e14ad23cef
child 1410
bfec2a1cc869
permissions
-rw-r--r--

8001664: refactor javadoc to use abstraction to handle files
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.doclets.internal.toolkit.builders;
    28 import java.io.*;
    29 import java.lang.reflect.*;
    30 import java.util.*;
    32 import com.sun.tools.doclets.internal.toolkit.*;
    33 import com.sun.tools.doclets.internal.toolkit.util.*;
    35 /**
    36  * The superclass for all builders.  A builder is a class that provides
    37  * the structure and content of API documentation.  A builder is completely
    38  * doclet independent which means that any doclet can use builders to
    39  * construct documentation, as long as it impelements the appropriate
    40  * writer interfaces.  For example, if a doclet wanted to use
    41  * {@link ConstantsSummaryBuilder} to build a constant summary, all it has to
    42  * do is implement the ConstantsSummaryWriter interface and pass it to the
    43  * builder using a WriterFactory.
    44  *
    45  *  <p><b>This is NOT part of any supported API.
    46  *  If you write code that depends on this, you do so at your own risk.
    47  *  This code and its internal interfaces are subject to change or
    48  *  deletion without notice.</b>
    49  *
    50  * @author Jamie Ho
    51  * @since 1.5
    52  */
    54 public abstract class AbstractBuilder {
    56     /**
    57      * The configuration used in this run of the doclet.
    58      */
    59     protected Configuration configuration;
    61     /**
    62      * Keep track of which packages we have seen for
    63      * efficiency purposes.  We don't want to copy the
    64      * doc files multiple times for a single package.
    65      */
    66     protected static Set<String> containingPackagesSeen;
    68     /**
    69      * True if we want to print debug output.
    70      */
    71     protected static final boolean DEBUG = false;
    73     /**
    74      * Construct a Builder.
    75      * @param configuration the configuration used in this run
    76      *        of the doclet.
    77      */
    78     public AbstractBuilder(Configuration configuration) {
    79         this.configuration = configuration;
    80     }
    82     /**
    83      * Return the name of this builder.
    84      *
    85      * @return the name of the builder.
    86      */
    87     public abstract String getName();
    89     /**
    90      * Build the documentation.
    91      *
    92      * @throws IOException there was a problem writing the output.
    93      */
    94     public abstract void build() throws IOException;
    96     /**
    97      * Build the documentation, as specified by the given XML element.
    98      *
    99      * @param node the XML element that specifies which component to document.
   100      * @param contentTree content tree to which the documentation will be added
   101      */
   102     protected void build(XMLNode node, Content contentTree) {
   103         String component = node.name;
   104         try {
   105             invokeMethod("build" + component,
   106                     new Class<?>[]{XMLNode.class, Content.class},
   107                     new Object[]{node, contentTree});
   108         } catch (NoSuchMethodException e) {
   109             e.printStackTrace();
   110             configuration.root.printError("Unknown element: " + component);
   111             throw new DocletAbortException();
   112         } catch (InvocationTargetException e) {
   113             e.getCause().printStackTrace();
   114         } catch (Exception e) {
   115             e.printStackTrace();
   116             configuration.root.printError("Exception " +
   117                     e.getClass().getName() +
   118                     " thrown while processing element: " + component);
   119             throw new DocletAbortException();
   120         }
   121     }
   123     /**
   124      * Build the documentation, as specified by the children of the given XML element.
   125      *
   126      * @param node the XML element that specifies which components to document.
   127      * @param contentTree content tree to which the documentation will be added
   128      */
   129     protected void buildChildren(XMLNode node, Content contentTree) {
   130         for (XMLNode child : node.children)
   131             build(child, contentTree);
   132     }
   134     /**
   135      * Given the name and parameters, invoke the method in the builder.  This
   136      * method is required to invoke the appropriate build method as instructed
   137      * by the builder XML file.
   138      *
   139      * @param methodName   the name of the method that we would like to invoke.
   140      * @param paramClasses the types for each parameter.
   141      * @param params       the parameters of the method.
   142      */
   143     protected void invokeMethod(String methodName, Class<?>[] paramClasses,
   144             Object[] params)
   145     throws Exception {
   146         if (DEBUG) {
   147             configuration.root.printError("DEBUG: " + this.getClass().getName() + "." + methodName);
   148         }
   149         Method method = this.getClass().getMethod(methodName, paramClasses);
   150         method.invoke(this, params);
   151     }
   152 }

mercurial