dol: initial dol commit
[jump.git] / dol / test / src / test / util / XMLValidator.java
1 package test.util;
2
3 import java.util.ResourceBundle;
4 import java.io.IOException;
5 import org.jdom.JDOMException;
6 import org.jdom.input.SAXBuilder;
7
8 /**
9  * Class to check well-formedness and validity of XML documents.
10  */
11 public class XMLValidator {
12
13     /**
14      * Check the well-formedness and validity of an XML document.
15      * The file is checked against the schema which is referenced from
16      * within the file. When the document is an XML schema, it is checked
17      * against <a href="http://www.w3.org/2001/XMLSchema.xsd"
18      * target="_blank">http://www.w3.org/2001/XMLSchema.xsd</a>.
19      *
20      * @param filename filename of the file to be checked
21      */
22     public static boolean isValid(String filename) {
23         ResourceBundle resourceBundle = ResourceBundle.getBundle("test");
24
25         SAXBuilder builder = new SAXBuilder(true);
26         builder.setFeature("http://xml.org/sax/features/validation",
27                 true);
28         builder.setFeature("http://apache.org/xml/features/validation/"
29                 + "schema", true);
30         builder.setFeature("http://apache.org/xml/features/validation/"
31                 + "schema-full-checking", true);
32         builder.setProperty("http://apache.org/xml/properties/schema/"
33                 + "external-schemaLocation",
34                 "http://www.w3.org/2001/XMLSchema "
35                 + "http://www.w3.org/2001/XMLSchema.xsd "
36                 + resourceBundle.getString("LOCAL_SCHEMAS"));
37
38         try {
39             builder.build(filename);
40         }
41         catch (JDOMException e) {
42             System.out.println("Found an error in " + filename + ".");
43             System.out.println(e.getMessage());
44             System.out.println("");
45             return false;
46         }
47         catch (IOException e) {
48             System.out.println("Found an error in " + filename + ".");
49             System.out.println(e.getMessage());
50             System.out.println("");
51             return false;
52         }
53
54         return true;
55     }
56 }