001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.ecm.webengine.util;
018
019import java.util.regex.Pattern;
020
021/**
022 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
023 */
024public class PathMatcher {
025
026    public static PathMatcher getRegexMatcher(String regex) {
027        return new RegexMatcher(regex);
028    }
029
030    public static PathMatcher getPrefixMatcher(String prefix) {
031        return new PrefixMatcher(prefix);
032    }
033
034    public static PathMatcher getAllMatcher() {
035        return new PathMatcher();
036    }
037
038    protected PathMatcher() {
039    }
040
041    public boolean match(String value) {
042        return true;
043    }
044
045    public static class RegexMatcher extends PathMatcher {
046        protected Pattern pattern;
047
048        public RegexMatcher(String regex) {
049            pattern = Pattern.compile(regex);
050        }
051
052        @Override
053        public boolean match(String value) {
054            return pattern.matcher(value).matches();
055        }
056    }
057
058    public static class PrefixMatcher extends PathMatcher {
059        protected String prefix;
060
061        public PrefixMatcher(String prefix) {
062            this.prefix = prefix;
063        }
064
065        @Override
066        public boolean match(String value) {
067            return value.startsWith(prefix);
068        }
069    }
070
071}