1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.io.IOException;
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;
34
* Demonstrates the use of the <code>DocType</code> class
35
* by validating XHTML.
38
* @author Elliotte Rusty Harold
42
public class XHTMLValidator {
44
public static void main(String[] args) {
46
for (int i = 0; i < args.length; i++) {
52
private static Builder builder = new Builder(true);
53
/* turn on validation ^^^^ */
56
public static void validate(String source) {
60
document = builder.build(source);
62
catch (ParsingException ex) {
63
System.out.println(source
64
+ " is invalid XML, and thus not XHTML.");
67
catch (IOException ex) {
68
System.out.println("Could not read: " + source);
72
// If we get this far, then the document is valid XML.
73
// Check to see whether the document is actually XHTML
75
DocType doctype = document.getDocType();
77
if (doctype == null) {
78
System.out.println("No DOCTYPE");
83
String name = doctype.getRootElementName();
84
String publicID = doctype.getPublicID();
86
if (!name.equals("html")) {
88
"Incorrect root element name " + name);
93
|| (!publicID.equals("-//W3C//DTD XHTML 1.0 Strict//EN")
95
"-//W3C//DTD XHTML 1.0 Transitional//EN")
97
"-//W3C//DTD XHTML 1.0 Frameset//EN"))) {
99
System.out.println(source
100
+ " does not seem to use an XHTML 1.0 DTD");
105
// Check the namespace on the root element
106
Element root = document.getRootElement();
107
String uri = root.getNamespaceURI();
108
String prefix = root.getNamespacePrefix();
109
if (!uri.equals("http://www.w3.org/1999/xhtml")) {
111
System.out.println(source
112
+ " does not properly declare the"
113
+ " http://www.w3.org/1999/xhtml namespace"
114
+ " on the root element");
116
if (!prefix.equals("")) {
118
System.out.println(source
119
+ " does not use the empty prefix for XHTML");
122
if (valid) System.out.println(source + " is valid XHTML.");