Source Code Cross Referenced for DelimParser.java in  » Report » datavision-1.1.0 » jimm » datavision » source » charsep » 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 » Report » datavision 1.1.0 » jimm.datavision.source.charsep 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package jimm.datavision.source.charsep;
002:
003:        import java.io.Reader;
004:        import java.io.IOException;
005:        import java.util.List;
006:        import java.util.ArrayList;
007:
008:        /**
009:         * Parses delimited data. Handles quotes and embedded delimiters.
010:         *
011:         * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
012:         */
013:        public class DelimParser {
014:
015:            public static final int EOF = -1;
016:
017:            protected char delimiter;
018:            protected Reader in;
019:            protected int pushbackChar;
020:
021:            /**
022:             * Constructor, using ',' as the delimiter. The caller must close
023:             * <var>in</var>.
024:             *
025:             * @param in input reader
026:             */
027:            public DelimParser(Reader in) {
028:                this (in, ',');
029:            }
030:
031:            /**
032:             * Constructor. The caller must close <var>in</var>.
033:             *
034:             * @param in input reader
035:             * @param delimiter delimiter character
036:             */
037:            public DelimParser(Reader in, char delimiter) {
038:                this .delimiter = delimiter;
039:                this .in = in;
040:                pushbackChar = EOF;
041:            }
042:
043:            /**
044:             * Returns an array of column data or <code>null</code> if there is no more
045:             * data. Handles delimiters and quotes within the data just as they are
046:             * generated by Excel comma- and tab-separated files.
047:             *
048:             * @return a <code>List</code> of strings; return <code>null</code> if
049:             * there is no more data.
050:             */
051:            public List parse() throws IOException {
052:                ArrayList columns = null;
053:                boolean insideQuotes = false;
054:                int numQuotesSeen = 0;
055:                StringBuffer buf = new StringBuffer();
056:
057:                int charAsInt;
058:                char c;
059:                char prevChar = '\0';
060:                while ((charAsInt = nextChar()) != EOF) {
061:                    c = (char) charAsInt;
062:
063:                    switch (c) {
064:                    case '"': // Quote character
065:                        if (!insideQuotes) { // Start of quoted column
066:                            insideQuotes = true;
067:                            numQuotesSeen = 0;
068:                        } else if (insideQuotes) { // Inside quoted column
069:                            if (numQuotesSeen == 1) { // This is second of doubled quotes
070:                                buf.append(c);
071:                                numQuotesSeen = 0;
072:                            } else
073:                                numQuotesSeen = 1;
074:                        }
075:                        break;
076:                    case '\n': // Linefeed/newline
077:                    case '\r':
078:                        if (insideQuotes) {
079:                            if (numQuotesSeen == 1) { // Closing quote at end of line
080:                                if (columns == null)
081:                                    columns = new ArrayList();
082:                                columns.add(buf.toString());
083:                                return columns;
084:                            } else
085:                                buf.append(c);
086:                        } else { // End of line; return columns
087:                            // Handle DOS line endings
088:                            if (c == '\r') { // Check for following '\n
089:                                charAsInt = nextChar();
090:                                c = (char) charAsInt;
091:                                if (c != '\n') // Eat following '\n' if it exists
092:                                    pushback(charAsInt); // Else put it back
093:                            }
094:
095:                            charAsInt = nextChar();
096:                            c = (char) charAsInt;
097:                            if (columns == null && buf.length() == 0
098:                                    && charAsInt == EOF)
099:                                return null; // Empty line at end of file
100:
101:                            pushback(charAsInt);
102:                            if (columns == null)
103:                                columns = new ArrayList();
104:                            columns.add(buf.toString());
105:                            return columns;
106:                        }
107:                        break;
108:                    default:
109:                        if (c == delimiter) { // Normal delimiter
110:                            if (!insideQuotes) {
111:                                if (columns == null)
112:                                    columns = new ArrayList();
113:                                columns.add(buf.toString());
114:                                buf = new StringBuffer();
115:                            } else { // Inside quoted column
116:                                // Delimiter at end of quoted column data
117:                                if (numQuotesSeen == 1) {
118:                                    insideQuotes = false;
119:                                    if (columns == null)
120:                                        columns = new ArrayList();
121:                                    columns.add(buf.toString());
122:                                    buf = new StringBuffer();
123:                                }
124:                                // Delimiter inside quoted column
125:                                else
126:                                    buf.append(delimiter);
127:                            }
128:                        } else { // Everything else
129:                            numQuotesSeen = 0;
130:                            buf.append(c);
131:                        }
132:                        break;
133:                    }
134:
135:                    prevChar = c;
136:                }
137:
138:                // We've reached EOF
139:                if (columns == null && buf.length() == 0) // Empty line at end of file
140:                    return null;
141:
142:                if (buf.length() > 0 || prevChar == delimiter) {
143:                    if (columns == null)
144:                        columns = new ArrayList();
145:                    columns.add(buf.toString());
146:                }
147:                return columns;
148:            }
149:
150:            protected int nextChar() throws IOException {
151:                if (pushbackChar == EOF)
152:                    return in.read();
153:                else {
154:                    int c = pushbackChar;
155:                    pushbackChar = EOF;
156:                    return c;
157:                }
158:            }
159:
160:            protected void pushback(int charAsInt) {
161:                pushbackChar = charAsInt;
162:            }
163:
164:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.