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.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 ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.jsp.JspContext ; 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.JspWriter ; 22 import javax.servlet.jsp.PageContext ; 23 import javax.servlet.jsp.tagext.*; 24 import java.io.IOException ; 25 import java.io.StringWriter ; 26 import java.io.Writer ; 27 import java.util.Locale ; 28 import java.util.ArrayList ; 29 30 33 public abstract class AbstractSimpleTag extends SimpleTagSupport implements INetuiTag 34 { 35 private static final Logger logger = Logger.getInstance(AbstractSimpleTag.class); 36 private ErrorHandling _eh; 38 42 public abstract String getTagName(); 43 44 50 protected String getBufferBody(boolean trim) 51 throws JspException , IOException  52 { 53 Writer body = new StringWriter (32); 54 JspFragment frag = getJspBody(); 55 if (frag == null) 56 return null; 57 frag.invoke(body); 58 String text = body.toString(); 59 if (trim && text != null) 60 text = text.trim(); 61 return (text.length() == 0) ? null : text; 62 } 63 64 73 protected final String setRequiredValueAttribute(String attrValue, String attrName) 74 throws JspException  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 s = Bundle.getString("Tags_AttrValueRequired", new Object []{attrName}); 81 registerTagError(s, null); 82 return null; 83 } 84 return attrValue; 85 } 86 87 94 protected final String setNonEmptyValueAttribute(String attrValue) 95 { 96 return ("".equals(attrValue)) ? null : attrValue; 97 } 98 99 103 protected Locale getUserLocale() { 104 return InternalUtils.lookupLocale(getJspContext()); 105 } 106 107 112 protected PageContext getPageContext() 113 { 114 JspContext ctxt = getJspContext(); 115 if (ctxt instanceof PageContext ) 116 return (PageContext ) ctxt; 117 118 assert(false) : "The JspContext was not a PageContext"; 120 logger.error("The JspContext was not a PageContext"); 121 return null; 122 } 123 124 128 protected final void write(String string) 129 throws JspException  130 { 131 JspContext ctxt = getJspContext(); 132 JspWriter writer = ctxt.getOut(); 133 try { 134 writer.print(string); 135 } 136 catch (IOException e) { 137 logger.error(Bundle.getString("Tags_WriteException"), e); 138 if (ctxt instanceof PageContext ) 139 RequestUtils.saveException((PageContext ) ctxt, e); 140 throw new JspException (e.getMessage(), e); 141 } 142 } 143 144 153 public void registerTagError(String message, Throwable e) 154 throws JspException  155 { 156 ErrorHandling eh = getErrorHandling(); 157 eh.registerTagError(message, getTagName(), this, e); 158 } 159 160 public void registerTagError(AbstractPageError error) 161 throws JspException  162 { 163 ErrorHandling eh = getErrorHandling(); 164 eh.registerTagError(error, this); 165 } 166 167 172 protected boolean hasErrors() 173 { 174 return (_eh != null); 175 } 176 177 183 protected void reportErrors() 184 throws JspException  185 { 186 assert(_eh != null); 187 String 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 getInlineError() 198 { 199 return _eh.getInlineError(getTagName()); 200 } 201 202 206 private ErrorHandling getErrorHandling() 207 { 208 if (_eh == null) { 209 _eh = new ErrorHandling(); 210 } 211 return _eh; 212 } 213 214 219 protected IScriptReporter getScriptReporter() 220 { 221 IScriptReporter sr = (IScriptReporter) SimpleTagSupport.findAncestorWithClass(this, IScriptReporter.class); 222 return sr; 223 } 224 225 229 protected IScriptReporter getHtmlTag(ServletRequest 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 243 final protected String rewriteName(String name) 244 { 245 PageContext pageContext = getPageContext(); 246 return URLRewriterService.getNamePrefix(pageContext.getServletContext(), pageContext.getRequest(), name) + name; 247 } 248 249 256 final protected String getIdForTagId(String tagId) 257 { 258 HttpServletRequest req = (HttpServletRequest ) getPageContext().getRequest(); 259 ArrayList list = (ArrayList ) 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 ) list.get(i)); 267 sb.append('.'); 268 } 269 sb.append(tagId); 270 return sb.toString(); 271 } 272 } 273 | Popular Tags |