简体   繁体   中英

Converting Jaxb attribute value to Upper Case during UnMarshalling

I want to convert uid attribute's value in MyJaxbModel class to uppercase during UnMarshalling. I did write UpperCaseAdapter that does the work for me. However with this approach, application performance is deteriorated to unacceptable level (as there are thousands of XML files unmarshalled to MyJaxbModel). I cannot use String.toUppperCase() in getter /setter as these JAXB models are auto generated from XSD and I dont want to tweak them.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "myJaxbModel")
public class MyJaxbModel
{
    protected String name;

    @XmlJavaTypeAdapter(UpperCaseAdapter.class)
    protected String uid;

    // getters and setters

}

public class UpperCaseAdapter extends XmlAdapter<String, String>
{
    @Override
    public String unmarshal( String value ) throws Exception
    {
        return value.toUpperCase();
    }

    @Override
    public String marshal( String value ) throws Exception
    {
        return value;
    }
}

<!--My XSD makes use of below xjc:javaType definition to auto-configure this-->
<xsd:simpleType name="uidType">
    <xsd:annotation>
        <xsd:appinfo>
            <xjc:javaType name="java.lang.String"
                adapter="jaxb.UpperCaseAdapter" />
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>

Expected Input : <myJaxbModel name="abc" uid="xyz" />

Expected Output : myJaxbModel.toString() -> MyJaxbModel[name=abc, uid=XYZ]

Is there a better approach to achieve desired results?

Why didn't you simply parse it to upper case in the getUid() or while setting?

if (uid != null){
   return uid.toUpperCase();
}
...

or

 ...
    if (uid != null){
       this.uir =  uid.toUpperCase();
    }

I think that is the easier and cleanest way to do it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM