1 30 31 32 package org.hsqldb.rowio; 33 34 import java.io.IOException ; 35 import java.math.BigDecimal ; 36 import java.sql.Date ; 37 import java.sql.Time ; 38 import java.sql.Timestamp ; 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 55 public class RowInputText extends RowInputBase implements RowInputInterface { 56 57 private String fieldSep; 59 private String varSep; 60 private String 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 text; 69 protected int line; 70 protected int field; 71 protected int next = 0; 72 protected boolean allQuoted; 73 74 80 public RowInputText(String fieldSep, String varSep, String longvarSep, 81 boolean allQuoted) { 82 83 super(new byte[0]); 84 85 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 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 getField(String sep, int sepLen, 124 boolean isEnd) throws IOException { 125 126 String 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 e) { 157 throw new IOException ( 158 Trace.getMessage( 159 Trace.TextDatabaseRowInput_getField3, true, new Object [] { 160 new Integer (field), e.toString() 161 })); 162 } 163 164 return s; 165 } 166 167 public String readString() throws IOException { 168 return getField(fieldSep, fieldSepLen, fieldSepEnd); 169 } 170 171 private String readVarString() throws IOException { 172 return getField(varSep, varSepLen, varSepEnd); 173 } 174 175 private String readLongVarString() throws IOException { 176 return getField(longvarSep, longvarSepLen, longvarSepEnd); 177 } 178 179 public short readShortData() throws IOException { 180 return (short) readIntData(); 181 } 182 183 public int readIntData() throws IOException { 184 185 String 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 { 201 throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, 202 "RowInputText"); 203 } 204 205 public int readType() throws IOException { 206 return 0; 207 } 208 209 protected boolean checkNull() { 210 211 return false; 213 } 214 215 protected String readChar(int type) throws IOException { 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 readSmallint() throws IOException , HsqlException { 233 234 String 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 readInteger() throws IOException , HsqlException { 250 251 String 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 readBigint() throws IOException , HsqlException { 267 268 String 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 readReal(int type) throws IOException , HsqlException { 284 285 String 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 readDecimal() throws IOException , HsqlException { 301 302 String 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 (s); 315 } 316 317 protected Time readTime() throws IOException , HsqlException { 318 319 String 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 readDate() throws IOException , HsqlException { 335 336 String 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 readTimestamp() throws IOException , HsqlException { 352 353 String 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 readBit() throws IOException , HsqlException { 369 370 String 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 readOther() throws IOException , HsqlException { 387 388 byte[] data; 389 String 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 , HsqlException { 407 408 String 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 |