01: /* Copyright 2002, 2003 Elliotte Rusty Harold
02:
03: This library is free software; you can redistribute it and/or modify
04: it under the terms of version 2.1 of the GNU Lesser General Public
05: License as published by the Free Software Foundation.
06:
07: This library is distributed in the hope that it will be useful,
08: but WITHOUT ANY WARRANTY; without even the implied warranty of
09: 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;
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 XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
50:
51: public Element startMakingElement(String name, String namespace) {
52:
53: if ("".equals(namespace) || null == namespace) {
54: return super .startMakingElement(name, XHTML_NAMESPACE);
55: } else
56: return super .startMakingElement(name, namespace);
57: }
58:
59: public static void main(String[] args) {
60:
61: if (args.length <= 0) {
62: System.out
63: .println("Usage: java nu.xom.samples.StreamingXHTMLQualifier URL");
64: return;
65: }
66:
67: try {
68: Builder parser = new Builder(new StreamingXHTMLQualifier());
69: Document doc = parser.build(args[0]);
70: Serializer out = new Serializer(System.out);
71: out.write(doc);
72: } catch (ParsingException ex) {
73: System.out.println(args[0] + " is not well-formed.");
74: System.out.println(ex.getMessage());
75: } catch (IOException ex) {
76: System.out
77: .println("Due to an IOException, the parser could not read "
78: + args[0]);
79: }
80:
81: }
82:
83: }
|