KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > CVSReader


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12
13
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.mmbase.util.logging.*;
21
22 /**
23  * Class for reading and parsing the contents of a CVS (comma value seperated) file.
24  *
25  * @deprecated not used. maybe move to 'tools' application
26  * @author Daniel Ockeloen
27  * @author Pierre van Rooden (javadocs)
28  * @version $Id: CVSReader.java,v 1.11 2005/10/05 10:44:00 michiel Exp $
29  */

30 public class CVSReader {
31
32     // logger
33
private static Logger log = Logging.getLoggerInstance(CVSReader.class.getName());
34
35     /**
36      * The CVS file to read.
37      */

38     String JavaDoc filename;
39     /**
40      * The CVS file header, which contains the column names.
41      * The header is represented by a <code>Hashtable</code> of name-values
42      * where name is a column name and value the index of that column.
43      */

44     protected Hashtable JavaDoc name2pos;
45     /**
46      * The content of the CVS file body (the records or rows).
47      * Each entry in <code>rows</code> represents a line or record in the CVS body.
48      * Each line is represented by a <code>Vector</code> of values. The position of those
49      * values matches with teh columns from the header.
50      */

51     protected Vector JavaDoc rows=new Vector JavaDoc();
52
53     /**
54      * Constructor for the CVS Reader.
55      * @param filename The CVS file to read
56      */

57     public CVSReader(String JavaDoc filename) {
58         readCVS(filename);
59     }
60
61     /**
62      * Reads the contents of a CVS file and extracts the header and body content.
63      * The body content of the CVS file is stored in the {@link #name2pos} field,
64      * the body content in the {@link #rows} field.
65      */

66     public void readCVS(String JavaDoc filename) {
67         String JavaDoc body=loadFile(filename);
68         StringTokenizer JavaDoc tok=new StringTokenizer JavaDoc(body,"\n\r");
69         if (tok.hasMoreTokens()) name2pos=decodeHeader(tok.nextToken());
70         rows=decodeBody(tok);
71     }
72
73     /**
74      * Parses the body text of a CVS file.
75      * Each row (line of text) in the body is a record whose fields are represented by a list of
76      * komma-separated, possibly quoted, elements.
77      * This routime converted the line into a <code>Vector</code> consisting of these elements.
78      * @param tok A tokenenized list of strings (lines) that make up the body text.
79      * @return a <code>Vector</code> containing, for each line in the CVS body, a list of elements.
80      */

81     Vector JavaDoc decodeBody(StringTokenizer JavaDoc mtok) {
82         Vector JavaDoc results=new Vector JavaDoc();
83
84         while (mtok.hasMoreTokens()) {
85             String JavaDoc line=mtok.nextToken();
86             Vector JavaDoc results2=new Vector JavaDoc();
87             StringTokenizer JavaDoc tok=new StringTokenizer JavaDoc(line,",\"\n\r",true);
88             String JavaDoc prebar=",";
89             while (tok.hasMoreTokens()) {
90                 String JavaDoc bar=tok.nextToken();
91                 if (bar.equals("\"")) {
92                     String JavaDoc part=tok.nextToken();
93                     String JavaDoc part2="";
94                     while (!part.equals("\"")) {
95                         part2+=part;
96                         part=tok.nextToken();
97                     }
98                     results2.addElement(part2);
99                 } else {
100                     if (bar.equals(",")) {
101                         if (prebar.equals(",") || !tok.hasMoreTokens()) {
102                             results2.addElement("");
103                         }
104                         if (!tok.hasMoreTokens()) {
105                             results2.addElement("");
106                         }
107                     } else {
108                         results2.addElement(bar);
109                     }
110                 }
111                 prebar=bar;
112             }
113             results.addElement(results2);
114         }
115         return results;
116     }
117
118     /**
119      * Converts a CVS Header line into a hashtable of header elements.
120      * @param line the headerline to parse (should exists of elements seperated by commas)
121      * @return a <code>Hashtable</code> containing the header values with their
122      * postition in the header
123      */

124     Hashtable JavaDoc decodeHeader(String JavaDoc line) {
125         int i=0;
126         Hashtable JavaDoc results=new Hashtable JavaDoc();
127         // XXX parsing on /n/r is not needed as a line cannot exist of multiple lines...
128
StringTokenizer JavaDoc tok=new StringTokenizer JavaDoc(line,",\n\r");
129         while (tok.hasMoreTokens()) {
130             String JavaDoc part=tok.nextToken();
131             part = Strip.DoubleQuote(part,Strip.BOTH);
132             results.put(part,new Integer JavaDoc(i++));
133         }
134         return results;
135     }
136
137     /**
138      * Reads the content of a file.
139      * @param filename path and name of the file to read
140      * @return the content of the file as a string
141      */

142     public String JavaDoc loadFile(String JavaDoc filename) {
143         try {
144             File JavaDoc sfile = new File JavaDoc(filename);
145             FileInputStream JavaDoc scan =new FileInputStream JavaDoc(sfile);
146             int filesize = (int)sfile.length();
147             byte[] buffer=new byte[filesize];
148             int len=scan.read(buffer,0,filesize);
149             if (len!=-1) {
150                 // XXX: ideally, we should use the preferred encoding,
151
// but this class cannot access MMBase
152
return new String JavaDoc(buffer);
153             }
154             scan.close();
155         } catch(Exception JavaDoc e) {
156             log.error(e);
157             log.error(Logging.stackTrace(e));
158         }
159         return null;
160     }
161
162     /**
163      * Returns the element at the given row and column.
164      * @param row the element row
165      * @param col the element column
166      * @return the element as a String.
167      */

168     public String JavaDoc getElement(int row,int col) {
169         Vector JavaDoc rw=(Vector JavaDoc)rows.elementAt(row);
170         String JavaDoc value=(String JavaDoc)rw.elementAt(col);
171         return value;
172     }
173
174
175     /**
176      * Returns the element at the given row and with the given column name.
177      * @param row the element row
178      * @param colname the element columnname
179      * @return the element as a String.
180      */

181     public String JavaDoc getElement(int row,String JavaDoc colname) {
182         Integer JavaDoc ii=(Integer JavaDoc)name2pos.get(colname);
183         if (ii!=null) {
184             int i=ii.intValue();
185             Vector JavaDoc rw=(Vector JavaDoc)rows.elementAt(row);
186             String JavaDoc value=(String JavaDoc)rw.elementAt(i);
187             return value;
188         }
189         return null;
190     }
191
192     /**
193      * Returns the number of rows in the CVS body.
194      */

195     public int size() {
196         return rows.size();
197     }
198 }
199
Popular Tags