KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > tags > ApplyPluginsTag


1      /*
2  * Created on Feb 27, 2004
3  */

4 package org.roller.presentation.weblog.tags;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.struts.util.RequestUtils;
9 import org.roller.pojos.WeblogEntryData;
10 import org.roller.presentation.velocity.PageHelper;
11 import org.roller.util.Utilities;
12
13 import java.io.IOException JavaDoc;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.jsp.JspException JavaDoc;
18 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
19
20 /**
21  * Apply configured PagePlugins to WeblogEntryData and display the result.
22  * @jsp.tag name="ApplyPlugins"
23  * @author David M Johnson
24  */

25 public class ApplyPluginsTag extends TagSupport JavaDoc
26 {
27     static final long serialVersionUID = 3166731504235428544L;
28     
29     private static final String JavaDoc HELPER_KEY = "roller.pageHelper";
30     private static Log mLogger =
31         LogFactory.getFactory().getInstance(ApplyPluginsTag.class);
32
33     private String JavaDoc name = null;
34     private String JavaDoc property = null;
35     private String JavaDoc scope = "request";
36     
37     private boolean stripHtml = false;
38     private int maxLength = -1;
39     private boolean skipFlag = false;
40
41     /**
42      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
43      */

44     public int doStartTag() throws JspException JavaDoc
45     {
46         WeblogEntryData entry =
47             (WeblogEntryData)RequestUtils.lookup(pageContext, name, property, scope);
48         
49         String JavaDoc xformed = null;
50
51         if (entry.getPlugins() != null)
52         {
53             // check to see if a PageHelper has already been created this request
54
PageHelper helper = (PageHelper)pageContext.getRequest().getAttribute(HELPER_KEY);
55             if (helper == null)
56             {
57                 helper = loadNewPageHelper();
58             }
59             helper.setSkipFlag(skipFlag);
60     
61             xformed = helper.renderPlugins(entry);
62         }
63         else
64         {
65             xformed = entry.getText();
66         }
67         
68         if (stripHtml)
69         {
70             // don't escape ampersands
71
xformed = Utilities.escapeHTML( Utilities.removeHTML(xformed), false );
72         }
73         
74         if (maxLength != -1)
75         {
76             xformed = Utilities.truncateNicely(xformed, maxLength, maxLength, "...");
77         }
78         
79         // somehow things (&#8220) are getting double-escaped
80
// but I cannot seem to track it down
81
xformed = Utilities.stringReplace(xformed, "&amp#", "&#");
82         
83         try
84         {
85             pageContext.getOut().println(xformed);
86         }
87         catch (IOException JavaDoc e)
88         {
89             throw new JspException JavaDoc("ERROR applying plugin to entry", e);
90         }
91         return TagSupport.SKIP_BODY;
92     }
93
94     /**
95      * PagePlugins need to be loaded and properly initialized for use.
96      * Also, store the PageHelper in the Request as it will likely be
97      * used more than once and this way we can skip a fair amount of overhead.
98      */

99     private PageHelper loadNewPageHelper()
100     {
101         PageHelper pageHelper = PageHelper.createPageHelper(
102             (HttpServletRequest JavaDoc)pageContext.getRequest(),
103             (HttpServletResponse JavaDoc)pageContext.getResponse());
104
105         pageContext.getRequest().setAttribute(HELPER_KEY, pageHelper);
106         return pageHelper;
107     }
108
109     /**
110      * Maximum length of text displayed, only applies if stripHtml is true.
111      * @jsp.attribute required="false"
112      * @return Returns the maxLength.
113      */

114     public int getMaxLength()
115     {
116         return maxLength;
117     }
118
119     /**
120      * Maximum length of text displayed, only applies if stripHtml is true.
121      * @param maxLength The maxLength to set.
122      */

123     public void setMaxLength(int maxLength)
124     {
125         this.maxLength = maxLength;
126     }
127
128     /**
129      * Set to true to strip all HTML markup from output.
130      * @jsp.attribute required="false"
131      * @return Returns the stripHtml.
132      */

133     public boolean getStripHtml()
134     {
135         return stripHtml;
136     }
137
138     /**
139      * Set to true to strip all HTML markup from output.
140      * @param stripHtml The stripHtml to set.
141      */

142     public void setStripHtml(boolean stripHtml)
143     {
144         this.stripHtml = stripHtml;
145     }
146
147     /**
148      * Set to true to inform PagePlugins if they
149      * should "skip" themselves.
150      *
151      * @jsp.attribute required="false"
152      * @return Returns the skipFlag.
153      */

154     public boolean getSkipFlag()
155     {
156         return skipFlag;
157     }
158
159     /**
160      * Set to true to inform PagePlugins if they
161      * should "skip" themselves.
162      * @param skipFlag The skipFlag to set.
163      */

164     public void setSkipFlag(boolean skipFlag)
165     {
166         this.skipFlag = skipFlag;
167     }
168
169     /**
170      * @return Returns the name.
171      */

172     public String JavaDoc getName() {
173         return name;
174     }
175     
176     /**
177      * @jsp.attribute required="true"
178      */

179     public void setName(String JavaDoc name) {
180         this.name = name;
181     }
182     
183     /**
184      * @return Returns the property.
185      */

186     public String JavaDoc getProperty() {
187         return property;
188     }
189     /**
190      * @jsp.attribute required="false"
191      */

192     public void setProperty(String JavaDoc property) {
193         this.property = property;
194     }
195     
196     /**
197      * @jsp.attribute required="false"
198      */

199     public String JavaDoc getScope() {
200         return scope;
201     }
202     /**
203      * @param scope The scope to set.
204      */

205     public void setScope(String JavaDoc scope) {
206         this.scope = scope;
207     }
208 }
209
Popular Tags