    001    /*****************************************************************************
    002     * Copyright (c) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Idea by Rachel Davies, Original code by various                           *
    009     *****************************************************************************/
    010    package org.nanocontainer.aop.defaults;
    011
    012    import org.apache.oro.text.regex.MalformedPatternException;
    013    import org.apache.oro.text.regex.Pattern;
    014    import org.apache.oro.text.regex.Perl5Compiler;
    015    import org.apache.oro.text.regex.Perl5Matcher;
    016    import org.nanocontainer.aop.ComponentPointcut;
    017    import org.nanocontainer.aop.MalformedRegularExpressionException;
    018
    019    /**
    020     * Component pointcut that matches the component name against a regular
    021     * expression.
    022     *
    023     * @author Stephen Molitor
    024     * @version $Revision: 3144 $
    025     */
    026    public class NameMatchesComponentPointcut implements ComponentPointcut {
    027
    028        private final Pattern pattern;
    029        private final Perl5Matcher matcher = new Perl5Matcher();
    030
    031        /**
    032         * Creates a new <code>NameMatchesComponentPointcut</code> that will match
    033         * the component key against the given regular expression. The regular
    034         * expression must be an <a
    035         * href="http://jakarta.apache.org/oro/index.html">ORO </a> Perl5 regular
    036         * expression.
    037         *
    038         * @param regex the regular expression to match against the component name.
    039         * @throws org.nanocontainer.aop.MalformedRegularExpressionException
    040         *          if the regular expression is
    041         *          invalid.
    042         */
    043        public NameMatchesComponentPointcut(String regex) throws MalformedRegularExpressionException {
    044            Perl5Compiler compiler = new Perl5Compiler();
    045            try {
    046                pattern = compiler.compile(regex);
    047            } catch (MalformedPatternException e) {
    048                throw new MalformedRegularExpressionException("malformed component name regular expression", e);
    049            }
    050        }
    051
    052        /**
    053         * Tests to see if the component key's toString() value matches the regular expression passed
    054         * to the constructor.
    055         *
    056         * @param componentKey the component key to match against.
    057         * @return true if the regular expression passed to the constructor matches
    058         *         against <code>componentKey</code>, else false.
    059         */
    060        public boolean picks(Object componentKey) {
    061            String componentName = (String) componentKey.toString();
    062            return matcher.contains(componentName, pattern);
    063        }
    064
    065    }