Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 157   Methods: 8
NCLOC: 88   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
RegexpMatcher.java 90% 100% 100% 98%
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
 34   
  *  necessary compilers and matchers, and by
 35   
  *  caching compiled patterns.
 36   
  *
 37   
  *  @author Howard Lewis Ship
 38   
  *  @since 3.0
 39   
  *
 40   
  **/
 41   
 
 42   
 public class RegexpMatcher
 43   
 {
 44   
     private PatternCompiler _patternCompiler;
 45   
 
 46   
     private PatternMatcher _matcher;
 47   
 
 48   
     private Map _compiledPatterns = new HashMap();
 49   
 
 50   
     private Map _escapedPatternStrings = new HashMap();
 51   
 
 52  266
     protected Pattern compilePattern(String pattern)
 53   
     {
 54  266
         if (_patternCompiler == null)
 55  126
             _patternCompiler = new Perl5Compiler();
 56   
 
 57  266
         try
 58   
         {
 59  266
             return _patternCompiler.compile(pattern, Perl5Compiler.SINGLELINE_MASK);
 60   
         }
 61   
         catch (MalformedPatternException ex)
 62   
         {
 63  2
             throw new ApplicationRuntimeException(ex);
 64   
         }
 65   
     }
 66   
 
 67  5512
     protected Pattern getCompiledPattern(String pattern)
 68   
     {
 69  5512
         Pattern result = (Pattern) _compiledPatterns.get(pattern);
 70   
 
 71  5512
         if (result == null)
 72   
         {
 73  266
             result = compilePattern(pattern);
 74  264
             _compiledPatterns.put(pattern, result);
 75   
         }
 76   
 
 77  5510
         return result;
 78   
     }
 79   
 
 80   
     /**
 81   
      *  Clears any previously compiled patterns.
 82   
      * 
 83   
      **/
 84   
 
 85  1
     public void clear()
 86   
     {
 87  1
         _compiledPatterns.clear();
 88   
     }
 89   
 
 90  5510
     protected PatternMatcher getPatternMatcher()
 91   
     {
 92  5510
         if (_matcher == null)
 93  124
             _matcher = new Perl5Matcher();
 94   
 
 95  5510
         return _matcher;
 96   
     }
 97   
 
 98  5483
     public boolean matches(String pattern, String input)
 99   
     {
 100  5483
         Pattern compiledPattern = getCompiledPattern(pattern);
 101   
 
 102  5482
         return getPatternMatcher().matches(input, compiledPattern);
 103   
     }
 104   
 
 105  21
     public boolean contains(String pattern, String input)
 106   
     {
 107  21
         Pattern compiledPattern = getCompiledPattern(pattern);
 108   
 
 109  20
         return getPatternMatcher().contains(input, compiledPattern);
 110   
     }
 111   
 
 112  2
     public String getEscapedPatternString(String pattern)
 113   
     {
 114  2
         String result = (String) _escapedPatternStrings.get(pattern);
 115   
 
 116  2
         if (result == null)
 117   
         {
 118  2
             result = Perl5Compiler.quotemeta(pattern);
 119   
 
 120  2
             _escapedPatternStrings.put(pattern, result);
 121   
         }
 122   
 
 123  2
         return result;
 124   
     }
 125   
 
 126   
     /**
 127   
      * Given an input string, finds all matches in an input string for the pattern.
 128   
      * 
 129   
      * @param pattern the regexp pattern for matching
 130   
      * @param input the string to search for matches within
 131   
      * @param subgroup the group (sub-expression) within the pattern to return as a match
 132   
      * @return array (possibly empty) of matching strings
 133   
      * 
 134   
      */
 135  8
     public String[] getMatches(String pattern, String input, int subgroup)
 136   
     {
 137  8
         Pattern compiledPattern = getCompiledPattern(pattern);
 138   
 
 139  8
         PatternMatcher matcher = getPatternMatcher();
 140  8
         PatternMatcherInput matcherInput = new PatternMatcherInput(input);
 141   
 
 142  8
         List matches = new ArrayList();
 143   
 
 144  8
         while (matcher.contains(matcherInput, compiledPattern))
 145   
         {
 146  18
             MatchResult match = matcher.getMatch();
 147   
 
 148  18
             String matchedInput = match.group(subgroup);
 149   
 
 150  18
             matches.add(matchedInput);
 151   
         }
 152   
 
 153  8
         return (String[]) matches.toArray(new String[matches.size()]);
 154   
     }
 155   
 
 156   
 }
 157