XHTMLValidator

import org.w3c.dom.*;
import org.apache.xerces.parsers.*; 
import java.io.*;
import org.xml.sax.*;


public class XHTMLValidator {

  public static void main(String[] args) {
    
    for (int i = 0; i < args.length; i++) {
      validate(args[i]);
    }   
    
  }

  private static DOMParser parser = new DOMParser();
  
  static {
    
    // turn on validation
    try {
      parser.setFeature("http://xml.org/sax/features/validation", true);
      parser.setErrorHandler(new ValidityErrorReporter());
    }
    catch (SAXNotRecognizedException e) {
      System.err.println(
       "Installed XML parser cannot validate; checking for well-formedness instead...");
    } 
    catch (SAXNotSupportedException e) {
      System.err.println(
       "Cannot turn on validation here; checking for well-formedness instead...");
    }     
    
  }
  
  // not thread safe
  public static void validate(String source) {
        
    try {

      try {
        parser.parse(source); 
        // ValidityErrorReporter prints any validity errors detected
      }
      catch (SAXException e) {  
        System.out.println(source + " is not well formed."); 
        return; 
      }
      
      // If we get this far, then the document is well-formed XML.
      // Check to see whether the document is actually XHTML    
      Document document = parser.getDocument();
    
      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(source + " 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(source 
         + " does not properly declare the http://www.w3.org/1999/xhtml namespace on the root element");        
      }
    
      // get ready for the next parse
      parser.reset();
      
    }
    catch (IOException e) {
      System.err.println("Could not read " + source);
    }
    catch (Exception e) {
      System.err.println(e);
      e.printStackTrace();
    }
    
  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified March 14, 2000