KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_web > deployment > api > Pattern


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 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
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: Pattern.java,v 1.2 2004/06/01 11:24:01 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_web.deployment.api;
28
29
30 /**
31  * Defines a Pattern object for JACC
32  * Allow to check if this pattern is a path prefix pattern, and can return the
33  * type of the pattern
34  * Implements Comparable interface to sort items
35  * @see JACC 3.1.3.1 and 3.1.3.3 for more details
36  * @author Florent Benoit
37  */

38 public class Pattern implements Comparable JavaDoc {
39
40     /**
41      * Exact pattern
42      * @see JACC 3.1.3.3 table 3.2
43      */

44     private static final int EXACT = 0;
45
46     /**
47      * Path prefix pattern
48      * @see JACC 3.1.3.3 table 3.2
49      */

50     private static final int PATH_PREFIX = 1;
51
52     /**
53      * Extension pattern
54      * @see JACC 3.1.3.3 table 3.2
55      */

56     private static final int EXTENSION = 2;
57
58     /**
59      * Default pattern
60      * @see JACC 3.1.3.3 table 3.2
61      */

62     private static final int DEFAULT = 3;
63
64     /**
65      * String representation of the pattern
66      */

67     private String JavaDoc pattern = null;
68
69     /**
70      * Type of the pattern
71      */

72     private int type;
73
74
75
76     /**
77      * Constructor
78      * @param pattern string representation of the pattern
79      */

80     public Pattern(String JavaDoc pattern) {
81         this.pattern = pattern;
82         defineTypePattern();
83     }
84
85     /**
86      * Defines the type of the pattern
87      * Avoid to compute the type every time it is asked
88      */

89     private void defineTypePattern() {
90         if (pattern.startsWith("/") && pattern.endsWith("/*")) {
91             // Path prefix
92
type = PATH_PREFIX;
93         } else if (pattern.startsWith("*.")) {
94             // Extension
95
type = EXTENSION;
96         } else if (pattern.equals("/")) {
97             // Default
98
type = DEFAULT;
99         } else {
100             // else it is EXACT
101
type = EXACT;
102         }
103     }
104
105
106     /**
107      * Test if this pattern is a path-prefix pattern or not
108      * (Starts with "/" and ends with "/*"
109      * @return true if this pattern is a path-prefix
110      */

111     public boolean isPathPrefix() {
112         return (type == PATH_PREFIX);
113     }
114
115
116     /**
117      * Test if this pattern is an extension pattern
118      * (Starts with ".*")
119      * @return true if this pattern is an extension pattern
120      */

121     public boolean isExtensionPattern() {
122         return (type == EXTENSION);
123     }
124
125
126     /**
127      * Test if this pattern is the default pattern
128      * (equals to "/")
129      * @return true if this pattern is the default pattern
130      */

131     public boolean isDefaultPattern() {
132         return (type == DEFAULT);
133     }
134
135
136     /**
137      * Test if this pattern is an exact pattern
138      * (not in the other case)
139      * @return true if this pattern is an exact pattern
140      */

141     public boolean isExactPattern() {
142         return (type == EXACT);
143     }
144
145
146     /**
147      * Test if the pattern starts with the given pattern
148      * It's the inverse of this definition :
149      * The other pattern starts with the substring of this pattern,
150      * minus its last 2 characters, and the next character
151      * of the other pattern, if there is one, is "/"
152      * @param substring string to test
153      * @return true if the pattern starts with the given pattern
154      */

155     public boolean isSubstringPattern(String JavaDoc substring) {
156         int size = substring.length();
157         if (size == 0) {
158             return true;
159         } else {
160             // true if next character = '/' if any or true if equals (no last char)
161
return (pattern.startsWith(substring)
162                     && (pattern.length() == size || pattern.substring(size).charAt(0) == '/'));
163         }
164     }
165
166
167     /**
168      * Test if this pattern matches another pattern
169      * This URL pattern matches another pattern if they
170      * are related, by case sensitive comparison, as follows:
171      * - their pattern values are String equivalent, or
172      * - this pattern is the path-prefix pattern "/*", or
173      * - this pattern is a path-prefix pattern (that is, it
174      * starts with "/" and ends with "/*") and the other
175      * pattern starts with the substring of this pattern,
176      * minus its last 2 characters, and the next character
177      * of the other pattern, if there is one, is "/", or
178      * - this pattern is an extension pattern (that is, it
179      * starts with "*.") and the other pattern ends with this
180      * pattern, or this pattern is the special default pattern,
181      * "/", which matches all other patterns.
182      * @param otherPattern pattern to check for matching
183      * @see JACC 3.1.3.3 for the definition of these rules
184      * @return true if the patterns match
185      */

186     public boolean isMatching(Pattern otherPattern) {
187         if (pattern.equals(otherPattern)) {
188             // case 1
189
return true;
190         } else if ((pattern.length() == 2) && isPathPrefix()) {
191             // case 2
192
return true;
193         } else if (isPathPrefix() && otherPattern.isSubstringPattern(pattern.substring(0, pattern.length() - 2))) {
194             // case 3
195
return true;
196         } else if (isExtensionPattern() && otherPattern.getValue().endsWith(pattern.substring(1))) {
197             // case 4
198
return true;
199          } else {
200              // case 5 or no match
201
return isDefaultPattern();
202          }
203     }
204
205     /**
206      * Gets the string representation of this object
207      * @return the string representation of this pattern
208      */

209     public String JavaDoc getValue() {
210         return pattern;
211     }
212
213
214     /**
215      * String representation
216      * @return the string representation of this pattern
217      */

218     public String JavaDoc toString() {
219         return getValue();
220     }
221
222     /**
223      * Tests if this object is equal to another object
224      * @param o given object to test
225      * @return true if the other object is a pattern object with the same value
226      */

227     public boolean equals(Object JavaDoc o) {
228         if (!(o instanceof Pattern)) {
229             return false;
230         }
231         return pattern.equals(((Pattern) o).getValue());
232     }
233
234     /**
235      * Gets the hashcode of this object
236      * @return hashcode of this object
237      */

238     public int hashCode() {
239         return pattern.hashCode();
240     }
241
242     /**
243      * Compares this object with the specified object for order.
244      * @param o object to compare
245      * @return a negative integer, zero, or a positive integer
246      * as this object is less than, equal to, or greater than
247      * the specified object.
248      */

249     public int compareTo(Object JavaDoc o) {
250         if (!(o instanceof Pattern)) {
251             return -1;
252         }
253         return pattern.compareTo(((Pattern) o).getValue());
254     }
255
256 }
257
Popular Tags