KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > Perl5RegexpMethodPointcut


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.support;
18
19 import org.apache.oro.text.regex.MalformedPatternException;
20 import org.apache.oro.text.regex.Pattern;
21 import org.apache.oro.text.regex.PatternMatcher;
22 import org.apache.oro.text.regex.Perl5Compiler;
23 import org.apache.oro.text.regex.Perl5Matcher;
24
25 /**
26  * Perl5-style regular expression pointcut. JavaBean properties are:
27  * <ul>
28  * <li>pattern: Perl5 regular expression for the fully-qualified method names to match
29  * <li>patterns: alternative property taking a String array of patterns.
30  * The result will be the union of these patterns.
31  * </ul>
32  *
33  * <p>Note: the regular expressions must be a match. For example,
34  * <code>.*get.*</code> will match com.mycom.Foo.getBar().
35  * <code>get.*</code> will not.
36  *
37  * <p>Currently uses the <a HREF="http://jakarta.apache.org/oro">Jakarta ORO</a>
38  * regular expression library. Does not require JDK 1.4+, in contrast to
39  * JdkRegexpMethodPointcut.
40  *
41  * @author Rod Johnson
42  * @author Rob Harrop
43  * @since 1.1
44  * @see JdkRegexpMethodPointcut
45  */

46 public class Perl5RegexpMethodPointcut extends AbstractRegexpMethodPointcut {
47     
48     /**
49      * ORO's compiled form of this pattern.
50      * ORO fields are transient as they're not serializable.
51      * They will be reinitialized on deserialization by
52      * the initPatternRepresentation() method.
53      */

54     private transient Pattern[] compiledPatterns = new Pattern[0];
55
56     private transient Pattern[] compiledExclusionPatterns = new Pattern[0];
57
58     /** ORO pattern matcher to use */
59     private transient PatternMatcher matcher;
60
61
62     /**
63      * Initializes the {@link Pattern ORO representation} of the supplied exclusion patterns.
64      */

65     protected void initPatternRepresentation(String JavaDoc[] patterns) throws IllegalArgumentException JavaDoc {
66         this.compiledPatterns = compilePatterns(patterns);
67         this.matcher = new Perl5Matcher();
68     }
69
70     /**
71      * Returns <code>true</code> if the {@link Pattern} at index <code>patternIndex</code>
72      * matches the supplied candidate <code>String</code>.
73      */

74     protected boolean matches(String JavaDoc pattern, int patternIndex) {
75         return this.matcher.matches(pattern, this.compiledPatterns[patternIndex]);
76     }
77
78     /**
79      * Initializes the {@link Pattern ORO representation} of the supplied exclusion patterns.
80      */

81     protected void initExcludedPatternRepresentation(String JavaDoc[] excludedPatterns) throws IllegalArgumentException JavaDoc {
82         this.compiledExclusionPatterns = compilePatterns(excludedPatterns);
83     }
84
85     /**
86      * Returns <code>true</code> if the exclusion {@link Pattern} at index <code>patternIndex</code>
87      * matches the supplied candidate <code>String</code>.
88      */

89     protected boolean matchesExclusion(String JavaDoc pattern, int patternIndex) {
90         return this.matcher.matches(pattern, this.compiledExclusionPatterns[patternIndex]);
91     }
92
93     /**
94      * Compiles the supplied pattern sources into a {@link Pattern} array.
95      */

96     private Pattern[] compilePatterns(String JavaDoc[] source) {
97         Perl5Compiler compiler = new Perl5Compiler();
98         Pattern[] destination = new Pattern[source.length];
99         for (int i = 0; i < source.length; i++) {
100             // compile the pattern to be thread-safe
101
try {
102                 destination[i] = compiler.compile(source[i], Perl5Compiler.READ_ONLY_MASK);
103             }
104             catch (MalformedPatternException ex) {
105                 throw new IllegalArgumentException JavaDoc(ex.getMessage());
106             }
107         }
108         return destination;
109     }
110
111 }
112
Popular Tags