1 23 24 27 package org.dbforms.taglib; 28 29 import org.dbforms.util.MessageResources; 30 31 import java.io.IOException ; 32 33 import java.util.Enumeration ; 34 import java.util.Vector ; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.jsp.JspException ; 38 import javax.servlet.jsp.JspWriter ; 39 import javax.servlet.jsp.PageContext ; 40 41 42 43 54 public class ErrorsTag extends TagSupportWithScriptHandler 55 implements javax.servlet.jsp.tagext.TryCatchFinally { 56 private String caption = "Error:"; 57 private String messagePrefix; 58 private String name = "errors"; 59 60 65 public void setCaption(String caption) { 66 this.caption = caption; 67 } 68 69 70 75 public String getCaption() { 76 return (this.caption); 77 } 78 79 80 86 public void setMessagePrefix(java.lang.String newMessagePrefix) { 87 messagePrefix = newMessagePrefix; 88 } 89 90 91 97 public java.lang.String getMessagePrefix() { 98 return messagePrefix; 99 } 100 101 102 107 public void setName(String name) { 108 this.name = name; 109 } 110 111 112 117 public String getName() { 118 return (this.name); 119 } 120 121 122 129 public void doCatch(Throwable t) throws Throwable { 130 throw t; 131 } 132 133 134 137 public void doFinally() { 138 messagePrefix = null; 139 name = "errors"; 140 caption = "Error:"; 141 } 142 143 144 146 153 public int doStartTag() throws JspException { 154 Vector originalErrors = (Vector ) pageContext.getAttribute(getName(), 155 PageContext.REQUEST_SCOPE); 156 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 157 158 if ((originalErrors != null) && (originalErrors.size() > 0)) { 159 Vector errors = this.extractUserDefinedErrors(originalErrors); 161 162 StringBuffer results = new StringBuffer (); 163 results.append(caption); 164 165 results.append("<ul>"); 166 167 for (int i = 0; i < errors.size(); i++) { 168 results.append("<li>"); 169 results.append(MessageResources.getMessage(request, 170 (String ) errors 171 .elementAt(i))); 172 results.append("</li>"); 173 } 174 175 results.append("</ul>"); 176 177 JspWriter writer = pageContext.getOut(); 179 180 try { 181 writer.print(results.toString()); 182 } catch (IOException e) { 183 throw new JspException (e.toString()); 184 } 185 } 186 187 return EVAL_BODY_INCLUDE; 189 } 190 191 192 200 public Vector extractUserDefinedErrors(Vector errors) { 201 Vector newErrors = new Vector (); 202 String message = null; 203 int index = 0; 204 205 String delimiter = this.getMessagePrefix(); 207 208 if (errors != null) { 209 Enumeration e = errors.elements(); 210 211 while (e.hasMoreElements()) { 212 message = ((Exception ) e.nextElement()).getMessage(); 213 214 if ((delimiter != null) 216 && ((index = message.indexOf(delimiter)) != -1)) { 217 message = message.substring(index + delimiter.length()); 219 } 220 221 newErrors.add(message); 222 } 223 } 224 225 return newErrors; 226 } 227 } 228 | Popular Tags |