KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > applications > filters > URIMatcher


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.applications.filters;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29 import java.util.regex.PatternSyntaxException JavaDoc;
30
31 /**
32  * Created by IntelliJ IDEA.
33  * User: lbj
34  * Date: 31-01-2004
35  * Time: 22:04:30
36  * To change this template use Options | File Templates.
37  */

38
39 public class URIMatcher
40 {
41     private Pattern JavaDoc[] patterns = new Pattern JavaDoc[0];
42
43     // ---- Constructor ----
44

45     private URIMatcher()
46     {
47     }
48
49     public URIMatcher(Pattern JavaDoc[] patterns)
50     {
51         this.patterns = patterns;
52     }
53
54     // ---- Public methods ----
55

56     public static URIMatcher compilePatterns(String JavaDoc[] strings, boolean caseSensitive) throws PatternSyntaxException JavaDoc
57     {
58         URIMatcher uriMatcher = new URIMatcher();
59         List JavaDoc patterns = new ArrayList JavaDoc();
60         if (strings != null && strings.length > 0)
61         {
62             for (int i=0;i<strings.length;i++)
63             {
64                 String JavaDoc str = strings[i];
65                 if (str != null)
66                 {
67                     str = str.trim();
68                     if (str.length() > 0)
69                     {
70                         String JavaDoc patternStr = patternize(str);
71
72                         Pattern JavaDoc pattern = null;
73                         if(caseSensitive)
74                             pattern = Pattern.compile(patternStr);
75                         else
76                             pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
77                         
78                         patterns.add(pattern);
79                     }
80                 }
81             }
82         }
83         uriMatcher.setPatterns((Pattern JavaDoc[])patterns.toArray(new Pattern JavaDoc[0]));
84         return uriMatcher;
85     }
86
87     public boolean matches(String JavaDoc URI)
88     {
89         if (URI != null && patterns != null && patterns.length > 0)
90         {
91             for (int i=0;i<patterns.length;i++)
92             {
93                 if (patterns[i].matcher(URI).matches())
94                     return true;
95             }
96         }
97         return false;
98     }
99
100     // ---- Private and protected methods ----
101

102     private static String JavaDoc patternize(String JavaDoc str)
103     {
104         str = str.replaceAll("\\.", "\\\\.");
105         str = str.replaceAll("\\:", "\\\\:");
106         str = str.replaceAll("\\*", ".*");
107         return str;
108     }
109
110     private void setPatterns(Pattern JavaDoc[] patterns)
111     {
112         this.patterns = patterns;
113     }
114
115 }
Popular Tags