KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > aop > defaults > NameMatchesComponentPointcut


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Idea by Rachel Davies, Original code by various *
9  *****************************************************************************/

10 package org.nanocontainer.aop.defaults;
11
12 import org.apache.oro.text.regex.MalformedPatternException;
13 import org.apache.oro.text.regex.Pattern;
14 import org.apache.oro.text.regex.Perl5Compiler;
15 import org.apache.oro.text.regex.Perl5Matcher;
16 import org.nanocontainer.aop.ComponentPointcut;
17 import org.nanocontainer.aop.MalformedRegularExpressionException;
18
19 /**
20  * Component pointcut that matches the component name against a regular
21  * expression.
22  *
23  * @author Stephen Molitor
24  * @version $Revision: 3144 $
25  */

26 public class NameMatchesComponentPointcut implements ComponentPointcut {
27
28     private final Pattern pattern;
29     private final Perl5Matcher matcher = new Perl5Matcher();
30
31     /**
32      * Creates a new <code>NameMatchesComponentPointcut</code> that will match
33      * the component key against the given regular expression. The regular
34      * expression must be an <a
35      * HREF="http://jakarta.apache.org/oro/index.html">ORO </a> Perl5 regular
36      * expression.
37      *
38      * @param regex the regular expression to match against the component name.
39      * @throws org.nanocontainer.aop.MalformedRegularExpressionException
40      * if the regular expression is
41      * invalid.
42      */

43     public NameMatchesComponentPointcut(String JavaDoc regex) throws MalformedRegularExpressionException {
44         Perl5Compiler compiler = new Perl5Compiler();
45         try {
46             pattern = compiler.compile(regex);
47         } catch (MalformedPatternException e) {
48             throw new MalformedRegularExpressionException("malformed component name regular expression", e);
49         }
50     }
51
52     /**
53      * Tests to see if the component key's toString() value matches the regular expression passed
54      * to the constructor.
55      *
56      * @param componentKey the component key to match against.
57      * @return true if the regular expression passed to the constructor matches
58      * against <code>componentKey</code>, else false.
59      */

60     public boolean picks(Object JavaDoc componentKey) {
61         String JavaDoc componentName = (String JavaDoc) componentKey.toString();
62         return matcher.contains(componentName, pattern);
63     }
64
65 }
Popular Tags