1 21 22 package org.apache.derby.impl.load; 23 24 import java.io.PrintStream ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.util.Date ; 28 import java.util.Properties ; 29 30 class ControlInfo 35 { 36 37 static final String ESCAPE = "Escape"; 38 static final String DEFAULT_ESCAPE = "\\"; 39 static final String QUOTE = "Quote"; 40 static final String DEFAULT_QUOTE = "'"; 41 static final String COMMIT_COUNT = "CommitCount"; 42 static final String DEFAULT_COMMIT_COUNT = "0"; 43 static final String START_ROW = "StartRow"; 44 static final String DEFAULT_START_ROW = "1"; 45 static final String STOP_ROW = "StopRow"; 46 static final String DEFAULT_STOP_ROW = "0"; 47 48 static final String FIELD_SEPARATOR = "FieldSeparator"; 49 static final String DEFAULT_FIELD_SEPARATOR = ","; 50 static final String RECORD_SEPARATOR = "RecordSeparator"; 51 static final String DEFAULT_RECORD_SEPARATOR = System.getProperty("line.separator"); 52 static final String COLUMN_DEFINITION = "ColumnDefinition"; 53 static final String DEFAULT_COLUMN_DEFINITION = "FALSE"; 54 static final String NULL_STRING = "Null"; 55 static final String DEFAULT_NULL_STRING = "NULL"; 56 static final String FORMAT = "Format"; 57 static final String DEFAULT_FORMAT = "ASCII_DELIMITED"; 58 static final String DB2_DELIMITED_FORMAT = "DB2_DELIMITED"; static final String FIELD_START_DELIMITER = "FieldStartDelimiter"; 60 static final String DEFAULT_FIELD_START_DELIMITER = "\""; 61 static final String FIELD_END_DELIMITER = "FieldEndDelimiter"; 62 static final String DEFAULT_FIELD_END_DELIMITER = "\""; 63 static final String COLUMN_WIDTHS = "ColumnWidths"; 64 static final String MESSAGE_FILE = "MessageFile"; 65 static final String DEFAULT_VERSION = "1"; 66 static final String VERSION = "Version"; 67 static final String NEWLINE = "\n"; 68 static final String COMMA = ","; 69 static final String SPACE = " "; 70 static final String TAB = "\t"; 71 static final String CR = "\r"; 72 static final String LF = "\n"; 73 static final String CRLF = "\r\n"; 74 static final String LFCR = "\n\r"; 75 static final String FF = "\f"; 76 static final String EMPTY_LINE = "\n\n"; 77 static final String SEMICOLON = ";"; 78 static final String DATA_CODESET = "DataCodeset"; 79 static final String HAS_DELIMETER_AT_END = "HasDelimeterAtEnd"; 80 81 static final String INTERNAL_NONE = "None"; 82 static final String INTERNAL_TRUE = "True"; 83 static final String INTERNAL_FALSE = "False"; 84 static final String INTERNAL_TAB = "Tab"; 85 static final String INTERNAL_SPACE = "Space"; 86 static final String INTERNAL_CR = "CR"; 87 static final String INTERNAL_LF = "LF"; 88 static final String INTERNAL_CRLF = "CR-LF"; 89 static final String INTERNAL_LFCR = "LF-CR"; 90 static final String INTERNAL_COMMA = "Comma"; 91 static final String INTERNAL_SEMICOLON = "Semicolon"; 92 static final String INTERNAL_NEWLINE = "New Line"; 93 static final String INTERNAL_FF = "FF"; 94 static final String INTERNAL_EMPTY_LINE = "Empty line"; 95 96 private Properties currentProperties; 97 98 public ControlInfo() throws Exception { 99 getCurrentProperties(); 100 if (getFieldSeparator().indexOf(getRecordSeparator()) != -1) { 102 throw LoadError.fieldAndRecordSeparatorsSubset(); 103 } 104 } 105 106 String getPropertyValue(String aKey) throws Exception { 108 return getCurrentProperties().getProperty(aKey); 109 } 110 111 private void loadDefaultValues() { 113 currentProperties = new Properties (); 114 currentProperties.put(FIELD_SEPARATOR, DEFAULT_FIELD_SEPARATOR); 115 currentProperties.put(RECORD_SEPARATOR, DEFAULT_RECORD_SEPARATOR); 116 currentProperties.put(COLUMN_DEFINITION, DEFAULT_COLUMN_DEFINITION); 117 currentProperties.put(NULL_STRING, DEFAULT_NULL_STRING); 118 currentProperties.put(FORMAT, DEFAULT_FORMAT); 119 currentProperties.put(FIELD_START_DELIMITER, DEFAULT_FIELD_START_DELIMITER); 120 currentProperties.put(FIELD_END_DELIMITER, DEFAULT_FIELD_END_DELIMITER); 121 currentProperties.put(VERSION, DEFAULT_VERSION); 122 currentProperties.put(HAS_DELIMETER_AT_END, INTERNAL_FALSE); 123 } 124 125 String getCurrentVersion() throws Exception { 127 return(DEFAULT_VERSION); 128 } 129 130 String getFormat() throws Exception { 132 return(getCurrentProperties().getProperty(FORMAT)); 133 } 134 135 int[] getColumnWidths() { 139 return null; 140 } 141 142 String getFieldSeparator() throws Exception { 144 String fieldSeparator = getCurrentProperties().getProperty(FIELD_SEPARATOR); 145 fieldSeparator = mapFromUserFriendlyFieldDelimiters(fieldSeparator); 146 return fieldSeparator; 147 } 148 149 String getFieldStartDelimiter() throws Exception { 150 return(getCurrentProperties().getProperty(FIELD_START_DELIMITER)); 151 } 152 153 String getFieldEndDelimiter() throws Exception { 154 return(getCurrentProperties().getProperty(FIELD_END_DELIMITER)); 155 } 156 157 String getRecordSeparator() throws Exception { 158 String recordSeparator = getCurrentProperties().getProperty(RECORD_SEPARATOR); 159 recordSeparator = mapFromUserFriendlyRecordDelimiters(recordSeparator); 160 return recordSeparator; 161 } 162 163 boolean getHasDelimiterAtEnd() throws Exception { 166 String hasDelimeterAtEnd = getCurrentProperties().getProperty(HAS_DELIMETER_AT_END); 167 return hasDelimeterAtEnd.equals(INTERNAL_TRUE); 168 } 169 String getHasDelimeterAtEndString() throws Exception { 170 String hasDelimeterAtEnd = getCurrentProperties().getProperty(HAS_DELIMETER_AT_END); 171 return hasDelimeterAtEnd; 172 } 173 String getNullString() throws Exception { 178 return(getCurrentProperties().getProperty(NULL_STRING)); 179 } 180 181 String getColumnDefinition() throws Exception { 183 return(getCurrentProperties().getProperty(COLUMN_DEFINITION)); 184 } 185 186 private String mapFromUserFriendlyFieldDelimiters(String aDelimiter) { 187 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_TAB.toUpperCase(java.util.Locale.ENGLISH))) 188 return TAB; 189 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_SPACE.toUpperCase(java.util.Locale.ENGLISH))) 190 return SPACE; 191 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_CR.toUpperCase(java.util.Locale.ENGLISH))) 192 return CR; 193 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_LF.toUpperCase(java.util.Locale.ENGLISH))) 194 return LF; 195 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_CRLF.toUpperCase(java.util.Locale.ENGLISH))) 196 return CRLF; 197 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_LFCR.toUpperCase(java.util.Locale.ENGLISH))) 198 return LFCR; 199 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_COMMA.toUpperCase(java.util.Locale.ENGLISH))) 200 return COMMA; 201 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_SEMICOLON.toUpperCase(java.util.Locale.ENGLISH))) 202 return SEMICOLON; 203 204 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\n", '\n'); 205 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\t", '\t'); 206 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\r", '\r'); 207 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\f", '\f'); 208 return aDelimiter; 209 } 210 211 private String commonToFieldAndRecordDelimiters(String aDelimiter, 216 String specialChars, char replacementChar) { 217 String beforeSpecialChars; 218 String afterSpecialChars; 219 int specialCharsPosition; 220 while (aDelimiter.indexOf(specialChars) != -1) { 221 specialCharsPosition = aDelimiter.indexOf(specialChars); 222 beforeSpecialChars = aDelimiter.substring(0,specialCharsPosition); 223 afterSpecialChars = aDelimiter.substring(specialCharsPosition+2); 224 aDelimiter = beforeSpecialChars + replacementChar + afterSpecialChars; 225 } 226 return aDelimiter; 227 } 228 229 private String mapFromUserFriendlyRecordDelimiters(String aDelimiter) { 230 if (aDelimiter.equals("\n")) 231 aDelimiter = INTERNAL_NEWLINE; 232 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_NEWLINE.toUpperCase(java.util.Locale.ENGLISH))) 233 return NEWLINE; 234 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_CR.toUpperCase(java.util.Locale.ENGLISH))) 235 return CR; 236 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_LF.toUpperCase(java.util.Locale.ENGLISH))) 237 return LF; 238 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_CRLF.toUpperCase(java.util.Locale.ENGLISH))) 239 return CRLF; 240 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_LFCR.toUpperCase(java.util.Locale.ENGLISH))) 241 return LFCR; 242 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_FF.toUpperCase(java.util.Locale.ENGLISH))) 243 return FF; 244 if (aDelimiter.toUpperCase(java.util.Locale.ENGLISH).equals(INTERNAL_EMPTY_LINE.toUpperCase(java.util.Locale.ENGLISH))) 245 return EMPTY_LINE; 246 247 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\n", '\n'); 248 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\t", '\t'); 249 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\r", '\r'); 250 aDelimiter = commonToFieldAndRecordDelimiters(aDelimiter, "\\f", '\f'); 251 return aDelimiter; 252 } 253 254 259 String getDataCodeset() throws Exception { 260 return(getCurrentProperties().getProperty(DATA_CODESET)); 261 } 262 263 264 268 Properties getCurrentProperties() throws Exception { 269 if (currentProperties == null) { 270 loadDefaultValues(); 271 } 272 return currentProperties; 273 } 274 275 276 277 278 280 public void setColumnWidths(String columnWidths) throws Exception { 281 if(columnWidths!=null) 282 currentProperties.setProperty(COLUMN_WIDTHS, columnWidths); 283 } 284 285 286 public void setFieldSeparator(String fieldSeperator) throws Exception { 287 if(fieldSeperator!=null) 288 currentProperties.setProperty(FIELD_SEPARATOR, fieldSeperator); 289 } 290 291 public void setFieldStartDelimiter(String fsdl) throws Exception { 292 if(fsdl!=null) 293 currentProperties.setProperty(FIELD_START_DELIMITER, fsdl); 294 } 295 296 public void setFieldEndDelimiter(String fedl) throws Exception { 297 if(fedl!=null) 298 currentProperties.setProperty(FIELD_END_DELIMITER, fedl); 299 } 300 301 public void setRecordSeparator(String recordSeperator) throws Exception { 302 if(recordSeperator!=null) 303 currentProperties.setProperty(RECORD_SEPARATOR, recordSeperator); 304 } 305 306 public void setHasDelimiterAtEnd(String hasDelimeterAtEnd) throws Exception { 307 if(hasDelimeterAtEnd!=null) 308 currentProperties.setProperty(HAS_DELIMETER_AT_END, hasDelimeterAtEnd); 309 } 310 311 public void setNullString(String nullString) throws Exception { 312 if(nullString!=null) 313 currentProperties.setProperty(NULL_STRING, nullString); 314 } 315 316 public void setcolumnDefinition(String columnDefinition) throws Exception { 318 if(columnDefinition!=null) 319 currentProperties.setProperty(COLUMN_DEFINITION, columnDefinition); 320 } 321 322 323 public void setDataCodeset(String codeset) throws Exception { 324 if(codeset!=null) 325 currentProperties.setProperty(DATA_CODESET, codeset); 326 } 327 328 329 public void setCharacterDelimiter(String charDelimiter) throws Exception { 330 if(charDelimiter !=null) 331 { 332 setFieldStartDelimiter(charDelimiter) ; 333 setFieldEndDelimiter(charDelimiter); 334 } 335 } 336 337 338 339 public void setControlProperties(String characterDelimiter , 340 String columnDelimiter, 341 String codeset) throws Exception 342 { 343 setCharacterDelimiter(characterDelimiter); 344 setFieldSeparator(columnDelimiter); 345 setDataCodeset(codeset); 346 validateDelimiters(); 348 } 349 350 351 private void validateDelimiters() throws Exception 352 { 353 354 355 char colDel = (getFieldSeparator()).charAt(0); 356 char charDel = (getFieldStartDelimiter()).charAt(0); 357 358 if(charDel == '.') 360 { 361 throw LoadError.periodAsCharDelimiterNotAllowed(); 362 } 363 364 365 if(colDel == charDel || 367 colDel == '.' || 368 Character.isSpaceChar(colDel) || 369 Character.isSpaceChar(charDel) 370 ) 371 { 372 throw LoadError.delimitersAreNotMutuallyExclusive(); 373 } 374 375 } 376 } 377 378 | Popular Tags |