Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Attribute; 27 import nu.xom.Builder; 28 import nu.xom.Document; 29 import nu.xom.Element; 30 import nu.xom.Elements; 31 import nu.xom.ParsingException; 32 33 43 public class XHTMLPurifier { 44 45 public final static String XHTML_NAMESPACE 46 = "http://www.w3.org/1999/xhtml"; 47 48 public static void main(String [] args) { 49 50 if (args.length <= 0) { 51 System.out.println("Usage: java nu.xom.samples.XHTMLPurifier URL"); 52 return; 53 } 54 55 try { 56 Builder parser = new Builder(); 57 Document doc = parser.build(args[0]); 58 Element root = doc.getRootElement(); 59 if (root.getNamespaceURI().equals(XHTML_NAMESPACE)) { 60 strip(root); 61 } 62 else { 63 System.out.println(args[0] 64 + " does not appear to be an XHTML document"); 65 return; 66 } 67 68 69 System.out.println(doc.toXML()); 70 } 71 catch (ParsingException ex) { 72 System.out.println(args[0] + " is not well-formed."); 73 System.out.println(ex.getMessage()); 74 } 75 catch (IOException ex) { 76 System.out.println( 77 "Due to an IOException, the parser could not read " 78 + args[0] 79 ); 80 } 81 82 } 83 84 public static void strip(Element element) { 85 86 if (element.getNamespaceURI().equals(XHTML_NAMESPACE)) { 87 88 for (int i = 0; i < element.getAttributeCount(); i++) { 90 Attribute attribute = element.getAttribute(i); 91 92 if (!"".equals(attribute.getNamespaceURI())) { 93 if (!"xml".equals(attribute.getNamespacePrefix())) { 94 attribute.detach(); 95 } 96 } 97 } 98 99 for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) { 101 String prefix = element.getNamespacePrefix(i); 102 element.removeNamespaceDeclaration(prefix); 103 } 104 105 Elements elements = element.getChildElements(); 106 for (int i = 0; i < elements.size(); i++) { 107 strip(elements.get(i)); 108 } 109 110 } 111 else { 112 element.detach(); 113 } 114 115 } 116 117 }
| Popular Tags
|