XHTMLValidator
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
public class XHTMLValidator {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: java XHTMLValidator URL");
return;
}
try {
DocumentBuilderFactory builderFactory
= DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
builderFactory.setValidating(true);
DocumentBuilder parser
= builderFactory.newDocumentBuilder();
parser.setErrorHandler(new ValidityErrorReporter());
Document document;
try {
document = parser.parse(args[0]);
// ValidityErrorReporter prints any validity errors detected
}
catch (SAXException e) {
System.out.println(args[0] + " is not valid.");
return;
}
// If we get this far, then the document is valid XML.
// Check to see whether the document is actually XHTML
DocumentType doctype = document.getDoctype();
if (doctype == null) {
System.out.println("No DOCTYPE");
return;
}
String name = doctype.getName();
String systemID = doctype.getSystemId();
String publicID = doctype.getPublicId();
if (!name.equals("html")) {
System.out.println("Incorrect root element name " + name);
}
if (publicID == null
|| (!publicID.equals("-//W3C//DTD XHTML 1.0 Strict//EN")
&& !publicID.equals(
"-//W3C//DTD XHTML 1.0 Transitional//EN")
&& !publicID.equals(
"-//W3C//DTD XHTML 1.0 Frameset//EN"))) {
System.out.println(args[0]
+ " does not seem to use an XHTML 1.0 DTD");
}
// Check the namespace on the root element
Element root = document.getDocumentElement();
String xmlnsValue = root.getAttribute("xmlns");
if (!xmlnsValue.equals("http://www.w3.org/1999/xhtml")) {
System.out.println(args[0]
+ " does not properly declare the"
+ " http://www.w3.org/1999/xhtml"
+ " namespace on the root element");
return;
}
System.out.println(args[0] + " is valid XHTML.");
}
catch (IOException e) {
System.err.println("Could not read " + args[0]);
}
catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
}
}