dol: initial dol commit
[jump.git] / dol / test / src / test / test / TreeValidator.java
1 package test.test;
2
3 import java.io.File;
4
5 import test.util.XMLValidator;
6
7 /**
8  * Class to check well-formedness and validity of XML documents.
9  */
10 public class TreeValidator {
11
12     /**
13      * Check the well-formedness and validity of XML files.
14      *
15      * @param args Specifies the XML file to be checked or the
16      *             directory (including subdirectories) where XML
17      *             and XSD files are checked. Multiple files or
18      *             directories can be specified.
19      */
20     public static void main(String args[]) throws Exception {
21         System.out.println("Run TreeValidator.");
22         if (args.length == 0)
23             browseDirectoryTree("./");
24         else {
25             for (int k = 0; k < args.length; k++)
26                 browseDirectoryTree(args[k]);
27         }
28         System.out.println("Finished.");
29     }
30
31     /**
32      * Method which defines what is done for each file found in the
33      * directory tree: When the file is an XML or XSD file, it is
34      * validated using
35      * {@link test.util.XMLValidator#isValid(java.lang.String)}.
36      *
37      * @param filename file to process
38      */
39     protected static void processFile(String filename) throws Exception {
40         if (filename.endsWith("xml") || filename.endsWith("xsd")) {
41             System.out.println("Checking " + filename + "...");
42             if (!XMLValidator.isValid(filename))
43               throw new Exception("File not valid.");
44         }
45     }
46
47     /**
48      * Iterate through the given directory tree. For each file found in
49      * the file, the method {@link #processFile(java.lang.String)} is
50      * called.
51      *
52      * @param path the path of the directory to be browsed
53      */
54     protected static void browseDirectoryTree(String path) throws Exception {
55         File file = new File(path);
56
57         if (!file.exists()) return;
58         if (!file.isDirectory()) return;
59
60         String filepath = file.getPath();
61         String filename = file.getName();
62
63         //loop through files in directory
64         String[] files = file.list();
65
66         for (int k = 0; k < files.length; k++) {
67             File newfile = new File(file.getPath(), files[k]);
68             if (newfile.isFile())
69                 processFile(path + System.getProperty("file.separator")
70                         + files[k]);
71             else if (newfile.isDirectory()) {
72                 browseDirectoryTree(file.getPath()
73                         + System.getProperty("file.separator")
74                         + files[k]);
75             }
76         }
77     }
78 }