001: package org.apache.turbine.util.parser;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.Reader;
023: import java.io.StreamTokenizer;
024: import java.util.List;
025:
026: /**
027: * CSVParser is used to parse a stream with comma-separated values and
028: * generate ParameterParser objects which can be used to
029: * extract the values in the desired type.
030: *
031: * <p>The class extends the abstract class DataStreamParser and implements
032: * initTokenizer with suitable values for CSV files to provide this
033: * functionality.
034: *
035: * <p>The class (indirectly through DataStreamParser) implements the
036: * java.util.Iterator interface for convenience.
037: * This allows simple use in a Velocity template for example:
038: *
039: * <pre>
040: * #foreach ($row in $csvfile)
041: * Name: $row.Name
042: * Description: $row.Description
043: * #end
044: * </pre>
045: *
046: * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
047: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
048: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
049: * @version $Id: CSVParser.java 534527 2007-05-02 16:10:59Z tv $
050: */
051: public class CSVParser extends DataStreamParser {
052: /**
053: * Create a new CSVParser instance. Requires a Reader to read the
054: * comma-separated values from. The column headers must be set
055: * independently either explicitly, or by reading the first line
056: * of the CSV values.
057: *
058: * @param in the input reader.
059: */
060: public CSVParser(Reader in) {
061: super (in, null, null);
062: }
063:
064: /**
065: * Create a new CSVParser instance. Requires a Reader to read the
066: * comma-separated values from, and a list of column names.
067: *
068: * @param in the input reader.
069: * @param columnNames a list of column names.
070: */
071: public CSVParser(Reader in, List columnNames) {
072: super (in, columnNames, null);
073: }
074:
075: /**
076: * Create a new CSVParser instance. Requires a Reader to read the
077: * comma-separated values from, a list of column names and a
078: * character encoding.
079: *
080: * @param in the input reader.
081: * @param columnNames a list of column names.
082: * @param characterEncoding the character encoding of the input.
083: */
084: public CSVParser(Reader in, List columnNames,
085: String characterEncoding) {
086: super (in, columnNames, characterEncoding);
087: }
088:
089: /**
090: * Initialize the StreamTokenizer instance used to read the lines
091: * from the input reader.
092: * It is now only needed to set the fieldSeparator
093: */
094: protected void initTokenizer(StreamTokenizer tokenizer) {
095: // everything is default so let's call super
096: super .initTokenizer(tokenizer);
097: // set the field separator to comma
098: super .setFieldSeparator(',');
099:
100: }
101: }
|