简体   繁体   中英

Case Insensitive Jaxb2Marshaller

Jaxb2Marshaller ( org.springframework.oxm.jaxb.Jaxb2Marshaller ) is part of Spring's O/X Mapping integration classes.

I use it as the unmarshaller of a StaxEventItemReader :

<bean class="org.springframework.batch.item.xml.StaxEventItemReader">
    <property name="marshaller">
        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>xx.xx.xx.MyBean1</value>
                    <value>xx.xx.xx.MyBean2</value>
                    <value>xx.xx.xx.MyBean3</value>
                </list>
            </property>
        </bean>
    </property>
</bean>

MyBeanX classes are annoted with javax.xml.bind.annotation annotations :

@XmlRootElement(name="MyBean1")
@XmlType(propOrder = {"MyBean2", "MyBean2"})
public class MyBean1 implements Serializable {

    private MyBean2 myBean2;
    private MyBean3 myBean3;

    @XmlElement(name="MyBean2")
    public MyBean2 getMyBean2() {
        return myBean2;
    }

    [...]
}

Unfortunately, the XMLs I need to unmarshall can have the case difference between them :

<MYBEAN1>
    <MYBEAN2></MYBEAN2>
    <MYBEAN3></MYBEAN3>
</MYBEAN1>    

<MyBean1>
    <MyBean2></MyBean2>
    <MyBean3></MyBean3>
</MyBean1>

<mybean1>
    <mybean2></mybean2>
    <mybean3></mybean3>
</mybean1>

or more variants...

Now, I need to be able to parse those XML case-insentively. What I saw so far was possible is to create a StreamReaderDelegate and pass the XMLStreamReader through it to either convert tags to lowercase or uppercase.

XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("file.xml"));
xsr = new MyStreamReaderDelegate(xsr);

Where MyStreamReaderDelegate looks like this :

private static class MyStreamReaderDelegate extends StreamReaderDelegate {

    public MyStreamReaderDelegate(XMLStreamReader xsr) {
        super(xsr);
    }

    @Override
    public String getAttributeLocalName(int index) {
        return super.getAttributeLocalName(index).toLowerCase();
    }

    @Override
    public String getLocalName() {
        return super.getLocalName().toLowerCase();
    }

}

My problem is I don't know what method to override (and in which class) to pass my XML through this delegate. Looking at Jax2Marshaller source code, I found out that an XMLStreamReader is used in the method unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) :

private Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException {
    XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource);
    if (streamReader != null) {
        return jaxbUnmarshaller.unmarshal(streamReader);
    }
    else {
        XMLEventReader eventReader = StaxUtils.getXMLEventReader(staxSource);
        if (eventReader != null) {
            return jaxbUnmarshaller.unmarshal(eventReader);
        }
        else {
            throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader");
        }
    }
}

So my question is, how do I override this method to add the delegate?

You can use version 2.6+ of Moxy to achieve case insensitive marshalling. Heres a link to the specific enhancement that was made for the specific purpose.

You will have to set UNMARSHALLING_CASE_INSENSITIVE property to true. Heres some sample code

final StaxEventItemReader<ProductDTO> itemReader = new StaxEventItemReader<>();
final Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
final Map<String, Boolean> properties = Maps.newHashMap();
properties.put(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE, Boolean.TRUE);
unMarshaller.setUnmarshallerProperties(properties);
itemReader.setUnmarshaller(unMarshaller);

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