5 import test.util.XMLValidator;
8 * Class to check well-formedness and validity of XML documents.
10 public class TreeValidator {
13 * Check the well-formedness and validity of XML files.
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.
20 public static void main(String args[]) throws Exception {
21 System.out.println("Run TreeValidator.");
23 browseDirectoryTree("./");
25 for (int k = 0; k < args.length; k++)
26 browseDirectoryTree(args[k]);
28 System.out.println("Finished.");
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
35 * {@link test.util.XMLValidator#isValid(java.lang.String)}.
37 * @param filename file to process
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.");
48 * Iterate through the given directory tree. For each file found in
49 * the file, the method {@link #processFile(java.lang.String)} is
52 * @param path the path of the directory to be browsed
54 protected static void browseDirectoryTree(String path) throws Exception {
55 File file = new File(path);
57 if (!file.exists()) return;
58 if (!file.isDirectory()) return;
60 String filepath = file.getPath();
61 String filename = file.getName();
63 //loop through files in directory
64 String[] files = file.list();
66 for (int k = 0; k < files.length; k++) {
67 File newfile = new File(file.getPath(), files[k]);
69 processFile(path + System.getProperty("file.separator")
71 else if (newfile.isDirectory()) {
72 browseDirectoryTree(file.getPath()
73 + System.getProperty("file.separator")