1/*
2 * Copyright (c) 2011, 2013, 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 */
25
26package com.sun.tools.internal.ws.wscompile;
27
28import com.sun.codemodel.internal.JCodeModel;
29import com.sun.tools.internal.ws.processor.model.Model;
30import java.io.IOException;
31import org.xml.sax.SAXException;
32
33/**
34 * Add-on that works on the generated source code.
35 *
36 * <p> This add-on will be called after the default generation has finished.
37 *
38 * @author Lukas Jungmann
39 * @since 2.2.6
40 */
41public abstract class Plugin {
42
43    /**
44     * Gets the option name to turn on this add-on.
45     *
46     * <p> For example, if "abc" is returned, "-abc" will turn on this plugin. A
47     * plugin needs to be turned on explicitly, or else no other methods of {@link Plugin}
48     * will be invoked.
49     *
50     * <p> When an option matches the name returned from this method, WsImport
51     * will then invoke {@link #parseArgument(Options, String[], int)}, allowing
52     * plugins to handle arguments to this option.
53     */
54    public abstract String getOptionName();
55
56    /**
57     * Gets the description of this add-on. Used to generate a usage screen.
58     *
59     * @return localized description message. should be terminated by \n.
60     */
61    public abstract String getUsage();
62
63    /**
64     * Parses an option <code>args[i]</code> and augment the <code>opt</code> object
65     * appropriately, then return the number of tokens consumed.
66     *
67     * <p> The callee doesn't need to recognize the option that the
68     * getOptionName method returns.
69     *
70     * <p> Once a plugin is activated, this method is called for options that
71     * WsImport didn't recognize. This allows a plugin to define additional
72     * options to customize its behavior.
73     *
74     * <p> Since options can appear in no particular order, WsImport allows
75     * sub-options of a plugin to show up before the option that activates a
76     * plugin (one that's returned by {@link #getOptionName()}.)
77     *
78     * But nevertheless a {@link Plugin} needs to be activated to participate in
79     * further processing.
80     *
81     * @return 0 if the argument is not understood. Otherwise return the number
82     * of tokens that are consumed, including the option itself. (so if you have
83     * an option like "-foo 3", return 2.)
84     * @exception BadCommandLineException If the option was recognized but
85     * there's an error. This halts the argument parsing process and causes
86     * WsImport to abort, reporting an error.
87     */
88    public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException {
89        return 0;
90    }
91
92    /**
93     * Notifies a plugin that it's activated.
94     *
95     * <p> This method is called when a plugin is activated through the command
96     * line option (as specified by {@link #getOptionName()}.
97     *
98     * <p> Noop by default.
99     *
100     */
101    public void onActivated(Options opts) throws BadCommandLineException {
102        // noop
103    }
104
105    /**
106     * Run the add-on.
107     *
108     * <p> This method is invoked after WsImport has internally finished the
109     * code generation. Plugins can tweak some of the generated code (or add
110     * more code) by altering {@link JCodeModel} obtained from {@link WsimportOptions#getCodeModel()
111     * } according to the current
112     * {@link Model WSDL model} and {@link WsimportOptions}.
113     *
114     * <p> Note that this method is invoked only when a {@link Plugin} is
115     * activated.
116     *
117     * @param wsdlModel This object allows access to the WSDL model used for
118     * code generation.
119     *
120     * @param options This object allows access to various options used for code
121     * generation as well as access to the generated code.
122     *
123     * @param errorReceiver Errors should be reported to this handler.
124     *
125     * @return If the add-on executes successfully, return true. If it detects
126     * some errors but those are reported and recovered gracefully, return
127     * false.
128     *
129     * @throws SAXException After an error is reported to {@link ErrorReceiver},
130     * the same exception can be thrown to indicate a fatal irrecoverable error. {@link ErrorReceiver}
131     * itself may throw it, if it chooses not to recover from the error.
132     */
133    public abstract boolean run(
134            Model wsdlModel, WsimportOptions options, ErrorReceiver errorReceiver) throws SAXException;
135}
136