View Javadoc
1   package net.sf.logdistiller.util;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.text.ParseException;
18  
19  public class StringCutter
20  {
21      private final String src;
22  
23      private int index = 0;
24  
25      public StringCutter( String src )
26      {
27          this.src = src;
28      }
29  
30      public StringCutter( String src, int startIndex )
31      {
32          this.src = src;
33          this.index = startIndex;
34      }
35  
36      public String parseTo( String delimiter )
37          throws ParseException
38      {
39          return parseTo( delimiter, true );
40      }
41  
42      public String parseTo( String delimiter, boolean failIfNotFound )
43          throws ParseException
44      {
45          int to = src.indexOf( delimiter, index );
46          if ( to < index )
47          {
48              if ( failIfNotFound )
49              {
50                  throw new ParseException( "delimiter '" + delimiter + "' not found starting from index " + index
51                                            + " in '" + src + "'", index );
52              }
53              String token = src.substring( index );
54              index = src.length();
55              return token;
56          }
57          String token = src.substring( index, to );
58          index = to + delimiter.length();
59          return token;
60      }
61  
62      public String getRemaining()
63      {
64          return src.substring( index );
65      }
66  }