-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataSource.java
More file actions
86 lines (82 loc) · 2.76 KB
/
DataSource.java
File metadata and controls
86 lines (82 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Ver Creation_Time Created_By Update_Time Updated_By Description
--- ------------- -------------- ----------- -------------- -------------------------------------------------------
1.0 2019-08-26 xlzhu@ips.com basic class for data source of csv&keyvalue&sqlfile;
1.1 2019-08-29 xlzhu@ips.com redesign java class
*/
import java.io.File;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class DataSource {
private static String REGEX_SEMI = ";\\s*\\r{0,1}\\n{0,1}";
//implement getting csv data function
protected List<String[]> getCSVDataList(String pathFile){
List<String[]> dataListCSV= new ArrayList<String[]>();
String lineFile;
String encodingFile="UTF-8";
String charSplit=",";
try{
File fl = new File(pathFile);
InputStreamReader read = new InputStreamReader(new FileInputStream(fl),encodingFile);
BufferedReader reader=new BufferedReader(read);
while ((lineFile = reader.readLine()) != null) {
String fields[] = lineFile.split(charSplit);
dataListCSV.add(fields);
}
read.close();
} catch (IOException e) {
e.printStackTrace();
}
return dataListCSV;
}
//implement getting config.properties
protected Properties getProps(){
String CONFIG_FILE="config.properties";
Properties prop = new Properties();
try{
prop.load(new FileInputStream(CONFIG_FILE));
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
protected String getPropValue(String pkey){
return getProps().getProperty(pkey);
}
//implement gettting pos of delimiter
protected int getDelimiterStartPos(String pStr,String delimiter){
String REGEX = "(?i)"+delimiter+"\\s*\\r{0,1}\\n{0,1}";
Pattern prm = Pattern.compile(REGEX);
Matcher mrm = prm.matcher(pStr);
//mrm.matches();
if (mrm.find()){
return mrm.start();
}else{
return -1;
}
}
protected int getDelimiterEndPos(String pStr,String delimiter){
String REGEX = "(?i)"+delimiter+"\\s*\\r{0,1}\\n{0,1}";
Pattern prm = Pattern.compile(REGEX);
Matcher mrm = prm.matcher(pStr);
//mrm.matches();
if (mrm.find()){
return mrm.end();
}else{
return -1;
}
}
protected String decorateSpecialChar(String str){
String stmp=str.replaceAll("\\(","\\\\(");
return "(?i)"+stmp.replaceAll("\\)","\\\\)");
}
//please customize this function,maybe visit your data from database or other source
protected abstract List<?> getDataList(String pDataStr,SqlLogging pLogger);
}