KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > jsp > ContentTag


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.jsp;
26
27 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
28 import org.radeox.filter.Filter;
29 import org.radeox.filter.HtmlRemoveFilter;
30 import org.radeox.util.logging.Logger;
31 import org.snipsnap.snip.Snip;
32 import org.dom4j.io.XMLWriter;
33 import org.dom4j.io.OutputFormat;
34
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.JspWriter JavaDoc;
37 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.StringWriter JavaDoc;
40
41
42 /*
43  * Tag that displays the content of a snip. Can remove HTML from the content
44  * and generate an extract.
45  *
46  * @author Stephan J. Schmidt
47  * @version $Id: ContentTag.java 1606 2004-05-17 10:56:18Z leo $
48  */

49
50 public class ContentTag extends TagSupport JavaDoc {
51   Snip snip = null;
52   boolean extract = false;
53   boolean removeHtml = false;
54   boolean encodeHtml = false;
55
56   public int doStartTag() throws JspException JavaDoc {
57     if (null != snip) {
58       String JavaDoc content = snip.getXMLContent();
59       if (removeHtml) {
60         Filter filter = new HtmlRemoveFilter();
61         content = filter.filter(content, null);
62         if (extract) {
63           if (content.length() > 40) {
64             content = content.substring(0, 40);
65             int ampIndex = content.lastIndexOf("&");
66             int colonIndex = content.lastIndexOf(";");
67             // did we cut a entity like &x1212; ?
68
if (ampIndex > colonIndex) {
69               content = content.substring(0, ampIndex - 1);
70             }
71             content = content + " ...";
72           }
73         }
74       }
75       if (encodeHtml) {
76         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
77         try {
78           OutputFormat outputFormat = new OutputFormat();
79           outputFormat.setNewlines(true);
80           XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);
81           xmlWriter.write(content);
82           xmlWriter.flush();
83         } catch (IOException JavaDoc e) {
84           Logger.warn("ContentTag: unable to write encoded content: "+e);
85         }
86         content = stringWriter.toString();
87       }
88       try {
89         JspWriter JavaDoc out = pageContext.getOut();
90         out.print(content);
91       } catch (IOException JavaDoc e) {
92         Logger.warn("doStartTag in ContentTag", e);
93       }
94     }
95     return super.doStartTag();
96   }
97
98   public void setEncode(boolean encode) {
99     this.encodeHtml = encode;
100   }
101
102   public void setExtract(boolean extract) {
103     this.extract = extract;
104   }
105
106   public void setRemoveHtml(boolean removeHtml) {
107     this.removeHtml = removeHtml;
108   }
109
110   public void setSnip(String JavaDoc snip) {
111     try {
112       this.snip = (Snip) ExpressionEvaluatorManager.evaluate("snip", snip, Snip.class, this, pageContext);
113     } catch (JspException JavaDoc e) {
114       Logger.warn("unable to evaluate expression", e);
115     }
116   }
117 }
118
Popular Tags