1 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 42 public class StreamingROT13 extends NodeFactory { 43 44 public static String rot13(String s) { 45 46 StringBuffer out = new StringBuffer (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 public Nodes makeComment(String data) { 61 return new Nodes(new Comment(rot13(data))); 62 } 63 64 public Nodes makeText(String data) { 65 return new Nodes(new Text(rot13(data))); 66 } 67 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)); 71 } 72 73 public Nodes makeProcessingInstruction( 74 String target, String data) { 75 return new Nodes(new ProcessingInstruction(rot13(target), rot13(data))); 76 } 77 78 public static void main(String [] 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 Document document = parser.build(args[0]); 90 91 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 } 108 } 109 | Popular Tags |