KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException 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.Column;
41 import org.hsqldb.HsqlDateTime;
42 import org.hsqldb.HsqlException;
43 import org.hsqldb.Trace;
44 import org.hsqldb.Types;
45 import org.hsqldb.types.Binary;
46 import org.hsqldb.types.JavaObject;
47
48 /**
49  * Class for reading 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 RowInputText extends RowInputBase implements RowInputInterface {
56
57     // text table specific
58
private String JavaDoc fieldSep;
59     private String JavaDoc varSep;
60     private String JavaDoc longvarSep;
61     private int fieldSepLen;
62     private int varSepLen;
63     private int longvarSepLen;
64     private boolean fieldSepEnd;
65     private boolean varSepEnd;
66     private boolean longvarSepEnd;
67     private int textLen;
68     protected String JavaDoc text;
69     protected int line;
70     protected int field;
71     protected int next = 0;
72     protected boolean allQuoted;
73
74     /**
75      * fredt@users - comment - in future may use a custom subclasse of
76      * InputStream to read the data.
77      *
78      * author: sqlbob@users (RMP)
79      */

80     public RowInputText(String JavaDoc fieldSep, String JavaDoc varSep, String JavaDoc longvarSep,
81                         boolean allQuoted) {
82
83         super(new byte[0]);
84
85         //-- Newline indicates that field should match to end of line.
86
if (fieldSep.endsWith("\n")) {
87             fieldSepEnd = true;
88             fieldSep = fieldSep.substring(0, fieldSep.length() - 1);
89         }
90
91         if (varSep.endsWith("\n")) {
92             varSepEnd = true;
93             varSep = varSep.substring(0, varSep.length() - 1);
94         }
95
96         if (longvarSep.endsWith("\n")) {
97             longvarSepEnd = true;
98             longvarSep = longvarSep.substring(0, longvarSep.length() - 1);
99         }
100
101         this.allQuoted = allQuoted;
102         this.fieldSep = fieldSep;
103         this.varSep = varSep;
104         this.longvarSep = longvarSep;
105         fieldSepLen = fieldSep.length();
106         varSepLen = varSep.length();
107         longvarSepLen = longvarSep.length();
108     }
109
110     public void setSource(String JavaDoc text, int pos, int byteSize) {
111
112         size = byteSize;
113         this.text = text;
114         textLen = text.length();
115         filePos = pos;
116         next = 0;
117
118         line++;
119
120         field = 0;
121     }
122
123     protected String JavaDoc getField(String JavaDoc sep, int sepLen,
124                               boolean isEnd) throws IOException JavaDoc {
125
126         String JavaDoc s = null;
127
128         try {
129             int start = next;
130
131             field++;
132
133             if (isEnd) {
134                 if ((next >= textLen) && (sepLen > 0)) {
135                     throw Trace.error(Trace.TextDatabaseRowInput_getField);
136                 } else if (text.endsWith(sep)) {
137                     next = textLen - sepLen;
138                 } else {
139                     throw Trace.error(Trace.TextDatabaseRowInput_getField2);
140                 }
141             } else {
142                 next = text.indexOf(sep, start);
143
144                 if (next == -1) {
145                     next = textLen;
146                 }
147             }
148
149             s = text.substring(start, next);
150             next += sepLen;
151             s = s.trim();
152
153             if (s.length() == 0) {
154                 s = null;
155             }
156         } catch (Exception JavaDoc e) {
157             throw new IOException JavaDoc(
158                 Trace.getMessage(
159                     Trace.TextDatabaseRowInput_getField3, true, new Object JavaDoc[] {
160                 new Integer JavaDoc(field), e.toString()
161             }));
162         }
163
164         return s;
165     }
166
167     public String JavaDoc readString() throws IOException JavaDoc {
168         return getField(fieldSep, fieldSepLen, fieldSepEnd);
169     }
170
171     private String JavaDoc readVarString() throws IOException JavaDoc {
172         return getField(varSep, varSepLen, varSepEnd);
173     }
174
175     private String JavaDoc readLongVarString() throws IOException JavaDoc {
176         return getField(longvarSep, longvarSepLen, longvarSepEnd);
177     }
178
179     public short readShortData() throws IOException JavaDoc {
180         return (short) readIntData();
181     }
182
183     public int readIntData() throws IOException JavaDoc {
184
185         String JavaDoc s = readString();
186
187         if (s == null) {
188             return 0;
189         }
190
191         s = s.trim();
192
193         if (s.length() == 0) {
194             return 0;
195         }
196
197         return Integer.parseInt(s);
198     }
199
200     public long readLongData() throws IOException JavaDoc {
201         throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION,
202                                  "RowInputText");
203     }
204
205     public int readType() throws IOException JavaDoc {
206         return 0;
207     }
208
209     protected boolean checkNull() {
210
211         // Return null on each column read instead.
212
return false;
213     }
214
215     protected String JavaDoc readChar(int type) throws IOException JavaDoc {
216
217         switch (type) {
218
219             case Types.CHAR :
220                 return readString();
221
222             case Types.VARCHAR :
223             case Types.VARCHAR_IGNORECASE :
224                 return readVarString();
225
226             case Types.LONGVARCHAR :
227             default :
228                 return readLongVarString();
229         }
230     }
231
232     protected Integer JavaDoc readSmallint() throws IOException JavaDoc, HsqlException {
233
234         String JavaDoc s = readString();
235
236         if (s == null) {
237             return null;
238         }
239
240         s = s.trim();
241
242         if (s.length() == 0) {
243             return null;
244         }
245
246         return Integer.valueOf(s);
247     }
248
249     protected Integer JavaDoc readInteger() throws IOException JavaDoc, HsqlException {
250
251         String JavaDoc s = readString();
252
253         if (s == null) {
254             return null;
255         }
256
257         s = s.trim();
258
259         if (s.length() == 0) {
260             return null;
261         }
262
263         return Integer.valueOf(s);
264     }
265
266     protected Long JavaDoc readBigint() throws IOException JavaDoc, HsqlException {
267
268         String JavaDoc s = readString();
269
270         if (s == null) {
271             return null;
272         }
273
274         s = s.trim();
275
276         if (s.length() == 0) {
277             return null;
278         }
279
280         return Long.valueOf(s);
281     }
282
283     protected Double JavaDoc readReal(int type) throws IOException JavaDoc, HsqlException {
284
285         String JavaDoc s = readString();
286
287         if (s == null) {
288             return null;
289         }
290
291         s = s.trim();
292
293         if (s.length() == 0) {
294             return null;
295         }
296
297         return Double.valueOf(s);
298     }
299
300     protected BigDecimal JavaDoc readDecimal() throws IOException JavaDoc, HsqlException {
301
302         String JavaDoc s = readString();
303
304         if (s == null) {
305             return null;
306         }
307
308         s = s.trim();
309
310         if (s.length() == 0) {
311             return null;
312         }
313
314         return new BigDecimal JavaDoc(s);
315     }
316
317     protected Time JavaDoc readTime() throws IOException JavaDoc, HsqlException {
318
319         String JavaDoc s = readString();
320
321         if (s == null) {
322             return null;
323         }
324
325         s = s.trim();
326
327         if (s.length() == 0) {
328             return null;
329         }
330
331         return HsqlDateTime.timeValue(s);
332     }
333
334     protected Date JavaDoc readDate() throws IOException JavaDoc, HsqlException {
335
336         String JavaDoc s = readString();
337
338         if (s == null) {
339             return null;
340         }
341
342         s = s.trim();
343
344         if (s.length() == 0) {
345             return null;
346         }
347
348         return HsqlDateTime.dateValue(s);
349     }
350
351     protected Timestamp JavaDoc readTimestamp() throws IOException JavaDoc, HsqlException {
352
353         String JavaDoc s = readString();
354
355         if (s == null) {
356             return null;
357         }
358
359         s = s.trim();
360
361         if (s.length() == 0) {
362             return null;
363         }
364
365         return HsqlDateTime.timestampValue(s);
366     }
367
368     protected Boolean JavaDoc readBit() throws IOException JavaDoc, HsqlException {
369
370         String JavaDoc s = readString();
371
372         if (s == null) {
373             return null;
374         }
375
376         s = s.trim();
377
378         if (s.length() == 0) {
379             return null;
380         }
381
382         return s.equalsIgnoreCase("TRUE") ? Boolean.TRUE
383                                           : Boolean.FALSE;
384     }
385
386     protected Object JavaDoc readOther() throws IOException JavaDoc, HsqlException {
387
388         byte[] data;
389         String JavaDoc s = readString();
390
391         if (s == null) {
392             return null;
393         }
394
395         s = s.trim();
396
397         if (s.length() == 0) {
398             return null;
399         }
400
401         data = Column.hexToByteArray(s);
402
403         return new JavaObject(data);
404     }
405
406     protected Binary readBinary(int type) throws IOException JavaDoc, HsqlException {
407
408         String JavaDoc s = readString();
409
410         if (s == null) {
411             return null;
412         }
413
414         s = s.trim();
415
416         if (s.length() == 0) {
417             return null;
418         }
419
420         return new Binary(Column.hexToByteArray(s), false);
421     }
422
423     public int getLineNumber() {
424         return line;
425     }
426
427     public void skippedLine() {
428         line++;
429     }
430
431     public void reset() {
432
433         text = "";
434         textLen = 0;
435         filePos = 0;
436         next = 0;
437         field = 0;
438         line = 0;
439     }
440 }
441
Popular Tags