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

Thu, 24 May 2018 18:02:46 +0800

author
aoqi
date
Thu, 24 May 2018 18:02:46 +0800
changeset 3446
e468915bad3a
parent 3315
6f0746b6de9f
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2003, 2016, 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 {
    55     public static class Context {
    56         /**
    57          * The configuration used in this run of the doclet.
    58          */
    59         final 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         final Set<String> containingPackagesSeen;
    68         /**
    69          * Shared parser for the builder XML file
    70          */
    71         final LayoutParser layoutParser;
    73         Context(Configuration configuration,
    74                 Set<String> containingPackagesSeen,
    75                 LayoutParser layoutParser) {
    76             this.configuration = configuration;
    77             this.containingPackagesSeen = containingPackagesSeen;
    78             this.layoutParser = layoutParser;
    79         }
    80     }
    82     /**
    83      * The configuration used in this run of the doclet.
    84      */
    85     protected final Configuration configuration;
    87     /**
    88      * Keep track of which packages we have seen for
    89      * efficiency purposes.  We don't want to copy the
    90      * doc files multiple times for a single package.
    91      */
    92     protected final Set<String> containingPackagesSeen;
    94     protected final LayoutParser layoutParser;
    96     /**
    97      * True if we want to print debug output.
    98      */
    99     protected static final boolean DEBUG = false;
   101     /**
   102      * Construct a Builder.
   103      * @param configuration the configuration used in this run
   104      *        of the doclet.
   105      */
   106     public AbstractBuilder(Context c) {
   107         this.configuration = c.configuration;
   108         this.containingPackagesSeen = c.containingPackagesSeen;
   109         this.layoutParser = c.layoutParser;
   110     }
   112     /**
   113      * Return the name of this builder.
   114      *
   115      * @return the name of the builder.
   116      */
   117     public abstract String getName();
   119     /**
   120      * Build the documentation.
   121      *
   122      * @throws IOException there was a problem writing the output.
   123      */
   124     public abstract void build() throws IOException;
   126     /**
   127      * Build the documentation, as specified by the given XML element.
   128      *
   129      * @param node the XML element that specifies which component to document.
   130      * @param contentTree content tree to which the documentation will be added
   131      */
   132     protected void build(XMLNode node, Content contentTree) {
   133         String component = node.name;
   134         try {
   135             invokeMethod("build" + component,
   136                     new Class<?>[]{XMLNode.class, Content.class},
   137                     new Object[]{node, contentTree});
   138         } catch (NoSuchMethodException e) {
   139             e.printStackTrace();
   140             configuration.root.printError("Unknown element: " + component);
   141             throw new DocletAbortException(e);
   142         } catch (InvocationTargetException e) {
   143             Throwable cause = e.getCause();
   144             if (cause instanceof FatalError) {
   145                 throw (FatalError) cause;
   146             } else if (cause instanceof DocletAbortException) {
   147                 throw (DocletAbortException) cause;
   148             } else {
   149                 throw new DocletAbortException(cause);
   150             }
   151         } catch (Exception e) {
   152             e.printStackTrace();
   153             configuration.root.printError("Exception " +
   154                     e.getClass().getName() +
   155                     " thrown while processing element: " + component);
   156             throw new DocletAbortException(e);
   157         }
   158     }
   160     /**
   161      * Build the documentation, as specified by the children of the given XML element.
   162      *
   163      * @param node the XML element that specifies which components to document.
   164      * @param contentTree content tree to which the documentation will be added
   165      */
   166     protected void buildChildren(XMLNode node, Content contentTree) {
   167         for (XMLNode child : node.children)
   168             build(child, contentTree);
   169     }
   171     /**
   172      * Given the name and parameters, invoke the method in the builder.  This
   173      * method is required to invoke the appropriate build method as instructed
   174      * by the builder XML file.
   175      *
   176      * @param methodName   the name of the method that we would like to invoke.
   177      * @param paramClasses the types for each parameter.
   178      * @param params       the parameters of the method.
   179      */
   180     protected void invokeMethod(String methodName, Class<?>[] paramClasses,
   181             Object[] params)
   182     throws Exception {
   183         if (DEBUG) {
   184             configuration.root.printError("DEBUG: " + this.getClass().getName() + "." + methodName);
   185         }
   186         Method method = this.getClass().getMethod(methodName, paramClasses);
   187         method.invoke(this, params);
   188     }
   189 }

mercurial