Question

I have a strange problem where my Castor-Maven-Plugin-generated java classes will fail to unmarshal the contents of an XML string.

The XML I'm trying to validate looks like this:

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

And I'm validating it against the following pattern:

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

The relevant part of the XSD that Castor generates the classes from:

...  
<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>  
...

The following exception is thrown when I attempt to unmarshall the XML String:

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}...

Now, according to IntelliJ´s XML validation and every other regex tool I've tried, this should not be a problem. Why does Castor seem to think so?

Was it helpful?

Solution

Castor does not have a (good) native Regex validator, you have to specify one in a config file. See http://castor.codehaus.org/conf-lib.html

Add a config file called castor.properties to the classpath containing the following:

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

You may have to add a dependency to Jakarta Regex to Maven as well:

 <dependency>
        <groupId>jakarta-regexp</groupId>
        <artifactId>jakarta-regexp</artifactId>
        <version>1.4</version>
    </dependency>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top