aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.util.Collections; aoqi@0: import java.util.List; aoqi@0: aoqi@0: import com.sun.tools.internal.xjc.generator.bean.field.FieldRendererFactory; aoqi@0: import com.sun.tools.internal.xjc.model.CPluginCustomization; aoqi@0: import com.sun.tools.internal.xjc.model.Model; aoqi@0: import com.sun.tools.internal.xjc.outline.Outline; aoqi@0: aoqi@0: import org.xml.sax.ErrorHandler; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Add-on that works on the generated source code. aoqi@0: * aoqi@0: *

aoqi@0: * This add-on will be called after the default bean generation aoqi@0: * has finished. aoqi@0: * aoqi@0: * @author aoqi@0: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: * aoqi@0: * @since aoqi@0: * JAXB RI 2.0 EA aoqi@0: */ aoqi@0: public abstract class Plugin { aoqi@0: aoqi@0: /** aoqi@0: * Gets the option name to turn on this add-on. aoqi@0: * aoqi@0: *

aoqi@0: * For example, if "abc" is returned, "-abc" will aoqi@0: * turn on this plugin. A plugin needs to be turned aoqi@0: * on explicitly, or else no other methods of {@link Plugin} aoqi@0: * will be invoked. aoqi@0: * aoqi@0: *

aoqi@0: * Starting 2.1, when an option matches the name returned aoqi@0: * from this method, XJC will then invoke {@link #parseArgument(Options, String[], int)}, aoqi@0: * allowing plugins to handle arguments to this option. aoqi@0: */ aoqi@0: public abstract String getOptionName(); aoqi@0: aoqi@0: /** aoqi@0: * Gets the description of this add-on. Used to generate aoqi@0: * a usage screen. aoqi@0: * aoqi@0: * @return aoqi@0: * localized description message. should be terminated by \n. aoqi@0: */ aoqi@0: public abstract String getUsage(); aoqi@0: aoqi@0: /** aoqi@0: * Parses an option args[i] and augment aoqi@0: * the opt object appropriately, then return aoqi@0: * the number of tokens consumed. aoqi@0: * aoqi@0: *

aoqi@0: * The callee doesn't need to recognize the option that the aoqi@0: * getOptionName method returns. aoqi@0: * aoqi@0: *

aoqi@0: * Once a plugin is activated, this method is called aoqi@0: * for options that XJC didn't recognize. This allows aoqi@0: * a plugin to define additional options to customize aoqi@0: * its behavior. aoqi@0: * aoqi@0: *

aoqi@0: * Since options can appear in no particular order, aoqi@0: * XJC allows sub-options of a plugin to show up before aoqi@0: * the option that activates a plugin (one that's returned aoqi@0: * by {@link #getOptionName().) aoqi@0: * aoqi@0: * But nevertheless a {@link Plugin} needs to be activated aoqi@0: * to participate in further processing. aoqi@0: * aoqi@0: * @return aoqi@0: * 0 if the argument is not understood. aoqi@0: * Otherwise return the number of tokens that are aoqi@0: * consumed, including the option itself. aoqi@0: * (so if you have an option like "-foo 3", return 2.) aoqi@0: * @exception BadCommandLineException aoqi@0: * If the option was recognized but there's an error. aoqi@0: * This halts the argument parsing process and causes aoqi@0: * XJC to abort, reporting an error. aoqi@0: */ aoqi@0: public int parseArgument( Options opt, String[] args, int i ) throws BadCommandLineException, IOException { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the list of namespace URIs that are supported by this plug-in aoqi@0: * as schema annotations. aoqi@0: * aoqi@0: *

aoqi@0: * If a plug-in returns a non-empty list, the JAXB RI will recognize aoqi@0: * these namespace URIs as vendor extensions aoqi@0: * (much like "http://java.sun.com/xml/ns/jaxb/xjc"). This allows users aoqi@0: * to write those annotations inside a schema, or in external binding files, aoqi@0: * and later plug-ins can access those annotations as DOM nodes. aoqi@0: * aoqi@0: *

aoqi@0: * See aoqi@0: * http://java.sun.com/webservices/docs/1.5/jaxb/vendorCustomizations.html aoqi@0: * for the syntax that users need to use to enable extension URIs. aoqi@0: * aoqi@0: * @return aoqi@0: * can be empty, be never be null. aoqi@0: */ aoqi@0: public List getCustomizationURIs() { aoqi@0: return Collections.emptyList(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Checks if the given tag name is a valid tag name for the customization element in this plug-in. aoqi@0: * aoqi@0: *

aoqi@0: * This method is invoked by XJC to determine if the user-specified customization element aoqi@0: * is really a customization or not. This information is used to pick the proper error message. aoqi@0: * aoqi@0: *

aoqi@0: * A plug-in is still encouraged to do the validation of the customization element in the aoqi@0: * {@link #run} method before using any {@link CPluginCustomization}, to make sure that it aoqi@0: * has proper child elements and attributes. aoqi@0: * aoqi@0: * @param nsUri aoqi@0: * the namespace URI of the element. Never null. aoqi@0: * @param localName aoqi@0: * the local name of the element. Never null. aoqi@0: */ aoqi@0: public boolean isCustomizationTagName(String nsUri,String localName) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Notifies a plugin that it's activated. aoqi@0: * aoqi@0: *

aoqi@0: * This method is called when a plugin is activated aoqi@0: * through the command line option (as specified by {@link #getOptionName()}. aoqi@0: * aoqi@0: *

aoqi@0: * This is a good opportunity to use aoqi@0: * {@link Options#setFieldRendererFactory(FieldRendererFactory, Plugin)} aoqi@0: * if a plugin so desires. aoqi@0: * aoqi@0: *

aoqi@0: * Noop by default. aoqi@0: * aoqi@0: * @since JAXB 2.0 EA4 aoqi@0: */ aoqi@0: public void onActivated(Options opts) throws BadCommandLineException { aoqi@0: // noop aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Performs the post-processing of the {@link Model}. aoqi@0: * aoqi@0: *

aoqi@0: * This method is invoked after XJC has internally finished aoqi@0: * the model construction. This is a chance for a plugin to aoqi@0: * affect the way code generation is performed. aoqi@0: * aoqi@0: *

aoqi@0: * Compared to the {@link #run(Outline, Options, ErrorHandler)} aoqi@0: * method, this method allows a plugin to work at the higher level aoqi@0: * conceptually closer to the abstract JAXB model, as opposed to aoqi@0: * Java syntax level. aoqi@0: * aoqi@0: *

aoqi@0: * Note that this method is invoked only when a {@link Plugin} aoqi@0: * is activated. aoqi@0: * aoqi@0: * @param model aoqi@0: * The object that represents the classes/properties to aoqi@0: * be generated. aoqi@0: * aoqi@0: * @param errorHandler aoqi@0: * Errors should be reported to this handler. aoqi@0: * aoqi@0: * @since JAXB 2.0.2 aoqi@0: */ aoqi@0: public void postProcessModel(Model model, ErrorHandler errorHandler) { aoqi@0: // noop aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Run the add-on. aoqi@0: * aoqi@0: *

aoqi@0: * This method is invoked after XJC has internally finished aoqi@0: * the code generation. Plugins can tweak some of the generated aoqi@0: * code (or add more code) by using {@link Outline} and {@link Options}. aoqi@0: * aoqi@0: *

aoqi@0: * Note that this method is invoked only when a {@link Plugin} aoqi@0: * is activated. aoqi@0: * aoqi@0: * @param outline aoqi@0: * This object allows access to various generated code. aoqi@0: * aoqi@0: * @param errorHandler aoqi@0: * Errors should be reported to this handler. aoqi@0: * aoqi@0: * @return aoqi@0: * If the add-on executes successfully, return true. aoqi@0: * If it detects some errors but those are reported and aoqi@0: * recovered gracefully, return false. aoqi@0: * aoqi@0: * @throws SAXException aoqi@0: * After an error is reported to {@link ErrorHandler}, the aoqi@0: * same exception can be thrown to indicate a fatal irrecoverable aoqi@0: * error. {@link ErrorHandler} itself may throw it, if it chooses aoqi@0: * not to recover from the error. aoqi@0: */ aoqi@0: public abstract boolean run( aoqi@0: Outline outline, Options opt, ErrorHandler errorHandler ) throws SAXException ; aoqi@0: }