KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > GroupRoutingCriteria


1 package org.jahia.services.usermanager;
2
3 /**
4  * <p>Title: </p>
5  * <p>Description: </p>
6  * <p>Copyright: Copyright (c) 2003</p>
7  * <p>Company: </p>
8  * @author not attributable
9  * @version 1.0
10  */

11
12 import java.util.Enumeration JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 /**
18  * <p>Title: a routing criteria is a set of pattern matching rules that
19  * define a criteria to route method calls to a specific object</p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: Jahia Inc.</p>
23  *
24  * @author Viceic Predrag Predrag.Viceic@ci.unil.ch>
25  * @version 1.0
26  */

27
28
29 public class GroupRoutingCriteria {
30     private String JavaDoc name;
31     private String JavaDoc description;
32     private Properties JavaDoc conditions;
33     private String JavaDoc destination;
34     private JahiaGroupManagerProvider providerInstance = null;
35
36     public GroupRoutingCriteria (String JavaDoc name,
37                                  String JavaDoc description,
38                                  Properties JavaDoc conditions,
39                                  String JavaDoc destination) {
40         this.name = name;
41         this.description = description;
42         this.conditions = conditions;
43         this.destination = destination;
44     }
45
46     public String JavaDoc getName () {
47         return name;
48     }
49
50     public String JavaDoc getDescription () {
51         return description;
52     }
53
54     public Properties JavaDoc getConditions () {
55         return conditions;
56     }
57
58     public String JavaDoc getDestination () {
59         return destination;
60     }
61
62
63     public boolean matchesValues (Properties JavaDoc values) {
64
65         // let's first test all the stupid stuff...
66
if (conditions == null) {
67             return false;
68         }
69         if (conditions.size () == 0) {
70             return false;
71         }
72         if (values == null) {
73             return false;
74         }
75         if (values.size () == 0) {
76             return false;
77         }
78
79         // now let's do some real work guys...
80
Enumeration JavaDoc valueKeys = values.keys ();
81         while (valueKeys.hasMoreElements ()) {
82             Object JavaDoc curKeyObj = valueKeys.nextElement ();
83             if (curKeyObj instanceof String JavaDoc) {
84                 String JavaDoc curKey = (String JavaDoc) curKeyObj;
85                 String JavaDoc curValue = values.getProperty (curKey);
86                 String JavaDoc curConditionPattern = conditions.getProperty (curKey);
87                 if (curConditionPattern != null) {
88                     // we found a matching condition for this property key
89
if (!starMatching (curConditionPattern, curValue)) {
90                         return false;
91                     }
92                 }
93             }
94         }
95         return true;
96     }
97
98     /**
99      * Case sensitive star pattern matching. eg t*t matches "test", but not
100      * "true"
101      *
102      * @param starPattern
103      * @param inputToTest
104      *
105      * @return true if the inputToTest string matches the starPattern pattern
106      */

107     private static boolean starMatching (String JavaDoc starPattern, String JavaDoc inputToTest) {
108         // we try to make every effort to determine a match as quickly as possible,
109
// but we must at least parse the pattern :(
110
StringTokenizer JavaDoc patternTokens = new StringTokenizer JavaDoc (starPattern, "*", false);
111         Vector JavaDoc patternMatchers = new Vector JavaDoc ();
112
113         while (patternTokens.hasMoreTokens ()) {
114             String JavaDoc curToken = patternTokens.nextToken ();
115             patternMatchers.add (curToken);
116         }
117
118         if (patternMatchers.size () == 0) {
119             return false;
120         }
121
122         if (!starPattern.startsWith ("*")) {
123             if (!inputToTest.startsWith ((String JavaDoc) patternMatchers.elementAt (0))) {
124                 // beginning doesn't match...
125
return false;
126             }
127         }
128         if (!starPattern.endsWith ("*")) {
129             if (!inputToTest.endsWith (
130                     (String JavaDoc) patternMatchers.elementAt (patternMatchers.size () - 1))) {
131                 // beginning doesn't match...
132
return false;
133             }
134         }
135
136         Enumeration JavaDoc patternMatchersEnum = patternMatchers.elements ();
137         int offsetInInput = 0;
138         int matchPos = 0;
139         String JavaDoc curMatcher = null;
140         while (patternMatchersEnum.hasMoreElements ()) {
141             curMatcher = (String JavaDoc) patternMatchersEnum.nextElement ();
142             matchPos = inputToTest.indexOf (curMatcher, offsetInInput);
143             if (matchPos == -1) {
144                 return false;
145             }
146             offsetInInput = matchPos + curMatcher.length ();
147             if (offsetInInput >= inputToTest.length ()) {
148                 // we still have pattern to match but we got to the end of
149
// the string too early.
150
return false;
151             }
152         }
153         // if we got here it means we've matched all the pattern matchers in
154
// the input string.
155
return true;
156     }
157
158 }
159
Popular Tags