KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > matcher > PathPattern


1 package org.sapia.soto.util.matcher;
2
3 import java.util.Stack JavaDoc;
4 import java.util.StringTokenizer JavaDoc;
5
6
7 /**
8  * Matches path patterns against strings. The path delimiter can be set; it defaults
9  * to a dot (.). An instance of this class can also be set as case-sensitive/insensitive.
10  *
11  * @author Yanick Duchesne
12  * <dl>
13  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
14  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
15  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
16  * </dl>
17  */

18 public class PathPattern implements Pattern {
19   private static final String JavaDoc ASTERISK = "**";
20   private static final String JavaDoc DOT = ".";
21   private PatternElement _first;
22   private boolean _ignoreCase;
23
24   private PathPattern(PatternElement first, boolean ignoreCase) {
25     _first = first;
26     _ignoreCase = ignoreCase;
27   }
28
29   /**
30    * @see org.sapia.soto.util.matcher.Pattern#matches(String)
31    */

32   public boolean matches(String JavaDoc str) {
33     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, DOT);
34     Stack JavaDoc tokens = new Stack JavaDoc();
35
36     while (st.hasMoreTokens()) {
37       if (_ignoreCase) {
38         tokens.add(0, st.nextToken().toLowerCase());
39       } else {
40         tokens.add(0, st.nextToken());
41       }
42     }
43
44     if (_first == null) {
45       return false;
46     }
47
48     return _first.matches(tokens);
49   }
50
51   public static PathPattern parse(String JavaDoc toParse, boolean ignoreCase) {
52     return parse(toParse, '.', ignoreCase);
53   }
54
55   public static PathPattern parse(String JavaDoc toParse, char pathDelim,
56     boolean ignoreCase) {
57     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(toParse, "" + pathDelim);
58     String JavaDoc token;
59
60     PatternElement first = null;
61     PatternElement current = null;
62     PatternElement previous = null;
63
64     while (st.hasMoreTokens()) {
65       token = st.nextToken();
66
67       if (ignoreCase) {
68         token.toLowerCase();
69       }
70
71       if (token.equals(ASTERISK)) {
72         current = new PathToken();
73       } else {
74         current = new CharSeq(token, ignoreCase);
75       }
76
77       if ((first == null) && (current != null)) {
78         first = current;
79       } else if (previous != null) {
80         previous.setNext(current);
81       }
82
83       previous = current;
84     }
85
86     return new PathPattern(first, ignoreCase);
87   }
88 }
89
Popular Tags