1 30 31 32 package org.hsqldb.rowio; 33 34 import java.io.IOException ; 35 36 import org.hsqldb.Trace; 37 38 47 public class RowInputTextQuoted extends RowInputText { 48 49 private static final int NORMAL_FIELD = 0; 50 private static final int NEED_END_QUOTE = 1; 51 private static final int FOUND_QUOTE = 2; 52 private char[] qtext; 53 54 public RowInputTextQuoted(String fieldSep, String varSep, 55 String longvarSep, boolean allQuoted) { 56 super(fieldSep, varSep, longvarSep, allQuoted); 57 } 58 59 public void setSource(String text, int pos, int byteSize) { 60 61 super.setSource(text, pos, byteSize); 62 63 qtext = text.toCharArray(); 64 } 65 66 protected String getField(String sep, int sepLen, 67 boolean isEnd) throws IOException { 68 69 String s = null; 71 72 if (next >= qtext.length || qtext[next] != '\"') { 73 return (super.getField(sep, sepLen, isEnd)); 74 } 75 76 try { 77 field++; 78 79 StringBuffer ret = new StringBuffer (); 80 boolean done = false; 81 int state = NORMAL_FIELD; 82 int end = -1; 83 84 if (!isEnd) { 85 end = text.indexOf(sep, next); 86 } 87 88 for (; next < qtext.length; next++) { 89 switch (state) { 90 91 case NORMAL_FIELD : 92 default : 93 if (next == end) { 94 next += sepLen; 95 done = true; 96 } else if (qtext[next] == '\"') { 97 98 state = NEED_END_QUOTE; 100 } else { 101 ret.append(qtext[next]); 102 } 103 break; 104 105 case NEED_END_QUOTE : 106 if (qtext[next] == '\"') { 107 state = FOUND_QUOTE; 108 } else { 109 ret.append(qtext[next]); 110 } 111 break; 112 113 case FOUND_QUOTE : 114 if (qtext[next] == '\"') { 115 116 ret.append(qtext[next]); 118 119 state = NEED_END_QUOTE; 120 } else { 121 next += sepLen - 1; 122 state = NORMAL_FIELD; 123 124 if (!isEnd) { 125 next++; 126 127 done = true; 128 } 129 } 130 break; 131 } 132 133 if (done) { 134 break; 135 } 136 } 137 138 s = ret.toString(); 139 } catch (Exception e) { 140 throw new IOException ( 141 Trace.getMessage( 142 Trace.QuotedTextDatabaseRowInput_getField2, true, 143 new Object [] { 144 new Integer (field), e.toString() 145 })); 146 } 147 148 return s; 149 } 150 } 151 | Popular Tags |