The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: JAXB:How to generate java convention names from uppercase XML tags

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 7 - Last Post: Mar 3, 2006 10:37 AM by: nicfagn
nicfagn

Posts: 37
JAXB:How to generate java convention names from uppercase XML tags
Posted: Feb 17, 2006 4:06 AM
  Click to reply to this thread Reply

Suppose to have a xml schema as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified"
  >
    
  <xs:element name="PERSON">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="FIRST_NAME" type="xs:string"/>
        <xs:element name="LAST_NAME" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  	
</xs:schema>


This is an example modeled after a real schema I have to use as is.
The noticeable thing, is the all uppercase convention used for all the elements tags.
If I run the xjc task (without binding customizations) to generate the corresponding java classes, I obtain the customary ObjectFactory and the PERSON class as follows:

@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "", propOrder = {
    "firstname",
    "lastname"
})
@XmlRootElement(name = "PERSON")
public class PERSON {
 
    @XmlElement(name = "FIRST_NAME")
    protected String firstname;
    @XmlElement(name = "LAST_NAME")
    protected String lastname;
 
    /**
     * Gets the value of the firstname property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getFIRSTNAME() {
        return firstname;
    }
 
    /**
     * Sets the value of the firstname property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setFIRSTNAME(String value) {
        this.firstname = value;
    }
 
    /**
     * Gets the value of the lastname property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getLASTNAME() {
        return lastname;
    }
 
    /**
     * Sets the value of the lastname property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setLASTNAME(String value) {
        this.lastname = value;
    }
 
}


This is all ok according to the jaxb2 proposed final draft specification (see appendix D.2 "The Name to Identifier Algorithm"), but the net result is that I get a class that doesn't follow the java naming convention.
So my question is: what is the least effort way to obtain a class following the java naming convention from this kind of schema?
I would like to point out that the real schema is really complex, so I'm looking for some kind of schema or global customization, because component customization would be too much of an effort.


Thanks

Nicola Fagnani

kohsuke

Posts: 3,962
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Feb 17, 2006 9:25 AM   in response to: nicfagn
  Click to reply to this thread Reply

One possibility is to generate files and they rename them by using an IDE. You'll be taking over the generated code at that point, plus if there are a large number of classes, it might not be very attractive.

The other possibility is to write a plugin and you write a little code to tell XJC how to convert XML names to Java names.

If you are interested in doing this, let's discuss this at users@jaxb.dev.java.net.

nicfagn

Posts: 37
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Feb 17, 2006 10:41 AM   in response to: kohsuke
  Click to reply to this thread Reply

> The other possibility is to write a plugin and you
> write a little code to tell XJC how to convert XML
> names to Java names.
>
> If you are interested in doing this, let's discuss
> this at users@jaxb.dev.java.net.

This seems the best solution and I'm interested.
Should I repost my first post on the above mailing list?

By the way, have you any clue about the reason this case was not considered in the specification?

Bye

Nicola

nicfagn

Posts: 37
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Feb 19, 2006 3:12 PM   in response to: kohsuke
  Click to reply to this thread Reply

Following your indications I made a plugin like this:
package nf.xjc.addon;
 
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 com.sun.xml.bind.api.impl.NameConverter;
import java.io.IOException;
import org.xml.sax.ErrorHandler;
 
 
/**
 * {@link Plugin} that forces capitalization of every word composing a XML name 
 * during conversion to a java name. 
 * XJC doesn't change a composing word starting with an upper case character 
 * (e.g. an uppercase word), but unconditional capitalization can be useful 
 * for processing XML documents whose tags are all uppercase with a separator 
 * char between words (e.g. ONE_WORD, ONE-WORD,...). 
 * Someone seems to like UPPERCASE chars :-)
 *
 * Example:
 *
 * default:
 *   FIRST_NAME -> FIRSTNAME
 *   FOOBar     -> FOOBar
 *   SSNCode    -> SSNCode
 *
 * capitalizing every word:
 *   FIRST_NAME -> FirstName
 *   FOOBar     -> FooBar
 *   SSNCode    -> SsnCode
 *
 * @author Nicola Fagnani
 */
public class CapitalizationPlugin extends Plugin {
 
    public String getOptionName() {
        return "capitalize-all";
    }
 
    public String getUsage() {
        return "  -capitalize-all    :  capitalizes every word composing an XML name";
    }
 
    public void onActivated(Options opts) throws BadCommandLineException {
        opts.setNameConverter( new CapitalizingNameConverter(), this );
    }
 
    public boolean run( Outline model, Options opt, ErrorHandler errorHandler ) {
        return true;
    }
 
}
 
class CapitalizingNameConverter extends NameConverter.Standard {
    /**
     * Always capitalizes the first character of the specified string,
     * and de-capitalize the rest of characters.
     */    
    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();        
    }
}

It seems to work with my all uppercase elements schema.
What do you think? Is there something to improve?

Thank you for your attention

Ciao
Nicola Fagnani

jimnick

Posts: 2
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Mar 2, 2006 2:51 AM   in response to: nicfagn
  Click to reply to this thread Reply

You could try this instead (assuming you can make non-functional changes to the schema, that is):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns: xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="PERSON">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class name="Person"/>
            </xs:appinfo>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="FIRST_NAME" type="xs:string">
                    <xs:appinfo>
                        <jaxb:class name="FirstName"/>
                    </xs:appinfo>
                </xs:element>
                <xs:element name="LAST_NAME" type="xs:string">
                    <xs:appinfo>
                        <jaxb:class name="LastName"/>
                    </xs:appinfo>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


nicfagn

Posts: 37
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Mar 2, 2006 3:46 AM   in response to: jimnick
  Click to reply to this thread Reply

> You could try this instead (assuming you can make
> non-functional changes to the schema, that is):
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:
> xs="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified">
>     <xs:element name="PERSON">
>         <xs:annotation>
>             <xs:appinfo>
>                 <jaxb:class name="Person"/>
>             </xs:appinfo>
>         </xs:annotation>
>         <xs:complexType>
>             <xs:sequence>
> <xs:element name="FIRST_NAME"
> ame="FIRST_NAME" type="xs:string">
>                     <xs:appinfo>
> <jaxb:class
>             <jaxb:class name="FirstName"/>
>                     </xs:appinfo>
>                 </xs:element>
> <xs:element name="LAST_NAME"
> name="LAST_NAME" type="xs:string">
>                     <xs:appinfo>
> <jaxb:class
>             <jaxb:class name="LastName"/>
>                     </xs:appinfo>
>                 </xs:element>
>             </xs:sequence>
>         </xs:complexType>
>     </xs:element>
> </xs:schema>
> 


Yes I could, but one of the appealing features of JAXB is the automatic mapping (for the most part) of XML to java.
In my case there is a lot of hand work to do following your suggestion.
Besides, it should be possible to specify such customizations in an external file.

kohsuke

Posts: 3,962
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Mar 2, 2006 4:58 PM   in response to: nicfagn
  Click to reply to this thread Reply

This is very nice! Are you interested in hosting this in http://jaxb2-commons.dev.java.net/ so that other people can use it?

(And it would be still great if you can join the users list. There are other plugin authors there and we'd like you to "hang around" with us.)

nicfagn

Posts: 37
Re: JAXB:How to generate java convention names from uppercase XML tags
Posted: Mar 3, 2006 10:37 AM   in response to: kohsuke
  Click to reply to this thread Reply

I'd really enjoy if others use it (and contribute to its improvement), also if it's really just a method override with a line omitted!!!
I already joined the list, but unfortunately I can follow only the topics I'm really interested in.
I will post the code there and see what happens...

Ciao

Nicola




 XML java.net RSS