01: package org.mandarax.util.regex;
02:
03: /*
04: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: import java.util.regex.*;
22:
23: /**
24: * An utility to match string containing wild cards.
25: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
26: * @version 3.4 <7 March 05>
27: * @since 3.0
28: */
29:
30: public class WildcardMatcher {
31: private char SOME = '%';
32: private char ONE = '_';
33: private char ESC = '\\';
34:
35: public static final WildcardMatcher DB_INSTANCE = new WildcardMatcher(
36: '%', '_', '\\');
37: public static final WildcardMatcher FILE_INSTANCE = new WildcardMatcher(
38: '*', '?', '\\');
39:
40: /**
41: * Constructor.
42: * @param some the character representing a sequence of characters
43: * @param one the character representing one character
44: * @param esc the character used to unmark special characters
45: */
46: public WildcardMatcher(char some, char one, char esc) {
47: super ();
48: SOME = some;
49: ONE = one;
50: ESC = esc;
51: }
52:
53: /**
54: * Matches a string and a pattern containing wildcards.
55: * @param pattern a pattern string
56: * @param string the string to be tested
57: * @return a boolean
58: */
59: public boolean matches(String pattern, String string) {
60: if (string == null || pattern == null)
61: return true;
62: Pattern p = getPattern(pattern);
63: Matcher m = p.matcher(string);
64: return m.matches();
65: }
66:
67: /**
68: * Get the RegEx pattern for the respective string with wild cards.
69: * @param pattern a pattern string
70: * @return a RegEx pattern instance
71: */
72: public Pattern getPattern(String pattern) {
73: String regExDef = replace(pattern, SOME, ".*");
74: regExDef = replace(regExDef, ONE, ".");
75: return Pattern.compile(regExDef);
76: }
77:
78: /**
79: * Utility to replace sub strings by patterns.
80: * @param text a string
81: * @param old the old text
82: * @param replacement the text replacing the old text
83: * @return the result of the replacement
84: */
85: public String replace(String text, char old, String replacement) {
86: StringBuffer buf = new StringBuffer();
87: for (int i = 0; i < text.length(); i++) {
88: char c = text.charAt(i);
89: if (c == old) {
90: if (i > 0 && buf.charAt(i - 1) == ESC)
91: buf.append(c);
92: else
93: buf.append(replacement);
94: } else
95: buf.append(c);
96: }
97: return buf.toString();
98: }
99: }
|