1 17 18 package org.objectweb.jac.aspects.i18n; 19 20 import org.aopalliance.intercept.ConstructorInvocation; 21 import org.aopalliance.intercept.MethodInvocation; 22 import org.objectweb.jac.core.AspectComponent; 23 import org.objectweb.jac.core.Interaction; 24 import org.objectweb.jac.core.Wrapper; 25 import org.objectweb.jac.util.Log; 26 import java.util.HashMap ; 27 28 32 33 public class I18nAC extends AspectComponent { 34 TranslatorWrapper translator = new TranslatorWrapper(this); 35 38 public void translateParameters(String classExpr, String methodExpr) { 39 Log.trace("i18n","translateParameters "+classExpr+"."+methodExpr); 40 pointcut( "ALL", classExpr, methodExpr, 41 translator, null ); 42 } 43 44 47 public void translateReturnedValue(String classExpr, String methodExpr) { 48 Log.trace("i18n","translateReturnedValue "+classExpr+"."+methodExpr); 49 pointcut( "ALL", classExpr, methodExpr, 50 translator, null ); 51 } 52 53 HashMap dict = new HashMap (); 54 55 public void addTranslation(String key, String translation) { 56 Log.trace("i18n","addTranslation "+key+"->"+translation); 57 dict.put(key,translation); 58 } 59 60 public class TranslatorWrapper extends Wrapper { 61 public TranslatorWrapper(AspectComponent ac) { 62 super(ac); 63 } 64 65 public Object translateParameters(Interaction interaction) { 66 Log.trace("i18n","translate parameters for "+interaction.method); 67 Log.trace("i18n",3,"dict: "+dict); 68 Object [] args = interaction.args; 69 Class [] argTypes = interaction.method.getParameterTypes(); 70 for (int i=0; i<args.length; i++) { 71 if (argTypes[i]==String .class) { 72 Log.trace("i18n","Arg "+i+"="+args[i]); 73 args[i] = translate(args[i]); 74 } 75 } 76 return proceed(interaction); 77 } 78 79 public Object translateReturnedValue(Interaction interaction) { 80 Log.trace("i18n","translate return value "+interaction.method); 81 Object returnedValue = proceed(interaction); 82 Log.trace("i18n",3,"dict: "+dict); 83 Log.trace("i18n","Returnedvalue="+returnedValue); 84 return translate(returnedValue); 85 } 86 87 public Object translate(Object value) { 88 if (dict.containsKey(value)) { 89 Log.trace("i18n",2,"translating "+value+"->"+dict.get(value)); 90 return dict.get(value); 91 } else { 92 return value; 93 } 94 } 95 96 98 public Object invoke(MethodInvocation invocation) throws Throwable { 99 return translateParameters((Interaction)invocation); 100 } 101 102 public Object construct(ConstructorInvocation invocation) throws Throwable { 103 return translateParameters((Interaction)invocation); 104 } 105 } 106 } 107 | Popular Tags |