001    package org.LiveGraph.dataFile.read;
002    
003    import java.util.ArrayList;
004    import java.util.Collections;
005    import java.util.List;
006    
007    import com.softnetConsult.utils.collections.ReadOnlyIterator;
008    
009    
010    /**
011     * This tokeniser can split a string into tokens using a separator which is a string
012     * itself. (Note that the standard tokeniser {@code java.util.StringTokenizer} only supports
013     * single characters as separators.) This tokeniser supports empty tokens.
014     * 
015     * <p style="font-size:smaller;">This product includes software developed by the
016     *    <strong>LiveGraph</strong> project and its contributors.<br />
017     *    (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>)<br />
018     *    Copyright (c) 2007 G. Paperin.<br />
019     *    All rights reserved.
020     * </p>
021     * <p style="font-size:smaller;">File: DataLineTokenizer.java</p> 
022     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
023     *    without modification, are permitted provided that the following terms and conditions are met:
024     * </p>
025     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
026     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
027     *    this list of conditions and the following disclaimer.<br />
028     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
029     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
030     *    and the following disclaimer in the documentation and/or other materials provided with
031     *    the distribution.<br />
032     *    3. All advertising materials mentioning features or use of this software or any derived
033     *    software must display the following acknowledgement:<br />
034     *    <em>This product includes software developed by the LiveGraph project and its
035     *    contributors.<br />(http://www.live-graph.org)</em><br />
036     *    4. All advertising materials distributed in form of HTML pages or any other technology
037     *    permitting active hyper-links that mention features or use of this software or any
038     *    derived software must display the acknowledgment specified in condition 3 of this
039     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
040     *    homepage (http://www.live-graph.org).
041     * </p>
042     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
043     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
044     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
045     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
046     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
047     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
048     * </p>
049     * 
050     * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>)
051     * @version {@value org.LiveGraph.LiveGraph#version}
052     */
053    public class DataLineTokenizer implements Iterable<String> {
054    
055    /**
056     * Internal buffer for the tokens.
057     */
058    private List<String> tokens = null;
059    
060    /**
061     * Creates a new tokeniser on the specified string using the specified separator.
062     * 
063     * @param line The string to tokenise.
064     * @param separator The separator to use.
065     */
066    public DataLineTokenizer(String line, String separator) {
067            tokens = new ArrayList<String>();
068            parseLine(line, separator);
069    }
070    
071    /**
072     * Internal routine used for parsing.
073     * 
074     * @param line The string to tokenise.
075     * @param sep The separator to use.
076     */
077    private void parseLine(String line, String sep) {
078            int p1 = 0, p2 = 0;
079            
080            while (p1 <= line.length()) {
081                    p2 = line.indexOf(sep, p1);
082                    if (-1 == p2)
083                            p2 = line.length();
084                    String tok = line.substring(p1, p2);
085                    tokens.add(tok);
086                    p1 = p2 + sep.length();
087            }
088    }
089    
090    /**
091     * Provides a read-only iteration over the tokens of this tokeniser.
092     * 
093     *  @return A read-only iterator over the tokens of this tokeniser
094     */
095    public ReadOnlyIterator<String> iterator() {
096            return new ReadOnlyIterator<String>(tokens.iterator());
097    }
098    
099    /**
100     * Provides this tokeniser's tokens as an unmodifiable list.
101     * 
102     * @return A read-only list of this tokeniser's tokens.
103     */
104    public List<String> getTokens() {
105            return Collections.unmodifiableList(tokens);
106    }
107    
108    }