1 23 24 33 package org.dbforms.taglib; 34 35 import org.dbforms.config.DbFormsErrors; 36 37 import java.io.IOException ; 38 39 import java.util.Iterator ; 40 import java.util.Vector ; 41 42 import javax.servlet.jsp.JspException ; 43 import javax.servlet.jsp.JspWriter ; 44 import javax.servlet.jsp.PageContext ; 45 46 47 48 54 public class DbXmlErrorsTag extends TagSupportWithScriptHandler 55 implements javax.servlet.jsp.tagext.TryCatchFinally { 56 private transient DbFormsErrors errors; 57 private String caption = "Error:"; 58 59 61 65 private String name = "errors"; 66 67 72 public void setCaption(String caption) { 73 this.caption = caption; 74 } 75 76 77 82 public String getCaption() { 83 return (this.caption); 84 } 85 86 87 92 public void setName(String name) { 93 this.name = name; 94 } 95 96 97 102 public String getName() { 103 return (this.name); 104 } 105 106 107 112 public void setPageContext(final javax.servlet.jsp.PageContext pageContext) { 113 super.setPageContext(pageContext); 114 this.errors = (DbFormsErrors) pageContext.getServletContext() 115 .getAttribute(DbFormsErrors.ERRORS); 116 } 117 118 119 126 public void doCatch(Throwable t) throws Throwable { 127 throw t; 128 } 129 130 131 134 public void doFinally() { 135 name = "errors"; 136 caption = "Error:"; 137 errors = null; 138 } 139 140 141 143 150 public int doStartTag() throws JspException { 151 Vector transformedErrors = new Vector (); 152 153 Vector originalErrors = (Vector ) pageContext.getAttribute(name, 154 PageContext.REQUEST_SCOPE); 155 156 if (errors == null) { 157 throw new JspException ("XML error handler is disabled, please supply xml error file on startup or use error tag instead!"); 158 } 159 160 if ((originalErrors != null) && (originalErrors.size() > 0)) { 161 Iterator iter = originalErrors.iterator(); 162 163 while (iter.hasNext()) { 164 Exception ex = (Exception ) iter.next(); 165 166 String result = errors.getXMLErrorMessage(ex.getMessage()); 167 168 if (result != null) { 170 transformedErrors.add(result); 171 } 172 } 173 174 StringBuffer results = new StringBuffer (); 175 results.append(caption); 176 results.append("<ul>"); 177 178 for (int i = 0; i < transformedErrors.size(); i++) { 179 results.append("<li>"); 180 results.append(transformedErrors.elementAt(i)); 181 results.append("</li>"); 182 } 183 184 results.append("</ul>"); 185 186 JspWriter writer = pageContext.getOut(); 188 189 try { 190 writer.print(results.toString()); 191 } catch (IOException e) { 192 throw new JspException (e.toString()); 193 } 194 } 195 196 return EVAL_BODY_INCLUDE; 198 } 199 } 200 | Popular Tags |