KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > Pointcut


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@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, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21
22 import java.io.Serializable JavaDoc;
23 import java.util.*;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.rtti.*;
26 import org.objectweb.jac.util.*;
27
28 /**
29  * This abstract class is the definition of a pointcut in org.objectweb.jac.
30  *
31  * @author <a mailto:renaud@aopsys.com>Renaud Pawlak</a>
32  * @see AspectComponent
33  * @see MethodPointcut */

34
35 public abstract class Pointcut implements Serializable JavaDoc {
36     static Logger logger = Logger.getLogger("pointcut");
37     static Logger loggerTags = Logger.getLogger("tags");
38     static Logger loggerKeywords = Logger.getLogger("pointcut.keywords");
39
40     /**
41      * Applies this pointcut to the given wrappee.
42      *
43      * @param wrappee the component the current pointcut is applied to
44      */

45     public abstract void applyTo(Wrappee wrappee, ClassItem cl);
46
47     /**
48      * Parses a keyword expression and returns its actual value as a
49      * regular expression regarding the context.
50      */

51     protected abstract String JavaDoc parseKeyword(Wrappee wrappee,
52                                            ClassItem cl,
53                                            String JavaDoc keywordExpr,
54                                            List parameters);
55
56     /**
57      * Replace elements of parameters "<attribute_name>" with members
58      * who have this attribute, and "member_name" with the member
59      * having that name.
60      * @param parameters Strings to replace
61      * @param cli replace with members of this class
62      * @return substituted list of MemberItem
63      */

64     protected List replaceTags(List parameters,ClassItem cli) {
65         if (cli==null || parameters==null)
66             return null;
67         Vector result = new Vector();
68         loggerTags.debug("check "+parameters+" on "+cli.getName());
69         Iterator it = parameters.iterator();
70         while (it.hasNext()) {
71             String JavaDoc param = (String JavaDoc)it.next();
72             if (param.startsWith("<") && param.endsWith(">")) {
73                 Collection taggedMembers;
74                 if (param.charAt(1)=='!')
75                     taggedMembers = cli.getTaggedFields(
76                         param.substring(2,param.length()-1),true);
77                 else
78                     taggedMembers = cli.getTaggedFields(
79                         param.substring(1,param.length()-1),false);
80                 result.addAll(taggedMembers);
81                 /*
82                   if (taggedMembers.size()==0) {
83                   result.add("#NONE#");
84                   } else {
85                   result.addAll(taggedMembers);
86                   }
87                 */

88             } else if (param.startsWith("{") && param.endsWith("}")) {
89                 result.addAll(cli.filterFields(param.substring(1,param.length()-1)));
90             } else {
91                 result.add(cli.getMember(param));
92             }
93         }
94         loggerTags.debug(" result="+result);
95         return result;
96     }
97
98     /**
99      * A generic method that parses a pointcut expression and stores
100      * the result within a vector.
101      *
102      * @param descr a humain readable desciption of the pointcut
103      * expression type (used to make logs clearer)
104      * @param expr a pointcut expression
105      * @param result the parsing result
106      * @param inv filled with Boolean, one per element in result
107      */

108     protected void parseExpr(String JavaDoc descr, Wrappee wrappee, ClassItem cl,
109                              String JavaDoc expr, String JavaDoc[] keywords,
110                              Vector result, Vector inv) {
111
112         result.clear();
113         inv.clear();
114         expr = Strings.replace(expr, " || ", "\\|");
115         int pos = skipSpaces(expr,0);
116         boolean end = false;
117
118         if (expr.charAt(0) == '!') {
119             pos = skipSpaces(expr,pos+1);
120             inv.add(Boolean.FALSE);
121         } else {
122             inv.add(Boolean.TRUE);
123         }
124         if (pos==-1)
125             throw new RuntimeException JavaDoc("Invalid expression: \""+expr+"\"");
126         while (!end) {
127             int newpos = expr.indexOf("&&", pos);
128             try {
129                 if (newpos != -1) {
130                     result.add(replaceKeywords(
131                         wrappee,cl,expr.substring(pos,newpos).trim(),keywords));
132                     pos = skipSpaces(expr,newpos + 2);
133                     if (pos==-1)
134                         throw new RuntimeException JavaDoc("Invalid expression: \""+expr+"\"");
135                     if (expr.charAt(pos) == '!') {
136                         pos = skipSpaces(expr,pos+1);
137                         if (pos==-1)
138                             throw new RuntimeException JavaDoc("Invalid expression: \""+expr+"\"");
139                         inv.add(Boolean.FALSE);
140                     } else {
141                         inv.add(Boolean.TRUE);
142                     }
143                 } else {
144                     result.add(
145                         replaceKeywords(wrappee,cl,expr.substring(pos).trim(),keywords));
146                     end = true;
147                 }
148             } catch (Exception JavaDoc e) {
149                 logger.error("Invalid pointcut definition, "+descr+
150                              " construction failed at position "+pos+": "+e);
151             }
152         }
153     }
154
155     /**
156      * Skips spaces in a string.
157      * @param str a string
158      * @param pos a position in the string
159      * @return the next position in str which >= pos whose character
160      * is not a white space, or -1.
161      */

162     static int skipSpaces(String JavaDoc str, int pos) {
163         while (pos<str.length()) {
164             if (!Character.isWhitespace(str.charAt(pos)))
165                 return pos;
166             pos++;
167         }
168         return -1;
169     }
170
171     /** Replaces the keywords within an expression. */
172     String JavaDoc replaceKeywords(Wrappee wrappee, ClassItem cl,
173                            String JavaDoc expr, String JavaDoc[] keywords) {
174         String JavaDoc newExpr = expr;
175         for(int i=0; i<keywords.length; i++) {
176             newExpr = replaceKeyword(wrappee, cl, newExpr, keywords[i]);
177         }
178         return newExpr;
179     }
180
181     /**
182      * Parses the parameters of a keyword
183      * @param params a string representing the parameters. Should start
184      * with '(' and end with ')'.
185      * @return a list of member items
186      */

187     List parseParameters(String JavaDoc params, ClassItem cli) {
188         if (params.equals("") || params.charAt(0) != '(')
189             return null;
190         /*
191           StringTokenizer st = new StringTokenizer( params, "," );
192           while (st.hasMoreTokens()) {
193           result.add(st.nextToken());
194           }
195         */

196         return replaceTags(
197             Strings.splitToList(params.substring(1, params.indexOf(')',0)),",")
198             ,cli);
199     }
200
201     int parametersLength(String JavaDoc params) {
202         if (params.equals( "" ))
203             return 0;
204         if (params.charAt(0) != '(')
205             return 0;
206         return params.indexOf(')',0)+1;
207     }
208
209     /** Replace a keyword within an expression. */
210     String JavaDoc replaceKeyword(Wrappee wrappee, ClassItem cl,
211                           String JavaDoc expr, String JavaDoc keyword) {
212
213         int pos = 0;
214         boolean end = false;
215         StringBuffer JavaDoc newExpr = new StringBuffer JavaDoc();
216         int keyLen = keyword.length();
217
218         while (!end) {
219             int newpos = expr.indexOf(keyword, pos);
220             try {
221                 if (newpos != -1) {
222                     loggerKeywords.debug("replacing keyword '"+keyword+
223                                          "' in expr :"+expr);
224                     newExpr.append(expr.substring(pos, newpos - pos));
225                     newExpr.append(
226                         parseKeyword(
227                             wrappee, cl, keyword,
228                             parseParameters(
229                                 expr.substring(newpos+keyword.length()),cl)));
230                     pos = newpos + keyLen +
231                         parametersLength(expr.substring(newpos+keyLen));
232                 } else {
233                     newExpr.append(expr.substring(pos));
234                     end = true;
235                 }
236             } catch (Exception JavaDoc e) {
237                 e.printStackTrace();
238                 //Log.error("Invalid keyword '"+keyword+"'");
239
}
240         }
241         return newExpr.toString();
242     }
243
244 }
245
Popular Tags