Forum Discussion

weinmang's avatar
weinmang
New Contributor
15 years ago

Groovy XML Validation with multiple imported schemas

Validating an XML document against a schema having multiple cascading schema imports each in their own namespace works in Java ...factory.newSchema(new Source[]...
However, attempting to use a source array to create a schema with the schema factory yields
java.lang.ClassCastException: [Ljava.util.ArrayList; cannot be cast to java.util.List [Ljava.util.ArrayList; cannot be cast to java.util.List

A snippet of my code follows:

def resultFIS = new StreamSource(new FileInputStream(resultXSD))
def profileFIS = new StreamSource(new FileInputStream(profileXSD))
def pageFIS = new StreamSource(new FileInputStream(pageXSD))
def appFIS = new StreamSource(new FileInputStream(appXSD))
def commonFIS = new StreamSource(new FileInputStream(commonXSD))
def schemaFiles = [commonFIS, appFIS, pageFIS, profileFIS, resultFIS]

def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(schemaFiles)

The error message is confusing. I hope it's something simple.
  • weinmang's avatar
    weinmang
    New Contributor
    Specific issues with multiple imported schemas

    Groovy defaults to using the Sun internal XMLSchemaFactory. That factory is not as
    full featured as the current implementation in Xerces 2.10. Please use the Xerces
    XMLSchemaFactory an NOT the javax SchemaFactory.

    Multiple schemas must be stacked in reverse order. For Example:

    Schema a.xsd imports schema b.xsd
    Schema b.xsd imports schema c.xst
    Create a Source array with new StreamSource for c.xsd
    followed by new StreamSource for b.xsd
    followed by new StreamSource for a.xsd

    Scenario:

    Result schema imports profile schema and common schema
    Profile schema imports page schema and common schema
    Page schema imports app schema and common schema
    App schema imports common schema

    Working example:

    def resultFIS = new StreamSource(new FileInputStream(resultXSD))
    def profileFIS = new StreamSource(new FileInputStream(profileXSD))
    def pageFIS = new StreamSource(new FileInputStream(pageXSD))
    def appFIS = new StreamSource(new FileInputStream(appXSD))
    def commonFIS = new StreamSource(new FileInputStream(commonXSD))
    def schemaFiles = [commonFIS, appFIS, pageFIS, profileFIS, resultFIS]

    def factory = XMLSchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema((Source[])schemaFiles.toArray())
    def validator = schema.newValidator()
    validator.validate(new StreamSource(new FileInputStream(resultXML)))
  • Hey folks,
    how do I integrate xerces 2.10? If I drop it into the SOAPUI folder structure my SOAPUI install craps out.

    Also, if I get it working, what is the syntax for importing the Xerces lib?

    Cheers,
    - Richard