|
| 1 | +package org.jvnet.jaxb2_commons.plugin.camelcase; |
| 2 | + |
| 3 | +import com.sun.tools.xjc.BadCommandLineException; |
| 4 | +import com.sun.tools.xjc.Options; |
| 5 | +import com.sun.tools.xjc.Plugin; |
| 6 | +import com.sun.tools.xjc.outline.Outline; |
| 7 | +import org.xml.sax.ErrorHandler; |
| 8 | + |
| 9 | +/** |
| 10 | + * {@link Plugin} that always converts an XML name into a camel case java name. |
| 11 | + * This plugin changes the first character of every word composing an XML name |
| 12 | + * to uppercase and the others to lowercase, while the default XJC behavior is |
| 13 | + * to do so only if the first character of the word is lowercase. |
| 14 | + * <pre> |
| 15 | + * |
| 16 | + * XJC default: |
| 17 | + * FIRST_NAME -> FIRSTNAME |
| 18 | + * FOOBar -> FOOBar |
| 19 | + * SSNCode -> SSNCode |
| 20 | + * |
| 21 | + * Camel case always: |
| 22 | + * FIRST_NAME -> FirstName |
| 23 | + * FOOBar -> FooBar |
| 24 | + * SSNCode -> SsnCode |
| 25 | + * |
| 26 | + * </pre> |
| 27 | + * |
| 28 | + * @author Nicola Fagnani |
| 29 | + * |
| 30 | + * This plugin came from here : org.andromda.thirdparty.jaxb2_commons:camelcase-always:1.0 |
| 31 | + */ |
| 32 | +public class CamelCasePlugin extends Plugin { |
| 33 | + |
| 34 | + /** Constant for the option string. */ |
| 35 | + protected final String OPTION_NAME = "Xcamelcase"; |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the option string used to turn on this plugin. |
| 39 | + * |
| 40 | + * @return |
| 41 | + * option string to invoke this plugin |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public String getOptionName() { |
| 45 | + return OPTION_NAME; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns a string specifying how to use this plugin and what it does. |
| 50 | + * |
| 51 | + * @return |
| 52 | + * string containing the plugin usage message |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public String getUsage() { |
| 56 | + return " -" + OPTION_NAME + |
| 57 | + " : converts every XML name to camel case"; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * On plugin activation, sets a customized NameConverter to adjust code |
| 62 | + * generation. |
| 63 | + * |
| 64 | + * @param opts |
| 65 | + * options used to invoke XJC |
| 66 | + * |
| 67 | + * @throws com.sun.tools.xjc.BadCommandLineException |
| 68 | + * if the plugin is invoked with wrong parameters |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void onActivated(Options opts) throws BadCommandLineException { |
| 72 | + opts.setNameConverter( new CamelCaseNameConverter(), this ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns true without touching the generated code. All the relevant |
| 77 | + * work is done during name conversion. |
| 78 | + * |
| 79 | + * @param model |
| 80 | + * This object allows access to various generated code. |
| 81 | + * |
| 82 | + * @param opts |
| 83 | + * options used to invoke XJC |
| 84 | +
|
| 85 | + * @param errorHandler |
| 86 | + * Errors should be reported to this handler. |
| 87 | + * |
| 88 | + * @return |
| 89 | + * If the add-on executes successfully, return true. |
| 90 | + * If it detects some errors but those are reported and |
| 91 | + * recovered gracefully, return false. |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public boolean run(Outline model, Options opts, ErrorHandler errorHandler ) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments