1 23 24 package org.dbforms.taglib; 25 26 import org.apache.commons.beanutils.PropertyUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.dbforms.config.Field; 31 import org.dbforms.config.FieldValue; 32 import org.dbforms.config.ResultSetVector; 33 34 import org.dbforms.util.Util; 35 36 import javax.servlet.jsp.JspException ; 37 38 39 40 45 public class TextFormatTag extends DbBaseHandlerTag 46 implements javax.servlet.jsp.tagext.TryCatchFinally { 47 private static Log logCat = LogFactory.getLog(TextFormatTag.class); 48 private Object fieldObject; private String contextVar; 50 private String pattern; 51 private String type; 52 private String value; 53 54 59 public void setContextVar(String contextVar) { 60 this.contextVar = contextVar; 61 } 62 63 64 69 public String getContextVar() { 70 return contextVar; 71 } 72 73 74 79 public Object getFieldObject() { 80 return fieldObject; 81 } 82 83 84 89 public void setPattern(String pattern) { 90 this.pattern = pattern; 91 } 92 93 94 99 public String getPattern() { 100 return pattern; 101 } 102 103 104 109 public void setType(String type) { 110 this.type = type; 111 } 112 113 114 119 public String getType() { 120 return type; 121 } 122 123 124 129 public void setValue(String value) { 130 this.value = value; 131 } 132 133 134 139 public String getValue() { 140 return value; 141 } 142 143 144 147 public void doCatch(Throwable t) throws Throwable { 148 throw t; 149 } 150 151 152 160 public int doEndTag() throws javax.servlet.jsp.JspException { 161 if (Util.isNull(getContextVar()) && Util.isNull(getValue())) { 162 throw new JspException ("either var or value must be setted!"); 163 } 164 165 Field field = new Field(); 166 167 if (!Util.isNull(getValue())) { 168 if (Util.isNull(getType())) { 169 throw new JspException ("value setted - type must be setted too!"); 170 } 171 172 field.setFieldType(getType()); 173 174 FieldValue fv = new FieldValue(field, getValue()); 175 fv.setPattern(getPattern()); 176 fieldObject = fv.getFieldValueAsObject(); 177 } else { 178 String search = getContextVar(); 179 int pos = search.indexOf("."); 180 181 ResultSetVector rsv = null; 182 183 if (getParentForm() != null) { 184 rsv = getParentForm() 185 .getResultSetVector(); 186 } 187 188 if (pos == -1) { 189 if (rsv != null) { 190 fieldObject = rsv.getAttribute(search); 191 } 192 193 if (fieldObject == null) { 194 fieldObject = pageContext.findAttribute(search); 196 } 197 } else { 198 try { 199 String search_bean = search.substring(0, pos); 201 search = search.substring(pos + 1); 202 203 Object bean = null; 204 205 if (rsv != null) { 206 bean = rsv.getAttribute(search_bean); 207 } 208 209 if (bean == null) { 210 bean = pageContext.findAttribute(search_bean); 212 } 213 214 if (bean != null) { 215 logCat.debug("calling PropertyUtils.getProperty " 216 + search_bean + " " + search); 217 fieldObject = PropertyUtils.getProperty(bean, search); 218 } 219 } catch (Exception e) { 220 throw new JspException (e.getMessage()); 221 } 222 } 223 224 if (fieldObject == null) { 225 throw new JspException ("object not found in context!"); 226 } 227 228 field.setTypeByObject(fieldObject); 229 } 230 231 this.setField(field); 232 233 String fieldValue = getFormattedFieldValue(); 234 fieldValue = escapeHTML(fieldValue); 235 236 try { 237 pageContext.getOut() 238 .write(fieldValue); 239 } catch (java.io.IOException ioe) { 240 throw new JspException ("IO Error: " + ioe.getMessage()); 242 } 243 244 return EVAL_PAGE; 245 } 246 247 248 251 public void doFinally() { 252 pattern = null; 253 type = null; 254 value = null; 255 contextVar = null; 256 super.doFinally(); 257 } 258 259 260 265 protected void setFieldObject(Object fieldObject) { 266 this.fieldObject = fieldObject; 267 } 268 } 269 | Popular Tags |