Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hsqldb/TextDatabaseRowInput.java


1   /* Copyright (c) 2001-2002, 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;
33  
34  import java.io.ByteArrayInputStream;
35  import java.io.IOException;
36  import java.sql.SQLException;
37  import java.sql.Types;
38  
39  /**
40   *  Class for reading the data for a database row in text table format.
41   *
42   * @author sqlbob@users (RMP)
43   * @version 1.7.0
44   */
45  class TextDatabaseRowInput extends org.hsqldb.DatabaseRowInput
46  implements org.hsqldb.DatabaseRowInputInterface {
47  
48      // text table specific
49      private String    fieldSep;
50      private String    varSep;
51      private String    longvarSep;
52      private int       fieldSepLen;
53      private int       varSepLen;
54      private int       longvarSepLen;
55      private boolean   fieldSepEnd;
56      private boolean   varSepEnd;
57      private boolean   longvarSepEnd;
58      private int       textLen;
59      protected String  text;
60      protected int     line;
61      protected int     field;
62      protected int     next = 0;
63      protected boolean emptyIsNull;
64  
65      /**
66       * fredt@users - comment - in future may use a custom subclasse of
67       * InputStream to read the data.
68       *
69       * @author sqlbob@users (RMP)
70       */
71      public TextDatabaseRowInput(String fieldSep, String varSep,
72                                  String longvarSep,
73                                  boolean emptyIsNull) throws IOException {
74  
75          super(new ByteArrayInputStream(new byte[0]));
76  
77          makeSystemId = true;
78  
79          //-- Newline indicates that field should match to end of line.
80          if (fieldSep.endsWith("\n")) {
81              fieldSepEnd = true;
82              fieldSep    = fieldSep.substring(0, fieldSep.length() - 1);
83          }
84  
85          if (varSep.endsWith("\n")) {
86              varSepEnd = true;
87              varSep    = varSep.substring(0, varSep.length() - 1);
88          }
89  
90          if (longvarSep.endsWith("\n")) {
91              longvarSepEnd = true;
92              longvarSep    = longvarSep.substring(0, longvarSep.length() - 1);
93          }
94  
95          this.emptyIsNull = emptyIsNull;
96          this.fieldSep    = fieldSep;
97          this.varSep      = varSep;
98          this.longvarSep  = longvarSep;
99          fieldSepLen      = fieldSep.length();
100         varSepLen        = varSep.length();
101         longvarSepLen    = longvarSep.length();
102     }
103 
104     public void setSource(String text, int pos) {
105 
106         this.text = text.substring(0, text.indexOf('\n'));
107         textLen   = this.text.length();
108         this.pos  = pos;
109         size      = text.length();
110         nextPos   = pos + size;
111         next      = 0;
112 
113         line++;
114 
115         field = 0;
116     }
117 
118     protected String getField(String sep, int sepLen,
119                               boolean isEnd) throws IOException {
120 
121         String s = (emptyIsNull) ? null
122                                  : "";
123 
124         try {
125             int start = next;
126 
127             field++;
128 
129             if (isEnd) {
130                 if ((next >= textLen) && (sepLen > 0)) {
131                     throw (new Exception("No end sep."));
132                 } else if (text.endsWith(sep)) {
133                     next = textLen - sepLen;
134                 } else {
135                     throw (new Exception("No end sep."));
136                 }
137             } else {
138                 next = text.indexOf(sep, start);
139 
140                 if (next == -1) {
141                     next = textLen;
142                 }
143             }
144 
145             s    = text.substring(start, next);
146             next += sepLen;
147 
148             if (emptyIsNull && s.equals("")) {
149                 s = null;
150             }
151         } catch (Exception e) {
152             throw (new IOException("line " + line + ", field " + field + " ("
153                                    + e.getMessage() + ")"));
154         }
155 
156         return (s);
157     }
158 
159     public String readString() throws IOException {
160         return (getField(fieldSep, fieldSepLen, fieldSepEnd));
161     }
162 
163     private String readVarString() throws IOException {
164         return (getField(varSep, varSepLen, varSepEnd));
165     }
166 
167     private String readLongVarString() throws IOException {
168         return (getField(longvarSep, longvarSepLen, longvarSepEnd));
169     }
170 
171     private byte[] readByteArray(String field)
172     throws IOException, SQLException {
173 
174         char         from[] = field.toCharArray();
175         String       hex;
176         StringBuffer to = new StringBuffer();
177 
178         for (int i = 0; i < from.length; i++) {
179             hex = Integer.toHexString(from[i]);
180 
181             if (hex.length() == 1) {
182                 to.append("0");
183             }
184 
185             to.append(hex);
186         }
187 
188         return ByteArray.hexToByteArray(to.toString());
189     }
190 
191     public int readIntData() throws IOException {
192 
193         String s = readString();
194 
195         if (s == null) {
196             return (0);
197         }
198 
199         return (Integer.parseInt(s));
200     }
201 
202     public int readType() throws IOException {
203         return 0;
204     }
205 
206     protected boolean checkNull() {
207 
208         // Return null on each column read instead.
209         return false;
210     }
211 
212     protected String readChar(int type) throws IOException {
213 
214         switch (type) {
215 
216             case Types.CHAR :
217                 return readString();
218 
219             case Types.VARCHAR :
220             case Column.VARCHAR_IGNORECASE :
221                 return readVarString();
222 
223             case Types.LONGVARCHAR :
224             default :
225                 return readLongVarString();
226         }
227     }
228 
229     protected Integer readSmallint() throws IOException, SQLException {
230 
231         String s = readString();
232 
233         if (s == null) {
234             return (null);
235         }
236 
237         return Integer.valueOf(s);
238     }
239 
240     protected Integer readInteger() throws IOException, SQLException {
241 
242         String s = readString();
243 
244         if (s == null) {
245             return (null);
246         }
247 
248         return Integer.valueOf(s);
249     }
250 
251     protected Long readBigint() throws IOException, SQLException {
252 
253         String s = readString();
254 
255         if (s == null) {
256             return (null);
257         }
258 
259         return Long.valueOf(s);
260     }
261 
262     protected Double readReal(int type) throws IOException, SQLException {
263 
264         String s = readString();
265 
266         if (s == null) {
267             return (null);
268         }
269 
270         return Double.valueOf(s);
271     }
272 
273     protected java.math.BigDecimal readDecimal()
274     throws IOException, SQLException {
275 
276         String s = readString();
277 
278         if (s == null) {
279             return (null);
280         }
281 
282         return new java.math.BigDecimal(s);
283     }
284 
285     protected java.sql.Time readTime() throws IOException, SQLException {
286 
287         String s = readString();
288 
289         if (s == null) {
290             return (null);
291         }
292 
293         return java.sql.Time.valueOf(s);
294     }
295 
296     protected java.sql.Date readDate() throws IOException, SQLException {
297 
298         String s = readString();
299 
300         if (s == null) {
301             return (null);
302         }
303 
304         return java.sql.Date.valueOf(s);
305     }
306 
307     protected java.sql.Timestamp readTimestamp()
308     throws IOException, SQLException {
309 
310         String s = readString();
311 
312         if (s == null) {
313             return (null);
314         }
315 
316         return java.sql.Timestamp.valueOf(s);
317     }
318 
319     protected Boolean readBit() throws IOException, SQLException {
320 
321         String s = readString();
322 
323         if (s == null) {
324             return (null);
325         }
326 
327         return Boolean.valueOf(s);
328     }
329 
330     protected Object readOther() throws IOException, SQLException {
331 
332         byte[] o;
333         String s = readLongVarString();
334 
335         if (s == null) {
336             return (null);
337         }
338 
339         o = readByteArray(s);
340 
341         return ByteArray.deserialize(o);
342     }
343 
344     protected byte[] readBinary(int type) throws IOException, SQLException {
345 
346         String s;
347 
348         switch (type) {
349 
350             case Types.BINARY :
351                 s = readString();
352 
353                 if (s == null) {
354                     return (null);
355                 }
356 
357                 return readByteArray(s);
358 
359             case Types.VARBINARY :
360                 s = readVarString();
361 
362                 if (s == null) {
363                     return (null);
364                 }
365 
366                 return readByteArray(s);
367 
368             case Types.LONGVARBINARY :
369             default :
370                 s = readLongVarString();
371 
372                 if (s == null) {
373                     return (null);
374                 }
375 
376                 return readByteArray(s);
377         }
378     }
379 
380     public void setNextPos(int pos) {
381         nextPos = pos;
382     }
383 
384     public void skippedLine() {
385         line++;
386     }
387 
388     public void reset() {
389 
390         text    = "";
391         textLen = 0;
392         pos     = 0;
393         size    = 0;
394         nextPos = 0;
395         next    = 0;
396         field   = 0;
397         line    = 0;
398     }
399 }