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: }
|