KickJava   Java API By Example, From Geeks To Geeks.

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


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.Document;
28 import nu.xom.Element;
29 import nu.xom.NodeFactory;
30 import nu.xom.ParsingException;
31 import nu.xom.Serializer;
32
33 /**
34  * <p>
35  * Demonstrates a custom <code>NodeFactory</code> that changes the
36  * namespaces of elements while building the document so a second
37  * tree walk is not required. Specifically, it adds the XHTML
38  * namespace <code>http://www.w3.org/1999/xhtml</code> to all
39  * elements.
40  * </p>
41  *
42  * @author Elliotte Rusty Harold
43  * @version 1.0
44  *
45  */

46
47 public class StreamingXHTMLQualifier extends NodeFactory {
48
49     public final static String JavaDoc XHTML_NAMESPACE
50       = "http://www.w3.org/1999/xhtml";
51
52     public Element startMakingElement(String JavaDoc name, String JavaDoc namespace) {
53         
54         if ("".equals(namespace) || null == namespace) {
55             return super.startMakingElement(name, XHTML_NAMESPACE);
56         }
57         else return super.startMakingElement(name, namespace);
58     }
59
60     public static void main(String JavaDoc[] args) {
61   
62         if (args.length <= 0) {
63           System.out.println(
64             "Usage: java nu.xom.samples.StreamingXHTMLQualifier URL"
65           );
66           return;
67         }
68         
69         try {
70           Builder parser = new Builder(new StreamingXHTMLQualifier());
71           Document doc = parser.build(args[0]);
72           Serializer out = new Serializer(System.out);
73           out.write(doc);
74         }
75         catch (ParsingException ex) {
76           System.out.println(args[0] + " is not well-formed.");
77           System.out.println(ex.getMessage());
78         }
79         catch (IOException JavaDoc ex) {
80           System.out.println(
81            "Due to an IOException, the parser could not read "
82            + args[0]
83           );
84         }
85   
86     }
87
88 }
89
Popular Tags