KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > util > WildCardPattern


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle.util;
26
27 import java.util.StringTokenizer JavaDoc;
28
29
30 /**
31  * Wildcard string pattern matching class. Only '*' is interpreted as wild card
32  * meaning the occurance of any number of arbitray characters.
33  * <p>
34  * This is a thread-safe immutable class.
35  * <p>
36  * Example: The code snippet
37  * <pre><tt>
38  * StringPattern pattern = new WildCardPattern("Hello*");
39  * System.out.println(pattern.matches("Hello world!"));
40  * System.out.println(pattern.matches("Hi Jim!"));
41  * </tt></pre>
42  * will produce the output
43  * <pre><tt>
44  * true
45  * false
46  * </tt></pre>
47  *
48  * @author Franz-Josef Elmer
49  */

50 public class WildCardPattern implements StringPattern
51 {
52   private static final String JavaDoc WILD_CARD = "*";
53   
54   /**
55    * Returns a {@link StringPattern} object based on a sequences of
56    * wild-card patterns separated by the specified delimiter characters.
57    * The return object matches a string if at least one of the
58    * wild-card pattern matches.
59    * @param patterns Wild-card patterns separated by delimiters defined
60    * in <tt>delimiters</tt>. The actual pattern will be trimed.
61    * That is, leading and trailing white-space characters are removed.
62    * @param delimiters Recognized delimiters.
63    * @return
64    */

65   public static StringPattern createFromsPatterns(String JavaDoc patterns,
66                                                   String JavaDoc delimiters)
67   {
68     if (delimiters.indexOf(WILD_CARD) >= 0)
69     {
70       throw new IllegalArgumentException JavaDoc("No wild card '" + WILD_CARD
71                             + "' are allowed as delimiters: " + delimiters);
72     }
73     OrStringPattern result = new OrStringPattern();
74     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(patterns, delimiters);
75     while (tokenizer.hasMoreTokens())
76     {
77       result.appendPattern(new WildCardPattern(tokenizer.nextToken().trim()));
78     }
79     return result;
80   }
81   
82   private final String JavaDoc _pattern;
83   private final String JavaDoc[] _constantParts;
84   private final boolean _startsWithAnything;
85   private final boolean _endsWithAnything;
86   
87   /**
88    * Creates an instance based on the specified pattern.
89    * @param pattern Pattern which may contain '*' wildcard characters.
90    * Must be not <tt>null</tt>.
91    */

92   public WildCardPattern(String JavaDoc pattern)
93   {
94     _pattern = pattern;
95     _startsWithAnything = pattern.startsWith(WILD_CARD);
96     _endsWithAnything = pattern.endsWith(WILD_CARD);
97     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(pattern, WILD_CARD);
98     _constantParts = new String JavaDoc[tokenizer.countTokens()];
99     for (int i = 0; i < _constantParts.length; i++)
100     {
101       _constantParts[i] = tokenizer.nextToken();
102     }
103   }
104
105   /**
106    * Returns the pattern as delivered to the constructor.
107    */

108   public String JavaDoc toString()
109   {
110     return _pattern;
111   }
112   
113   /**
114    * @return <tt>false</tt> if <tt>string == null</tt>.
115    */

116   public boolean matches(String JavaDoc string)
117   {
118     return string == null ? false : matches(string, 0, 0);
119   }
120   
121   private boolean matches(String JavaDoc string, int indexInString,
122                           int indexInConstantParts)
123   {
124     boolean result = true;
125     if (indexInConstantParts < _constantParts.length)
126     {
127       String JavaDoc constantPart = _constantParts[indexInConstantParts];
128       do
129       {
130         int index = string.indexOf(constantPart, indexInString);
131         if (index < 0
132             || (indexInString == 0 && !_startsWithAnything && index > 0))
133         {
134           result = false;
135           break;
136         }
137         indexInString = index + constantPart.length();
138         result = matches(string, indexInString, indexInConstantParts + 1);
139       } while (result == false);
140     } else
141     {
142       result = result && (_endsWithAnything || indexInString == string.length());
143     }
144     return result;
145   }
146 }
147
Popular Tags