dol: initial dol commit
[jump.git] / dol / test / src / test / test / TreeValidator.java
diff --git a/dol/test/src/test/test/TreeValidator.java b/dol/test/src/test/test/TreeValidator.java
new file mode 100644 (file)
index 0000000..4deede8
--- /dev/null
@@ -0,0 +1,78 @@
+package test.test;
+
+import java.io.File;
+
+import test.util.XMLValidator;
+
+/**
+ * Class to check well-formedness and validity of XML documents.
+ */
+public class TreeValidator {
+
+    /**
+     * Check the well-formedness and validity of XML files.
+     *
+     * @param args Specifies the XML file to be checked or the
+     *             directory (including subdirectories) where XML
+     *             and XSD files are checked. Multiple files or
+     *             directories can be specified.
+     */
+    public static void main(String args[]) throws Exception {
+        System.out.println("Run TreeValidator.");
+        if (args.length == 0)
+            browseDirectoryTree("./");
+        else {
+            for (int k = 0; k < args.length; k++)
+                browseDirectoryTree(args[k]);
+        }
+        System.out.println("Finished.");
+    }
+
+    /**
+     * Method which defines what is done for each file found in the
+     * directory tree: When the file is an XML or XSD file, it is
+     * validated using
+     * {@link test.util.XMLValidator#isValid(java.lang.String)}.
+     *
+     * @param filename file to process
+     */
+    protected static void processFile(String filename) throws Exception {
+        if (filename.endsWith("xml") || filename.endsWith("xsd")) {
+            System.out.println("Checking " + filename + "...");
+            if (!XMLValidator.isValid(filename))
+              throw new Exception("File not valid.");
+        }
+    }
+
+    /**
+     * Iterate through the given directory tree. For each file found in
+     * the file, the method {@link #processFile(java.lang.String)} is
+     * called.
+     *
+     * @param path the path of the directory to be browsed
+     */
+    protected static void browseDirectoryTree(String path) throws Exception {
+        File file = new File(path);
+
+        if (!file.exists()) return;
+        if (!file.isDirectory()) return;
+
+        String filepath = file.getPath();
+        String filename = file.getName();
+
+        //loop through files in directory
+        String[] files = file.list();
+
+        for (int k = 0; k < files.length; k++) {
+            File newfile = new File(file.getPath(), files[k]);
+            if (newfile.isFile())
+                processFile(path + System.getProperty("file.separator")
+                        + files[k]);
+            else if (newfile.isDirectory()) {
+                browseDirectoryTree(file.getPath()
+                        + System.getProperty("file.separator")
+                        + files[k]);
+            }
+        }
+    }
+}