Coverage Report - net.sf.jmatchparser.util.ExpectString
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpectString
90%
46/51
62%
10/16
2
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util;
 34  
 
 35  
 import java.util.List;
 36  
 import java.util.regex.Matcher;
 37  
 import java.util.regex.Pattern;
 38  
 
 39  
 /**
 40  
  * A class that can "expect" parts at the start or end of a string, and get/set
 41  
  * the remaining part (rest). In most cases, {@link ExpectReader} is more
 42  
  * flexible, but sometimes this class might be more handy.
 43  
  */
 44  
 public class ExpectString {
 45  
 
 46  
         private String rest;
 47  
         private int offset;
 48  
 
 49  
         /**
 50  
          * Create a new expect string
 51  
          * 
 52  
          * @param rest
 53  
          *            The rest still to be matched
 54  
          */
 55  3
         public ExpectString(String rest) {
 56  3
                 this.rest = rest;
 57  3
                 this.offset = 0;
 58  3
         }
 59  
 
 60  
         // generic methods
 61  
 
 62  
         private void cleanup() {
 63  5
                 if (offset != 0) {
 64  1
                         rest = rest.substring(offset);
 65  1
                         offset = 0;
 66  
                 }
 67  5
         }
 68  
 
 69  
         /**
 70  
          * Return the rest that has not yet been parsed.
 71  
          */
 72  
         public String getRest() {
 73  4
                 return rest.substring(offset);
 74  
         }
 75  
 
 76  
         /**
 77  
          * Change the rest that has not yet been parsed, and return the old rest.
 78  
          */
 79  
         public String changeRest(String newRest) {
 80  1
                 String oldRest = rest.substring(offset);
 81  1
                 rest = newRest;
 82  1
                 offset = 0;
 83  1
                 return oldRest;
 84  
         }
 85  
 
 86  
         /**
 87  
          * Expect some variable text before some fixed string.
 88  
          * 
 89  
          * @param s
 90  
          *            the fixed string
 91  
          * @return the variable text
 92  
          */
 93  
         public String expectStringBeforeString(String s) {
 94  1
                 int pos = rest.indexOf(s, offset);
 95  1
                 if (pos == -1)
 96  0
                         throw new ExpectException(getRest(), "'...'" + s);
 97  1
                 String result = rest.substring(offset, pos);
 98  1
                 offset = pos + s.length();
 99  1
                 return result;
 100  
         }
 101  
 
 102  
         /**
 103  
          * Expect some fixed string followed by some variable text at the end of the
 104  
          * rest.
 105  
          * 
 106  
          * @param s
 107  
          *            the fixed string
 108  
          * @return the variable text
 109  
          */
 110  
         public String expectStringAfterString(String s) {
 111  1
                 cleanup();
 112  1
                 int pos = rest.lastIndexOf(s);
 113  1
                 if (pos == -1)
 114  0
                         throw new ExpectException(rest, s + "'...'");
 115  1
                 String result = rest.substring(pos + s.length());
 116  1
                 rest = rest.substring(0, pos);
 117  1
                 return result;
 118  
         }
 119  
 
 120  
         /**
 121  
          * Expect a fixed string at the beginning.
 122  
          */
 123  
         public void expect(String start) {
 124  2
                 if (!rest.startsWith(start, offset)) {
 125  0
                         throw new ExpectException(getRest(), start);
 126  
                 }
 127  2
                 offset += start.length();
 128  2
         }
 129  
 
 130  
         /**
 131  
          * Expect a fixed string at the end.
 132  
          */
 133  
         public void expectEnd(String end) {
 134  1
                 if (!rest.endsWith(end)) {
 135  0
                         throw new ExpectException(getRest(), end);
 136  
                 }
 137  1
                 rest = rest.substring(offset, rest.length() - end.length());
 138  1
                 offset = 0;
 139  1
         }
 140  
 
 141  
         // regex matches
 142  
 
 143  
         /**
 144  
          * Expect a regex at the begin of the string and return a matcher for it.
 145  
          * The matcher will have an additional group containing the rest.
 146  
          */
 147  
         public Matcher expectRegex(String regex) {
 148  1
                 return expectRegex(regex + "(.*)", -1);
 149  
         }
 150  
 
 151  
         /**
 152  
          * Expect a regex that matches the complete string. The keptGroup will
 153  
          * remain in the rest.
 154  
          */
 155  
         public Matcher expectRegex(String pattern, int keptGroup) {
 156  2
                 return expectRegex(PatternCache.compile(pattern), keptGroup);
 157  
         }
 158  
 
 159  
         /**
 160  
          * Expect a regex that matches the complete string. The keptGroup will
 161  
          * remain in the rest.
 162  
          */
 163  
         public Matcher expectRegex(Pattern pattern, int keptGroup) {
 164  3
                 cleanup();
 165  3
                 Matcher m = pattern.matcher(rest);
 166  3
                 if (!m.matches()) {
 167  0
                         throw ExpectException.fromRegex(rest, pattern);
 168  
                 }
 169  3
                 if (keptGroup == -1)
 170  1
                         keptGroup = m.groupCount();
 171  3
                 rest = m.group(keptGroup);
 172  3
                 return m;
 173  
         }
 174  
 
 175  
         // multi regex match
 176  
 
 177  
         /**
 178  
          * Match the given regex more than once against the rest.
 179  
          * 
 180  
          * @see ExpectReader#ensureMultiRegexMatch(String, Pattern, Pattern, int)
 181  
          */
 182  
         public List<Matcher> expectMultiRegexMatch(String regex, int restGroup) {
 183  1
                 return expectMultiRegexMatch(PatternCache.compile(regex),
 184  
                                                 restGroup);
 185  
         }
 186  
 
 187  
         /**
 188  
          * Match the given regex more than once against the rest.
 189  
          * 
 190  
          * @see ExpectReader#ensureMultiRegexMatch(String, Pattern, Pattern, int)
 191  
          */
 192  
         public List<Matcher> expectMultiRegexMatch(Pattern pattern, int restGroup) {
 193  1
                 cleanup();
 194  1
                 List<Matcher> lm = ExpectReader.ensureMultiRegexMatch(rest, pattern, null, restGroup);
 195  1
                 if (lm.size() > 0) {
 196  1
                         rest = lm.get(lm.size() - 1).group(restGroup);
 197  
                 }
 198  1
                 return lm;
 199  
         }
 200  
 }