1 package org.apache.beehive.netui.tags; 2 3 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 4 5 import org.apache.beehive.netui.util.Bundle; 6 import org.apache.beehive.netui.util.logging.Logger; 7 import org.apache.struts.util.ResponseUtils; 8 9 import javax.servlet.jsp.JspException ; 10 import javax.servlet.jsp.PageContext ; 11 import javax.servlet.jsp.tagext.JspTag ; 12 import javax.servlet.jsp.tagext.SimpleTagSupport ; 13 import java.io.IOException ; 14 import java.io.Writer ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 public class ErrorHandling 19 { 20 23 private static boolean reportErrorInPage = true; 24 private static final Logger logger = Logger.getInstance(ErrorHandling.class); 25 26 private List _errors; 27 28 37 public void registerTagError(String message, String tagName, JspTag tag, Throwable e) 38 throws JspException 39 { 40 assert (message != null) : "parameter 'message' must not be null."; 41 42 if (_errors == null) 44 _errors = new ArrayList (); 45 46 TagErrorInfo tei = new TagErrorInfo(); 47 48 tei.tagType = tagName; 49 tei.message = message; 50 _errors.add(tei); 51 52 IErrorReporter er = getErrorReporter(tag); 53 if (er == null) { 54 tei.errorNo = -1; 55 if (!reportErrorInPage) { 56 String s = Bundle.getString("Tags_NoInPageErrorReporting", new Object []{message}); 57 if (e == null) 58 logger.error(s); 59 else 60 logger.error(s, e); 61 throw new JspException (message); 63 } 64 return; 65 } 66 67 er.addError(tei); 69 assert (tei.errorNo > 0); 70 if (!reportErrorInPage) { 71 String s = Bundle.getString("Tags_NoInPageErrorReporting", new Object []{message}); 72 if (e == null) 73 logger.error(s); 74 else 75 logger.error(s, e); 76 throw new JspException (s); 78 } 79 return; 80 } 81 82 87 public void registerTagError(AbstractPageError error, JspTag tag) 88 throws JspException 89 { 90 assert (error != null); 91 92 if (_errors == null) 94 _errors = new ArrayList (); 95 96 _errors.add(error); 97 98 IErrorReporter er = getErrorReporter(tag); 99 if (er == null) { 100 error.errorNo = -1; 101 return; 102 } 103 104 er.addError(error); 106 assert (error.errorNo > 0); 107 } 108 109 115 public boolean hasErrors() 116 { 117 return (_errors != null); 118 } 119 120 126 public void reportErrors(Writer writer, String tagName) 127 throws JspException 128 { 129 try { 130 writer.write(getErrorsReport(tagName)); 131 } 132 catch (IOException e) { 133 throw new JspException (e.getMessage(), e); 134 } 135 } 136 137 public String getInlineError(String tagName) 138 { 139 if (isInlineErrors()) { 140 AbstractPageError info = (AbstractPageError) _errors.get(0); 141 return getInlineError(info, tagName); 142 } 143 return null; 144 } 145 146 147 154 public String getErrorsReport(String tagName) 155 { 156 assert _errors != null; 157 assert _errors.size() > 0; 158 159 int cnt = _errors.size(); 160 161 InternalStringBuilder sb = new InternalStringBuilder(128); 162 163 AbstractPageError info = (AbstractPageError) _errors.get(0); 165 if (isInlineErrors()) { 166 String s = getInlineError(info, tagName); 167 return s; 168 } 169 170 String s; 172 s = Bundle.getString("Tag_Header", 173 new Object []{tagName, Integer.toString(cnt)}); 174 sb.append(s); 175 176 Object [] args = new Object [4]; 177 for (int i = 0; i < cnt; i++) { 178 Object o = _errors.get(i); 179 if (o instanceof EvalErrorInfo) { 180 EvalErrorInfo e = (EvalErrorInfo) o; 181 assert info != null; 182 183 args[0] = Bundle.getString("Expression_Error"); 184 args[1] = e.attr; 185 args[2] = e.expression; 186 args[3] = e.evalExcp.getMessage(); 187 s = Bundle.getString("Expression_Error_Line", args); 188 } 189 else if (o instanceof TagErrorInfo) { 190 TagErrorInfo e = (TagErrorInfo) o; 191 assert info != null; 192 193 args[0] = Bundle.getString("Tag_Error"); 194 args[1] = e.message; 195 s = Bundle.getString("Tag_Error_Line", args); 196 } 197 sb.append(s); 198 } 199 200 s = Bundle.getString("Tag_Footer"); 201 sb.append(s); 202 return sb.toString(); 203 } 204 205 209 public static void reportCollectedErrors(InternalStringBuilder sb, JspTag tag) 210 { 211 IErrorReporter er = getErrorReporter(tag); 212 if (er == null) 213 return; 214 215 assert (sb != null); 216 ArrayList errors = er.returnErrors(); 217 if (errors == null || errors.size() == 0) 218 return; 219 220 assert(errors.size() > 0); 221 222 String s; 223 s = Bundle.getString("Footer_Error_Header"); 225 sb.append(s); 226 227 int cnt = errors.size(); 228 Object [] args = new Object [5]; 229 for (int i = 0; i < cnt; i++) { 230 Object o = errors.get(i); 231 assert (o != null); 232 if (o instanceof EvalErrorInfo) { 233 EvalErrorInfo err = (EvalErrorInfo) o; 234 args[0] = Integer.toString(err.errorNo); 235 args[1] = err.tagType; 236 args[2] = err.attr; 237 args[3] = err.expression; 238 args[4] = err.evalExcp.getMessage(); 239 s = Bundle.getString("Footer_Error_Expr_Body", args); 240 sb.append(s); 241 } 242 else if (o instanceof TagErrorInfo) { 243 TagErrorInfo tei = (TagErrorInfo) o; 244 args[0] = Integer.toString(tei.errorNo); 245 args[1] = tei.tagType; 246 args[2] = tei.message; 247 s = Bundle.getString("Footer_Error_Tag_Body", args); 248 sb.append(s); 249 } 250 } 251 252 s = Bundle.getString("Footer_Error_Footer"); 254 sb.append(s); 255 } 256 257 261 public static void reportCollectedErrors(PageContext pc, JspTag tag) 262 { 263 IErrorReporter er = getErrorReporter(tag); 264 if (er == null) 265 return; 266 267 assert (pc != null); 268 ArrayList errors = er.returnErrors(); 269 if (errors == null || errors.size() == 0) 270 return; 271 272 assert(errors.size() > 0); 273 274 String s; 275 s = Bundle.getString("Footer_Error_Header"); 277 write(pc, s); 278 279 int cnt = errors.size(); 280 Object [] args = new Object [5]; 281 for (int i = 0; i < cnt; i++) { 282 Object o = errors.get(i); 283 assert (o != null); 284 if (o instanceof EvalErrorInfo) { 285 EvalErrorInfo err = (EvalErrorInfo) o; 286 args[0] = Integer.toString(err.errorNo); 287 args[1] = err.tagType; 288 args[2] = err.attr; 289 args[3] = err.expression; 290 args[4] = err.evalExcp.getMessage(); 291 s = Bundle.getString("Footer_Error_Expr_Body", args); 292 write(pc, s); 293 } 294 else if (o instanceof TagErrorInfo) { 295 TagErrorInfo tei = (TagErrorInfo) o; 296 args[0] = Integer.toString(tei.errorNo); 297 args[1] = tei.tagType; 298 args[2] = tei.message; 299 s = Bundle.getString("Footer_Error_Tag_Body", args); 300 write(pc, s); 301 } 302 } 303 304 s = Bundle.getString("Footer_Error_Footer"); 306 write(pc, s); 307 } 308 309 private boolean isInlineErrors() 310 { 311 AbstractPageError info = (AbstractPageError) _errors.get(0); 312 return (info.errorNo > 0); 313 } 314 315 private String getInlineError(AbstractPageError info, String tagName) 316 { 317 String s; 318 if (info instanceof EvalErrorInfo) { 319 s = Bundle.getString("Expression_Error"); 320 s = Bundle.getString("Inline_error", 321 new Object []{ 322 s, 323 Integer.toString(info.errorNo), 324 tagName, 325 }); 326 } 327 else if (info instanceof TagErrorInfo) { 328 s = Bundle.getString("Tag_Error"); 329 s = Bundle.getString("Inline_error", 330 new Object []{ 331 s, 332 Integer.toString(info.errorNo), 333 tagName, 334 }); 335 } 336 else { 337 s = null; 338 assert true : "Unhandled type"; 339 } 340 return s; 341 } 342 343 348 private static IErrorReporter getErrorReporter(JspTag tag) 349 { 350 if (tag instanceof IErrorReporter && ((IErrorReporter) tag).isReporting()) 351 return (IErrorReporter) tag; 352 353 IErrorReporter er = (IErrorReporter) SimpleTagSupport.findAncestorWithClass(tag, IErrorReporter.class); 355 while (er != null) { 356 if (er.isReporting()) 357 return er; 358 er = (IErrorReporter) SimpleTagSupport.findAncestorWithClass((JspTag ) er, IErrorReporter.class); 359 } 360 return null; 361 } 362 363 private static final void write(PageContext pc, String string) 364 { 365 try { 366 ResponseUtils.write(pc, string); 367 } 368 catch (JspException e) { 369 logger.error(Bundle.getString("Tags_WriteException"), e); 370 } 371 } 372 } 373 | Popular Tags |