325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.util.Collections;
325N/Aimport java.util.List;
325N/A
325N/Aimport com.sun.tools.internal.xjc.generator.bean.field.FieldRendererFactory;
325N/Aimport com.sun.tools.internal.xjc.model.CPluginCustomization;
325N/Aimport com.sun.tools.internal.xjc.model.Model;
325N/Aimport com.sun.tools.internal.xjc.outline.Outline;
325N/A
325N/Aimport org.xml.sax.ErrorHandler;
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/A/**
325N/A * Add-on that works on the generated source code.
325N/A *
325N/A * <p>
325N/A * This add-on will be called after the default bean generation
325N/A * has finished.
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
325N/A *
325N/A * @since
325N/A * JAXB RI 2.0 EA
325N/A */
325N/Apublic abstract class Plugin {
325N/A
325N/A /**
325N/A * Gets the option name to turn on this add-on.
325N/A *
325N/A * <p>
325N/A * For example, if "abc" is returned, "-abc" will
325N/A * turn on this plugin. A plugin needs to be turned
325N/A * on explicitly, or else no other methods of {@link Plugin}
325N/A * will be invoked.
325N/A *
325N/A * <p>
325N/A * Starting 2.1, when an option matches the name returned
325N/A * from this method, XJC will then invoke {@link #parseArgument(Options, String[], int)},
325N/A * allowing plugins to handle arguments to this option.
325N/A */
325N/A public abstract String getOptionName();
325N/A
325N/A /**
325N/A * Gets the description of this add-on. Used to generate
325N/A * a usage screen.
325N/A *
325N/A * @return
325N/A * localized description message. should be terminated by \n.
325N/A */
325N/A public abstract String getUsage();
325N/A
325N/A /**
325N/A * Parses an option <code>args[i]</code> and augment
325N/A * the <code>opt</code> object appropriately, then return
325N/A * the number of tokens consumed.
325N/A *
325N/A * <p>
325N/A * The callee doesn't need to recognize the option that the
325N/A * getOptionName method returns.
325N/A *
325N/A * <p>
325N/A * Once a plugin is activated, this method is called
325N/A * for options that XJC didn't recognize. This allows
325N/A * a plugin to define additional options to customize
325N/A * its behavior.
325N/A *
325N/A * <p>
325N/A * Since options can appear in no particular order,
325N/A * XJC allows sub-options of a plugin to show up before
325N/A * the option that activates a plugin (one that's returned
325N/A * by {@link #getOptionName().)
325N/A *
325N/A * But nevertheless a {@link Plugin} needs to be activated
325N/A * to participate in further processing.
325N/A *
325N/A * @return
325N/A * 0 if the argument is not understood.
325N/A * Otherwise return the number of tokens that are
325N/A * consumed, including the option itself.
325N/A * (so if you have an option like "-foo 3", return 2.)
325N/A * @exception BadCommandLineException
325N/A * If the option was recognized but there's an error.
325N/A * This halts the argument parsing process and causes
325N/A * XJC to abort, reporting an error.
325N/A */
325N/A public int parseArgument( Options opt, String[] args, int i ) throws BadCommandLineException, IOException {
325N/A return 0;
325N/A }
325N/A
325N/A /**
325N/A * Returns the list of namespace URIs that are supported by this plug-in
325N/A * as schema annotations.
325N/A *
325N/A * <p>
325N/A * If a plug-in returns a non-empty list, the JAXB RI will recognize
325N/A * these namespace URIs as vendor extensions
325N/A * (much like "http://java.sun.com/xml/ns/jaxb/xjc"). This allows users
325N/A * to write those annotations inside a schema, or in external binding files,
325N/A * and later plug-ins can access those annotations as DOM nodes.
325N/A *
325N/A * <p>
325N/A * See <a href="http://java.sun.com/webservices/docs/1.5/jaxb/vendorCustomizations.html">
325N/A * http://java.sun.com/webservices/docs/1.5/jaxb/vendorCustomizations.html</a>
325N/A * for the syntax that users need to use to enable extension URIs.
325N/A *
325N/A * @return
325N/A * can be empty, be never be null.
325N/A */
325N/A public List<String> getCustomizationURIs() {
325N/A return Collections.emptyList();
325N/A }
325N/A
325N/A /**
325N/A * Checks if the given tag name is a valid tag name for the customization element in this plug-in.
325N/A *
325N/A * <p>
325N/A * This method is invoked by XJC to determine if the user-specified customization element
325N/A * is really a customization or not. This information is used to pick the proper error message.
325N/A *
325N/A * <p>
325N/A * A plug-in is still encouraged to do the validation of the customization element in the
325N/A * {@link #run} method before using any {@link CPluginCustomization}, to make sure that it
325N/A * has proper child elements and attributes.
325N/A *
325N/A * @param nsUri
325N/A * the namespace URI of the element. Never null.
325N/A * @param localName
325N/A * the local name of the element. Never null.
325N/A */
325N/A public boolean isCustomizationTagName(String nsUri,String localName) {
325N/A return false;
325N/A }
325N/A
325N/A /**
325N/A * Notifies a plugin that it's activated.
325N/A *
325N/A * <p>
325N/A * This method is called when a plugin is activated
325N/A * through the command line option (as specified by {@link #getOptionName()}.
325N/A *
325N/A * <p>
325N/A * This is a good opportunity to use
325N/A * {@link Options#setFieldRendererFactory(FieldRendererFactory, Plugin)}
325N/A * if a plugin so desires.
325N/A *
325N/A * <p>
325N/A * Noop by default.
325N/A *
325N/A * @since JAXB 2.0 EA4
325N/A */
325N/A public void onActivated(Options opts) throws BadCommandLineException {
325N/A // noop
325N/A }
325N/A
325N/A /**
325N/A * Performs the post-processing of the {@link Model}.
325N/A *
325N/A * <p>
325N/A * This method is invoked after XJC has internally finished
325N/A * the model construction. This is a chance for a plugin to
325N/A * affect the way code generation is performed.
325N/A *
325N/A * <p>
325N/A * Compared to the {@link #run(Outline, Options, ErrorHandler)}
325N/A * method, this method allows a plugin to work at the higher level
325N/A * conceptually closer to the abstract JAXB model, as opposed to
325N/A * Java syntax level.
325N/A *
325N/A * <p>
325N/A * Note that this method is invoked only when a {@link Plugin}
325N/A * is activated.
325N/A *
325N/A * @param model
325N/A * The object that represents the classes/properties to
325N/A * be generated.
325N/A *
325N/A * @param errorHandler
325N/A * Errors should be reported to this handler.
325N/A *
325N/A * @since JAXB 2.0.2
325N/A */
325N/A public void postProcessModel(Model model, ErrorHandler errorHandler) {
325N/A // noop
325N/A }
325N/A
325N/A /**
325N/A * Run the add-on.
325N/A *
325N/A * <p>
325N/A * This method is invoked after XJC has internally finished
325N/A * the code generation. Plugins can tweak some of the generated
325N/A * code (or add more code) by using {@link Outline} and {@link Options}.
325N/A *
325N/A * <p>
325N/A * Note that this method is invoked only when a {@link Plugin}
325N/A * is activated.
325N/A *
325N/A * @param outline
325N/A * This object allows access to various generated code.
325N/A *
325N/A * @param errorHandler
325N/A * Errors should be reported to this handler.
325N/A *
325N/A * @return
325N/A * If the add-on executes successfully, return true.
325N/A * If it detects some errors but those are reported and
325N/A * recovered gracefully, return false.
325N/A *
325N/A * @throws SAXException
325N/A * After an error is reported to {@link ErrorHandler}, the
325N/A * same exception can be thrown to indicate a fatal irrecoverable
325N/A * error. {@link ErrorHandler} itself may throw it, if it chooses
325N/A * not to recover from the error.
325N/A */
325N/A public abstract boolean run(
325N/A Outline outline, Options opt, ErrorHandler errorHandler ) throws SAXException ;
325N/A}