001: /**
002: * Copyright (c) 2001, Sergey A. Samokhodkin
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without modification,
006: * are permitted provided that the following conditions are met:
007: *
008: * - Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * - Redistributions in binary form
011: * must reproduce the above copyright notice, this list of conditions and the following
012: * disclaimer in the documentation and/or other materials provided with the distribution.
013: * - Neither the name of jregex nor the names of its contributors may be used
014: * to endorse or promote products derived from this software without specific prior
015: * written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
018: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
021: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
022: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
024: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
025: * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * @version 1.2_01
028: */package jregex;
029:
030: /**
031: * A Pattern subclass that accepts a simplified pattern syntax:
032: * <li><code>?<code> - matches any single character;
033: * <li><code>*<code> - matches any number of any characters;
034: * <li>all the rest - matches itself.
035: * Each wildcard takes a capturing group withing a pattern.
036: *
037: * @see Pattern
038: */
039:
040: public class WildcardPattern extends Pattern {
041: //a wildcard class, see WildcardPattern(String,String,int)
042: public static final String WORD_CHAR = "\\w";
043:
044: //a wildcard class, see WildcardPattern(String,String,int)
045: public static final String ANY_CHAR = ".";
046:
047: private static final String defaultSpecials = "[]().{}+|^$\\";
048: private static final String defaultWcClass = ANY_CHAR;
049:
050: protected static String convertSpecials(String s, String wcClass,
051: String specials) {
052: int len = s.length();
053: StringBuffer sb = new StringBuffer();
054: for (int i = 0; i < len; i++) {
055: char c = s.charAt(i);
056: switch (c) {
057: case '*':
058: sb.append("(");
059: sb.append(wcClass);
060: sb.append("*)");
061: break;
062: case '?':
063: sb.append("(");
064: sb.append(wcClass);
065: sb.append(")");
066: break;
067: default:
068: if (specials.indexOf(c) >= 0)
069: sb.append('\\');
070: sb.append(c);
071: }
072: }
073: return sb.toString();
074: }
075:
076: private String str;
077:
078: /**
079: * @param wc The pattern
080: */
081: public WildcardPattern(String wc) {
082: this (wc, true);
083: }
084:
085: /**
086: * @param wc The pattern
087: * @param icase If true, the pattern is case-insensitive.
088: */
089: public WildcardPattern(String wc, boolean icase) {
090: this (wc, icase ? DEFAULT | IGNORE_CASE : DEFAULT);
091: }
092:
093: /**
094: * @param wc The pattern
095: * @param flags The bitwise OR of any of REFlags.* . The only meaningful
096: * flags are REFlags.IGNORE_CASE and REFlags.DOTALL (the latter allows
097: * the wildcards to match the EOL characters).
098: */
099: public WildcardPattern(String wc, int flags) {
100: compile(wc, defaultWcClass, defaultSpecials, flags);
101: }
102:
103: /**
104: * @param wc The pattern
105: * @param wcClass The wildcard class, could be any of WORD_CHAR or ANY_CHAR
106: * @param flags The bitwise OR of any of REFlags.* . The only meaningful
107: * flags are REFlags.IGNORE_CASE and REFlags.DOTALL (the latter allows
108: * the wildcards to match the EOL characters).
109: */
110: public WildcardPattern(String wc, String wcClass, int flags) {
111: compile(wc, wcClass, defaultSpecials, flags);
112: }
113:
114: protected WildcardPattern() {
115: }
116:
117: protected void compile(String wc, String wcClass, String specials,
118: int flags) {
119: String converted = convertSpecials(wc, wcClass, specials);
120: try {
121: compile(converted, flags);
122: } catch (PatternSyntaxException e) {
123: //something unexpected
124: throw new Error(e.getMessage() + "; original expr: " + wc
125: + ", converted: " + converted);
126: }
127: str = wc;
128: }
129:
130: public String toString() {
131: return str;
132: }
133:
134: /*
135: public static void main(String[] args){
136: Pattern p=new WildcardPattern("*.???");
137: Matcher m=p.matcher("abc.def");
138: //System.out.println(p.toString_d());
139: while(m.proceed()){
140: System.out.println(m);
141: System.out.println("groups: "+m.groupv());
142: }
143: }
144: */
145: }
|