1 18 package org.apache.beehive.netui.tags; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 import org.apache.beehive.netui.util.internal.ServletUtils; 22 23 import org.apache.beehive.netui.core.urls.URLRewriterService; 24 import org.apache.beehive.netui.script.ExpressionEvaluationException; 25 import org.apache.beehive.netui.tags.javascript.IScriptReporter; 26 import org.apache.beehive.netui.tags.javascript.ScriptContainer; 27 import org.apache.beehive.netui.tags.naming.FormDataNameInterceptor; 28 import org.apache.beehive.netui.tags.naming.INameInterceptor; 29 import org.apache.beehive.netui.tags.naming.IndexedNameInterceptor; 30 import org.apache.beehive.netui.util.Bundle; 31 import org.apache.beehive.netui.util.logging.Logger; 32 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 33 import org.apache.struts.Globals; 34 import org.apache.struts.util.RequestUtils; 35 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.jsp.JspException ; 39 import javax.servlet.jsp.JspWriter ; 40 import javax.servlet.jsp.PageContext ; 41 import javax.servlet.jsp.tagext.BodyTagSupport ; 42 import javax.servlet.jsp.tagext.SimpleTagSupport ; 43 import javax.servlet.jsp.tagext.Tag ; 44 import java.io.IOException ; 45 import java.util.*; 46 47 67 public abstract class AbstractClassicTag 68 extends BodyTagSupport implements INetuiTag 69 { 70 72 private static final Logger logger = Logger.getInstance(AbstractClassicTag.class); 73 74 78 public static final List DefaultNamingChain; 79 80 84 public static final String NETUI_UNIQUE_CNT = "netui.unique.id"; 85 86 90 private static final INameInterceptor formRewriter = new FormDataNameInterceptor(); 91 92 95 protected static final String EMPTY_STRING = ""; 96 97 static 99 { 100 List l = new ArrayList(2); 101 l.add(new FormDataNameInterceptor()); 102 l.add(new IndexedNameInterceptor()); 103 DefaultNamingChain = Collections.unmodifiableList(l); 104 } 105 106 private ErrorHandling _eh; 108 110 114 public abstract String getTagName(); 115 116 120 protected void localRelease() 121 { 122 _eh = null; 123 } 124 125 129 protected Locale getUserLocale() 130 { 131 return InternalUtils.lookupLocale(pageContext.getRequest()); 132 } 133 134 138 protected final void write(String string) 139 { 140 JspWriter writer = pageContext.getOut(); 141 try { 142 writer.print(string); 143 } 144 catch (IOException e) { 145 logger.error(Bundle.getString("Tags_WriteException"), e); 146 RequestUtils.saveException((PageContext ) pageContext, e); 147 } 148 } 149 150 152 158 protected List getNamingChain() 159 { 160 return null; 161 } 162 163 175 protected String applyNamingChain(String name) 176 throws JspException 177 { 178 assert (name != null) : "The name parameter may not be null"; 179 180 List namingChain = getNamingChain(); 181 if (namingChain == null) 182 return rewriteName(name); 183 184 188 try { 189 String newName = name; 190 int cnt = namingChain.size(); 191 for (int i = 0; i < cnt; i++) { 192 195 newName = ((INameInterceptor) namingChain.get(i)).rewriteName(newName, this); 196 197 } 200 201 return rewriteName(newName); 202 } 203 catch (ExpressionEvaluationException ee) { 204 logger.error(Bundle.getString("Tags_ExpressionQualifyingFailure", name)); 206 207 EvalErrorInfo info = new EvalErrorInfo(); 209 info.evalExcp = ee; 210 info.expression = name; 211 info.attr = "dataSource"; 212 info.tagType = getTagName(); 213 214 registerTagError(info); 216 return null; 217 } 218 } 219 220 229 protected String qualifyAttribute(String name) 230 throws JspException 231 { 232 if (name == null) 233 return null; 234 235 try { 237 name = formRewriter.rewriteName(name, this); 238 } 239 catch (ExpressionEvaluationException e) { 240 String s = Bundle.getString("Tags_DataSourceExpressionError", new Object []{name, e.toString()}); 241 registerTagError(s, null); 242 } 243 return name; 244 } 245 246 252 final protected String rewriteName(String name) 253 { 254 return URLRewriterService.getNamePrefix(pageContext.getServletContext(), pageContext.getRequest(), name) + name; 255 } 256 257 264 final protected String getIdForTagId(String tagId) 265 { 266 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 267 ArrayList list = (ArrayList) 268 org.apache.beehive.netui.tags.RequestUtils.getOuterAttribute(req, 269 ScriptContainer.SCOPE_ID); 270 if (list == null) 271 return tagId; 272 InternalStringBuilder sb = new InternalStringBuilder(); 273 for (int i=0;i<list.size();i++) { 274 sb.append((String )list.get(i)); 275 sb.append('.'); 276 } 277 sb.append(tagId); 278 return sb.toString(); 279 280 281 294 } 295 296 298 307 protected final String setRequiredValueAttribute(String attrValue, String attrName) 308 throws JspException 309 { 310 assert(attrValue != null) : "parameter 'attrValue' must not be null"; 311 assert(attrName != null) : "parameter 'attrName' must not be null"; 312 313 if ("".equals(attrValue)) { 314 String s = Bundle.getString("Tags_AttrValueRequired", new Object []{attrName}); 315 registerTagError(s, null); 316 return null; 317 } 318 return attrValue; 319 } 320 321 328 protected final String setNonEmptyValueAttribute(String attrValue) 329 { 330 return ("".equals(attrValue)) ? null : attrValue; 331 } 332 333 335 343 protected int reportAndExit(int returnValue) 344 throws JspException 345 { 346 if (hasErrors()) { 347 reportErrors(); 348 } 349 localRelease(); 350 return returnValue; 351 } 352 353 362 public void registerTagError(String message, Throwable e) 363 throws JspException 364 { 365 ErrorHandling eh = getErrorHandling(); 366 eh.registerTagError(message, getTagName(), this, e); 367 } 368 369 376 public void registerTagError(AbstractPageError error) 377 throws JspException 378 { 379 ErrorHandling eh = getErrorHandling(); 380 eh.registerTagError(error, this); 381 } 382 383 388 protected boolean hasErrors() 389 { 390 return (_eh != null); 391 } 392 393 399 protected void reportErrors() 400 throws JspException 401 { 402 assert(_eh != null); 403 String err = _eh.getErrorsReport(getTagName()); 404 IErrorCollector ec = (IErrorCollector) SimpleTagSupport.findAncestorWithClass(this, IErrorCollector.class); 405 if (ec != null) { 406 ec.collectChildError(err); 407 } 408 else { 409 write(err); 410 } 411 } 412 413 419 protected String getErrorsReport() 420 { 421 assert _eh != null; 422 return _eh.getErrorsReport(getTagName()); 423 } 424 425 426 430 private ErrorHandling getErrorHandling() 431 { 432 if (_eh == null) { 433 _eh = new ErrorHandling(); 434 } 435 return _eh; 436 } 437 438 440 445 protected IScriptReporter getScriptReporter() 446 { 447 IScriptReporter sr = (IScriptReporter) SimpleTagSupport.findAncestorWithClass(this, IScriptReporter.class); 448 return sr; 449 } 450 451 453 458 protected int getNextId(ServletRequest req) 459 { 460 Integer i = (Integer ) req.getAttribute(NETUI_UNIQUE_CNT); 461 if (i == null) { 462 i = new Integer (0); 463 } 464 465 int ret = i.intValue(); 466 req.setAttribute(NETUI_UNIQUE_CNT, new Integer (ret + 1)); 467 return ret; 468 } 469 } 470 | Popular Tags |