Wed, 01 Dec 2010 11:02:38 -0800
6851834: Javadoc doclet needs a structured approach to generate the output HTML.
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2003, 2008, 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 com.sun.tools.doclets.internal.toolkit.*;
29 import com.sun.tools.doclets.internal.toolkit.util.*;
30 import java.io.*;
31 import java.lang.reflect.*;
32 import java.util.*;
34 /**
35 * The superclass for all builders. A builder is a class that provides
36 * the structure and content of API documentation. A builder is completely
37 * doclet independent which means that any doclet can use builders to
38 * construct documentation, as long as it impelements the appropriate
39 * writer interfaces. For example, if a doclet wanted to use
40 * {@link ConstantsSummaryBuilder} to build a constant summary, all it has to
41 * do is implement the ConstantsSummaryWriter interface and pass it to the
42 * builder using a WriterFactory.
43 *
44 * This code is not part of an API.
45 * It is implementation that is subject to change.
46 * Do not use it as an API
47 *
48 * @author Jamie Ho
49 * @since 1.5
50 */
52 public abstract class AbstractBuilder {
54 /**
55 * The configuration used in this run of the doclet.
56 */
57 protected Configuration configuration;
59 /**
60 * Keep track of which packages we have seen for
61 * efficiency purposes. We don't want to copy the
62 * doc files multiple times for a single package.
63 */
64 protected static Set<String> containingPackagesSeen;
66 /**
67 * True if we want to print debug output.
68 */
69 protected static final boolean DEBUG = false;
71 /**
72 * Construct a Builder.
73 * @param configuration the configuration used in this run
74 * of the doclet.
75 */
76 public AbstractBuilder(Configuration configuration) {
77 this.configuration = configuration;
78 }
80 /**
81 * Return the name of this builder.
82 *
83 * @return the name of the builder.
84 */
85 public abstract String getName();
87 /**
88 * Build the documentation.
89 *
90 * @throws IOException there was a problem writing the output.
91 */
92 public abstract void build() throws IOException;
94 /**
95 * Build the documentation, as specified by the given XML element.
96 *
97 * @param node the XML element that specifies which component to document.
98 * @param contentTree content tree to which the documentation will be added
99 */
100 protected void build(XMLNode node, Content contentTree) {
101 String component = node.name;
102 try {
103 invokeMethod("build" + component,
104 new Class<?>[]{XMLNode.class, Content.class},
105 new Object[]{node, contentTree});
106 } catch (NoSuchMethodException e) {
107 e.printStackTrace();
108 configuration.root.printError("Unknown element: " + component);
109 throw new DocletAbortException();
110 } catch (InvocationTargetException e) {
111 e.getCause().printStackTrace();
112 } catch (Exception e) {
113 e.printStackTrace();
114 configuration.root.printError("Exception " +
115 e.getClass().getName() +
116 " thrown while processing element: " + component);
117 throw new DocletAbortException();
118 }
119 }
121 /**
122 * Build the documentation, as specified by the children of the given XML element.
123 *
124 * @param node the XML element that specifies which components to document.
125 * @param contentTree content tree to which the documentation will be added
126 */
127 protected void buildChildren(XMLNode node, Content contentTree) {
128 for (XMLNode child : node.children)
129 build(child, contentTree);
130 }
132 /**
133 * Given the name and parameters, invoke the method in the builder. This
134 * method is required to invoke the appropriate build method as instructed
135 * by the builder XML file.
136 *
137 * @param methodName the name of the method that we would like to invoke.
138 * @param paramClasses the types for each parameter.
139 * @param params the parameters of the method.
140 */
141 protected void invokeMethod(String methodName, Class<?>[] paramClasses,
142 Object[] params)
143 throws Exception {
144 if (DEBUG) {
145 configuration.root.printError("DEBUG: " + this.getClass().getName() + "." + methodName);
146 }
147 Method method = this.getClass().getMethod(methodName, paramClasses);
148 method.invoke(this, params);
149 }
150 }