001package net.sf.logdistiller.util;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.text.ParseException;
018
019public class StringCutter
020{
021    private final String src;
022
023    private int index = 0;
024
025    public StringCutter( String src )
026    {
027        this.src = src;
028    }
029
030    public StringCutter( String src, int startIndex )
031    {
032        this.src = src;
033        this.index = startIndex;
034    }
035
036    public String parseTo( String delimiter )
037        throws ParseException
038    {
039        return parseTo( delimiter, true );
040    }
041
042    public String parseTo( String delimiter, boolean failIfNotFound )
043        throws ParseException
044    {
045        int to = src.indexOf( delimiter, index );
046        if ( to < index )
047        {
048            if ( failIfNotFound )
049            {
050                throw new ParseException( "delimiter '" + delimiter + "' not found starting from index " + index
051                                          + " in '" + src + "'", index );
052            }
053            String token = src.substring( index );
054            index = src.length();
055            return token;
056        }
057        String token = src.substring( index, to );
058        index = to + delimiter.length();
059        return token;
060    }
061
062    public String getRemaining()
063    {
064        return src.substring( index );
065    }
066}