1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 import org.apache.derby.iapi.error.StandardException; 26 27 30 class DateTimeParser 31 { 32 33 private String str; 34 private String trimmedString; 35 private int len; 36 private int fieldStart; 37 private char currentSeparator; 38 39 DateTimeParser( String str) 40 { 41 this.str = str; 42 len = str.length(); 43 } 44 45 59 int parseInt( int maxDigits, boolean truncationAllowed, char[] separator, boolean isFraction) 60 throws StandardException 61 { 62 int number = 0; 63 char c; 64 int digitCount = 0; 65 66 for( ; fieldStart < len; fieldStart++) 67 { 68 c = str.charAt( fieldStart); 69 if( Character.isDigit( c)) 70 { 71 if( digitCount >= maxDigits) 72 throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION); 73 digitCount++; 74 number = number*10 + Character.digit( c, 10); 75 } 76 else 77 break; 78 } 79 if( truncationAllowed ? (digitCount == 0 && !isFraction) : (digitCount != maxDigits)) 80 throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION); 81 82 updateCurrentSeparator(); 83 84 if( separator == null) 85 { 86 if( fieldStart < len) 88 fieldStart++; 89 } 90 else 91 { 92 int sepIdx; 93 for( sepIdx = 0; sepIdx < separator.length; sepIdx++) 94 { 95 if( separator[sepIdx] != 0) 96 { 97 if( currentSeparator == separator[sepIdx]) 98 { 99 fieldStart++; 100 break; 101 } 102 } 103 else 104 { 105 int j; 107 for( j = fieldStart; j < len; j++) 108 { 109 if( str.charAt( j) != ' ') 110 break; 111 } 112 if( j == len) 113 { 114 fieldStart = j; 115 break; 116 } 117 } 118 } 119 if( sepIdx >= separator.length) 120 throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION); 121 } 122 123 if( isFraction) 124 { 125 for(int i = digitCount; i < maxDigits; i++) 126 number *= 10; 127 } 128 return number; 129 } 131 140 int parseChoice( String [] choices) throws StandardException 141 { 142 for( int choiceIdx = 0; choiceIdx < choices.length; choiceIdx++) 143 { 144 String choice = choices[ choiceIdx]; 145 int choiceLen = choice.length(); 146 if( fieldStart + choiceLen <= len) 147 { 148 int i; 149 for( i = 0; i < choiceLen; i++) 150 { 151 if( choice.charAt( i) != str.charAt( fieldStart + i)) 152 break; 153 } 154 if( i == choiceLen) 155 { 156 fieldStart += choiceLen; 157 updateCurrentSeparator(); 158 return choiceIdx; 159 } 160 } 161 } 162 throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION); 163 } 165 private void updateCurrentSeparator() 166 { 167 if( fieldStart >= len) 168 currentSeparator = 0; 169 else 170 { 171 currentSeparator = str.charAt( fieldStart); 172 if( currentSeparator == ' ') 173 { 174 for( int i = fieldStart + 1; i < len; i++) 176 { 177 if( str.charAt( i) != ' ') 178 return; 179 } 180 currentSeparator = 0; 181 fieldStart = len; 182 } 183 } 184 } 186 192 String checkEnd() throws StandardException 193 { 194 int end = fieldStart; 195 for( ; fieldStart < len; fieldStart++) 196 { 197 if( str.charAt( fieldStart) != ' ') 198 throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION); 199 } 200 currentSeparator = 0; 201 while( end > 0 && str.charAt( end - 1) == ' ') 202 end--; 203 trimmedString = (end == len) ? str : str.substring( 0, end); 204 return trimmedString; 205 } 207 213 String getTrimmedString() 214 { 215 return trimmedString; 216 } 217 218 221 char nextSeparator() 222 { 223 for( int i = fieldStart + 1; i < len; i++) 224 { 225 char c = str.charAt( i); 226 if( ! Character.isLetterOrDigit( c)) 227 return c; 228 } 229 return 0; 230 } 231 232 236 char getCurrentSeparator() 237 { 238 return currentSeparator; 239 } 240 } 241 | Popular Tags |