001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.config.types;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.log.Log;
033: import com.caucho.util.CharBuffer;
034: import com.caucho.util.L10N;
035:
036: import javax.annotation.PostConstruct;
037: import java.util.logging.Logger;
038: import java.util.regex.Pattern;
039: import java.util.regex.PatternSyntaxException;
040:
041: /**
042: * Abstract type building a path pattern. The pattern follows ant.
043: */
044: public class PathPatternType {
045: static final L10N L = new L10N(PathPatternType.class);
046: static final Logger log = Log.open(PathPatternType.class);
047:
048: private Pattern _pattern;
049:
050: public PathPatternType() {
051: }
052:
053: public PathPatternType(String pattern) throws ConfigException,
054: PatternSyntaxException {
055: setName(pattern);
056: }
057:
058: /**
059: * Sets the pattern name.
060: */
061: public void setName(String pattern) throws ConfigException,
062: PatternSyntaxException {
063: CharBuffer cb = new CharBuffer();
064:
065: cb.append("^");
066:
067: int i = 0;
068: int length = pattern.length();
069:
070: while (i < length && pattern.charAt(i) == '/')
071: i++;
072:
073: for (; i < length; i++) {
074: char ch = pattern.charAt(i);
075:
076: if (ch == '/')
077: cb.append('/');
078: else if (ch != '*')
079: cb.append(ch);
080: else if (length <= i + 1 || pattern.charAt(i + 1) != '*')
081: cb.append("[^/]*");
082: else if (i > 0 && pattern.charAt(i - 1) != '/')
083: throw new ConfigException(L.l(
084: "'{0}' is an invalid pattern at '**'", pattern));
085: else if (i + 2 < length && pattern.charAt(i + 2) == '/') {
086: cb.append("([^/]*/)*");
087: i += 2;
088: } else if (i + 2 < length)
089: throw new ConfigException(L.l(
090: "'{0}' is an invalid pattern at '**'", pattern));
091: else {
092: cb.append(".*");
093: i++;
094: }
095: }
096:
097: cb.append("$");
098:
099: _pattern = Pattern.compile(cb.toString());
100: }
101:
102: /**
103: * Sets the pattern name.
104: */
105: public void addText(String text) throws ConfigException,
106: PatternSyntaxException {
107: text = text.trim();
108:
109: if (!text.equals(""))
110: setName(text);
111: }
112:
113: /**
114: * initialize the pattern.
115: */
116: @PostConstruct
117: public void init() throws ConfigException {
118: if (_pattern == null)
119: throw new ConfigException(L
120: .l("pattern requires 'name' attribute."));
121: }
122:
123: /**
124: * Check for a match.
125: */
126: public boolean isMatch(String path) {
127: return _pattern.matcher(path).matches();
128: }
129:
130: public String toString() {
131: return "PathPatternType[" + _pattern.pattern() + "]";
132: }
133: }
|