01: /*
02: JSPWiki - a JSP-based WikiWiki clone.
03:
04: Copyright (C) 2001-2006 Janne Jalkanen (Janne.Jalkanen@iki.fi)
05:
06: This program is free software; you can redistribute it and/or modify
07: it under the terms of the GNU Lesser General Public License as published by
08: the Free Software Foundation; either version 2.1 of the License, or
09: (at your option) any later version.
10:
11: This program is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public License
17: along with this program; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package com.ecyrd.jspwiki.render;
21:
22: import java.io.IOException;
23: import java.util.Iterator;
24: import java.util.List;
25:
26: import org.apache.log4j.Logger;
27: import org.jdom.JDOMException;
28: import org.jdom.Text;
29: import org.jdom.xpath.XPath;
30:
31: import com.ecyrd.jspwiki.WikiContext;
32: import com.ecyrd.jspwiki.parser.WikiDocument;
33:
34: /**
35: * A simple renderer that just renders all the text() nodes
36: * from the DOM tree. This is very useful for cleaning away
37: * all of the XHTML.
38: *
39: * @author Janne Jalkanen
40: * @since 2.4
41: */
42: public class CleanTextRenderer extends WikiRenderer {
43: private static final String ALL_TEXT_NODES = "//text()";
44:
45: protected static final Logger log = Logger
46: .getLogger(CleanTextRenderer.class);
47:
48: public CleanTextRenderer(WikiContext context, WikiDocument doc) {
49: super (context, doc);
50: }
51:
52: public String getString() throws IOException {
53: StringBuffer sb = new StringBuffer();
54:
55: try {
56: XPath xp = XPath.newInstance(ALL_TEXT_NODES);
57:
58: List nodes = xp.selectNodes(m_document.getDocument());
59:
60: for (Iterator i = nodes.iterator(); i.hasNext();) {
61: Object el = i.next();
62:
63: if (el instanceof Text) {
64: sb.append(((Text) el).getValue());
65: }
66: }
67: } catch (JDOMException e) {
68: log.error("Could not parse XPATH expression");
69: throw new IOException(e.getMessage());
70: }
71:
72: return sb.toString();
73: }
74: }
|