How to Validate a xml file with catalogs using Xerces2-j

326 views Asked by At

I'm trying to validate an xml file using oasis catalog. What i need is to give the path of the xml file and the path of the catalog that contains the xsd as input and to get the validation (true or error message) as output.

What i have done so far is :

    File schemaFile = new File("personal.xsd"); // etc.
    Source xmlFile = new StreamSource(new File("personal.xml"));
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1"); // we need to validate with v1.1


    try {
        Schema schema = schemaFactory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        validator.validate(xmlFile);
        System.out.println(xmlFile.getSystemId() + " is valid");
    } catch (SAXException e) {
        System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
    } catch (IOException e) {
        e.printStackTrace();
    }

So it's working so far.

The issue is to add the catalog. I found some code sample but it is not working :

Source xmlFile = new StreamSource(new File("personal.xml"));
String [] catalogs =  {"file:///catalog.xml"};
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
    try {
        
        XMLCatalogResolver resolver = new XMLCatalogResolver();
        resolver.setPreferPublic(true);
        resolver.setCatalogList(catalogs);
        schemaFactory.setResourceResolver(resolver);
        
        Schema schema = schemaFactory.newSchema();
        Validator validator = schema.newValidator();
        validator.validate(xmlFile);

}

In the end, the xsd is not find and the validation fails.

Can someone explain me what i am doing wrong ? i feel like it should be simple but i'm missing something.

Thank.

1

There are 1 answers

0
Morgan Wolf On

After 2 weeks of research here my conclusion

I never managed to do it with Xerces-2j. You can do it with the xerces in the JRE (javaSE-1.8), here is how you can do it.

import java.io.File;
import java.io.StringWriter;

import java.net.URL;

//import javax.xml.validation.SchemaFactory;

import java.net.URI;
import java.net.URISyntaxException;

import javax.xml.XMLConstants;
//import javax.xml.catalog.CatalogFeatures;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.Validator;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class Foo
{
    private static int errorCount = 0;

    public static void main(String[] args)
    {
        final String  catalogFile = "javax.xml.catalog.files";//CatalogFeatures.Feature.FILES.getPropertyName();
        final String  catalogPath = "catalog.xml";

        final ClassLoader  classLoader = Foo.class.getClassLoader();
        try
        {
            final URL  catalogUrl = classLoader.getResource(catalogPath);
            final URI  catalog = catalogUrl.toURI();

            if (catalog != null)
            {
                System.out.println("catalog : "+ catalogFile);
                SchemaFactory  schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//              SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
                Schema  schema = schemaFactory.newSchema();

                StreamSource  source = new StreamSource(new File("personal.xml"));
                Validator  validator = schema.newValidator();
                validator.setProperty(catalogFile, catalog.toString());
                validator.setErrorHandler(new MyErrorHandler());
                StringWriter  writer = new StringWriter();
                StreamResult  result = new StreamResult(writer);
                validator.validate(source, result);

                System.out.println(writer);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    private static class MyErrorHandler implements ErrorHandler {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("Warning: ");
            printException(e);
        }
        public void error(SAXParseException e) throws SAXException {
            System.out.println("Error: ");
            printException(e);
        }
        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("Fattal error: ");
            printException(e);
        }
        private void printException(SAXParseException e) {
            errorCount++;
            System.out.println("   Line number: "+e.getLineNumber());
            System.out.println("   Column number: "+e.getColumnNumber());
            System.out.println("   Message: "+e.getMessage());
            System.out.println();
        }
    }
}

GL if you want to use xerces-2J