KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > rowio > RowOutputText


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.rowio;
33
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.math.BigDecimal JavaDoc;
36 import java.sql.Date JavaDoc;
37 import java.sql.Time JavaDoc;
38 import java.sql.Timestamp JavaDoc;
39
40 import org.hsqldb.CachedRow;
41 import org.hsqldb.Trace;
42 import org.hsqldb.Types;
43 import org.hsqldb.lib.StringConverter;
44 import org.hsqldb.persist.TextCache;
45 import org.hsqldb.types.Binary;
46 import org.hsqldb.types.JavaObject;
47
48 /**
49  * Class for writing the data for a database row in text table format.
50  *
51  * @author sqlbob@users (RMP)
52  * @version 1.8.0
53  * @since 1.7.0
54  */

55 public class RowOutputText extends RowOutputBase {
56
57     protected String JavaDoc fieldSep;
58     protected String JavaDoc varSep;
59     protected String JavaDoc longvarSep;
60     private boolean fieldSepEnd;
61     private boolean varSepEnd;
62     private boolean longvarSepEnd;
63     private String JavaDoc nextSep = "";
64     private boolean nextSepEnd;
65     protected boolean allQuoted;
66     private String JavaDoc encoding;
67
68     public RowOutputText(String JavaDoc fieldSep, String JavaDoc varSep, String JavaDoc longvarSep,
69                          boolean allQuoted, String JavaDoc encoding) {
70
71         super();
72
73         initTextDatabaseRowOutput(fieldSep, varSep, longvarSep, allQuoted,
74                                   encoding);
75     }
76
77     private void initTextDatabaseRowOutput(String JavaDoc fieldSep, String JavaDoc varSep,
78                                            String JavaDoc longvarSep,
79                                            boolean allQuoted,
80                                            String JavaDoc encoding) {
81
82         //-- Newline indicates that field should match to end of line.
83
if (fieldSep.endsWith("\n")) {
84             fieldSepEnd = true;
85             fieldSep = fieldSep.substring(0, fieldSep.length() - 1);
86         }
87
88         if (varSep.endsWith("\n")) {
89             varSepEnd = true;
90             varSep = varSep.substring(0, varSep.length() - 1);
91         }
92
93         if (longvarSep.endsWith("\n")) {
94             longvarSepEnd = true;
95             longvarSep = longvarSep.substring(0, longvarSep.length() - 1);
96         }
97
98         this.fieldSep = fieldSep;
99         this.varSep = varSep;
100         this.longvarSep = longvarSep;
101         this.allQuoted = allQuoted;
102         this.encoding = encoding;
103     }
104
105     public void writeEnd() {
106
107         // terminate at the end of row
108
if (nextSepEnd) {
109             writeBytes(nextSep);
110         }
111
112         writeBytes(TextCache.NL);
113     }
114
115     public void writeSize(int size) {
116
117         // initialise at the start of row
118
nextSep = "";
119         nextSepEnd = false;
120     }
121
122     public void writeType(int type) {
123
124         //--do Nothing
125
}
126
127     public void writeString(String JavaDoc s) {
128
129         s = checkConvertString(s, fieldSep);
130
131         // error
132
if (s == null) {
133             return;
134         }
135
136         // writeBytes(s);
137
byte[] bytes = getBytes(s);
138
139         write(bytes, 0, bytes.length);
140
141         nextSep = fieldSep;
142         nextSepEnd = fieldSepEnd;
143     }
144
145     protected void writeVarString(String JavaDoc s) {
146
147         s = checkConvertString(s, varSep);
148
149         if (s == null) {
150             return;
151         }
152
153         // writeBytes(s);
154
byte[] bytes = getBytes(s);
155
156         write(bytes, 0, bytes.length);
157
158         nextSep = varSep;
159         nextSepEnd = varSepEnd;
160     }
161
162     protected void writeLongVarString(String JavaDoc s) {
163
164         s = checkConvertString(s, longvarSep);
165
166         if (s == null) {
167             return;
168         }
169
170         // writeBytes(s);
171
byte[] bytes = getBytes(s);
172
173         write(bytes, 0, bytes.length);
174
175         nextSep = longvarSep;
176         nextSepEnd = longvarSepEnd;
177     }
178
179     protected String JavaDoc checkConvertString(String JavaDoc s, String JavaDoc sep) {
180
181         if (s.indexOf('\n') != -1 || s.indexOf('\r') != -1) {
182             throw new IllegalArgumentException JavaDoc(
183                 Trace.getMessage(Trace.TEXT_STRING_HAS_NEWLINE));
184         } else if (s.indexOf(sep) != -1) {
185             return null;
186         }
187
188         return s;
189     }
190
191     private byte[] getBytes(String JavaDoc s) {
192
193         byte[] bytes = null;
194
195         try {
196             bytes = s.getBytes(encoding);
197         } catch (UnsupportedEncodingException JavaDoc e) {
198             bytes = s.getBytes();
199         }
200
201         return bytes;
202     }
203
204     protected void writeByteArray(byte[] b) {
205
206         ensureRoom(b.length * 2);
207         StringConverter.writeHex(this.getBuffer(), count, b);
208
209         count += b.length * 2;
210     }
211
212     public void writeShortData(short i) {
213         writeIntData(i);
214     }
215
216     public void writeIntData(int i) {
217
218         writeBytes(Integer.toString(i));
219
220         nextSep = fieldSep;
221         nextSepEnd = fieldSepEnd;
222     }
223
224     public void writeIntData(int i, int position) {
225         throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION,
226                                  "RowInputText");
227     }
228
229     public void writeLongData(long i) {
230         throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION,
231                                  "RowInputText");
232     }
233
234 // fredt@users - comment - methods used for writing each SQL type
235
protected void writeFieldType(int type) {
236
237         writeBytes(nextSep);
238
239         switch (type) {
240
241             case Types.VARCHAR :
242             case Types.VARCHAR_IGNORECASE :
243                 nextSep = varSep;
244                 nextSepEnd = varSepEnd;
245                 break;
246
247             case Types.LONGVARCHAR :
248                 nextSep = longvarSep;
249                 nextSepEnd = longvarSepEnd;
250                 break;
251
252             default :
253                 nextSep = fieldSep;
254                 nextSepEnd = fieldSepEnd;
255                 break;
256         }
257     }
258
259     protected void writeNull(int type) {
260         writeFieldType(type);
261     }
262
263     protected void writeChar(String JavaDoc s, int t) {
264
265         switch (t) {
266
267             case Types.CHAR :
268                 writeString(s);
269
270                 return;
271
272             case Types.VARCHAR :
273             case Types.VARCHAR_IGNORECASE :
274                 writeVarString(s);
275
276                 return;
277
278             case Types.LONGVARCHAR :
279             default :
280                 writeLongVarString(s);
281
282                 return;
283         }
284     }
285
286     protected void writeSmallint(Number JavaDoc o) {
287         writeString(o.toString());
288     }
289
290     protected void writeInteger(Number JavaDoc o) {
291         writeString(o.toString());
292     }
293
294     protected void writeBigint(Number JavaDoc o) {
295         writeString(o.toString());
296     }
297
298     protected void writeReal(Double JavaDoc o, int type) {
299         writeString(o.toString());
300     }
301
302     protected void writeDecimal(BigDecimal JavaDoc o) {
303         writeString(o.toString());
304     }
305
306     protected void writeBit(Boolean JavaDoc o) {
307         writeString(o.toString());
308     }
309
310     protected void writeDate(Date JavaDoc o) {
311         writeString(o.toString());
312     }
313
314     protected void writeTime(Time JavaDoc o) {
315         writeString(o.toString());
316     }
317
318     protected void writeTimestamp(Timestamp JavaDoc o) {
319         writeString(o.toString());
320     }
321
322     protected void writeOther(JavaObject o) {
323
324         byte[] ba = o.getBytes();
325
326         writeByteArray(ba);
327     }
328
329     protected void writeBinary(Binary o, int t) {
330         writeByteArray(o.getBytes());
331     }
332
333     public int getSize(CachedRow r) {
334
335         reset();
336
337         try {
338             writeSize(0);
339             writeData(r.getData(), r.getTable());
340             writeEnd();
341         } catch (Exception JavaDoc e) {
342             reset();
343
344 // throw (Trace.error(Trace.FILE_IO_ERROR, e.toString()));
345
}
346
347         int rowsize = size();
348
349         reset();
350
351         return rowsize;
352     }
353 }
354
Popular Tags