KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > XHTMLValidator


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
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 /**
33  * <p>
34  * Demonstrates the use of the <code>DocType</code> class
35  * by validating XHTML.
36  * </p>
37  *
38  * @author Elliotte Rusty Harold
39  * @version 1.0
40  *
41  */

42 public class XHTMLValidator {
43
44   public static void main(String JavaDoc[] 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                          /* turn on validation ^^^^ */
54   
55   // not thread safe
56
public static void validate(String JavaDoc 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 JavaDoc ex) {
68         System.out.println("Could not read: " + source);
69         return;
70       }
71       
72       // If we get this far, then the document is valid XML.
73
// Check to see whether the document is actually XHTML
74
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         // verify the DOCTYPE
83
String JavaDoc name = doctype.getRootElementName();
84         String JavaDoc 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       // Check the namespace on the root element
106
Element root = document.getRootElement();
107       String JavaDoc uri = root.getNamespaceURI();
108       String JavaDoc 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