Naveed S
Naveed S

Reputation: 5266

Make xjc generate class with member variable names exactly same as element names

Is there a way to specify in external binding that the member variable names in generated classes should follow the same letter case (and not the java conventions), on a schema level (i.e. not a globalbinding)?

I am having elements named as XYZProperty in the schema, which should have corresponding member variable named as XYZProperty itself and not xyzProperty. I tried adding the following in the binding file, but it didn't work:

<jxb:bindings node="//xsd:complexType[@name='SomeType']/xsd:sequence/xsd:element[@name='XYZProperty']">
    <jxb:property name="XYZProperty"/>
</jxb:bindings>

where //xsd:complexType[@name='SomeType']/xsd:sequence/xsd:element[@name='XYZProperty'] is the xpath to the element in the schema.

Upvotes: 1

Views: 1247

Answers (1)

Ilya
Ilya

Reputation: 2167

OpenJDK implementation of xjc uses com.sun.xml.internal.bind.api.impl.NameConverter#toVariableName to convert property name to member variable name. It seems that there is no implementation that can leave variable name 'as is'. If it is applicable you can write your own xjc plugin, that will set private property name to its public name. Plugin may be like this:

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.model.CClassInfo;
import com.sun.tools.xjc.model.CPropertyInfo;
import com.sun.tools.xjc.model.Model;
import com.sun.tools.xjc.outline.Outline;

public class XJCPlugin extends Plugin {
    @Override
    public String getOptionName() {
        return "XsameElementNames";
    }

    @Override
    public int parseArgument(Options opt, String[] args, int i) {
        return 1;
    }

    @Override
    public String getUsage() {
        return "  -XsameElementNames    :  set property private name as its public name";
    }

    @Override
    public void postProcessModel(Model model, ErrorHandler errorHandler) {
        for (CClassInfo c : model.beans().values()) {
            for (CPropertyInfo prop : c.getProperties()) {
                prop.setName(false, prop.getName(true));
            }
        }
    }

    @Override
    public boolean run(Outline arg0, Options arg1, ErrorHandler arg2) throws SAXException {
        return true;
    }
}

Upvotes: 1

Related Questions