【问题标题】:Castor not recognizing value as valid according to regexCastor 没有根据正则表达式将值识别为有效
【发布时间】:2013-08-30 12:35:49
【问题描述】:

我有一个奇怪的问题,我的 Castor-Maven-Plugin 生成的 java 类将无法解组 XML 字符串的内容。

我要验证的 XML 如下所示:

...
    <mediaType>audio/media</mediaType>
...

我正在根据以下模式对其进行验证:

(audio|video|document|application|example|image|message|model|multipart|text)/.+

Castor 从中生成类的 XSD 的相关部分:

...  
<xs:simpleType name="mediaType">  
        <xs:annotation>   
            <xs:documentation>  
                The internet media type of the paired file. For example, a MP3 file would be audio/mpeg.  
            </xs:documentation>
        </xs:annotation>  
        <xs:restriction base="xs:string">  
            <xs:pattern value="(audio|video|document|application|example|image|message|model|multipart|text)/.+"/>  
        </xs:restriction>  
    </xs:simpleType>  
...

当我尝试解组 XML 字符串时抛出以下异常:

org.exolab.castor.xml.MarshalException: The following exception occured while validating field: _mediaType of class: my.domain.GeneratedClass: audio/mpegdoes not match the required regular expression: "(audio|video|document|application|example|image|message|model|multipart|text)/.+"{File: [not available]; line: 23; column: 12}...

现在,根据 IntelliJ 的 XML 验证和我尝试过的所有其他正则表达式工具,这应该不是问题。为什么 Castor 似乎这么认为?

【问题讨论】:

  • 各个地方的旧帖子表明 Castor 上的旧版本存在错误。我猜您运行的是较新版本,但您使用的是什么版本?
  • 我使用的是 1.3.0.1,虽然我刚刚更新到 1.3.1。我和一个同事刚刚解决了这个问题。见下文。

标签: java xml regex maven castor


【解决方案1】:

Castor 没有(好的)本地正则表达式验证器,您必须在配置文件中指定一个。见http://castor.codehaus.org/conf-lib.html

将名为 castor.properties 的配置文件添加到包含以下内容的类路径:

org.exolab.castor.regexp=org.exolab.castor.util.JakartaRegExpEvaluator

您可能还需要将 Jakarta Regex 的依赖项添加到 Maven:

 <dependency>
        <groupId>jakarta-regexp</groupId>
        <artifactId>jakarta-regexp</artifactId>
        <version>1.4</version>
    </dependency>

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多