1 23 24 package org.objectweb.fractal.adl.attributes; 25 26 import java.io.PrintWriter ; 27 import java.lang.reflect.Method ; 28 import java.util.Map ; 29 30 35 36 public class StaticJavaAttributeBuilder implements AttributeBuilder { 37 38 42 public void setAttribute ( 43 final Object component, 44 final String attributeController, 45 final String name, 46 final String value, 47 final Object context) throws Exception 48 { 49 ClassLoader loader = null; 50 if (context instanceof Map ) { 51 loader = (ClassLoader )((Map )context).get("classloader"); 52 } 53 if (loader == null) { 54 loader = getClass().getClassLoader(); 55 } 56 57 PrintWriter pw = (PrintWriter )((Map )context).get("printwriter"); 58 if (((String )component).startsWith("P")) { 59 String v = value; 60 Class c = loader.loadClass(attributeController); 61 String attrName = Character.toUpperCase(name.charAt(0)) + name.substring(1); 62 Method getter = c.getMethod("get" + attrName, new Class [0]); 63 if (getter.getReturnType() == String .class) { 64 StringBuffer buf = new StringBuffer (); 65 buf.append("\""); 66 for (int i = 0; i < v.length(); ++i) { 67 char car = v.charAt(i); 68 if (car == '\n') { 69 buf.append("\\n"); 70 } else if (car == '\\') { 71 buf.append("\\\\"); 72 } else if (car == '"') { 73 buf.append("\\\""); 74 } else { 75 buf.append(car); 76 } 77 } 78 buf.append("\""); 79 v = buf.toString(); 80 } 81 pw.print(component); 82 pw.print(".set"); 83 pw.print(name); 84 pw.print('('); 85 pw.print(v); 86 pw.println(");"); 87 } else { 88 } 90 } 91 } | Popular Tags |