1 /* $Id: XMLValidator.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.helper.validator;
4 import java.io.IOException;
6 import org.jdom.JDOMException;
7 import org.jdom.input.SAXBuilder;
10 * Class to check well-formedness and validity of XML documents.
12 public class XMLValidator {
15 * Check the well-formedness and validity of an XML document.
16 * The file is checked against the schema which is referenced from
17 * within the file. When the document is an XML schema, it is checked
18 * against <a href="http://www.w3.org/2001/XMLSchema.xsd"
19 * target="_blank">http://www.w3.org/2001/XMLSchema.xsd</a>.
21 * @param filename filename of the file to be checked
23 public static boolean isValid(String filename) {
24 SAXBuilder builder = new SAXBuilder(true);
25 builder.setFeature("http://xml.org/sax/features/validation",
27 builder.setFeature("http://apache.org/xml/features/validation/"
29 builder.setFeature("http://apache.org/xml/features/validation/"
30 + "schema-full-checking", true);
31 builder.setProperty("http://apache.org/xml/properties/schema/"
32 + "external-schemaLocation",
33 "http://www.w3.org/2001/XMLSchema "
34 + "http://www.w3.org/2001/XMLSchema.xsd "
35 + dol.util.SchemaLocation.getExternalSchemaLocation());
37 builder.build(filename);
39 catch (JDOMException e) {
40 System.out.println("Found an error in " + filename + ".");
41 System.out.println(e.getMessage());
42 System.out.println("");
45 catch (IOException e) {
46 System.out.println("Found an error in " + filename + ".");
47 System.out.println(e.getMessage());
48 System.out.println("");
56 * Main function. We might extend the validator to validate different
57 * kind of XMLs based on different schemas.
59 * @param args The command line argument.
61 public static void main(String[] args) {
63 /* Check command line parameters */
64 /* maybe add options to check both external/internal schema */
65 if (args.length != 1) {
66 System.out.println("Usage: XMLValidator <file>");
71 if (XMLValidator.isValid(args[0])){
72 System.out.println(args[0] + " is valid.");