dol: initial dol commit
[jump.git] / dol / src / dol / helper / validator / XMLValidator.java
diff --git a/dol/src/dol/helper/validator/XMLValidator.java b/dol/src/dol/helper/validator/XMLValidator.java
new file mode 100644 (file)
index 0000000..b65b9a8
--- /dev/null
@@ -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 <a href="http://www.w3.org/2001/XMLSchema.xsd"
+     * target="_blank">http://www.w3.org/2001/XMLSchema.xsd</a>.
+     *
+     * @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 <file>");
+            System.out.println();
+            return;
+        }
+
+        if (XMLValidator.isValid(args[0])){
+            System.out.println(args[0] + " is valid.");
+        } else {
+            System.exit(1);
+        }
+    }
+}