Skip to content

Commit

Permalink
[#460] take back plugins from org.andromda.thirdparty.jaxb2_commons g…
Browse files Browse the repository at this point in the history
…roupId
  • Loading branch information
laurentschoelens committed Apr 11, 2024
1 parent 2f082dc commit c3488a9
Show file tree
Hide file tree
Showing 34 changed files with 1,522 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ JAXB2 Basics can only be used with JAXB/XJC 2.3.x. JAXB/XJC versions 2.2.x and e
* [Commons Lang Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB2-Commons-Lang-Plugin) - generates the `toString()`, `hashCode()` and `equals()` methods using Apache commons-lang3.
* [Default Value Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB2-Default-Value-Plugin) - modifies the JAXB code model to set default values to the schema `default` attribute.
* [Fluent API Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB2-Fluent-API-Plugin) - support a fluent api in addition to the default (JavaBean) setter methods.
* [Namespace Prefix Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB2-Namespace-Prefix-Plugin) - adds `javax.xml.bind.annotation.XmlNs` annotations to `package-info.java` files
* [Namespace Prefix Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB2-Namespace-Prefix-Plugin) - adds `jakarta.xml.bind.annotation.XmlNs` annotations to `package-info.java` files
* [Value Constructor Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB2-Value-Constructor-Plugin) - generates another constructor, taking an argument for each field in the class and initialises the field with the argument value.
* [Boolean Getter Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-Boolean-Getter-Plugin) - generates getters for boolean property in `getXXX` instead of `isXXX`.
* [CamelCase Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-CamelCase-Plugin) - modifies the classes name generated by XJC to always be in CamelCase.
* [Xml ElementWrapper Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-XML-ElementWrapper-Plugin) - generates `jakarta.xml.bind.annotation.XmlElementWrapper` annotation to simplify generated structure.
* [Parent Pointer Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-Parent-Pointer-Plugin) - generates getter in child elements to get the parent object (depends on `jaxb-plugins-runtime`)
* [Property Listener Injector Plugin](https://github.com/highsource/jaxb-tools/wiki/JAXB-Property-Listener-Injector-Plugin) - adds methods in order to configure the generation of events on each `setXXX` method

## Credits

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.jvnet.jaxb2_commons.plugin.booleangetter;

import com.sun.codemodel.JMethod;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.Outline;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

import java.util.Collection;

/**
* XJC plugin to generate getXX functions instead of isXX functions for boolean values.
* The motivation is to make using XJC generated classes easier to use with Spring, since
* Spring expects accessors to be called getXX.
*
* @author Adam Burnett
*
* This plugin came from here : org.andromda.thirdparty.jaxb2_commons:booleangetter:1.0
*/
public class BooleanGetter extends Plugin {

protected final String OPTION_NAME = "Xboolean-getter";

@Override
public String getOptionName() {
return OPTION_NAME;
}

@Override
public String getUsage() {
return "-" + OPTION_NAME + " : Generate getXX instead of isXX functions for boolean values\n";
}

@Override
public boolean run(Outline model, Options opts, ErrorHandler errors) throws SAXException {
for (ClassOutline co : model.getClasses()) {
Collection<JMethod> methods = co.implClass.methods();
// pretty simple, just look at all our generated methods and rename isXX to getXX
for (JMethod m : methods) {
if (m.name().startsWith("is")) {
m.name("get" + m.name().substring("is".length()));
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jvnet.jaxb2_commons.plugin.camelcase;

import com.sun.xml.bind.api.impl.NameConverter;

/**
* Name converter that unconditionally converts to camel case.
*/
class CamelCaseNameConverter extends NameConverter.Standard {

/**
* Changes the first character of the specified string to uppercase,
* and the rest of characters to lowercase.
*
* @param
* s string to capitalize
*
* @return
* capitalized string
*/
@Override
public String capitalize(String s) {
StringBuilder sb = new StringBuilder(s.length());
sb.append(Character.toUpperCase(s.charAt(0)));
sb.append(s.substring(1).toLowerCase());
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.jvnet.jaxb2_commons.plugin.camelcase;

import com.sun.tools.xjc.BadCommandLineException;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.Outline;
import org.xml.sax.ErrorHandler;

/**
* {@link Plugin} that always converts an XML name into a camel case java name.
* This plugin changes the first character of every word composing an XML name
* to uppercase and the others to lowercase, while the default XJC behavior is
* to do so only if the first character of the word is lowercase.
* <pre>
*
* XJC default:
* FIRST_NAME -&gt; FIRSTNAME
* FOOBar -&gt; FOOBar
* SSNCode -&gt; SSNCode
*
* Camel case always:
* FIRST_NAME -&gt; FirstName
* FOOBar -&gt; FooBar
* SSNCode -&gt; SsnCode
*
* </pre>
*
* @author Nicola Fagnani
*
* This plugin came from here : org.andromda.thirdparty.jaxb2_commons:camelcase-always:1.0
*/
public class CamelCasePlugin extends Plugin {

/** Constant for the option string. */
protected final String OPTION_NAME = "Xcamelcase";

/**
* Returns the option string used to turn on this plugin.
*
* @return
* option string to invoke this plugin
*/
@Override
public String getOptionName() {
return OPTION_NAME;
}

/**
* Returns a string specifying how to use this plugin and what it does.
*
* @return
* string containing the plugin usage message
*/
@Override
public String getUsage() {
return " -" + OPTION_NAME +
" : converts every XML name to camel case";
}

/**
* On plugin activation, sets a customized NameConverter to adjust code
* generation.
*
* @param opts
* options used to invoke XJC
*
* @throws com.sun.tools.xjc.BadCommandLineException
* if the plugin is invoked with wrong parameters
*/
@Override
public void onActivated(Options opts) throws BadCommandLineException {
opts.setNameConverter( new CamelCaseNameConverter(), this );
}

/**
* Returns true without touching the generated code. All the relevant
* work is done during name conversion.
*
* @param model
* This object allows access to various generated code.
*
* @param opts
* options used to invoke XJC
* @param errorHandler
* Errors should be reported to this handler.
*
* @return
* If the add-on executes successfully, return true.
* If it detects some errors but those are reported and
* recovered gracefully, return false.
*/
@Override
public boolean run(Outline model, Options opts, ErrorHandler errorHandler ) {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.jvnet.jaxb2_commons.plugin.elementwrapper;

import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.tools.xjc.model.CClassInfo;
import com.sun.tools.xjc.model.CPropertyInfo;
import com.sun.tools.xjc.model.CTypeInfo;

/**
* @author bjh
*/
class Candidate {
protected CClassInfo classInfo;
protected CPropertyInfo propertyInfo;
protected CTypeInfo propertyTypeInfo;

protected JDefinedClass implClass;
protected JFieldVar field;
protected String wrappedSchemaTypeName = null;

public Candidate(CClassInfo classInfo) {
this.classInfo = classInfo;
this.propertyInfo = classInfo.getProperties().get(0);
this.propertyTypeInfo = propertyInfo.ref().iterator().next();
this.wrappedSchemaTypeName = XmlElementWrapperPlugin.elementName(propertyInfo);
}

public CClassInfo getClassInfo() {
return classInfo;
}

public CPropertyInfo getPropertyInfo() {
return propertyInfo;
}

public CTypeInfo getPropertyTypeInfo() {
return propertyTypeInfo;
}

public String getClassName() {
return classInfo.fullName();
}

public String getFieldName() {
return getPropertyInfo().getName(false);
}

public String getFieldTypeName() {
return propertyTypeInfo.getType().fullName();
}

public String getWrappedSchemaTypeName() {
return wrappedSchemaTypeName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jvnet.jaxb2_commons.plugin.elementwrapper;

enum Instantiation {
EARLY, LAZY
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.sun.tools.xjc.model.nav.NClass;
import com.sun.tools.xjc.model.nav.NType;

public class ElementWrapperPlugin extends AbstractModelPlugin {
public class OldElementWrapperPlugin extends AbstractModelPlugin {

@Override
public String getOptionName() {
Expand Down

0 comments on commit c3488a9

Please sign in to comment.