Coverage Report - org.apache.tapestry.util.RegexpMatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexpMatcher
0%
0/54
0%
0/8
3.857
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util;
 16  
 
 17  
 import org.apache.commons.pool.KeyedPoolableObjectFactory;
 18  
 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 20  
 import org.apache.oro.text.regex.Perl5Compiler;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.HashMap;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 import java.util.regex.Matcher;
 27  
 import java.util.regex.Pattern;
 28  
 
 29  
 /**
 30  
  * Streamlines the interface to ORO by implicitly constructing the necessary compilers and matchers,
 31  
  * and by caching compiled patterns.
 32  
  * 
 33  
  */
 34  
 
 35  
 public class RegexpMatcher
 36  
 {    
 37  
     private static final long SLEEP_TIME = 1000 * 60 * 4;
 38  
 
 39  
     private static final long EVICT_IDLE_TIME = 1000 * 60 * 60;
 40  
     
 41  0
     private final KeyedPoolableObjectFactory _factory = new RegexpPoolObjectFactory();
 42  
 
 43  
     private final GenericKeyedObjectPool _pool;
 44  
     
 45  0
     private Map _escapedPatternStrings = new HashMap();
 46  
     
 47  
     public RegexpMatcher()
 48  0
     {
 49  0
         _pool = new GenericKeyedObjectPool(_factory);
 50  
         
 51  0
         _pool.setMinEvictableIdleTimeMillis(EVICT_IDLE_TIME);
 52  0
         _pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME);
 53  0
     }
 54  
     
 55  
     /**
 56  
      * Clears any previously compiled patterns.
 57  
      */
 58  
     public void clear()
 59  
     {
 60  0
         _pool.clear();
 61  0
     }
 62  
     
 63  
     public boolean matches(String pattern, String input)
 64  
     {
 65  0
         Pattern compiled = null;
 66  
         
 67  
         try {
 68  
             
 69  0
             compiled = (Pattern)_pool.borrowObject(pattern);
 70  
             
 71  0
             return compiled.matcher(input).matches();
 72  
             
 73  0
         } catch (Exception e) {
 74  
             
 75  0
             throw new ApplicationRuntimeException(e);
 76  
         } finally {
 77  
             
 78  0
             try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { }
 79  
         }
 80  
     }
 81  
     
 82  
     public boolean contains(String pattern, String input)
 83  
     {
 84  0
         Pattern compiled = null;
 85  
         
 86  
         try {
 87  
             
 88  0
             compiled = (Pattern)_pool.borrowObject(pattern);
 89  
             
 90  0
             return compiled.matcher(input).find();
 91  
             
 92  0
         } catch (Exception e) {
 93  
             
 94  0
             throw new ApplicationRuntimeException(e);
 95  
         } finally {
 96  
             
 97  0
             try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { }
 98  
         }
 99  
     }
 100  
 
 101  
     public String getEscapedPatternString(String pattern)
 102  
     {
 103  0
         String result = (String) _escapedPatternStrings.get(pattern);
 104  
         
 105  0
         if (result == null)
 106  
         {
 107  0
             result = Perl5Compiler.quotemeta(pattern);
 108  
 
 109  0
             _escapedPatternStrings.put(pattern, result);
 110  
         }
 111  
         
 112  0
         return result;
 113  
     }
 114  
     
 115  
     /**
 116  
      * Given an input string, finds all matches in an input string for the pattern.
 117  
      * 
 118  
      * @param pattern
 119  
      *            the regexp pattern for matching
 120  
      * @param input
 121  
      *            the string to search for matches within
 122  
      * @return array (possibly empty) of matches
 123  
      * @since 4.0
 124  
      */
 125  
     public RegexpMatch[] getMatches(String pattern, String input)
 126  
     {
 127  0
         Pattern compiled = null;
 128  
         
 129  
         try {
 130  
             
 131  0
             compiled = (Pattern)_pool.borrowObject(pattern);
 132  
 
 133  0
             Matcher matcher = compiled.matcher(input);
 134  0
             List matches = new ArrayList();
 135  
             
 136  0
             while (matcher.find())
 137  
             {
 138  0
                 int length = matcher.groupCount();
 139  0
                 String[] groups = new String[length + 1];
 140  0
                 groups[0] = matcher.group();
 141  
                 
 142  0
                 for (int i=1; i <= length; i++) {
 143  0
                     groups[i] = matcher.group(i);
 144  
                 }
 145  
 
 146  0
                 matches.add(new RegexpMatch(length, groups));
 147  0
             }
 148  
             
 149  0
             return (RegexpMatch[]) matches.toArray(new RegexpMatch[matches.size()]);
 150  
             
 151  0
         } catch (Exception e) {
 152  
             
 153  0
             throw new ApplicationRuntimeException(e);
 154  
         } finally {
 155  
             
 156  0
             try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { }
 157  
         }
 158  
     }
 159  
 
 160  
     /**
 161  
      * Given an input string, finds all matches in an input string for the pattern.
 162  
      * 
 163  
      * @param pattern
 164  
      *            the regexp pattern for matching
 165  
      * @param input
 166  
      *            the string to search for matches within
 167  
      * @param subgroup
 168  
      *            the group (sub-expression) within the pattern to return as a match
 169  
      * @return array (possibly empty) of matching strings
 170  
      */
 171  
     public String[] getMatches(String pattern, String input, int subgroup)
 172  
     {
 173  0
         Pattern compiled = null;
 174  
         
 175  
         try {
 176  
 
 177  0
             compiled = (Pattern)_pool.borrowObject(pattern);
 178  
 
 179  0
             Matcher matcher = compiled.matcher(input);
 180  0
             List matches = new ArrayList();
 181  
             
 182  0
             while (matcher.find())
 183  
             {
 184  0
                 String matchedInput = matcher.group(subgroup);
 185  
                 
 186  0
                 matches.add(matchedInput);
 187  0
             }
 188  
 
 189  0
             return (String[]) matches.toArray(new String[matches.size()]);
 190  
             
 191  0
         } catch (Exception e) {
 192  
 
 193  0
             throw new ApplicationRuntimeException(e);
 194  
         } finally {
 195  
 
 196  0
             try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { }
 197  
         }
 198  
     }
 199  
 
 200  
 }