View Javadoc

1   package org.apache.turbine.util.parser;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Reader;
20  import java.io.StreamTokenizer;
21  import java.util.List;
22  
23  /***
24   * CSVParser is used to parse a stream with comma-separated values and
25   * generate ParameterParser objects which can be used to
26   * extract the values in the desired type.
27   *
28   * <p>The class extends the abstract class DataStreamParser and implements
29   * initTokenizer with suitable values for CSV files to provide this
30   * functionality.
31   *
32   * <p>The class (indirectly through DataStreamParser) implements the
33   * java.util.Iterator interface for convenience.
34   * This allows simple use in a Velocity template for example:
35   *
36   * <pre>
37   * #foreach ($row in $csvfile)
38   *   Name: $row.Name
39   *   Description: $row.Description
40   * #end
41   * </pre>
42   *
43   * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
44   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
45   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46   * @version $Id: CSVParser.java 280154 2005-09-11 17:26:30Z henning $
47   */
48  public class CSVParser extends DataStreamParser
49  {
50      /***
51       * Create a new CSVParser instance. Requires a Reader to read the
52       * comma-separated values from. The column headers must be set
53       * independently either explicitly, or by reading the first line
54       * of the CSV values.
55       *
56       * @param in the input reader.
57       */
58      public CSVParser(Reader in)
59      {
60          super(in, null, null);
61      }
62  
63      /***
64       * Create a new CSVParser instance. Requires a Reader to read the
65       * comma-separated values from, and a list of column names.
66       *
67       * @param in the input reader.
68       * @param columnNames a list of column names.
69       */
70      public CSVParser(Reader in, List columnNames)
71      {
72          super(in, columnNames, null);
73      }
74  
75      /***
76       * Create a new CSVParser instance. Requires a Reader to read the
77       * comma-separated values from, a list of column names and a
78       * character encoding.
79       *
80       * @param in the input reader.
81       * @param columnNames a list of column names.
82       * @param characterEncoding the character encoding of the input.
83       */
84      public CSVParser(Reader in, List columnNames, String characterEncoding)
85      {
86          super(in, columnNames, characterEncoding);
87      }
88  
89      /***
90       * Initialize the StreamTokenizer instance used to read the lines
91       * from the input reader.
92       * It is now only needed to set the fieldSeparator
93       */
94      protected void initTokenizer(StreamTokenizer tokenizer)
95      {
96          // everything is default so let's call super
97          super.initTokenizer(tokenizer);
98          // set the field separator to comma
99          super.setFieldSeparator(',');
100 
101     }
102 }