1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 import java.util.Stack ; 26 27 import nu.xom.Attribute; 28 import nu.xom.Builder; 29 import nu.xom.DocType; 30 import nu.xom.Document; 31 import nu.xom.Element; 32 import nu.xom.NodeFactory; 33 import nu.xom.Nodes; 34 import nu.xom.ParsingException; 35 import nu.xom.Serializer; 36 37 58 59 public class StreamingXHTMLPurifier extends NodeFactory { 60 61 private Stack namespaces = new Stack (); 62 private Nodes empty = new Nodes(); 63 public final static String XHTML_NAMESPACE 64 = "http://www.w3.org/1999/xhtml"; 65 66 public Nodes makeText(String data) { 68 if (inXHTML()) return super.makeText(data); 69 return empty; 70 } 71 72 public Nodes makeComment(String data) { 73 if (inXHTML()) return super.makeComment(data); 74 return empty; 75 } 76 77 78 private boolean inXHTML() { 79 if (namespaces.isEmpty()) return true; String currentNamespace = (String ) (namespaces.peek()); 81 if (XHTML_NAMESPACE.equals(currentNamespace)) return true; 82 return false; 83 } 84 85 public Element startMakingElement(String name, String namespace) { 86 87 namespaces.push(namespace); 88 if (XHTML_NAMESPACE.equals(namespace)) { 89 return super.startMakingElement(name, namespace); 90 } 91 return null; 92 } 93 94 public Nodes finishMakingElement(Element element) { 95 namespaces.pop(); 96 int namespaceCount = element.getNamespaceDeclarationCount(); 97 for (int i = 0; i < namespaceCount; i++) { 98 String prefix = element.getNamespacePrefix(i); 99 element.removeNamespaceDeclaration(prefix); 100 if (element.getNamespaceDeclarationCount() < namespaceCount) { 101 i--; 102 namespaceCount--; 103 } 104 } 105 return new Nodes(element); 106 } 107 108 public Nodes makeDocType(String rootElementName, 109 String publicID, String systemID) { 110 return new Nodes(new DocType("html", 111 "PUBLIC \"-//W3C//DTD XHTML Basic 1.0//EN\"", 112 "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd")); 113 } 114 115 public Nodes makeProcessingInstruction( 116 String target, String data) { 117 if (inXHTML()) { 118 return super.makeProcessingInstruction(target, data); 119 } 120 return empty; 121 } 122 123 public Nodes makeAttribute(String name, String URI, 124 String value, Attribute.Type type) { 125 if ("".equals(URI) 126 || "http://www.w3.org/XML/1998/namespace".equals(URI)) { 127 return super.makeAttribute(name, URI, value, type); 128 } 129 return empty; 130 } 131 132 public static void main(String [] args) { 133 134 if (args.length == 0) { 135 System.out.println( 136 "Usage: java nu.xom.samples.StreamingXHTMLPurifier URL" 137 ); 138 return; 139 } 140 141 StreamingXHTMLPurifier factory = new StreamingXHTMLPurifier(); 142 Builder builder = new Builder(factory); 143 144 try { 145 Document doc = builder.build(args[0]); 146 Serializer serializer = new Serializer(System.out); 147 serializer.write(doc); 148 } 149 catch (ParsingException ex) { 151 System.out.println(args[0] + " is not well-formed."); 152 System.out.println(ex.getMessage()); 153 ex.printStackTrace(); 154 } 155 catch (IOException ex) { 156 System.out.println(ex); 157 } 158 159 } 160 161 } 162 | Popular Tags |