001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.support;
018:
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import java.util.regex.PatternSyntaxException;
022:
023: /**
024: * Java 1.4+ regular expression pointcut. JavaBean properties are:
025: * <ul>
026: * <li>pattern: Java 1.4 regular expression for the fully-qualified method names to match
027: * <li>patterns: alternative property taking a String array of patterns. The result will
028: * be the union of these patterns.
029: * </ul>
030: *
031: * <p>Note: the regular expressions must be a match. For example,
032: * <code>.*get.*</code> will match com.mycom.Foo.getBar().
033: * <code>get.*</code> will not.
034: *
035: * <p>Requires JDK 1.4+, as it builds on the <code>java.util.regex</code> package.
036: * As alternative on JDK 1.3 or for Perl5-style regular expression parsing,
037: * consider Perl5RegexpMethodPointcut.
038: *
039: * @author Dmitriy Kopylenko
040: * @author Rob Harrop
041: * @since 1.1
042: * @see Perl5RegexpMethodPointcut
043: */
044: public class JdkRegexpMethodPointcut extends
045: AbstractRegexpMethodPointcut {
046:
047: /**
048: * Java 1.4 compiled form of the patterns.
049: */
050: private transient Pattern[] compiledPatterns = new Pattern[0];
051:
052: /**
053: * Java 1.4 compiled form of the exclusion patterns.
054: */
055: private transient Pattern[] compiledExclusionPatterns = new Pattern[0];
056:
057: /**
058: * Initialize Java 1.4 {@link Pattern Patterns} from the supplied <code>String[]</code>.
059: */
060: protected void initPatternRepresentation(String[] patterns)
061: throws PatternSyntaxException {
062: this .compiledPatterns = compilePatterns(patterns);
063: }
064:
065: /**
066: * Returns <code>true</code> if the {@link Pattern} at index <code>patternIndex</code>
067: * matches the supplied candidate <code>String</code>.
068: */
069: protected boolean matches(String pattern, int patternIndex) {
070: Matcher matcher = this .compiledPatterns[patternIndex]
071: .matcher(pattern);
072: return matcher.matches();
073: }
074:
075: /**
076: * Initialize Java 1.4 exclusion {@link Pattern Patterns} from the supplied <code>String[]</code>.
077: */
078: protected void initExcludedPatternRepresentation(
079: String[] excludedPatterns) throws IllegalArgumentException {
080: this .compiledExclusionPatterns = compilePatterns(excludedPatterns);
081: }
082:
083: /**
084: * Returns <code>true</code> if the exclusion {@link Pattern} at index <code>patternIndex</code>
085: * matches the supplied candidate <code>String</code>.
086: */
087: protected boolean matchesExclusion(String candidate,
088: int patternIndex) {
089: Matcher matcher = this .compiledExclusionPatterns[patternIndex]
090: .matcher(candidate);
091: return matcher.matches();
092: }
093:
094: /**
095: * Compiles the supplied <code>String[]</code> into an array of
096: * {@link Pattern} objects and returns that array.
097: */
098: private Pattern[] compilePatterns(String[] source) {
099: Pattern[] destination = new Pattern[source.length];
100: for (int i = 0; i < source.length; i++) {
101: destination[i] = Pattern.compile(source[i]);
102: }
103: return destination;
104: }
105:
106: }
|