KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > filter > NameExpression


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
// $Id: NameExpression.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose.filter;
28
29 // used packages
30
import java.io.Serializable JavaDoc;
31 import java.lang.reflect.Member JavaDoc;
32
33 import org.apache.regexp.RE;
34 import org.apache.regexp.RESyntaxException;
35
36
37 /**
38  * Class NameExpression defines (and encapsulates) some handy mathing routines.
39  *
40  *
41  * @version $Revision: 1.1.1.1 $
42  * @author Andrei Popovici
43  */

44 class NameExpression implements Serializable JavaDoc
45 {
46   String JavaDoc regexp;
47   transient RE re = null;
48   NameExpression(String JavaDoc regexp)
49     {
50
51       this.regexp = regexp;
52       try
53     {
54       re = new RE(regexp);
55     }
56       catch (RESyntaxException e)
57     {
58       throw new IllegalArgumentException JavaDoc("NameExpression: " + regexp + " is not a valid regexp." +
59                          e.toString());
60     }
61
62     }
63
64   private RE getRE()
65     {
66       try
67     {
68       if (re == null)
69         re = new RE(regexp);
70     }
71       catch (RESyntaxException cannotB)
72     {
73       throw new Error JavaDoc("NameExpression.getRE: regezp was not verified");
74     }
75       return re;
76     }
77
78   boolean classMatchesRegexp(Class JavaDoc cls)
79     {
80       //System.out.println(" ->>>>>>>>> NameExpression - classMatchesRegexp -> className(cls.getName()) = " + className(cls.getName())); //angy test
81
//System.out.println(" ->>>>>>>>> NameExpression - classMatchesRegexp -> regexp = " + regexp); //angy test
82
return getRE().match(className(cls.getName()));
83     }
84
85   boolean packageMatchesRegexp(Class JavaDoc cls)
86     {
87       return getRE().match(packageName(cls.getName()));
88     }
89
90   boolean qualifiedClassMatchesRegexp(Class JavaDoc cls)
91     {
92       return getRE().match(cls.getName());
93     }
94
95   boolean memberMatchesRegexp(Member JavaDoc m)
96     {
97       return getRE().match(m.getName());
98     }
99
100
101   private String JavaDoc className(String JavaDoc qualifiedClassName)
102     {
103       int lastDotIndex = qualifiedClassName.lastIndexOf('.');
104       return qualifiedClassName.substring(lastDotIndex+1);
105     }
106
107   private String JavaDoc packageName(String JavaDoc qualifiedClassName)
108     {
109       int lastDotIndex = qualifiedClassName.lastIndexOf('.');
110       return (lastDotIndex != -1)?qualifiedClassName.substring(0,lastDotIndex):"";
111     }
112
113   public String JavaDoc toString()
114     {
115       return regexp;
116     }
117
118 }
119
120
121 //======================================================================
122
//
123
// $Log: NameExpression.java,v $
124
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
125
// Imported from ETH Zurich
126
//
127
// Revision 1.3 2003/07/02 12:42:57 anicoara
128
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
129
//
130
// Revision 1.2 2003/05/06 15:51:40 popovici
131
// Mozilla-ification
132
//
133
// Revision 1.1 2003/05/05 13:58:06 popovici
134
// renaming from runes to prose
135
//
136
// Revision 1.4 2003/04/17 14:46:04 popovici
137
// ThiS renamed to This; additional method renamings
138
//
139
// Revision 1.3 2003/04/17 13:59:37 popovici
140
// This class and methods renamed
141
//
142
// Revision 1.2 2003/03/04 18:24:09 popovici
143
// Refactoring of the specializer. De-obfuscation
144
// by exposing the inner classes of the factory methods
145
// The specializer implementations now end in 'filter',
146
// whereas the old factory methods 'xxxS' remain
147
// pure bootstrap objects.
148
//
149
// Revision 1.1 2002/05/16 09:18:30 popovici
150
// ClasseS and CFlow replaced with Target, This; the crosscut spec package is refactorized. Now all
151
// crosscuts are grouped (abstract definitions + concrete definitions). Crosscuts explicitely are dynamic and static, and
152
// or/Not/And combinations can be created.
153
//
154
Popular Tags