Spring Batch CharacterEscapeHandler属性被忽略

时间:2014-08-05 22:15:29

标签: spring escaping spring-batch jaxb2

与JAXB2Marshaller一起使用时,看起来像Spring Batch StaxEventItemWriter无法识别已设置的Marshaller属性。有没有人有过在SpringBatch中使用CharacterEscapeHandler的经验?

我需要JAXB忽略转义特殊字符,例如'<'和'>'到'& LT;”和'& GT;” (请忽略空格)我按照以下方式覆盖了我的JAXB2Marshaller的'initJaxbMarshaller',但它似乎无法识别..

@Override
    protected void initJaxbMarshaller(Marshaller marshaller)throws JAXBException{
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(CharacterEscapeHandler.class.getName(),  new CharacterEscapeHandler() {
            @Override
            public void escape(char[] buf, int start, int length,
                    boolean isAttVal, Writer out)
                    throws IOException {
                for (int i = start; i < start + length; i++) {
                    char ch = buf[i];
                    log.debug(ch);
                    out.write(ch);
                }
                //log.debug("Just flushing char --> "+ch.toString());
                //writer.write(ch, start, length);
            }
        });
    }

编辑:添加更多信息..

所需的XML输出

<?xml version="1.0" encoding="ISO-8859-1"?>
<LOAD FORMAT="XMLFormat" OVERRIDE="true">
  <USER ID="someId">
    <NAME>userName</NAME>
    <COMPANY><![CDATA[companyName]]></COMPANY>
  </USER>
  .....
<LOAD>

Java对象映射

@XmlRootElement(name="USER")
@XmlType (propOrder={"name","comapny"})
public class User {

    private String id;
    private String name;
    @XmlJavaTypeAdapter(value=CdataAdapter.class)
    private String company;

    //Getters and Setters
}

用于处理CDATA的CdataAdapter类

public class CdataAdapter extends XmlAdapter<String, String>{

    @Override
        public String marshal(String v) throws Exception {
            return "<![CDATA[" + v + "]]>";
        }

        @Override
        public String unmarshal(String v) throws Exception {
            return v;
        }
    }

JAXB2Marshaller Spring config

<bean id="userXMLMarshaller" class="org.test.model.xml.marshallers.CustomJAXB2XMLMarshaller">
   <property name="classesToBeBound">
    <list>
        <value>org.test.xml.User</value>
    </list>
    </property>
  </bean>

实际XML输出

<?xml version="1.0" encoding="ISO-8859-1"?>
<LOAD FORMAT="XMLFormat" OVERRIDE="true">
  <USER ID="someId">
    <NAME>userName</NAME>
    <COMPANY>&lt;![CDATA[companyName]]&gt;</COMPANY>
  </USER>
  .....
<LOAD>

此外,它看起来已经有一个Spring批处理的错误,它没有正确地输出XML并且有修复它;链接 - https://jira.spring.io/browse/BATCH-2113

您是否认为与CharacterEscapeHandler的情况相同?任何建议都表示赞赏..

0 个答案:

没有答案