public long extract(File inputFile, File outputFile) throws Exception {
sos.hostware.File inFile = null;
sos.hostware.File outFile = null;
SOSOrderedHashtable outputFields = new SOSOrderedHashtable();
boolean outputFieldNamesWrite = false;
int posTypeBegin = 0;
int posTypeEnd = 0;
String fieldName = "";
long fieldCount = 0;
long recordCount = 0;
long successRecordCount = 0;
long errorRecordCount = 0;
try {
if (this.getOutputFieldDelimiterQuote().equals("\\")) this.setOutputFieldDelimiterQuote(this.getOutputFieldDelimiterQuote() + this.getOutputFieldDelimiterQuote());
inFile = new sos.hostware.File();
if (this.getInputFileType().equals("database")) {
// hostWare file names start with "-"
if (this.getInputFilePath().startsWith("-")) {
inFile.open(this.getInputFilePath());
} else {
inFile.open(this.getInputFilenamePrefix() + this.getInputFilePath());
}
} else if (this.getInputFileType().equals("file")) {
if (inputFile != null && inputFile.getName().startsWith("-")) {
inFile.open(inputFile.getName());
} else if (inputFile != null) {
inFile.open(this.getInputFilenamePrefix() + inputFile.getAbsolutePath());
} else {
throw new Exception("no input file was specified");
}
} else {
throw new Exception("illegal input file type: " + this.getInputFileType());
}
outFile = new sos.hostware.File();
if (this.getOutputFileType().equals("database")) {
// hostWare file names start with "-"
if (this.getOutputFilePath().startsWith("-")) {
outFile.open(this.getOutputFilePath());
} else {
outFile.open(this.getOutputFilenamePrefix() + this.getOutputFilePath());
}
} else if (this.getOutputFileType().equals("file")) {
if (outputFile != null && outputFile.getName().startsWith("-")) {
outFile.open(outputFile.getName());
} else if (outputFile != null) {
outFile.open(this.getOutputFilenamePrefix() + outputFile.getAbsolutePath());
} else {
throw new Exception("no output file was specified");
}
} else {
throw new Exception("unsupported output file type: " + this.getOutputFileType());
}
// optionally add field names to output file
if (this.getOutputFieldNames() != null && this.getOutputFieldNames().length() > 0) {
if (this.getOutputFieldNames().equalsIgnoreCase("true") || this.getOutputFieldNames().equalsIgnoreCase("yes") || this.getOutputFieldNames().equals("1") ) {
outputFieldNamesWrite = true;
} else if (!this.getOutputFieldNames().equalsIgnoreCase("false") && !this.getOutputFieldNames().equalsIgnoreCase("no") && !this.getOutputFieldNames().equals("0") ) {
String[] fields = this.getOutputFieldNames().split(",");
String line = "";
for(int i=0; i<fields.length; i++) {
if (i>0) line += this.getOutputFieldSeparator();
int pos = fields[i].toLowerCase().indexOf(":");
if (pos > -1) {
line += fields[i].substring(0, pos);
} else {
line += fields[i];
}
}
outFile.put_line(line);
}
}
// lookup output fields from the output file specification ...
String outputFieldNames = this.getOutputFilenamePrefix() + this.getOutputFilePath() + " ";
posTypeBegin = outputFieldNames.toLowerCase().indexOf("-type=(");
if (posTypeBegin > 0) {
posTypeEnd = outputFieldNames.indexOf(") ", posTypeBegin);
if (posTypeEnd > 0) {
String[] fields = outputFieldNames.substring(posTypeBegin+7, posTypeEnd).split(",");
int pos = 0;
for(int i=0; i<fields.length; i++) {
pos = fields[i].toLowerCase().indexOf(":");
if (pos > -1) {
outputFields.put(fields[i].substring(0, pos).toLowerCase(), fields[i].substring(pos+1));
} else {
outputFields.put(fields[i].toLowerCase(), "string");
}
}
}
}
// ... should no output fields have been found, then lookup output fields from the input file specification
if (outputFields.isEmpty()) {
String inputFieldNames = this.getInputFilenamePrefix() + this.getInputFilePath() + " ";
posTypeBegin = inputFieldNames.toLowerCase().indexOf("-type=(");
if (posTypeBegin > 0) {
posTypeEnd = inputFieldNames.indexOf(") ", posTypeBegin);
if (posTypeEnd > 0) {
String[] fields = inputFieldNames.substring(posTypeBegin+7, posTypeEnd).split(",");
for(int i=0; i<fields.length; i++) {
int pos = fields[i].indexOf(":");
if (pos > -1) {
outputFields.put(fields[i].substring(0, pos).toLowerCase(), fields[i].substring(pos+1));
} else {
outputFields.put(fields[i].toLowerCase(), "string");
}
}
}
}
}
while (!inFile.eof())
{
Record record = inFile.get();
String line = "";
recordCount++;
fieldCount = 0;
if (outputFieldNamesWrite) {
if (outputFields.isEmpty()) {
for(int i=0; i<record.field_count(); i++) {
if (i>0) line += this.getOutputFieldSeparator();
line += record.field_name(i);
}
} else {
Iterator it = outputFields.iterateKeys();
int itCount = 0;
while(it.hasNext()) {
if (itCount>0) line += this.getOutputFieldSeparator();
line += (String) it.next();
itCount++;
}
}
outFile.put_line(line);
line = "";
outputFieldNamesWrite = false;
}
// should no output fields have been found, then use them from the first ecord
if (outputFields.isEmpty() && recordCount == 1) {
for(int i=0; i<record.field_count(); i++) {
outputFields.put(record.field_name(i).toLowerCase(), "string");
}
}
try {
int outputFieldCount = 0;
Iterator it = outputFields.iterateKeys();
while(it.hasNext()) {
fieldName = (String) it.next();
fieldCount++;
if (record.string(fieldName) != null && record.string(fieldName).length() > 0) {
String fieldType = (String) outputFields.get(fieldName.toLowerCase());
if (fieldType != null && fieldType.toLowerCase().equals("string")) {
if (this.getOutputFieldDelimiterQuote().length() > 0) {
if (outputFieldCount>0) line += this.getOutputFieldSeparator();
line += this.getOutputFieldDelimiter() + record.string(fieldName).replaceAll("\\" + this.getOutputFieldDelimiter(), this.getOutputFieldDelimiterQuote() + this.getOutputFieldDelimiter()) + this.getOutputFieldDelimiter();
outputFieldCount++;