Source Code Cross Referenced for CVSReader.java in  » Database-ORM » MMBase » org » mmbase » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » MMBase » org.mmbase.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.