处理XMLfile XStream API时的ConversionException

时间:2017-09-02 10:05:06

标签: java regex xml xstream

我在使用OVAL验证框架处理XML文件时遇到异常。我正在使用OVAL验证框架进行验证。我按照现场记录的用户指南进行了操作。 http://oval.sourceforge.net/userguide.html

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception     : java.lang.RuntimeException
cause-message       : null
class               : java.util.regex.Pattern
required-type       : java.util.regex.Pattern
converter-type      : com.thoughtworks.xstream.converters.extended.RegexPatternConverter
path                : /oval/class/field/matchPattern/pattern
line number         : 13
class[1]            : net.sf.oval.constraint.MatchPatternCheck
converter-type[1]   : net.sf.oval.configuration.xml.XMLConfigurer$1
class[2]            : net.sf.oval.configuration.pojo.elements.FieldConfiguration
class[3]            : net.sf.oval.configuration.pojo.elements.ClassConfiguration
class[4]            : net.sf.oval.configuration.pojo.POJOConfigurer
version             : 1.4.9
-------------------------------
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception     : java.lang.RuntimeException
cause-message       : null
class               : java.util.regex.Pattern
required-type       : java.util.regex.Pattern
converter-type      : com.thoughtworks.xstream.converters.extended.RegexPatternConverter
path                : /oval/class/field/matchPattern/pattern
line number         : 13
class[1]            : net.sf.oval.constraint.MatchPatternCheck
converter-type[1]   : net.sf.oval.configuration.xml.XMLConfigurer$1
class[2]            : net.sf.oval.configuration.pojo.elements.FieldConfiguration
class[3]            : net.sf.oval.configuration.pojo.elements.ClassConfiguration
class[4]            : net.sf.oval.configuration.pojo.POJOConfigurer
version             : 1.4.9
-------------------------------
    com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)

我的XML是

<?xml version="1.0" ?>
<oval xmlns="http://oval.sf.net/oval-configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration.xsd">
    <!-- define checks for the acme.model.User class -->
    <!-- overwrite=false means already defined checks for this class will not 
        be removed -->
    <class type="org.rbac.form.ConfigurationLoginForm" overwrite="false">
        <field name="password">
            <notNull message="password.can.not.be null"></notNull>
            <notEmpty message="password.can.not.be.empty"></notEmpty>
            <length min="4" max="20" message="password.length.must.be.between.min.max"></length>
            <matchPattern matchAll="true">
                <pattern pattern="^[a-z0-9]{8}$" flags="0" />
            </matchPattern>
        </field>

    </class>
</oval>

这是针对xml验证对象的方法

XMLConfigurer xmlConfigurer = new XMLConfigurer(FormValidator.class.getResourceAsStream("/validation/login_configuration.xml"));
        Guard guard = new Guard(xmlConfigurer);
        Validator validator = new Validator(guard.getConfigurers());
        ResourceBundleMessageResolver messageResolver = new ResourceBundleMessageResolver();
        messageResolver.addMessageBundle(resourceBundle);
        Validator.setMessageResolver(messageResolver);
        Validator.setContextRenderer(new ResourceBundleValidationContextRenderer());
        List<ConstraintViolation> LS = validator.validate(formObject);
        return LS;

如果我删除模式,那么它可以正常工作。任何建议。

2 个答案:

答案 0 :(得分:0)

我在使用1.4.10时也遇到了类似的问题。 (OP似乎正在使用1.4.9)

遵循http://oval.sourceforge.net/changes-report.html#a1.84的变更日志中提到的1.4.5版本,但不幸的是遇到了另一个问题。 (ArrayIndexOutOfBoundsException)

不是最佳解决方案-但是在我更改为1.4.1之后,验证对于我的情况似乎仍然可以正常工作。

<!-- https://mvnrepository.com/artifact/net.sf.oval/oval -->
<dependency>
    <groupId>net.sf.oval</groupId>
    <artifactId>oval</artifactId>
    <version>1.90</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.1</version>
</dependency>

我的用例如下:

<class type="com.yyy.xxx.Author">
    <field name="name">
        <notNull></notNull>
        <notEmpty></notEmpty>
        <length max="255"></length>
    </field>
    <field name="email">
        <notNull></notNull>
        <notEmpty></notEmpty>
        <matchPattern matchAll="true">
            <pattern pattern="^[a-zA-Z0-9.!#$%&amp;’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"></pattern>
        </matchPattern>
        <length max="255"></length>
    </field>
    <field name="ctryCd">
        <notNull></notNull>
        <notEmpty></notEmpty>
        <length max="3"></length>
    </field>
</class>

OP大约晚了一年,但是希望它将对其他有类似问题的人有所帮助。

答案 1 :(得分:-1)

XStream的RegExPatternConverter通常期望XML元素作为子元素而不是属性:

<pattern>
  <pattern>^[a-z0-9]$</pattern>
  <flags>0</flags>
</pattern>