001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.util;
020
021import java.util.regex.Pattern;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026public class PathMatcher {
027
028    public static PathMatcher getRegexMatcher(String regex) {
029        return new RegexMatcher(regex);
030    }
031
032    public static PathMatcher getPrefixMatcher(String prefix) {
033        return new PrefixMatcher(prefix);
034    }
035
036    public static PathMatcher getAllMatcher() {
037        return new PathMatcher();
038    }
039
040    protected PathMatcher() {
041    }
042
043    public boolean match(String value) {
044        return true;
045    }
046
047    public static class RegexMatcher extends PathMatcher {
048        protected Pattern pattern;
049
050        public RegexMatcher(String regex) {
051            pattern = Pattern.compile(regex);
052        }
053
054        @Override
055        public boolean match(String value) {
056            return pattern.matcher(value).matches();
057        }
058    }
059
060    public static class PrefixMatcher extends PathMatcher {
061        protected String prefix;
062
063        public PrefixMatcher(String prefix) {
064            this.prefix = prefix;
065        }
066
067        @Override
068        public boolean match(String value) {
069            return value.startsWith(prefix);
070        }
071    }
072
073}