处理XMLfile XStream API时的ConversionException

问题描述 投票:2回答:2

我在使用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;

如果我删除模式然后它工作正常。任何建议。

java regex xml xstream
2个回答
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
投票

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

<pattern>
  <pattern>^[a-z0-9]$</pattern>
  <flags>0</flags>
</pattern>
© www.soinside.com 2019 - 2024. All rights reserved.