com.viaoa.util.file.LoadDelimitedFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oa-core Show documentation
Show all versions of oa-core Show documentation
Object Automation library
The newest version!
/* Copyright 1999 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.util.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import com.viaoa.util.OAArray;
/**
* Read a text file that has delimited rows.
* @author vincevia
*/
public abstract class LoadDelimitedFile {
/**
* Read text file, where each line uses "\\r\\n", and parse based on delimiter.
* @param file
* @param sep
* @param bQuoted if column data could have quotes around it
* @throws Exception
*/
public void read(File file, String sep, boolean bQuoted) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(file));
for (int i=1;;i++) {
String line = reader.readLine();
if (line == null) break;
String[] ss = parse(line, sep, bQuoted, i);
if (ss != null) process(ss, i);
}
reader.close();
}
// parse line into columns
public String[] parse(String line, String sep, boolean bQuoted, int lineNumber) {
int pos = 0;
String[] flds = new String[0];
StringTokenizer tok = new StringTokenizer(line, sep, true);
boolean bLastWasSep = true;
for ( ;tok.hasMoreTokens(); ) {
String word = tok.nextToken();
if (word.equals(sep)) {
pos++;
if (bLastWasSep) {
flds = Arrays.copyOf(flds, pos);
flds[pos-1] = "";
}
bLastWasSep = true;
}
else {
int x = word.length();
if (bQuoted && x > 2) {
if ( word.charAt(0) == '"' && word.charAt(x-1) == '"') {
word = word.substring(1, x-1);
}
}
flds = Arrays.copyOf(flds, pos+1);
flds[pos] = word;
bLastWasSep = false;
}
}
return flds;
}
public abstract void process(String[] columns, int lineNumber);
public static void main(String[] args) throws Exception {
final ArrayList alTable = new ArrayList();
alTable.add("ZITBAL");
String s = "COLUMN_NAME,TABLE_NAME,TABLE_OWNER,ORDINAL_POSITION,DATA_TYPE,LENGTH,NUMERIC_SCALE,IS_NULLABLE,IS_UPDATABLE,LONG_COMMENT,"+
"HAS_DEFAULT,COLUMN_HEADING,STORAGE,NUMERIC_PRECISION,CCSID,TABLE_SCHEMA,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,"+
"CHARACTER_OCTET_LENGTH,NUMERIC_PRECISION_RADIX,DATETIME_PRECISION,COLUMN_TEXT,SYSTEM_COLUMN_NAME,"+
"SYSTEM_TABLE_NAME,SYSTEM_TABLE_SCHEMA,USER_DEFINED_TYPE_SCHEMA,USER_DEFINED_TYPE_NAME,IS_IDENTITY,IDENTITY_GENERATION,"+
"IDENTITY_START,IDENTITY_INCREMENT,IDENTITY_MINIMUM,IDENTITY_MAXIMUM,IDENTITY_CYCLE,IDENTITY_CACHE,IDENTITY_ORDER,"+
"COLUMN_EXPRESSION,HIDDEN";
/*
[0] column name
[1] table name
[4] dataType
[5] length
7-8 could be for key?
[9] long comment
[11] COLUMN_HEADING
*/
final ArrayList alHeading = new ArrayList();
StringTokenizer tok = new StringTokenizer(s, ",", false);
for ( ;tok.hasMoreTokens(); ) {
String word = tok.nextToken();
alHeading.add(word);
}
LoadDelimitedFile ldf = new LoadDelimitedFile() {
int cnt;
@Override
public void process(String[] columns, int lineNumber) {
if (columns == null) return;
int x = columns.length;
if (x < 13) return;
String s = columns[1];
if (!alTable.contains(s)) return;
System.out.println(lineNumber+"> "+(++cnt)+") "+columns[1]+", "+columns[0]+", "+columns[4]+", "+columns[5]+", "+columns[9]);
/*
int x2 = alHeading.size();
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy