KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > AbstractSimpleTag


1 package org.apache.beehive.netui.tags;
2
3 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
4 import org.apache.beehive.netui.util.internal.ServletUtils;
5
6 //import org.apache.beehive.netui.pageflow.util.URLRewriterService;
7
import org.apache.beehive.netui.tags.html.Html;
8 import org.apache.beehive.netui.tags.javascript.IScriptReporter;
9 import org.apache.beehive.netui.tags.javascript.ScriptContainer;
10 import org.apache.beehive.netui.util.Bundle;
11 import org.apache.beehive.netui.util.logging.Logger;
12 import org.apache.beehive.netui.core.urls.URLRewriterService;
13 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
14 import org.apache.struts.Globals;
15 import org.apache.struts.util.RequestUtils;
16
17 import javax.servlet.ServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.jsp.JspContext JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspWriter JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.*;
24 import java.io.IOException JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * @netui:tag
32  */

33 public abstract class AbstractSimpleTag extends SimpleTagSupport implements INetuiTag
34 {
35     private static final Logger logger = Logger.getInstance(AbstractSimpleTag.class);
36     private ErrorHandling _eh; // This class will track and handle errors
37

38     /**
39      * Return the name of the tag. Used by error reporting to get the name of the tag.
40      * @return the name of the tag.
41      */

42     public abstract String JavaDoc getTagName();
43
44     /**
45      * @param trim
46      * @return String
47      * @throws JspException
48      * @throws IOException
49      */

50     protected String JavaDoc getBufferBody(boolean trim)
51             throws JspException JavaDoc, IOException JavaDoc
52     {
53         Writer JavaDoc body = new StringWriter JavaDoc(32);
54         JspFragment frag = getJspBody();
55         if (frag == null)
56             return null;
57         frag.invoke(body);
58         String JavaDoc text = body.toString();
59         if (trim && text != null)
60             text = text.trim();
61         return (text.length() == 0) ? null : text;
62     }
63
64     /**
65      * Report an error if the value of <code>attrValue</code> is equal to the empty string, otherwise return
66      * that value. If <code>attrValue</code> is equal to the empty string, an error is registered and
67      * null is returned.
68      * @param attrValue The value to be checked for the empty string
69      * @param attrName The name of the attribute
70      * @return either the attrValue if it is not the empty string or null
71      * @throws JspException A JspException will be thrown if inline error reporting is turned off.
72      */

73     protected final String JavaDoc setRequiredValueAttribute(String JavaDoc attrValue, String JavaDoc attrName)
74             throws JspException JavaDoc
75     {
76         assert(attrValue != null) : "parameter '" + attrValue + "' must not be null";
77         assert(attrName != null) : "parameter '" + attrName + "' must not be null";
78
79         if ("".equals(attrValue)) {
80             String JavaDoc s = Bundle.getString("Tags_AttrValueRequired", new Object JavaDoc[]{attrName});
81             registerTagError(s, null);
82             return null;
83         }
84         return attrValue;
85     }
86
87     /**
88      * Filter out the empty string value and return either the value or null. When the value of
89      * <code>attrValue</code> is equal to the empty string this will return null, otherwise it will
90      * return the value of <code>attrValue</code>.
91      * @param attrValue This is the value we will check for the empty string.
92      * @return either the value of attrValue or null
93      */

94     protected final String JavaDoc setNonEmptyValueAttribute(String JavaDoc attrValue)
95     {
96         return ("".equals(attrValue)) ? null : attrValue;
97     }
98
99     /**
100      * This method will return the user local of the request.
101      * @return the Locale object to use when rendering this tag
102      */

103     protected Locale JavaDoc getUserLocale() {
104         return InternalUtils.lookupLocale(getJspContext());
105     }
106
107     /**
108      * This method will attempt to cast the JspContext into a PageContext. If this fails,
109      * it will log an exception.
110      * @return PageContext
111      */

112     protected PageContext JavaDoc getPageContext()
113     {
114         JspContext JavaDoc ctxt = getJspContext();
115         if (ctxt instanceof PageContext JavaDoc)
116             return (PageContext JavaDoc) ctxt;
117
118         // assert the page context and log an error in production
119
assert(false) : "The JspContext was not a PageContext";
120         logger.error("The JspContext was not a PageContext");
121         return null;
122     }
123
124     /**
125      * This mehod will write the passed string to the response.
126      * @param string to be written to the response.
127      */

128     protected final void write(String JavaDoc string)
129             throws JspException JavaDoc
130     {
131         JspContext JavaDoc ctxt = getJspContext();
132         JspWriter JavaDoc writer = ctxt.getOut();
133         try {
134             writer.print(string);
135         }
136         catch (IOException JavaDoc e) {
137             logger.error(Bundle.getString("Tags_WriteException"), e);
138             if (ctxt instanceof PageContext JavaDoc)
139                 RequestUtils.saveException((PageContext JavaDoc) ctxt, e);
140             throw new JspException JavaDoc(e.getMessage(), e);
141         }
142     }
143
144     /**
145      * This will report an error from a tag. The error will
146      * contain a message. If error reporting is turned off,
147      * the message will be returned and the caller should throw
148      * a JspException to report the error.
149      * @param message - the message to register with the error
150      * @throws javax.servlet.jsp.JspException - if in-page error reporting is turned off this method will always
151      * throw a JspException.
152      */

153     public void registerTagError(String JavaDoc message, Throwable JavaDoc e)
154             throws JspException JavaDoc
155     {
156         ErrorHandling eh = getErrorHandling();
157         eh.registerTagError(message, getTagName(), this, e);
158     }
159
160     public void registerTagError(AbstractPageError error)
161             throws JspException JavaDoc
162     {
163         ErrorHandling eh = getErrorHandling();
164         eh.registerTagError(error, this);
165     }
166
167     /**
168      * This method will return <code>true</code> if there have been any errors registered on this
169      * tag. Otherwise it returns <code>false</code>
170      * @return <code>true</code> if errors have been reported on this tag.
171      */

172     protected boolean hasErrors()
173     {
174         return (_eh != null);
175     }
176
177     /**
178      * This method will write out the <code>String</code> returned by <code>getErrorsReport</code> to the
179      * response output stream.
180      * @throws JspException if <code>write</code> throws an exception.
181      * @see #write
182      */

183     protected void reportErrors()
184             throws JspException JavaDoc
185     {
186         assert(_eh != null);
187         String JavaDoc err = _eh.getErrorsReport(getTagName());
188         IErrorCollector ec = (IErrorCollector) SimpleTagSupport.findAncestorWithClass(this, IErrorCollector.class);
189         if (ec != null) {
190             ec.collectChildError(err);
191         }
192         else {
193             write(err);
194         }
195     }
196
197     protected String JavaDoc getInlineError()
198     {
199         return _eh.getInlineError(getTagName());
200     }
201
202     /**
203      * This method will return an ErrorHandling instance.
204      * @return
205      */

206     private ErrorHandling getErrorHandling()
207     {
208         if (_eh == null) {
209             _eh = new ErrorHandling();
210         }
211         return _eh;
212     }
213
214     /**
215      * Return the closest <code>ScriptReporter</code> in the parental chain. Searching starts
216      * at this node an moves upward through the parental chain.
217      * @return a <code>ScriptReporter</code> or null if there is not one found.
218      */

219     protected IScriptReporter getScriptReporter()
220     {
221         IScriptReporter sr = (IScriptReporter) SimpleTagSupport.findAncestorWithClass(this, IScriptReporter.class);
222         return sr;
223     }
224
225     /**
226      * This method will return the scriptReporter that is represented by the HTML tag.
227      * @return IScriptReporter
228      */

229     protected IScriptReporter getHtmlTag(ServletRequest JavaDoc req)
230     {
231         Html html = (Html) req.getAttribute(Html.HTML_TAG_ID);
232         if (html != null && html instanceof IScriptReporter)
233             return (IScriptReporter) html;
234         return null;
235     }
236
237     /**
238      * This method will rewrite the name (id) by passing it to the
239      * URL Rewritter and getting back a value.
240      * @param name the name that will be rewritten
241      * @return a name that has been rewritten by the URLRewriterService.
242      */

243     final protected String JavaDoc rewriteName(String JavaDoc name)
244     {
245         PageContext JavaDoc pageContext = getPageContext();
246         return URLRewriterService.getNamePrefix(pageContext.getServletContext(), pageContext.getRequest(), name) + name;
247     }
248
249     /**
250      * This method will generate a real id based upon the passed in tagId. The generated
251      * id will be constucted by searching upward for all the script containers that have a
252      * scope id set. These will form a fully qualified id.
253      * @param tagId The base tagId set on a tag
254      * @return an id value formed by considering all of the scope id's found in the tag hierarchy.
255      */

256     final protected String JavaDoc getIdForTagId(String JavaDoc tagId)
257     {
258         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) getPageContext().getRequest();
259         ArrayList JavaDoc/*<String>*/ list = (ArrayList JavaDoc/*<String>*/)
260                 org.apache.beehive.netui.tags.RequestUtils.getOuterAttribute(req,
261                         ScriptContainer.SCOPE_ID);
262         if (list == null)
263             return tagId;
264         InternalStringBuilder sb = new InternalStringBuilder();
265         for (int i=0;i<list.size();i++) {
266             sb.append((String JavaDoc) list.get(i));
267             sb.append('.');
268         }
269         sb.append(tagId);
270         return sb.toString();
271     }
272 }
273
Popular Tags