1 18 19 package org.objectweb.kilim.model.mapping; 20 21 import java.util.HashMap ; 22 import javax.swing.JTextArea ; 23 24 import org.objectweb.kilim.KilimException; 25 26 29 public class TextAreaMapper implements Mapper { 30 private Mapper wrappedMapper; 31 private JTextArea textArea; 32 private String prefix = "\n "; 33 34 38 public TextAreaMapper(Mapper aMapper) { 39 wrappedMapper = aMapper; 40 textArea = new JTextArea ("++++++++++++++START TRACE++++++++++++++++++++"); 41 } 42 43 46 public TextAreaMapper() { 47 this(null); 48 } 49 50 54 public JTextArea getTextArea() { 55 return textArea; 56 } 57 58 61 public void enterContext(MappingContext aContext) throws KilimException { } 62 63 66 public void leaveContext(MappingContext aContext) throws KilimException { } 67 68 71 public Object getGetterValue (Object aSupport, boolean isStatic, String fieldName, MappingContext aContext) throws KilimException { 72 Object resultValue = null; 73 if (wrappedMapper != null) { 74 resultValue = wrappedMapper.getGetterValue (aSupport, isStatic, fieldName, aContext); 75 } 76 textArea.append(prefix + getResultString(resultValue) + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + fieldName + ";"); 77 return resultValue; 79 } 80 81 84 public void executeSetter(Object aSupport, boolean isStatic, String fieldName, Object toBeSet, MappingContext aContext) throws KilimException { 85 textArea.append(prefix + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + fieldName + " = " + toBeSet + ";"); 86 87 if (wrappedMapper != null) { 88 wrappedMapper.executeSetter(aSupport, isStatic, fieldName, toBeSet, aContext); 89 } 90 } 92 93 96 public Object getMethodValue(Object aSupport, boolean isStatic, String aMethodName, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 97 Object resultValue = null; 98 if (wrappedMapper != null) { 99 resultValue = wrappedMapper.getMethodValue (aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext); 100 } 101 102 StringBuffer bffr = new StringBuffer (getResultString(resultValue) + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName); 103 addParameters(bffr , paramObjects , typeNames); 104 bffr.append(";"); 105 textArea.append(prefix + bffr.toString()); 106 return resultValue; 108 } 109 110 113 public void executeMethod(Object aSupport, boolean isStatic, String aMethodName, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 114 if (wrappedMapper != null) { 115 wrappedMapper.executeMethod(aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext); 116 } 117 118 StringBuffer bffr = new StringBuffer (((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName); 119 addParameters(bffr , paramObjects , typeNames); 120 bffr.append(";"); 121 textArea.append(prefix + bffr.toString()); 122 } 124 125 128 public Object getConstructorValue(Class aClass, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 129 Object resultValue = null; 130 if (wrappedMapper != null) { 131 resultValue = wrappedMapper.getConstructorValue(aClass, paramObjects, typeNames, aContext); 132 } 133 134 StringBuffer bffr = new StringBuffer (getVariableName(resultValue) + " = new " + aClass.getName()); 135 addParameters(bffr , paramObjects , typeNames); 136 bffr.append(";"); 137 textArea.append(prefix + bffr.toString()); 138 return resultValue; 140 } 141 142 145 public void executeConstructor(Class aClass, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 146 if (wrappedMapper != null) { 147 wrappedMapper.getConstructorValue(aClass, paramObjects, typeNames, aContext); 148 } 149 150 StringBuffer bffr = new StringBuffer ("new " + aClass.getName()); 151 addParameters(bffr , paramObjects , typeNames); 152 bffr.append(";"); 153 textArea.append(prefix + bffr.toString()); 154 } 156 157 160 public Object getExternalValue(Object aValue, MappingContext aContext) throws KilimException { 161 Object resultValue = null; 162 if (wrappedMapper != null) { 163 resultValue = wrappedMapper.getExternalValue(aValue, aContext); 164 } 165 return resultValue; 166 } 167 168 171 public Object getPropertyValue(Object aValue, MappingContext aContext) throws KilimException { 172 Object resultValue = null; 173 if (wrappedMapper != null) { 174 resultValue = wrappedMapper.getPropertyValue(aValue, aContext); 175 } 176 return resultValue; 177 } 178 179 182 public Object getClassValue(String aClassName, MappingContext aContext) throws KilimException { 183 Object resultValue = null; 184 if (wrappedMapper != null) { 185 resultValue = wrappedMapper.getClassValue(aClassName, aContext); 186 } 187 return resultValue; 188 } 189 190 193 public Object getEventSourceValue(MappingContext aContext) throws KilimException { 194 Object resultValue = null; 195 if (wrappedMapper != null) { 196 resultValue = wrappedMapper.getEventSourceValue(aContext); 197 } 198 return resultValue; 199 } 200 201 204 public Object getNullElementValue(MappingContext aContext) throws KilimException { 205 Object resultValue = null; 206 if (wrappedMapper != null) { 207 resultValue = wrappedMapper.getNullElementValue(aContext); 208 } 209 return resultValue; 210 211 } 212 213 217 public void executeNullElement(MappingContext aContext) { 218 if (wrappedMapper != null) { 219 executeNullElement(aContext); 220 } 221 } 222 223 225 private static final void addParameters(StringBuffer bffr , Object [] paramObjects, String [] typeNames) { 226 bffr.append("("); 227 for (int i = 0 ; i < paramObjects.length ; i++) { 228 if (i != 0) { bffr.append(" , "); } 229 bffr.append(normalizeToString(paramObjects[i])); 230 } 231 bffr.append(")"); 232 } 233 234 private static String normalizeToString(Object obj) { 235 if (obj instanceof Number ) { return obj.toString(); } 236 return getVariableName(obj); 237 } 238 239 private static HashMap variables = new HashMap () , inverse_table = new HashMap (); 240 241 private static String getResultString(Object obj) { 242 String rslt = getVariableName(obj); 243 if (rslt == null) { return ""; } 244 return rslt + " = "; 245 } 246 247 private static String getVariableName(Object obj) { 248 249 if (obj == null) { return null; } 250 251 Object vl = inverse_table.get(obj); 252 if (vl != null) { return (String ) vl; } 253 254 String clss_nm = obj.getClass().getName(); 255 String nm = clss_nm.substring(clss_nm.lastIndexOf('.') + 1); 256 nm = nm.toLowerCase(); 257 Object allrd_thr = variables.get(nm); 258 int nb = 0; 259 while (allrd_thr != null) { 260 allrd_thr = variables.get(nm + (++nb)); 261 } 262 if (nb != 0) { nm = nm + nb; } 263 variables.put (nm , obj); 264 inverse_table.put(obj , nm); 265 return nm; 266 } 267 } | Popular Tags |