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 StaticFractalAttributeBuilder 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 String v = value; 58 Class c = loader.loadClass(attributeController); 59 String attrName = Character.toUpperCase(name.charAt(0)) + name.substring(1); 60 Method getter = c.getMethod("get" + attrName, new Class [0]); 61 if (getter.getReturnType() == String .class) { 62 StringBuffer buf = new StringBuffer (); 63 buf.append("\""); 64 for (int i = 0; i < v.length(); ++i) { 65 char car = v.charAt(i); 66 if (car == '\n') { 67 buf.append("\\n"); 68 } else if (car == '\\') { 69 buf.append("\\\\"); 70 } else if (car == '"') { 71 buf.append("\\\""); 72 } else { 73 buf.append(car); 74 } 75 } 76 buf.append("\""); 77 v = buf.toString(); 78 } 79 PrintWriter pw = (PrintWriter )((Map )context).get("printwriter"); 80 pw.print("(("); 81 pw.print(attributeController); 82 pw.print(")Fractal.getAttributeController("); 83 pw.print(component); 84 pw.print(")).set"); 85 pw.print(attrName); 86 pw.print("("); 87 pw.print(v); 88 pw.println(");"); 89 } 90 } | Popular Tags |