View Javadoc

1   package org.apache.commons.digester3;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.xml.sax.Attributes;
26  
27  /**
28   * <p>
29   * Rules implementation that uses regular expression matching for paths.
30   * </p>
31   * <p>
32   * The regex implementation is pluggable, allowing different strategies to be used. The basic way that this class work
33   * does not vary. All patterns are tested to see if they match the path using the regex matcher. All those that do are
34   * return in the order which the rules were added.
35   * </p>
36   * 
37   * @since 1.5
38   */
39  public class RegexRules
40      extends AbstractRulesImpl
41  {
42  
43      // --------------------------------------------------------- Fields
44  
45      /** All registered <code>Rule</code>'s */
46      private ArrayList<RegisteredRule> registeredRules = new ArrayList<RegisteredRule>();
47  
48      /** The regex strategy used by this RegexRules */
49      private RegexMatcher matcher;
50  
51      // --------------------------------------------------------- Constructor
52  
53      /**
54       * Construct sets the Regex matching strategy.
55       * 
56       * @param matcher the regex strategy to be used, not null
57       */
58      public RegexRules( RegexMatcher matcher )
59      {
60          setRegexMatcher( matcher );
61      }
62  
63      // --------------------------------------------------------- Properties
64  
65      /**
66       * Gets the current regex matching strategy.
67       *
68       * @return the current regex matching strategy.
69       */
70      public RegexMatcher getRegexMatcher()
71      {
72          return matcher;
73      }
74  
75      /**
76       * Sets the current regex matching strategy.
77       * 
78       * @param matcher use this RegexMatcher, not null
79       */
80      public void setRegexMatcher( RegexMatcher matcher )
81      {
82          if ( matcher == null )
83          {
84              throw new IllegalArgumentException( "RegexMatcher must not be null." );
85          }
86          this.matcher = matcher;
87      }
88  
89      // --------------------------------------------------------- Public Methods
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      protected void registerRule( String pattern, Rule rule )
96      {
97          registeredRules.add( new RegisteredRule( pattern, rule ) );
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     public void clear()
104     {
105         registeredRules.clear();
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     public List<Rule> match( String namespaceURI, String pattern, String name, Attributes attributes )
112     {
113         //
114         // not a particularly quick implementation
115         // regex is probably going to be slower than string equality
116         // so probably should have a set of strings
117         // and test each only once
118         //
119         // XXX FIX ME - Time And Optimize
120         //
121         ArrayList<Rule> rules = new ArrayList<Rule>( registeredRules.size() );
122         for ( RegisteredRule rr : registeredRules )
123         {
124             if ( matcher.match( pattern, rr.pattern ) )
125             {
126                 rules.add( rr.rule );
127             }
128         }
129         return rules;
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     public List<Rule> rules()
136     {
137         ArrayList<Rule> rules = new ArrayList<Rule>( registeredRules.size() );
138         for ( RegisteredRule rr : registeredRules )
139         {
140             rules.add( rr.rule );
141         }
142         return rules;
143     }
144 
145     /** Used to associate rules with paths in the rules list */
146     private static class RegisteredRule
147     {
148         String pattern;
149 
150         Rule rule;
151 
152         RegisteredRule( String pattern, Rule rule )
153         {
154             this.pattern = pattern;
155             this.rule = rule;
156         }
157     }
158 
159 }