001: /*
002: * (c) Copyright 2006 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.csv;
028:
029: import org.databene.benerator.IllegalGeneratorStateException;
030: import org.databene.commons.ConversionException;
031: import org.databene.commons.HeavyweightIterator;
032: import org.databene.document.csv.CSVTokenizer;
033: import org.databene.document.csv.CSVTokenType;
034:
035: import java.io.IOException;
036:
037: /**
038: * Iterates through cells of a CSV file.<br/>
039: * <br/>
040: * Created: 26.08.2006 18:52:08
041: * @author Volker Bergmann
042: */
043: public class CSVCellIterator implements HeavyweightIterator<String> {
044:
045: /** The source uri */
046: private String uri;
047:
048: private char separator;
049:
050: /** The tokenizer for CSV file access */
051: private CSVTokenizer tokenizer;
052:
053: // constructors ----------------------------------------------------------------------------------------------------
054:
055: public CSVCellIterator(String uri, char separator)
056: throws IOException {
057: this .uri = uri;
058: this .separator = separator;
059: this .tokenizer = new CSVTokenizer(uri, separator);
060: skipEOLs();
061: }
062:
063: // properties ------------------------------------------------------------------------------------------------------
064:
065: public String getUri() {
066: return uri;
067: }
068:
069: public char getSeparator() {
070: return separator;
071: }
072:
073: // Iterator implementation -----------------------------------------------------------------------------------------
074:
075: public boolean hasNext() {
076: return tokenizer != null;
077: }
078:
079: public String next() {
080: if (tokenizer.ttype == CSVTokenType.EOF)
081: throw new IllegalGeneratorStateException(
082: "Generator has finished");
083: try {
084: String result = tokenizer.cell;
085: skipEOLs();
086: if (tokenizer.ttype == CSVTokenType.EOF)
087: close();
088: return result;
089: } catch (ConversionException e) {
090: throw new RuntimeException(e);
091: }
092: }
093:
094: public void remove() {
095: throw new UnsupportedOperationException(
096: "Operation not supported: remove()");
097: }
098:
099: /**
100: * @see org.databene.benerator.Generator#close()
101: */
102: public void close() {
103: if (tokenizer != null) {
104: tokenizer.close();
105: tokenizer = null;
106: }
107: }
108:
109: // private helpers -------------------------------------------------------------------------------------------------
110:
111: private void skipEOLs() {
112: try {
113: do {
114: tokenizer.next();
115: } while (tokenizer.ttype == CSVTokenType.EOL);
116: } catch (IOException e) {
117: throw new IllegalGeneratorStateException(e);
118: }
119: }
120:
121: }
|