001 /* 002 Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.i18n; 019 020 import org.aopalliance.intercept.ConstructorInvocation; 021 import org.aopalliance.intercept.MethodInvocation; 022 import org.objectweb.jac.core.AspectComponent; 023 import org.objectweb.jac.core.Interaction; 024 import org.objectweb.jac.core.Wrapper; 025 import org.objectweb.jac.util.Log; 026 import java.util.HashMap; 027 028 /** 029 * Internationalisation aspect. It allows to translate parameters or 030 * return values. 031 */ 032 033 public class I18nAC extends AspectComponent { 034 TranslatorWrapper translator = new TranslatorWrapper(this); 035 /** 036 * Translate string parameters of methods. 037 */ 038 public void translateParameters(String classExpr, String methodExpr) { 039 Log.trace("i18n","translateParameters "+classExpr+"."+methodExpr); 040 pointcut( "ALL", classExpr, methodExpr, 041 translator, null ); 042 } 043 044 /** 045 * Translate the returned value. 046 */ 047 public void translateReturnedValue(String classExpr, String methodExpr) { 048 Log.trace("i18n","translateReturnedValue "+classExpr+"."+methodExpr); 049 pointcut( "ALL", classExpr, methodExpr, 050 translator, null ); 051 } 052 053 HashMap dict = new HashMap(); 054 055 public void addTranslation(String key, String translation) { 056 Log.trace("i18n","addTranslation "+key+"->"+translation); 057 dict.put(key,translation); 058 } 059 060 public class TranslatorWrapper extends Wrapper { 061 public TranslatorWrapper(AspectComponent ac) { 062 super(ac); 063 } 064 065 public Object translateParameters(Interaction interaction) { 066 Log.trace("i18n","translate parameters for "+interaction.method); 067 Log.trace("i18n",3,"dict: "+dict); 068 Object[] args = interaction.args; 069 Class[] argTypes = interaction.method.getParameterTypes(); 070 for (int i=0; i<args.length; i++) { 071 if (argTypes[i]==String.class) { 072 Log.trace("i18n","Arg "+i+"="+args[i]); 073 args[i] = translate(args[i]); 074 } 075 } 076 return proceed(interaction); 077 } 078 079 public Object translateReturnedValue(Interaction interaction) { 080 Log.trace("i18n","translate return value "+interaction.method); 081 Object returnedValue = proceed(interaction); 082 Log.trace("i18n",3,"dict: "+dict); 083 Log.trace("i18n","Returnedvalue="+returnedValue); 084 return translate(returnedValue); 085 } 086 087 public Object translate(Object value) { 088 if (dict.containsKey(value)) { 089 Log.trace("i18n",2,"translating "+value+"->"+dict.get(value)); 090 return dict.get(value); 091 } else { 092 return value; 093 } 094 } 095 096 // TODO: implement translate return value 097 098 public Object invoke(MethodInvocation invocation) throws Throwable { 099 return translateParameters((Interaction)invocation); 100 } 101 102 public Object construct(ConstructorInvocation invocation) throws Throwable { 103 return translateParameters((Interaction)invocation); 104 } 105 } 106 }