KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > load > ControlInfo


1 /*
2
3    Derby - Class org.apache.derby.impl.load.ControlInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.load;
23
24 import java.io.PrintStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 //read the control file properties. If the passed parameter for control file
31
//name is null, assigns default values to the properties. Also, if the control
32
//file has message property in it, it sends the errors to that file by
33
//redirecting system err to that message file
34
class ControlInfo
35 {
36
37   static final String JavaDoc ESCAPE = "Escape";
38   static final String JavaDoc DEFAULT_ESCAPE = "\\";
39   static final String JavaDoc QUOTE = "Quote";
40   static final String JavaDoc DEFAULT_QUOTE = "'";
41   static final String JavaDoc COMMIT_COUNT = "CommitCount";
42   static final String JavaDoc DEFAULT_COMMIT_COUNT = "0";
43   static final String JavaDoc START_ROW = "StartRow";
44   static final String JavaDoc DEFAULT_START_ROW = "1";
45   static final String JavaDoc STOP_ROW = "StopRow";
46   static final String JavaDoc DEFAULT_STOP_ROW = "0";
47
48   static final String JavaDoc FIELD_SEPARATOR = "FieldSeparator";
49   static final String JavaDoc DEFAULT_FIELD_SEPARATOR = ",";
50   static final String JavaDoc RECORD_SEPARATOR = "RecordSeparator";
51   static final String JavaDoc DEFAULT_RECORD_SEPARATOR = System.getProperty("line.separator");
52   static final String JavaDoc COLUMN_DEFINITION = "ColumnDefinition";
53   static final String JavaDoc DEFAULT_COLUMN_DEFINITION = "FALSE";
54   static final String JavaDoc NULL_STRING = "Null";
55   static final String JavaDoc DEFAULT_NULL_STRING = "NULL";
56   static final String JavaDoc FORMAT = "Format";
57   static final String JavaDoc DEFAULT_FORMAT = "ASCII_DELIMITED";
58   static final String JavaDoc DB2_DELIMITED_FORMAT = "DB2_DELIMITED"; //beetle 5007
59
static final String JavaDoc FIELD_START_DELIMITER = "FieldStartDelimiter";
60   static final String JavaDoc DEFAULT_FIELD_START_DELIMITER = "\"";
61   static final String JavaDoc FIELD_END_DELIMITER = "FieldEndDelimiter";
62   static final String JavaDoc DEFAULT_FIELD_END_DELIMITER = "\"";
63   static final String JavaDoc COLUMN_WIDTHS = "ColumnWidths";
64   static final String JavaDoc MESSAGE_FILE = "MessageFile";
65   static final String JavaDoc DEFAULT_VERSION = "1";
66   static final String JavaDoc VERSION = "Version";
67   static final String JavaDoc NEWLINE = "\n";
68   static final String JavaDoc COMMA = ",";
69   static final String JavaDoc SPACE = " ";
70   static final String JavaDoc TAB = "\t";
71   static final String JavaDoc CR = "\r";
72   static final String JavaDoc LF = "\n";
73   static final String JavaDoc CRLF = "\r\n";
74   static final String JavaDoc LFCR = "\n\r";
75   static final String JavaDoc FF = "\f";
76   static final String JavaDoc EMPTY_LINE = "\n\n";
77   static final String JavaDoc SEMICOLON = ";";
78   static final String JavaDoc DATA_CODESET = "DataCodeset";
79   static final String JavaDoc HAS_DELIMETER_AT_END = "HasDelimeterAtEnd";
80
81   static final String JavaDoc INTERNAL_NONE = "None";
82   static final String JavaDoc INTERNAL_TRUE = "True";
83   static final String JavaDoc INTERNAL_FALSE = "False";
84   static final String JavaDoc INTERNAL_TAB = "Tab";
85   static final String JavaDoc INTERNAL_SPACE = "Space";
86   static final String JavaDoc INTERNAL_CR = "CR";
87   static final String JavaDoc INTERNAL_LF = "LF";
88   static final String JavaDoc INTERNAL_CRLF = "CR-LF";
89   static final String JavaDoc INTERNAL_LFCR = "LF-CR";
90   static final String JavaDoc INTERNAL_COMMA = "Comma";
91   static final String JavaDoc INTERNAL_SEMICOLON = "Semicolon";
92   static final String JavaDoc INTERNAL_NEWLINE = "New Line";
93   static final String JavaDoc INTERNAL_FF = "FF";
94   static final String JavaDoc INTERNAL_EMPTY_LINE = "Empty line";
95
96   private Properties JavaDoc currentProperties;
97
98   public ControlInfo() throws Exception JavaDoc {
99     getCurrentProperties();
100     //the field and record separators can't be subset of each other
101
if (getFieldSeparator().indexOf(getRecordSeparator()) != -1) {
102        throw LoadError.fieldAndRecordSeparatorsSubset();
103     }
104   }
105
106   //read the value of a given property
107
String JavaDoc getPropertyValue(String JavaDoc aKey) throws Exception JavaDoc {
108     return getCurrentProperties().getProperty(aKey);
109    }
110
111   //following are the default values for few of the properties
112
private void loadDefaultValues() {
113     currentProperties = new Properties JavaDoc();
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   //get control file version.
126
String JavaDoc getCurrentVersion() throws Exception JavaDoc {
127     return(DEFAULT_VERSION);
128   }
129
130   //2 possible formats: fixed and delimited. default is ASCII_DELIMITED
131
String JavaDoc getFormat() throws Exception JavaDoc {
132     return(getCurrentProperties().getProperty(FORMAT));
133   }
134
135   //read the column widths property which is comma delimited.
136
//In case of fixed format, if column widths are missing, it will
137
//throw an exception
138
int[] getColumnWidths() {
139       return null;
140   }
141
142   //default is DEFAULT_FIELD_SEPARATOR
143
String JavaDoc getFieldSeparator() throws Exception JavaDoc {
144     String JavaDoc fieldSeparator = getCurrentProperties().getProperty(FIELD_SEPARATOR);
145     fieldSeparator = mapFromUserFriendlyFieldDelimiters(fieldSeparator);
146     return fieldSeparator;
147   }
148
149   String JavaDoc getFieldStartDelimiter() throws Exception JavaDoc {
150     return(getCurrentProperties().getProperty(FIELD_START_DELIMITER));
151   }
152
153   String JavaDoc getFieldEndDelimiter() throws Exception JavaDoc {
154     return(getCurrentProperties().getProperty(FIELD_END_DELIMITER));
155   }
156
157   String JavaDoc getRecordSeparator() throws Exception JavaDoc {
158     String JavaDoc recordSeparator = getCurrentProperties().getProperty(RECORD_SEPARATOR);
159     recordSeparator = mapFromUserFriendlyRecordDelimiters(recordSeparator);
160     return recordSeparator;
161   }
162
163   //to be used to cover cases where column delimeters are placed at the end of
164
//each column resulting in an extra delimeter at the end of a row.
165
boolean getHasDelimiterAtEnd() throws Exception JavaDoc {
166     String JavaDoc hasDelimeterAtEnd = getCurrentProperties().getProperty(HAS_DELIMETER_AT_END);
167     return hasDelimeterAtEnd.equals(INTERNAL_TRUE);
168   }
169   String JavaDoc getHasDelimeterAtEndString() throws Exception JavaDoc {
170     String JavaDoc hasDelimeterAtEnd = getCurrentProperties().getProperty(HAS_DELIMETER_AT_END);
171     return hasDelimeterAtEnd;
172   }
173   //if at the time of export, the column has null into it, we will spit
174
//nullString in the output file.
175
//If at the time of import, we see nullString for a column, we will
176
//send null as part of resultSet interface
177
String JavaDoc getNullString() throws Exception JavaDoc {
178     return(getCurrentProperties().getProperty(NULL_STRING));
179   }
180
181   //for fixed format, get column definitions
182
String JavaDoc getColumnDefinition() throws Exception JavaDoc {
183     return(getCurrentProperties().getProperty(COLUMN_DEFINITION));
184   }
185
186   private String JavaDoc mapFromUserFriendlyFieldDelimiters(String JavaDoc 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   //vjbms: when user types \n in vjbms, it comes as 2 characters \ and n
212
//and not just one character '\n' That's the reason for the following
213
//check. I look for "\n" and replace it with '\n'. Same thing for \t
214
// \r and \f
215
private String JavaDoc commonToFieldAndRecordDelimiters(String JavaDoc aDelimiter,
216   String JavaDoc specialChars, char replacementChar) {
217     String JavaDoc beforeSpecialChars;
218     String JavaDoc 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 JavaDoc mapFromUserFriendlyRecordDelimiters(String JavaDoc 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   /**
255    *
256    * @return Code set, can return null for use the default code set.
257    * @throws Exception
258    */

259   String JavaDoc getDataCodeset() throws Exception JavaDoc {
260     return(getCurrentProperties().getProperty(DATA_CODESET));
261   }
262
263
264   /**read the control file properties into a local variable which is used later on
265   *In case there is no control file, read the default values for these properties
266     * @exception Exception if there is an error
267     */

268   Properties JavaDoc getCurrentProperties() throws Exception JavaDoc{
269     if (currentProperties == null) {
270        loadDefaultValues();
271     }
272     return currentProperties;
273   }
274
275
276
277     
278     // Following set routines can be used to change the default properties
279

280     public void setColumnWidths(String JavaDoc columnWidths) throws Exception JavaDoc {
281         if(columnWidths!=null)
282             currentProperties.setProperty(COLUMN_WIDTHS, columnWidths);
283     }
284
285
286     public void setFieldSeparator(String JavaDoc fieldSeperator) throws Exception JavaDoc {
287         if(fieldSeperator!=null)
288             currentProperties.setProperty(FIELD_SEPARATOR, fieldSeperator);
289     }
290
291     public void setFieldStartDelimiter(String JavaDoc fsdl) throws Exception JavaDoc {
292         if(fsdl!=null)
293             currentProperties.setProperty(FIELD_START_DELIMITER, fsdl);
294     }
295
296     public void setFieldEndDelimiter(String JavaDoc fedl) throws Exception JavaDoc {
297         if(fedl!=null)
298             currentProperties.setProperty(FIELD_END_DELIMITER, fedl);
299     }
300
301     public void setRecordSeparator(String JavaDoc recordSeperator) throws Exception JavaDoc {
302         if(recordSeperator!=null)
303             currentProperties.setProperty(RECORD_SEPARATOR, recordSeperator);
304     }
305
306     public void setHasDelimiterAtEnd(String JavaDoc hasDelimeterAtEnd) throws Exception JavaDoc {
307         if(hasDelimeterAtEnd!=null)
308             currentProperties.setProperty(HAS_DELIMETER_AT_END, hasDelimeterAtEnd);
309     }
310   
311     public void setNullString(String JavaDoc nullString) throws Exception JavaDoc {
312         if(nullString!=null)
313             currentProperties.setProperty(NULL_STRING, nullString);
314     }
315
316     //for fixed format, set column definitions
317
public void setcolumnDefinition(String JavaDoc columnDefinition) throws Exception JavaDoc {
318         if(columnDefinition!=null)
319             currentProperties.setProperty(COLUMN_DEFINITION, columnDefinition);
320     }
321
322
323     public void setDataCodeset(String JavaDoc codeset) throws Exception JavaDoc {
324         if(codeset!=null)
325             currentProperties.setProperty(DATA_CODESET, codeset);
326     }
327
328     
329     public void setCharacterDelimiter(String JavaDoc charDelimiter) throws Exception JavaDoc{
330         if(charDelimiter !=null)
331         {
332             setFieldStartDelimiter(charDelimiter) ;
333             setFieldEndDelimiter(charDelimiter);
334         }
335     }
336
337
338     
339     public void setControlProperties(String JavaDoc characterDelimiter ,
340                                      String JavaDoc columnDelimiter,
341                                      String JavaDoc codeset) throws Exception JavaDoc
342     {
343         setCharacterDelimiter(characterDelimiter);
344         setFieldSeparator(columnDelimiter);
345         setDataCodeset(codeset);
346         //check whether the delimiters are valid ones
347
validateDelimiters();
348     }
349
350
351     private void validateDelimiters() throws Exception JavaDoc
352     {
353
354
355         char colDel = (getFieldSeparator()).charAt(0);
356         char charDel = (getFieldStartDelimiter()).charAt(0);
357
358         //The period was specified as a character string delimiter.
359
if(charDel == '.')
360         {
361             throw LoadError.periodAsCharDelimiterNotAllowed();
362         }
363         
364         
365         //A delimiter is not valid or is used more than once.
366
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