KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
25
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;
31 import nu.xom.Nodes;
32 import nu.xom.ParsingException;
33 import nu.xom.ProcessingInstruction;
34 import nu.xom.Serializer;
35 import nu.xom.Text;
36
37 /**
38  * @author Elliotte Rusty Harold
39  * @version 1.0
40  *
41  */

42 public class StreamingROT13 extends NodeFactory {
43
44     public static String JavaDoc rot13(String JavaDoc s) {
45     
46         StringBuffer JavaDoc out = new StringBuffer JavaDoc(s.length());
47         for (int i = 0; i < s.length(); i++) {
48           int c = s.charAt(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);
54         }
55         return out.toString();
56     
57     }
58
59     // We don't really need the comments. We just want to print them.
60
public Nodes makeComment(String JavaDoc data) {
61         return new Nodes(new Comment(rot13(data)));
62     }
63
64     public Nodes makeText(String JavaDoc data) {
65         return new Nodes(new Text(rot13(data)));
66     }
67
68     public Nodes makeAttribute(String JavaDoc name, String JavaDoc namespace,
69       String JavaDoc value, Attribute.Type type) {
70         return new Nodes(new Attribute(name, namespace, rot13(value), type));
71     }
72
73     public Nodes makeProcessingInstruction(
74       String JavaDoc target, String JavaDoc data) {
75         return new Nodes(new ProcessingInstruction(rot13(target), rot13(data)));
76     }
77
78   public static void main(String JavaDoc[] args) {
79
80     if (args.length <= 0) {
81       System.out.println("Usage: java nu.xom.samples.StreamingROT13 URL");
82       return;
83     }
84     
85     try {
86       Builder parser = new Builder(new StreamingROT13());
87       
88       // Read the document
89
Document document = parser.build(args[0]);
90       
91       // Write it out again
92
Serializer serializer = new Serializer(System.out);
93       serializer.write(document);
94
95     }
96     catch (IOException ex) {
97       System.out.println(
98       "Due to an IOException, the parser could not encode " + args[0]
99       );
100     }
101     catch (ParsingException ex) {
102       System.out.println(ex);
103       ex.printStackTrace();
104     }
105      
106   } // end main
107

108 }
109
Popular Tags