X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Fhelper%2Fvalidator%2FXMLValidator.java;fp=dol%2Fsrc%2Fdol%2Fhelper%2Fvalidator%2FXMLValidator.java;h=b65b9a8a99818dfb92a7c2e667167d9003549883;hb=8c411cf24ed0eb889191aaeafd8fa1e69081df42;hp=0000000000000000000000000000000000000000;hpb=dea7a4fb1ed110d3ce6e6d9255103d724bd66c0e;p=jump.git diff --git a/dol/src/dol/helper/validator/XMLValidator.java b/dol/src/dol/helper/validator/XMLValidator.java new file mode 100644 index 0000000..b65b9a8 --- /dev/null +++ b/dol/src/dol/helper/validator/XMLValidator.java @@ -0,0 +1,77 @@ +/* $Id: XMLValidator.java 1 2010-02-24 13:03:05Z haidw $ */ +package dol.helper.validator; + +import java.io.IOException; + +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; + +/** + * Class to check well-formedness and validity of XML documents. + */ +public class XMLValidator { + + /** + * Check the well-formedness and validity of an XML document. + * The file is checked against the schema which is referenced from + * within the file. When the document is an XML schema, it is checked + * against http://www.w3.org/2001/XMLSchema.xsd. + * + * @param filename filename of the file to be checked + */ + public static boolean isValid(String filename) { + SAXBuilder builder = new SAXBuilder(true); + builder.setFeature("http://xml.org/sax/features/validation", + true); + builder.setFeature("http://apache.org/xml/features/validation/" + + "schema", true); + builder.setFeature("http://apache.org/xml/features/validation/" + + "schema-full-checking", true); + builder.setProperty("http://apache.org/xml/properties/schema/" + + "external-schemaLocation", + "http://www.w3.org/2001/XMLSchema " + + "http://www.w3.org/2001/XMLSchema.xsd " + + dol.util.SchemaLocation.getExternalSchemaLocation()); + try { + builder.build(filename); + } + catch (JDOMException e) { + System.out.println("Found an error in " + filename + "."); + System.out.println(e.getMessage()); + System.out.println(""); + return false; + } + catch (IOException e) { + System.out.println("Found an error in " + filename + "."); + System.out.println(e.getMessage()); + System.out.println(""); + return false; + } + + return true; + } + + /** + * Main function. We might extend the validator to validate different + * kind of XMLs based on different schemas. + * + * @param args The command line argument. + */ + public static void main(String[] args) { + + /* Check command line parameters */ + /* maybe add options to check both external/internal schema */ + if (args.length != 1) { + System.out.println("Usage: XMLValidator "); + System.out.println(); + return; + } + + if (XMLValidator.isValid(args[0])){ + System.out.println(args[0] + " is valid."); + } else { + System.exit(1); + } + } +}