KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > RegexRules


1 /* $Id: RegexRules.java 179716 2005-06-03 04:06:00Z skitching $
2  *
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.commons.digester;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * <p>Rules implementation that uses regular expression matching for paths.</p>
26  *
27  * <p>The regex implementation is pluggable, allowing different strategies to be used.
28  * The basic way that this class work does not vary.
29  * All patterns are tested to see if they match the path using the regex matcher.
30  * All those that do are return in the order which the rules were added.</p>
31  *
32  * @since 1.5
33  */

34
35 public class RegexRules extends AbstractRulesImpl {
36
37     // --------------------------------------------------------- Fields
38

39     /** All registered <code>Rule</code>'s */
40     private ArrayList JavaDoc registeredRules = new ArrayList JavaDoc();
41     /** The regex strategy used by this RegexRules */
42     private RegexMatcher matcher;
43
44     // --------------------------------------------------------- Constructor
45

46     /**
47      * Construct sets the Regex matching strategy.
48      *
49      * @param matcher the regex strategy to be used, not null
50      * @throws IllegalArgumentException if the strategy is null
51      */

52     public RegexRules(RegexMatcher matcher) {
53         setRegexMatcher(matcher);
54     }
55
56     // --------------------------------------------------------- Properties
57

58     /**
59      * Gets the current regex matching strategy.
60      */

61     public RegexMatcher getRegexMatcher() {
62         return matcher;
63     }
64     
65     /**
66      * Sets the current regex matching strategy.
67      *
68      * @param matcher use this RegexMatcher, not null
69      * @throws IllegalArgumentException if the strategy is null
70      */

71     public void setRegexMatcher(RegexMatcher matcher) {
72         if (matcher == null) {
73             throw new IllegalArgumentException JavaDoc("RegexMatcher must not be null.");
74         }
75         this.matcher = matcher;
76     }
77     
78     // --------------------------------------------------------- Public Methods
79

80     /**
81      * Register a new Rule instance matching the specified pattern.
82      *
83      * @param pattern Nesting pattern to be matched for this Rule
84      * @param rule Rule instance to be registered
85      */

86     protected void registerRule(String JavaDoc pattern, Rule rule) {
87         registeredRules.add(new RegisteredRule(pattern, rule));
88     }
89
90     /**
91      * Clear all existing Rule instance registrations.
92      */

93     public void clear() {
94         registeredRules.clear();
95     }
96
97     /**
98      * Finds matching rules by using current regex matching strategy.
99      * The rule associated with each path that matches is added to the list of matches.
100      * The order of matching rules is the same order that they were added.
101      *
102      * @param namespaceURI Namespace URI for which to select matching rules,
103      * or <code>null</code> to match regardless of namespace URI
104      * @param pattern Nesting pattern to be matched
105      * @return a list of matching <code>Rule</code>'s
106      */

107     public List JavaDoc match(String JavaDoc namespaceURI, String JavaDoc pattern) {
108         //
109
// not a particularly quick implementation
110
// regex is probably going to be slower than string equality
111
// so probably should have a set of strings
112
// and test each only once
113
//
114
// XXX FIX ME - Time And Optimize
115
//
116
ArrayList JavaDoc rules = new ArrayList JavaDoc(registeredRules.size());
117         Iterator JavaDoc it = registeredRules.iterator();
118         while (it.hasNext()) {
119             RegisteredRule next = (RegisteredRule) it.next();
120             if (matcher.match(pattern, next.pattern)) {
121                 rules.add(next.rule);
122             }
123         }
124         return rules;
125     }
126
127
128     /**
129      * Return a List of all registered Rule instances, or a zero-length List
130      * if there are no registered Rule instances. If more than one Rule
131      * instance has been registered, they <strong>must</strong> be returned
132      * in the order originally registered through the <code>add()</code>
133      * method.
134      */

135     public List JavaDoc rules() {
136         ArrayList JavaDoc rules = new ArrayList JavaDoc(registeredRules.size());
137         Iterator JavaDoc it = registeredRules.iterator();
138         while (it.hasNext()) {
139             rules.add(((RegisteredRule) it.next()).rule);
140         }
141         return rules;
142     }
143     
144     /** Used to associate rules with paths in the rules list */
145     private class RegisteredRule {
146         String JavaDoc pattern;
147         Rule rule;
148         
149         RegisteredRule(String JavaDoc pattern, Rule rule) {
150             this.pattern = pattern;
151             this.rule = rule;
152         }
153     }
154 }
155
Popular Tags