KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > i18n > I18nAC


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

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 JavaDoc;
27
28 /**
29  * Internationalisation aspect. It allows to translate parameters or
30  * return values.
31   */

32
33 public class I18nAC extends AspectComponent {
34    TranslatorWrapper translator = new TranslatorWrapper(this);
35    /**
36     * Translate string parameters of methods.
37     */

38    public void translateParameters(String JavaDoc classExpr, String JavaDoc methodExpr) {
39       Log.trace("i18n","translateParameters "+classExpr+"."+methodExpr);
40       pointcut( "ALL", classExpr, methodExpr,
41                 translator, null );
42    }
43
44    /**
45     * Translate the returned value.
46     */

47    public void translateReturnedValue(String JavaDoc classExpr, String JavaDoc methodExpr) {
48       Log.trace("i18n","translateReturnedValue "+classExpr+"."+methodExpr);
49       pointcut( "ALL", classExpr, methodExpr,
50                 translator, null );
51    }
52
53    HashMap JavaDoc dict = new HashMap JavaDoc();
54
55    public void addTranslation(String JavaDoc key, String JavaDoc 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 JavaDoc translateParameters(Interaction interaction) {
66          Log.trace("i18n","translate parameters for "+interaction.method);
67          Log.trace("i18n",3,"dict: "+dict);
68          Object JavaDoc[] args = interaction.args;
69          Class JavaDoc[] argTypes = interaction.method.getParameterTypes();
70          for (int i=0; i<args.length; i++) {
71             if (argTypes[i]==String JavaDoc.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 JavaDoc translateReturnedValue(Interaction interaction) {
80          Log.trace("i18n","translate return value "+interaction.method);
81          Object JavaDoc 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 JavaDoc translate(Object JavaDoc 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     // TODO: implement translate return value
97

98     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
99         return translateParameters((Interaction)invocation);
100     }
101
102     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
103         return translateParameters((Interaction)invocation);
104     }
105    }
106 }
107
Popular Tags