duke@1: /* ohair@798: * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.doclets.internal.toolkit.builders; duke@1: duke@1: import com.sun.tools.doclets.internal.toolkit.*; duke@1: import com.sun.tools.doclets.internal.toolkit.util.*; duke@1: import java.io.*; duke@1: import java.lang.reflect.*; duke@1: import java.util.*; duke@1: duke@1: /** duke@1: * The superclass for all builders. A builder is a class that provides duke@1: * the structure and content of API documentation. A builder is completely duke@1: * doclet independent which means that any doclet can use builders to duke@1: * construct documentation, as long as it impelements the appropriate duke@1: * writer interfaces. For example, if a doclet wanted to use duke@1: * {@link ConstantsSummaryBuilder} to build a constant summary, all it has to duke@1: * do is implement the ConstantsSummaryWriter interface and pass it to the duke@1: * builder using a WriterFactory. duke@1: * duke@1: * This code is not part of an API. duke@1: * It is implementation that is subject to change. duke@1: * Do not use it as an API duke@1: * duke@1: * @author Jamie Ho duke@1: * @since 1.5 duke@1: */ duke@1: duke@1: public abstract class AbstractBuilder { duke@1: duke@1: /** duke@1: * The configuration used in this run of the doclet. duke@1: */ duke@1: protected Configuration configuration; duke@1: duke@1: /** duke@1: * Keep track of which packages we have seen for duke@1: * efficiency purposes. We don't want to copy the duke@1: * doc files multiple times for a single package. duke@1: */ jjg@74: protected static Set containingPackagesSeen; duke@1: duke@1: /** duke@1: * True if we want to print debug output. duke@1: */ duke@1: protected static final boolean DEBUG = false; duke@1: duke@1: /** duke@1: * Construct a Builder. duke@1: * @param configuration the configuration used in this run duke@1: * of the doclet. duke@1: */ duke@1: public AbstractBuilder(Configuration configuration) { duke@1: this.configuration = configuration; duke@1: } duke@1: duke@1: /** duke@1: * Return the name of this builder. duke@1: * duke@1: * @return the name of the builder. duke@1: */ duke@1: public abstract String getName(); duke@1: duke@1: /** duke@1: * Build the documentation. duke@1: * duke@1: * @throws IOException there was a problem writing the output. duke@1: */ duke@1: public abstract void build() throws IOException; duke@1: duke@1: /** jjg@589: * Build the documentation, as specified by the given XML element. duke@1: * jjg@589: * @param node the XML element that specifies which component to document. bpatel@766: * @param contentTree content tree to which the documentation will be added duke@1: */ bpatel@766: protected void build(XMLNode node, Content contentTree) { jjg@589: String component = node.name; jjg@589: try { jjg@589: invokeMethod("build" + component, bpatel@766: new Class[]{XMLNode.class, Content.class}, bpatel@766: new Object[]{node, contentTree}); jjg@589: } catch (NoSuchMethodException e) { jjg@589: e.printStackTrace(); jjg@589: configuration.root.printError("Unknown element: " + component); jjg@589: throw new DocletAbortException(); jjg@589: } catch (InvocationTargetException e) { jjg@589: e.getCause().printStackTrace(); jjg@589: } catch (Exception e) { jjg@589: e.printStackTrace(); jjg@589: configuration.root.printError("Exception " + bpatel@766: e.getClass().getName() + bpatel@766: " thrown while processing element: " + component); jjg@589: throw new DocletAbortException(); duke@1: } duke@1: } duke@1: duke@1: /** jjg@589: * Build the documentation, as specified by the children of the given XML element. jjg@589: * jjg@589: * @param node the XML element that specifies which components to document. bpatel@766: * @param contentTree content tree to which the documentation will be added jjg@589: */ bpatel@766: protected void buildChildren(XMLNode node, Content contentTree) { bpatel@766: for (XMLNode child : node.children) bpatel@766: build(child, contentTree); jjg@589: } jjg@589: jjg@589: /** duke@1: * Given the name and parameters, invoke the method in the builder. This duke@1: * method is required to invoke the appropriate build method as instructed duke@1: * by the builder XML file. duke@1: * duke@1: * @param methodName the name of the method that we would like to invoke. duke@1: * @param paramClasses the types for each parameter. duke@1: * @param params the parameters of the method. duke@1: */ jjg@589: protected void invokeMethod(String methodName, Class[] paramClasses, duke@1: Object[] params) jjg@589: throws Exception { jjg@589: if (DEBUG) { bpatel@766: configuration.root.printError("DEBUG: " + this.getClass().getName() + "." + methodName); jjg@589: } jjg@589: Method method = this.getClass().getMethod(methodName, paramClasses); jjg@589: method.invoke(this, params); jjg@589: } duke@1: }