1 16 17 package org.apache.catalina.storeconfig; 18 19 import java.beans.IndexedPropertyDescriptor ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.io.PrintWriter ; 23 import java.util.Iterator ; 24 25 import org.apache.catalina.deploy.ResourceBase; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.tomcat.util.IntrospectionUtils; 29 30 36 public class StoreAppender { 37 private static Log log = LogFactory.getLog(StoreAppender.class); 38 39 42 private static Class persistables[] = { String .class, Integer .class, 43 Integer.TYPE, Boolean .class, Boolean.TYPE, Byte .class, Byte.TYPE, 44 Character .class, Character.TYPE, Double .class, Double.TYPE, 45 Float .class, Float.TYPE, Long .class, Long.TYPE, Short .class, 46 Short.TYPE, }; 47 48 55 public void printCloseTag(PrintWriter aWriter, StoreDescription aDesc) 56 throws Exception { 57 aWriter.print("</"); 58 aWriter.print(aDesc.getTag()); 59 aWriter.println(">"); 60 } 61 62 71 public void printOpenTag(PrintWriter aWriter, int indent, Object bean, 72 StoreDescription aDesc) throws Exception { 73 aWriter.print("<"); 74 aWriter.print(aDesc.getTag()); 75 if (aDesc.isAttributes() && bean != null) 76 printAttributes(aWriter, indent, bean, aDesc); 77 aWriter.println(">"); 78 } 79 80 89 public void printTag(PrintWriter aWriter, int indent, Object bean, 90 StoreDescription aDesc) throws Exception { 91 aWriter.print("<"); 92 aWriter.print(aDesc.getTag()); 93 if (aDesc.isAttributes() && bean != null) 94 printAttributes(aWriter, indent, bean, aDesc); 95 aWriter.println("/>"); 96 } 97 98 106 public void printTagContent(PrintWriter aWriter, String tag, String content) 107 throws Exception { 108 aWriter.print("<"); 109 aWriter.print(tag); 110 aWriter.print(">"); 111 aWriter.print(convertStr(content)); 112 aWriter.print("</"); 113 aWriter.print(tag); 114 aWriter.println(">"); 115 } 116 117 125 public void printTagValueArray(PrintWriter aWriter, String tag, int indent, 126 String [] elements) { 127 if (elements != null && elements.length > 0) { 128 printIndent(aWriter, indent + 2); 129 aWriter.print("<"); 130 aWriter.print(tag); 131 aWriter.print(">"); 132 for (int i = 0; i < elements.length; i++) { 133 printIndent(aWriter, indent + 4); 134 aWriter.print(elements[i]); 135 if (i + 1 < elements.length) 136 aWriter.println(","); 137 } 138 printIndent(aWriter, indent + 2); 139 aWriter.print("</"); 140 aWriter.print(tag); 141 aWriter.println(">"); 142 } 143 } 144 145 153 public void printTagArray(PrintWriter aWriter, String tag, int indent, 154 String [] elements) throws Exception { 155 if (elements != null) { 156 for (int i = 0; i < elements.length; i++) { 157 printIndent(aWriter, indent); 158 printTagContent(aWriter, tag, elements[i]); 159 } 160 } 161 } 162 163 170 public void printIndent(PrintWriter aWriter, int indent) { 171 for (int i = 0; i < indent; i++) { 172 aWriter.print(' '); 173 } 174 } 175 176 189 public void printAttributes(PrintWriter writer, int indent, Object bean, 190 StoreDescription desc) throws Exception { 191 192 printAttributes(writer, indent, true, bean, desc); 193 194 } 195 196 211 public void printAttributes(PrintWriter writer, int indent, 212 boolean include, Object bean, StoreDescription desc) 213 throws Exception { 214 215 String className = bean.getClass().getName(); 217 218 if (include && desc != null && !desc.isStandard()) { 220 writer.print(" className=\""); 221 writer.print(bean.getClass().getName()); 222 writer.print("\""); 223 } 224 225 PropertyDescriptor descriptors[] = Introspector.getBeanInfo( 227 bean.getClass()).getPropertyDescriptors(); 228 if (descriptors == null) { 229 descriptors = new PropertyDescriptor [0]; 230 } 231 232 Object bean2 = defaultInstance(bean); 234 for (int i = 0; i < descriptors.length; i++) { 235 if (descriptors[i] instanceof IndexedPropertyDescriptor ) { 236 continue; } 238 if (!isPersistable(descriptors[i].getPropertyType()) 239 || (descriptors[i].getReadMethod() == null) 240 || (descriptors[i].getWriteMethod() == null)) { 241 continue; } 243 if (desc.isTransientAttribute(descriptors[i].getName())) { 244 continue; } 246 Object value = IntrospectionUtils.getProperty(bean, descriptors[i] 247 .getName()); 248 if (value == null) { 249 continue; } 251 Object value2 = IntrospectionUtils.getProperty(bean2, 252 descriptors[i].getName()); 253 if (value.equals(value2)) { 254 continue; 256 } 257 if (isPrintValue(bean, bean2, descriptors[i].getName(), desc)) 258 printValue(writer, indent, descriptors[i].getName(), value); 259 } 260 261 if (bean instanceof ResourceBase) { 262 ResourceBase resource = (ResourceBase) bean; 263 for (Iterator iter = resource.listProperties(); iter.hasNext();) { 264 String name = (String ) iter.next(); 265 Object value = resource.getProperty(name); 266 if (!isPersistable(value.getClass())) { 267 continue; 268 } 269 if (desc.isTransientAttribute(name)) { 270 continue; } 272 printValue(writer, indent, name, value); 273 274 } 275 } 276 } 277 278 291 public boolean isPrintValue(Object bean, Object bean2, String attrName, 292 StoreDescription desc) { 293 boolean printValue = false; 294 295 Object value = IntrospectionUtils.getProperty(bean, attrName); 296 if (value != null) { 297 Object value2 = IntrospectionUtils.getProperty(bean2, attrName); 298 printValue = !value.equals(value2); 299 300 } 301 return printValue; 302 } 303 304 312 public Object defaultInstance(Object bean) throws InstantiationException , 313 IllegalAccessException { 314 return bean.getClass().newInstance(); 315 } 316 317 324 public void printValue(PrintWriter writer, int indent, String name, 325 Object value) { 326 if (!(value instanceof String )) { 327 value = value.toString(); 328 } 329 writer.println(); 330 printIndent(writer, indent + 4); 331 writer.print(name); 332 writer.print("=\""); 333 String strValue = convertStr((String ) value); 334 writer.print(strValue); 335 writer.print("\""); 336 } 337 338 342 343 public String convertStr(String input) { 344 345 StringBuffer filtered = new StringBuffer (input.length()); 346 char c; 347 for (int i = 0; i < input.length(); i++) { 348 c = input.charAt(i); 349 if (c == '<') { 350 filtered.append("<"); 351 } else if (c == '>') { 352 filtered.append(">"); 353 } else if (c == '\'') { 354 filtered.append("'"); 355 } else if (c == '"') { 356 filtered.append("""); 357 } else if (c == '&') { 358 filtered.append("&"); 359 } else { 360 filtered.append(c); 361 } 362 } 363 return (filtered.toString()); 364 } 365 366 373 protected boolean isPersistable(Class clazz) { 374 375 for (int i = 0; i < persistables.length; i++) { 376 if (persistables[i] == clazz) { 377 return (true); 378 } 379 } 380 return (false); 381 382 } 383 } | Popular Tags |