KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > SimpleUrlPattern


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import java.util.regex.Pattern JavaDoc;
16
17
18 /**
19  * An implementation of URLPattern which matches strings using simple <code>*</code> or <code>?</code> wildcards.
20  * @author Sameer Charles
21  * @author Fabrizio Giustina
22  * @todo rewrite this class using ant-style path comparison and avoiding regexp. See
23  * org.springframework.util.AntPathMatcher in spring 1.2 for a nice implementation
24  * @version $Revision $ ($Author $)
25  */

26 public final class SimpleUrlPattern implements UrlPattern {
27
28     /**
29      * Stable serialVersionUID.
30      */

31     private static final long serialVersionUID = 222L;
32
33     /**
34      * Regexp pattern used for the simple keyword <code>*</code>
35      */

36     private static final String JavaDoc MULTIPLE_CHAR_PATTERN = "[\\p{L}[a-z[A-Z[0-9[!\"#$%&'()*+,-./:; <=>?@\\^_`{|}~\\[\\]]]]]]*"; //$NON-NLS-1$
37

38     /**
39      * Regexp pattern used for the simple keyword <code>?</code>
40      */

41     private static final String JavaDoc SINGLE_CHAR_PATTERN = "[\\p{L}[a-z[A-Z[0-9[!\"#$%&'()*+,-./:; <=>?@\\^_`{|}~\\[\\]]]]]]?"; //$NON-NLS-1$
42

43     /**
44      * Regexp pattern used in match().
45      */

46     private Pattern JavaDoc pattern;
47
48     /**
49      * Pattern length. Longer patterns have higher priority.
50      */

51     private int length;
52
53     /**
54      * Compile a regexp pattern handling <code>*</code> and <code>?</code> chars.
55      * @param string input string
56      * @return a RegExp pattern
57      */

58     public SimpleUrlPattern(String JavaDoc string) {
59         this.length = string.length();
60         this.pattern = Pattern.compile(getEncodedString(string));
61     }
62
63     /**
64      * Replace all "*" with <code>RegexWildcardPattern.MULTIPLE_CHAR_PATTERN</code>.
65      * @param str input string
66      * @return string where all the occurrences of <code>*</code> and <code>?</code> are replaced with a regexp
67      * pattern.
68      */

69     private static String JavaDoc getEncodedString(String JavaDoc str) {
70         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
71         char[] chars = str.toCharArray();
72         int i = 0, last = 0;
73         while (i < chars.length) {
74             char c = chars[i];
75             if (c == '*') {
76                 stringBuffer.append(chars, last, i - last);
77                 stringBuffer.append(MULTIPLE_CHAR_PATTERN);
78                 last = i + 1;
79             }
80             else if (c == '?') {
81                 stringBuffer.append(chars, last, i - last);
82                 stringBuffer.append(SINGLE_CHAR_PATTERN);
83                 last = i + 1;
84             }
85             i++;
86         }
87         stringBuffer.append(chars, last, i - last);
88         return stringBuffer.toString();
89     }
90
91     /**
92      * @see info.magnolia.cms.util.UrlPattern#match(java.lang.String)
93      */

94     public boolean match(String JavaDoc str) {
95         return this.pattern.matcher(str).matches();
96     }
97
98     /**
99      * @see info.magnolia.cms.util.UrlPattern#getLength()
100      */

101     public int getLength() {
102         return this.length;
103     }
104
105     public String JavaDoc toString() {
106         return "SimpleUrlPattern{" + pattern + '}';
107     }
108 }
109
Popular Tags