001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.util.Hashtable;
015: import java.util.StringTokenizer;
016: import java.util.Vector;
017:
018: import org.mmbase.util.logging.*;
019:
020: /**
021: * Class for reading and parsing the contents of a CVS (comma value seperated) file.
022: *
023: * @deprecated not used. maybe move to 'tools' application
024: * @author Daniel Ockeloen
025: * @author Pierre van Rooden (javadocs)
026: * @version $Id: CVSReader.java,v 1.13 2007/02/25 17:56:59 nklasens Exp $
027: */
028: public class CVSReader {
029:
030: // logger
031: private static Logger log = Logging
032: .getLoggerInstance(CVSReader.class.getName());
033:
034: /**
035: * The CVS file to read.
036: */
037: String filename;
038: /**
039: * The CVS file header, which contains the column names.
040: * The header is represented by a <code>Hashtable</code> of name-values
041: * where name is a column name and value the index of that column.
042: */
043: protected Hashtable<String, Integer> name2pos;
044: /**
045: * The content of the CVS file body (the records or rows).
046: * Each entry in <code>rows</code> represents a line or record in the CVS body.
047: * Each line is represented by a <code>Vector</code> of values. The position of those
048: * values matches with teh columns from the header.
049: */
050: protected Vector<Vector<String>> rows = new Vector<Vector<String>>();
051:
052: /**
053: * Constructor for the CVS Reader.
054: * @param filename The CVS file to read
055: */
056: public CVSReader(String filename) {
057: readCVS(filename);
058: }
059:
060: /**
061: * Reads the contents of a CVS file and extracts the header and body content.
062: * The body content of the CVS file is stored in the {@link #name2pos} field,
063: * the body content in the {@link #rows} field.
064: */
065: public void readCVS(String filename) {
066: String body = loadFile(filename);
067: StringTokenizer tok = new StringTokenizer(body, "\n\r");
068: if (tok.hasMoreTokens())
069: name2pos = decodeHeader(tok.nextToken());
070: rows = decodeBody(tok);
071: }
072:
073: /**
074: * Parses the body text of a CVS file.
075: * Each row (line of text) in the body is a record whose fields are represented by a list of
076: * komma-separated, possibly quoted, elements.
077: * This routime converted the line into a <code>Vector</code> consisting of these elements.
078: * @param tok A tokenenized list of strings (lines) that make up the body text.
079: * @return a <code>Vector</code> containing, for each line in the CVS body, a list of elements.
080: */
081: Vector<Vector<String>> decodeBody(StringTokenizer mtok) {
082: Vector<Vector<String>> results = new Vector<Vector<String>>();
083:
084: while (mtok.hasMoreTokens()) {
085: String line = mtok.nextToken();
086: Vector<String> results2 = new Vector<String>();
087: StringTokenizer tok = new StringTokenizer(line, ",\"\n\r",
088: true);
089: String prebar = ",";
090: while (tok.hasMoreTokens()) {
091: String bar = tok.nextToken();
092: if (bar.equals("\"")) {
093: String part = tok.nextToken();
094: String part2 = "";
095: while (!part.equals("\"")) {
096: part2 += part;
097: part = tok.nextToken();
098: }
099: results2.addElement(part2);
100: } else {
101: if (bar.equals(",")) {
102: if (prebar.equals(",") || !tok.hasMoreTokens()) {
103: results2.addElement("");
104: }
105: if (!tok.hasMoreTokens()) {
106: results2.addElement("");
107: }
108: } else {
109: results2.addElement(bar);
110: }
111: }
112: prebar = bar;
113: }
114: results.addElement(results2);
115: }
116: return results;
117: }
118:
119: /**
120: * Converts a CVS Header line into a hashtable of header elements.
121: * @param line the headerline to parse (should exists of elements seperated by commas)
122: * @return a <code>Hashtable</code> containing the header values with their
123: * postition in the header
124: */
125: Hashtable<String, Integer> decodeHeader(String line) {
126: int i = 0;
127: Hashtable<String, Integer> results = new Hashtable<String, Integer>();
128: // XXX parsing on /n/r is not needed as a line cannot exist of multiple lines...
129: StringTokenizer tok = new StringTokenizer(line, ",\n\r");
130: while (tok.hasMoreTokens()) {
131: String part = tok.nextToken();
132: part = Strip.DoubleQuote(part, Strip.BOTH);
133: results.put(part, i++);
134: }
135: return results;
136: }
137:
138: /**
139: * Reads the content of a file.
140: * @param filename path and name of the file to read
141: * @return the content of the file as a string
142: */
143: public String loadFile(String filename) {
144: try {
145: File sfile = new File(filename);
146: FileInputStream scan = new FileInputStream(sfile);
147: int filesize = (int) sfile.length();
148: byte[] buffer = new byte[filesize];
149: int len = scan.read(buffer, 0, filesize);
150: if (len != -1) {
151: // XXX: ideally, we should use the preferred encoding,
152: // but this class cannot access MMBase
153: return new String(buffer);
154: }
155: scan.close();
156: } catch (Exception e) {
157: log.error(e);
158: log.error(Logging.stackTrace(e));
159: }
160: return null;
161: }
162:
163: /**
164: * Returns the element at the given row and column.
165: * @param row the element row
166: * @param col the element column
167: * @return the element as a String.
168: */
169: public String getElement(int row, int col) {
170: Vector<String> rw = rows.elementAt(row);
171: String value = rw.elementAt(col);
172: return value;
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 getElement(int row, String colname) {
182: Integer ii = name2pos.get(colname);
183: if (ii != null) {
184: int i = ii.intValue();
185: Vector<String> rw = rows.elementAt(row);
186: String value = 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: }
|