Project

Profile

Help

Bug #4099 » SaxonTester.java

Gerben Abbink, 2019-01-16 20:43

 
import com.saxonica.config.*;
import javax.xml.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import net.sf.saxon.s9api.*;
import org.xml.sax.*;

public class SaxonTester{

public static void main(String[] args){
try{
EnterpriseConfiguration configuration = new EnterpriseConfiguration();
Processor processor = new Processor(configuration);
// http://www.saxonica.com/documentation/index.html#!javadoc/net.sf.saxon.s9api/SchemaManager
SchemaManager schemaManager = processor.getSchemaManager();
schemaManager.setErrorListener(new MyErrorListener());
schemaManager.load(new StreamSource("SaxonTester.xsd"));

SchemaValidator schemaValidator = schemaManager.newSchemaValidator();

System.out.println("=============");
System.out.println("WITHOUT ErrorListener");
System.out.println("=============");
try{
schemaValidator.setErrorListener(null);
schemaValidator.validate(new StreamSource("SaxonTester.xml"));
}
catch(Exception e){
}
System.out.println("=============");
System.out.println("");

System.out.println("=============");
System.out.println("WITH ErrorListener");
System.out.println("Possible bugs:");
System.out.println("- Why are the 2 warnings still written to stderr(stdout?), even if there's an error listener?");
System.out.println("- Validation should stop if the error listener throws an TransformerException.");
System.out.println(" https://docs.oracle.com/javase/9/docs/api/javax/xml/transform/ErrorListener.html#error-javax.xml.transform.TransformerException-");
System.out.println("=============");
try{
schemaValidator.setErrorListener(new MyErrorListener());
schemaValidator.validate(new StreamSource("SaxonTester.xml"));
}
catch(Exception e){
}
System.out.println("=============");
System.out.println("");
}
catch(Exception e){
System.out.println(String.format("EXCEPTION %s %s", e.getClass().getName(), e.getMessage()));
}
}

private static class MyErrorListener implements ErrorListener{
public void error(TransformerException e) throws TransformerException{
System.out.println(String.format("error - %s", e.getMessage()));
System.out.println("I WILL THROW TransformerException");
throw new TransformerException("STOP IT");
}
public void fatalError(TransformerException e) throws TransformerException{
System.out.println(String.format("fatalError - %s", e.getMessage()));
}
public void warning(TransformerException e) throws TransformerException{
System.out.println(String.format("warning - %s", e.getMessage()));
}
}
}
(1-1/3)