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   * TSVParser is used to parse a stream with tab-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 TSV 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 $tsvfile)
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   * @version $Id: TSVParser.java 264148 2005-08-29 14:21:04Z henning $
46   */
47  public class TSVParser extends DataStreamParser
48  {
49      /***
50       * Create a new TSVParser instance. Requires a Reader to read the
51       * tab-separated values from. The column headers must be set
52       * independently either explicitly, or by reading the first line
53       * of the TSV values.
54       *
55       * @param in the input reader.
56       */
57      public TSVParser(Reader in)
58      {
59          super(in, null, null);
60      }
61  
62      /***
63       * Create a new TSVParser instance. Requires a Reader to read the
64       * tab-separated values from, and a list of column names.
65       *
66       * @param in the input reader.
67       * @param columnNames a list of column names.
68       */
69      public TSVParser(Reader in, List columnNames)
70      {
71          super(in, columnNames, null);
72      }
73  
74      /***
75       * Create a new TSVParser instance. Requires a Reader to read the
76       * tab-separated values from, a list of column names and a
77       * character encoding.
78       *
79       * @param in the input reader.
80       * @param columnNames a list of column names.
81       * @param characterEncoding the character encoding of the input.
82       */
83      public TSVParser(Reader in, List columnNames, String characterEncoding)
84      {
85          super(in, columnNames, characterEncoding);
86      }
87  
88      /***
89       * Initialize the StreamTokenizer instance used to read the lines
90       * from the input reader.
91       * It is now only needed to set the fieldSeparator
92       */
93      protected void initTokenizer(StreamTokenizer tokenizer)
94      {
95          super.initTokenizer(tokenizer);
96          // set the field separator to tabs.
97          super.setFieldSeparator('\t');
98      }
99  }