1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Builder; 27 import nu.xom.DocType; 28 import nu.xom.Document; 29 import nu.xom.Element; 30 import nu.xom.ParsingException; 31 32 42 public class XHTMLValidator { 43 44 public static void main(String [] args) { 45 46 for (int i = 0; i < args.length; i++) { 47 validate(args[i]); 48 } 49 50 } 51 52 private static Builder builder = new Builder(true); 53 54 55 public static void validate(String source) { 57 58 Document document; 59 try { 60 document = builder.build(source); 61 } 62 catch (ParsingException ex) { 63 System.out.println(source 64 + " is invalid XML, and thus not XHTML."); 65 return; 66 } 67 catch (IOException ex) { 68 System.out.println("Could not read: " + source); 69 return; 70 } 71 72 boolean valid = true; 75 DocType doctype = document.getDocType(); 76 77 if (doctype == null) { 78 System.out.println("No DOCTYPE"); 79 valid = false; 80 } 81 else { 82 String name = doctype.getRootElementName(); 84 String publicID = doctype.getPublicID(); 85 86 if (!name.equals("html")) { 87 System.out.println( 88 "Incorrect root element name " + name); 89 valid = false; 90 } 91 92 if (publicID == null 93 || (!publicID.equals("-//W3C//DTD XHTML 1.0 Strict//EN") 94 && !publicID.equals( 95 "-//W3C//DTD XHTML 1.0 Transitional//EN") 96 && !publicID.equals( 97 "-//W3C//DTD XHTML 1.0 Frameset//EN"))) { 98 valid = false; 99 System.out.println(source 100 + " does not seem to use an XHTML 1.0 DTD"); 101 } 102 } 103 104 105 Element root = document.getRootElement(); 107 String uri = root.getNamespaceURI(); 108 String prefix = root.getNamespacePrefix(); 109 if (!uri.equals("http://www.w3.org/1999/xhtml")) { 110 valid = false; 111 System.out.println(source 112 + " does not properly declare the" 113 + " http://www.w3.org/1999/xhtml namespace" 114 + " on the root element"); 115 } 116 if (!prefix.equals("")) { 117 valid = false; 118 System.out.println(source 119 + " does not use the empty prefix for XHTML"); 120 } 121 122 if (valid) System.out.println(source + " is valid XHTML."); 123 124 } 125 126 } 127 | Popular Tags |