Source Code Cross Referenced for DataTextWriter.java in  » J2EE » Sofia » com » salmonllc » 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 » J2EE » Sofia » com.salmonllc.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //** Copyright Statement ***************************************************
002:        //The Salmon Open Framework for Internet Applications (SOFIA)
003:        // Copyright (C) 1999 - 2002, Salmon LLC
004:        //
005:        // This program is free software; you can redistribute it and/or
006:        // modify it under the terms of the GNU General Public License version 2
007:        // as published by the Free Software Foundation;
008:        // 
009:        // This program is distributed in the hope that it will be useful,
010:        // but WITHOUT ANY WARRANTY; without even the implied warranty of
011:        // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012:        // GNU General Public License for more details.
013:        // 
014:        // You should have received a copy of the GNU General Public License
015:        // along with this program; if not, write to the Free Software
016:        // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017:        // 
018:        // For more information please visit http://www.salmonllc.com
019:        //** End Copyright Statement ***************************************************
020:        package com.salmonllc.util;
021:
022:        /////////////////////////
023:        //$Archive: /JADE/SourceCode/com/salmonllc/util/DataTextWriter.java $
024:        //$Author: Dan $ 
025:        //$Revision: 7 $ 
026:        //$Modtime: 10/30/02 2:59p $ 
027:        /////////////////////////
028:
029:        import java.util.*;
030:        import java.io.*;
031:
032:        /**
033:         * This object allows you to write a flat text file with data elements separated by Separators and such. 
034:         */
035:        public class DataTextWriter {
036:            private int NumColumns = 0;
037:            private Vector[] Columns;
038:            private String LineBreak = "\r\n";
039:            private String Separator = ",";
040:            private boolean Quoted = false;
041:            private char QuoteCharacter = '\0';
042:            private StringBuffer sbTextBuffer = new StringBuffer();
043:
044:            /**
045:             * Builds a DataTextWriter with each row containing iNumColumns.
046:             * @param iNumCols The number of columns in a row of the DataTextWriter.
047:             */
048:            public DataTextWriter(int iNumCols) {
049:                this (iNumCols, "", "", '\0', false);
050:            }
051:
052:            /**
053:             * Builds a DataTextWriter with each row containing iNumCols and each piece of Data separated by sSeparator.
054:             * @param iNumCols The number of columns in a row of the DataTextWriter.
055:             * @param sSeparator The separator used to separate data columns in a row.
056:             */
057:
058:            public DataTextWriter(int iNumCols, String sSeparator) {
059:                this (iNumCols, sSeparator, "", '\0', false);
060:            }
061:
062:            /**
063:             * Builds a DataTextWriter with each row containing iNumCols and each piece of Data separated by sSeparator 
064:             * and row separated by sLineBreak.
065:             * @param iNumCols The number of columns in a row of the DataTextWriter.
066:             * @param sSeparator The separator used to separate data columns in a row.
067:             * @param sLineBreak The separator used to separate individual rows.
068:             */
069:
070:            public DataTextWriter(int iNumCols, String sSeparator,
071:                    String sLineBreak) {
072:                this (iNumCols, sSeparator, sLineBreak, '\0', false);
073:            }
074:
075:            /**
076:             * Builds a DataTextWriter with each row containing iNumColumns and each piece of Data separated by sSeparator 
077:             * and row separated by sLineBreak and each piece of Data is surrounded by cQuoteChar if bQuoted is true.
078:             * @param iNumCols The number of columns in a row of the DataTextWriter.
079:             * @param sSeparator The separator used to separate data columns in a row.
080:             * @param sLineBreak The separator used to separate individual rows.
081:             * @param cQuoteChar The character used to quote indivividual data columns.
082:             * @param bQuoted The flag to indicate if data columns are to be quoted.
083:             */
084:
085:            public DataTextWriter(int iNumCols, String sSeparator,
086:                    String sLineFeed, char cQuoteChar, boolean bQuoted) {
087:                NumColumns = iNumCols;
088:                Separator = sSeparator;
089:                LineBreak = sLineFeed;
090:                QuoteCharacter = cQuoteChar;
091:                Quoted = bQuoted;
092:                Columns = new Vector[iNumCols];
093:                for (int i = 0; i < iNumCols; i++)
094:                    Columns[i] = new Vector(100, 100);
095:            }
096:
097:            /**
098:             * Adds a piece of data to a specific column in this DataTextWriter. 
099:             * @param iColumn The column which the specified data belongs to.
100:             * @param sData The data for the specified column.
101:             */
102:
103:            public void addData(int iColumn, String sData)
104:                    throws DataTextWriterException {
105:                if (iColumn < 0 || iColumn >= NumColumns)
106:                    throw new DataTextWriterException(
107:                            "Column Index is out of range. Valid Range is (0-"
108:                                    + (NumColumns - 1) + ").");
109:                Columns[iColumn].addElement(sData);
110:                int iCount = Columns[0].size();
111:                for (int i = 1; i < NumColumns; i++)
112:                    if (Columns[i].size() != iCount)
113:                        return;
114:                int i = Columns[iColumn].size() - 1;
115:                for (int j = 0; j < (NumColumns - 1); j++) {
116:                    if (Quoted)
117:                        sbTextBuffer.append(QuoteCharacter
118:                                + (String) Columns[j].elementAt(i)
119:                                + QuoteCharacter + Separator);
120:                    else
121:                        sbTextBuffer.append((String) Columns[j].elementAt(i)
122:                                + Separator);
123:                }
124:                if (Quoted)
125:                    sbTextBuffer.append(QuoteCharacter
126:                            + (String) Columns[NumColumns - 1].elementAt(i)
127:                            + QuoteCharacter + LineBreak);
128:                else
129:                    sbTextBuffer.append((String) Columns[NumColumns - 1]
130:                            .elementAt(i)
131:                            + LineBreak);
132:            }
133:
134:            /**
135:             * This method returns the Data in DataTextWriter in formatted output string.
136:             * @return Formatted Output
137:             */
138:            public String getText() /*throws DataTextWriterException*/{
139:                //	StringBuffer sb=new StringBuffer();
140:                /*	int iCount=Columns[0].size(); 
141:                 for (int i=1;i<NumColumns;i++)
142:                 if (Columns[i].size()!=iCount)
143:                 throw new DataTextWriterException("There is not an equal number data values in each column.");*/
144:                /*	for (int i=0;i<iCount;i++) {
145:                 for (int j=0;j<NumColumns-1;j++) {
146:                 if (Quoted) 
147:                 sb.append(QuoteCharacter+(String)Columns[j].elementAt(i)+QuoteCharacter+Separator);
148:                 else
149:                 sb.append((String)Columns[j].elementAt(i)+Separator);
150:                 }
151:                 if (Quoted) 
152:                 sb.append(QuoteCharacter+(String)Columns[NumColumns-1].elementAt(i)+QuoteCharacter+LineBreak);
153:                 else
154:                 sb.append((String)Columns[NumColumns-1].elementAt(i)+LineBreak);
155:                 } */
156:                return sbTextBuffer.toString();
157:            }
158:
159:            /**
160:             * This method returns the length of the Data in DataTextWriter in formatted output string.
161:             * @return Formatted Output
162:             */
163:            public int getTextLength() {
164:                return sbTextBuffer.length();
165:            }
166:
167:            /**
168:             * Sets the LineBreak Separator in this DataTextWriter. 
169:             * @param sLineBreak The separator used to separate individual rows.
170:             */
171:            public void setLineBreak(String sLineBreak) {
172:                LineBreak = sLineBreak;
173:            }
174:
175:            /**
176:             * Sets the Character to be used for Quoting Data Columns in this DataTextWriter. 
177:             * @param cQuoteChar The character used to quote individual data columns.
178:             */
179:            public void setQuoteCharacter(char cQuoteChar) {
180:                QuoteCharacter = cQuoteChar;
181:            }
182:
183:            /**
184:             * Sets the Flag to to inicate that Data Columns are to be quoted in this DataTextWriter. 
185:             * @param bQuoted The flag used to indicate if data columns are quoted.
186:             */
187:            public void setQuoted(boolean bQuoted) {
188:                Quoted = bQuoted;
189:            }
190:
191:            /**
192:             * Sets the Separator to be used for separating individual Data Columns in this DataTextWriter. 
193:             * @param sSeparator The separator used to separate individual data columns.
194:             */
195:            public void setSeparator(String sSeparator) {
196:                Separator = sSeparator;
197:            }
198:
199:            /**
200:             * This method creates a flat file containing the data added to this DataTextWriter.
201:             * @param sFilename The name of flat file to create.
202:             */
203:            public void writeFile(String sFilename)
204:                    throws DataTextWriterException {
205:                try {
206:                    FileOutputStream fos = new FileOutputStream(sFilename);
207:                    PrintWriter pw = new PrintWriter(fos);
208:                    pw.print(getText());
209:                    pw.close();
210:                    fos.close();
211:                } catch (Exception e) {
212:                    throw new DataTextWriterException(e.getMessage());
213:                }
214:            }
215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.