Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 184   Methods: 9
NCLOC: 101   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RegexpMatcher.java 91.7% 100% 100% 98.4%
coverage coverage
 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 java.util.ArrayList;
 18    import java.util.HashMap;
 19    import java.util.List;
 20    import java.util.Map;
 21   
 22    import org.apache.hivemind.ApplicationRuntimeException;
 23    import org.apache.oro.text.regex.MalformedPatternException;
 24    import org.apache.oro.text.regex.MatchResult;
 25    import org.apache.oro.text.regex.Pattern;
 26    import org.apache.oro.text.regex.PatternCompiler;
 27    import org.apache.oro.text.regex.PatternMatcher;
 28    import org.apache.oro.text.regex.PatternMatcherInput;
 29    import org.apache.oro.text.regex.Perl5Compiler;
 30    import org.apache.oro.text.regex.Perl5Matcher;
 31   
 32    /**
 33    * Streamlines the interface to ORO by implicitly constructing the necessary compilers and matchers,
 34    * and by caching compiled patterns.
 35    *
 36    * @author Howard Lewis Ship
 37    * @since 3.0
 38    */
 39   
 40    public class RegexpMatcher
 41    {
 42    private PatternCompiler _patternCompiler;
 43   
 44    private PatternMatcher _matcher;
 45   
 46    private Map _compiledPatterns = new HashMap();
 47   
 48    private Map _escapedPatternStrings = new HashMap();
 49   
 50  299 protected Pattern compilePattern(String pattern)
 51    {
 52  299 if (_patternCompiler == null)
 53  132 _patternCompiler = new Perl5Compiler();
 54   
 55  299 try
 56    {
 57  299 return _patternCompiler.compile(pattern, Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
 58    }
 59    catch (MalformedPatternException ex)
 60    {
 61  2 throw new ApplicationRuntimeException(ex);
 62    }
 63    }
 64   
 65  5129 protected Pattern getCompiledPattern(String pattern)
 66    {
 67  5129 Pattern result = (Pattern) _compiledPatterns.get(pattern);
 68   
 69  5129 if (result == null)
 70    {
 71  299 result = compilePattern(pattern);
 72  297 _compiledPatterns.put(pattern, result);
 73    }
 74   
 75  5127 return result;
 76    }
 77   
 78    /**
 79    * Clears any previously compiled patterns.
 80    */
 81   
 82  1 public void clear()
 83    {
 84  1 _compiledPatterns.clear();
 85    }
 86   
 87  5127 protected PatternMatcher getPatternMatcher()
 88    {
 89  5127 if (_matcher == null)
 90  130 _matcher = new Perl5Matcher();
 91   
 92  5127 return _matcher;
 93    }
 94   
 95  5091 public boolean matches(String pattern, String input)
 96    {
 97  5091 Pattern compiledPattern = getCompiledPattern(pattern);
 98   
 99  5090 return getPatternMatcher().matches(input, compiledPattern);
 100    }
 101   
 102  18 public boolean contains(String pattern, String input)
 103    {
 104  18 Pattern compiledPattern = getCompiledPattern(pattern);
 105   
 106  17 return getPatternMatcher().contains(input, compiledPattern);
 107    }
 108   
 109  10 public String getEscapedPatternString(String pattern)
 110    {
 111  10 String result = (String) _escapedPatternStrings.get(pattern);
 112   
 113  10 if (result == null)
 114    {
 115  10 result = Perl5Compiler.quotemeta(pattern);
 116   
 117  10 _escapedPatternStrings.put(pattern, result);
 118    }
 119   
 120  10 return result;
 121    }
 122   
 123    /**
 124    * Given an input string, finds all matches in an input string for the pattern.
 125    *
 126    * @param pattern
 127    * the regexp pattern for matching
 128    * @param input
 129    * the string to search for matches within
 130    * @return array (possibly empty) of matches
 131    * @since 4.0
 132    */
 133  17 public RegexpMatch[] getMatches(String pattern, String input)
 134    {
 135  17 Pattern compiledPattern = getCompiledPattern(pattern);
 136   
 137  17 PatternMatcher matcher = getPatternMatcher();
 138  17 PatternMatcherInput matcherInput = new PatternMatcherInput(input);
 139   
 140  17 List matches = new ArrayList();
 141   
 142  17 while (matcher.contains(matcherInput, compiledPattern))
 143    {
 144  18 MatchResult match = matcher.getMatch();
 145   
 146  18 matches.add(new RegexpMatch(match));
 147    }
 148   
 149  17 return (RegexpMatch[]) matches.toArray(new RegexpMatch[matches.size()]);
 150    }
 151   
 152    /**
 153    * Given an input string, finds all matches in an input string for the pattern.
 154    *
 155    * @param pattern
 156    * the regexp pattern for matching
 157    * @param input
 158    * the string to search for matches within
 159    * @param subgroup
 160    * the group (sub-expression) within the pattern to return as a match
 161    * @return array (possibly empty) of matching strings
 162    */
 163  3 public String[] getMatches(String pattern, String input, int subgroup)
 164    {
 165  3 Pattern compiledPattern = getCompiledPattern(pattern);
 166   
 167  3 PatternMatcher matcher = getPatternMatcher();
 168  3 PatternMatcherInput matcherInput = new PatternMatcherInput(input);
 169   
 170  3 List matches = new ArrayList();
 171   
 172  3 while (matcher.contains(matcherInput, compiledPattern))
 173    {
 174  8 MatchResult match = matcher.getMatch();
 175   
 176  8 String matchedInput = match.group(subgroup);
 177   
 178  8 matches.add(matchedInput);
 179    }
 180   
 181  3 return (String[]) matches.toArray(new String[matches.size()]);
 182    }
 183   
 184    }