Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLSchema validation with Catalog.xml file for entity resolving

i have a schema.xsd which includes and modifies xhtml like this:

<xs:redefine schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">
...
</xs:redefine>

Now i have written a Validator, which

  • reads the schema from xml file
  • uses a CatalogManager for resolving entities

it works fine as it does not load any files from the net but rather finds xhtml11.xsd as given in my catalog.xml file.

public class XmlTemplateValidator implements TemplateValidator
{
    public List<SAXParseException> validate ( String xml ) throws Exception
    {
        Reader input = new StringReader(xml);
        InputSource inputSource = new InputSource(input);

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        SAXParser parser = factory.newSAXParser();
        parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(new CatalogResolver());
        DefaultErrorHandler handler = new DefaultErrorHandler();
        reader.setErrorHandler(handler);
        reader.parse(inputSource);
        return handler.getSaxParseExceptions();
    }

}

Now i want exactly the same thing, but i want to give the schema inside my validator (so not let the author say against which schema it should validate, but rather let the validator decide which schema to use.

public class NewXmlTemplateValidator implements TemplateValidator
{
    static final String schemaSource    = "schema.xsd";

    public List<SAXParseException> validate ( String xml ) throws Exception
    {
        Reader input = new StringReader(xml);
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL schemaUrl = getClass().getResource(schemaSource);
        Schema schema = factory.newSchema(schemaUrl);
        Validator validator = schema.newValidator();
        DefaultErrorHandler handler = new DefaultErrorHandler();
        validator.setErrorHandler(handler);
        Source source = new StreamSource(input);
        validator.validate(source);
        return handler.getSaxParseExceptions();
    }
}

It works but it does load all xhtml files form the net which takes rather long and is not what i want.

So i want to validate a XML String against a predefined schema with proper entity resolving via a catalog.xml definition.

How can i easily add a CatalogResolver to the second setup?

like image 708
Janning Avatar asked Sep 09 '11 16:09

Janning


1 Answers

Add an XMLCatalogResolver in the second example like this:

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = getClass().getResource(schemaSource);
String catalog = getClass().getResource("/catalog.xml").getFile();
XMLCatalogResolver resolver = new XMLCatalogResolver(new String[] { catalog });
factory.setResourceResolver(resolver);
like image 79
Janning Avatar answered Oct 12 '22 23:10

Janning