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;
26
import nu.xom.Attribute;
27
import nu.xom.Comment;
28
import nu.xom.Document;
29
import nu.xom.Builder;
30
import nu.xom.NodeFactory;
32
import nu.xom.ParsingException;
33
import nu.xom.ProcessingInstruction;
34
import nu.xom.Serializer;
38
* @author Elliotte Rusty Harold
42
public class StreamingROT13 extends NodeFactory {
44
public static String rot13(String s) {
46
StringBuffer out = new StringBuffer(s.length());
47
for (int i = 0; i < s.length(); i++) {
49
if (c >= 'A' && c <= 'M') out.append((char) (c+13));
50
else if (c >= 'N' && c <= 'Z') out.append((char) (c-13));
51
else if (c >= 'a' && c <= 'm') out.append((char) (c+13));
52
else if (c >= 'n' && c <= 'z') out.append((char) (c-13));
53
else out.append((char) c);
55
return out.toString();
59
// We don't really need the comments. We just want to print them.
60
public Nodes makeComment(String data) {
61
return new Nodes(new Comment(rot13(data)));
64
public Nodes makeText(String data) {
65
return new Nodes(new Text(rot13(data)));
68
public Nodes makeAttribute(String name, String namespace,
69
String value, Attribute.Type type) {
70
return new Nodes(new Attribute(name, namespace, rot13(value), type));
73
public Nodes makeProcessingInstruction(
74
String target, String data) {
75
return new Nodes(new ProcessingInstruction(rot13(target), rot13(data)));
78
public static void main(String[] args) {
80
if (args.length <= 0) {
81
System.out.println("Usage: java nu.xom.samples.StreamingROT13 URL");
86
Builder parser = new Builder(new StreamingROT13());
89
Document document = parser.build(args[0]);
92
Serializer serializer = new Serializer(System.out);
93
serializer.write(document);
96
catch (IOException ex) {
98
"Due to an IOException, the parser could not encode " + args[0]
101
catch (ParsingException ex) {
102
System.out.println(ex);
103
ex.printStackTrace();