+package test.util;
+
+import java.util.ResourceBundle;
+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) {
+ ResourceBundle resourceBundle = ResourceBundle.getBundle("test");
+
+ 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 "
+ + resourceBundle.getString("LOCAL_SCHEMAS"));
+
+ 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;
+ }
+}