KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
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 /**
34  * <p>
35  * Demonstrates the removal of elements and
36  * their content from a document.
37  * </p>
38  *
39  * @author Elliotte Rusty Harold
40  * @version 1.0
41  *
42  */

43 public class XHTMLPurifier {
44
45      public final static String JavaDoc XHTML_NAMESPACE
46        = "http://www.w3.org/1999/xhtml";
47
48     public static void main(String JavaDoc[] 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 JavaDoc 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             // Strip out non XHTML attributes
89
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             // Strip out additional namespaces
100
for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
101                 String JavaDoc 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