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