1 16 package org.apache.taglibs.input; 17 18 import java.beans.IntrospectionException ; 19 import java.beans.PropertyDescriptor ; 20 import java.io.IOException ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.servlet.jsp.JspTagException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.tagext.Tag ; 29 import javax.servlet.jsp.tagext.TagSupport ; 30 31 46 47 class Util { 48 49 50 public static void printAttributes(JspWriter out, Map attributes) 51 throws JspTagException , IOException { 52 if (attributes != null) { 53 Iterator i = attributes.keySet().iterator(); 54 while (i.hasNext()) { 55 Object oKey = i.next(); 56 Object oVal = attributes.get(oKey); 57 58 64 if (!(oKey instanceof String ) 65 || (oVal != null && !(oVal instanceof String ))) 66 throw new JspTagException ( 67 "all members in attributes Map must be Strings"); 68 String key = (String ) oKey; 69 String value = (String ) oVal; 70 71 if (key.equals("name") || key.equals("value") 73 || key.equals("type") || key.equals("checked")) 74 throw new JspTagException ("illegal key '" + key 75 + "'found in attributes Map"); 76 77 82 if (value == null) 83 value = key; 84 85 out.print(quote(key) + "=\"" + quote(value) + "\" "); 86 } 87 } 88 } 89 90 91 public static String quote(String x) { 92 if (x == null) 93 return null; 94 else { 95 x = replace(x, "&", "&"); 97 x = replace(x, "\"", """); 98 x = replace(x, "<", "<"); 99 x = replace(x, ">", ">"); 100 return x; 101 } 102 } 103 104 115 public static String replace(String subject, String find, String replace) { 116 StringBuffer buf = new StringBuffer (); 117 int l = find.length(); 118 int s = 0; 119 int i = subject.indexOf(find); 120 while (i != -1) { 121 buf.append(subject.substring(s, i)); 122 buf.append(replace); 123 s = i + l; 124 i = subject.indexOf(find, s); 125 } 126 buf.append(subject.substring(s)); 127 return buf.toString(); 128 } 129 130 134 public static Object beanPropertyValueObject(Object bean, String name) 135 throws JspTagException { 136 if (bean != null) { 137 Method reader = null; 138 Object [] params = null; 139 140 try { 142 PropertyDescriptor prop = new PropertyDescriptor (name, bean 143 .getClass()); 144 reader = prop.getReadMethod(); 145 } catch (IntrospectionException e) { 146 try { 149 reader = bean.getClass().getMethod("get", 150 new Class [] { Object .class }); 151 params = new Object [] { name }; 152 } catch (NoSuchMethodException f) { 153 try { 155 reader = bean.getClass().getMethod("get", 156 new Class [] { String .class }); 157 params = new Object [] { name }; 158 } catch (NoSuchMethodException g) { 159 } 161 } 162 } 163 164 if (reader != null) { 166 try { 167 return reader.invoke(bean, params); 168 } catch (IllegalAccessException e) { 169 } catch (IllegalArgumentException e) { 170 } catch (InvocationTargetException e) { 171 throw new JspTagException ("Exception getting property \"" 172 + name + "\" from bean " 173 + bean.getClass().getName() + ": " 174 + e.getTargetException()); 175 } 176 } 177 } 178 179 return null; 180 } 181 182 186 public static String beanPropertyValue(Object bean, String name) 187 throws JspTagException { 188 Object value = beanPropertyValueObject(bean, name); 189 return (value != null ? value.toString() : null); 190 } 191 192 197 public static String [] beanPropertyValues(Object bean, String name) 198 throws JspTagException { 199 Object value = beanPropertyValueObject(bean, name); 200 if (value != null) { 201 if (value.getClass().isArray()) { 203 int n = java.lang.reflect.Array.getLength(value); 205 String [] strs = new String [n]; 206 for (int i = 0; i < n; i++) { 207 Object o = java.lang.reflect.Array.get(value, i); 208 strs[i] = (o != null ? o.toString() : null); 209 } 210 return strs; 211 } 212 else { 215 return new String [] { value.toString() }; 216 } 217 } else { 218 return null; 219 } 220 } 221 222 225 public static Form findFormTag(Tag tag) { 226 Tag formTag = TagSupport.findAncestorWithClass(tag, Form.class); 227 if (formTag != null) { 228 return (Form) formTag; 229 } else { 230 return null; 231 } 232 } 233 234 238 public static String defaultFormBeanId(Tag tag) { 239 Form form = findFormTag(tag); 240 if (form != null) { 241 return form.getBean(); 242 } else { 243 return null; 244 } 245 } 246 } | Popular Tags |