Skip to content

Instantly share code, notes, and snippets.

@ebruchez
Last active August 29, 2015 14:02
Show Gist options
  • Save ebruchez/c369ddb9f1f0af4f521d to your computer and use it in GitHub Desktop.
Save ebruchez/c369ddb9f1f0af4f521d to your computer and use it in GitHub Desktop.
package hu.unideb.inf.validator;
import java.io.File;
import java.net.URL;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXParseException;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class SchemaValidator {
private static Options options = new Options();
static {
OptionGroup schemaOptions = new OptionGroup();
schemaOptions.setRequired(false);
schemaOptions.addOption(
OptionBuilder
.withLongOpt("schemaFile")
.hasArg()
.withArgName("file")
.withDescription("read schema from the file specified")
.create("sf")
);
schemaOptions.addOption(
OptionBuilder
.withLongOpt("schemaUrl")
.hasArg()
.withArgName("url")
.withDescription("read schema from the URL specified")
.create("su")
);
options.addOptionGroup(schemaOptions);
OptionGroup instanceOptions = new OptionGroup();
instanceOptions.addOption(
OptionBuilder
.withLongOpt("instanceFile")
.hasArg()
.withArgName("file")
.withDescription("read instance from the file specified")
.create("if")
);
instanceOptions.addOption(
OptionBuilder
.withLongOpt("instanceUrl")
.hasArg()
.withArgName("url")
.withDescription("read instance from the URL specified")
.create("iu")
);
instanceOptions.setRequired(true);
options.addOptionGroup(instanceOptions);
}
private static void printHelpAndExit(int status) {
new HelpFormatter().printHelp("java " + SchemaValidator.class.getName(), options, true);
System.exit(status);
}
public static void main(String[] args) {
CommandLineParser parser = new BasicParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch(ParseException e) {
System.err.println(e.getMessage());
printHelpAndExit(1);
}
System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema/v1.1",
"org.apache.xerces.jaxp.validation.XMLSchema11Factory");
try {
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
sf.setErrorHandler(new SimpleErrorHandler(true));
Schema schema = null;
if (cmd.hasOption("sf")) {
schema = sf.newSchema(new File(cmd.getOptionValue("sf")));
} else if (cmd.hasOption("su")) {
schema = sf.newSchema(new URL(cmd.getOptionValue("su")));
} else {
schema = sf.newSchema();
}
StreamSource instance = null;
if (cmd.hasOption("if")) {
instance = new StreamSource(cmd.getOptionValue("if"));
} else if (cmd.hasOption("iu")) {
instance = new StreamSource(cmd.getOptionValue("iu"));
}
Validator validator = schema.newValidator();
validator.setErrorHandler(new SimpleErrorHandler(false));
validator.validate(instance);
} catch(SAXParseException e) {
} catch(Exception e) {
System.err.println(e.getMessage());
}
}
}
package hu.unideb.inf.validator;
import java.io.PrintWriter;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
public class SimpleErrorHandler implements ErrorHandler {
private PrintWriter out;
private boolean failOnError = false;
public SimpleErrorHandler() {
out = new PrintWriter(System.err, true);
}
public SimpleErrorHandler(boolean failOnError) {
this();
this.failOnError = failOnError;
}
public SimpleErrorHandler(PrintWriter out) {
this.out = out;
}
public void warning(SAXParseException e) throws SAXException {
printException("Warning", e);
}
public void error(SAXParseException e) throws SAXException {
printException("Error", e);
if (failOnError) throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
printException("Fatal error", e);
throw e;
}
private void printException(String prefix, SAXParseException e) throws SAXException {
out.format("[%s] %s:%d:%d: %s%n", prefix, e.getSystemId(), e.getLineNumber(), e.getColumnNumber(), e.getMessage());
}
}
@ebruchez
Copy link
Author

Example code for Xerces validation (see http://jeszysblog.wordpress.com/2012/09/27/free-and-open-source-xsd-1-1-validation-tool/) under Apache license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment