-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVParse.java
177 lines (156 loc) · 6.28 KB
/
SVParse.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
// Abstract Layer for parsing input data with character separator
public abstract class SVParse extends Parse {
// Constructor & Initializer
// inputFile: path to inputFile
// dataStore: data store to insert into
// separator: character delimiter
public SVParse( String _inputFile, DataStore _dataStore, boolean _noHeader, char _separator ) {
super( _inputFile, _dataStore, _noHeader );
separator = _separator;
}
private char separator = ( char ) 0; // Separator character sequence
// Implementation for parsing character delimited file
public void Parse()
throws StorageException
{
ArrayList<String> heading = null; // CSV headings
boolean header = !noHeader; // expect heading in input
int hlen = 0; // number of headings
ArrayList<Pair<String,String>> keyVals = null; // row of parsed key/val pairs
ArrayList<Pair<String,String>> keys = dataStore.GetKeys();
if ( null == keys )
throw new StorageException( "No schema" );
// no header in input, get headings from the schema
if ( true == noHeader ) {
heading = new ArrayList<String>();
for ( Pair<String,String> key : keys )
heading.add( key.getKey() );
hlen = heading.size();
}
int len = lines.size(); // number of lines
for ( int i = 0; i < len; i++ ) {
// heading
if ( true == header ) {
heading = Split( lines.get( i ), separator );
// store the headings in lowercase, and remove leading/trailing whitespace
hlen = heading.size();
for ( int j = 0; j < hlen; j++ )
heading.set( j, heading.get( j ).trim().toLowerCase() );
header = false;
continue;
}
// Allocate a list of key/value pairs for each row
keyVals = new ArrayList<Pair<String,String>>();
// Spit the line
ArrayList<String> cols = Split( lines.get( i ), separator );
int clen = cols.size();
if ( hlen != keys.size() ) {
throw new StorageException( "Number of columns in header incorrect" );
}
if ( clen != hlen ) {
throw new StorageException( "Number of columns in row incorrect" );
}
for ( int j = 0; j < clen; j++ ) {
String value = cols.get( j );
try {
switch ( keys.get( j ).getValue() ) {
case "string16" : if ( value.length() > 16 )
throw new StorageException( "Value too long for string16 data type: " + value );
break;
case "string32" : if ( value.length() > 32 )
throw new StorageException( "Value too long for string32 data type: " + value );
break;
case "string64" : if ( value.length() > 64 )
throw new StorageException( "Value too long for string64 data type: " + value );
break;
case "string128": if ( value.length() > 128 )
throw new StorageException( "Value too long for string128 data type: " + value );
break;
case "char" : if ( value.length() > 1 )
throw new StorageException( "Value too long for char data type: " + value );
break;
case "short" : if ( value.startsWith( "0x" ) || value.startsWith( "0X") )
value = String.valueOf( Short.parseShort( value.substring( 2 ), 16 ) );
else
Short.parseShort( value );
break;
case "integer" : if ( value.startsWith( "0x" ) || value.startsWith( "0X") )
value = String.valueOf( Integer.parseInt( value.substring( 2 ), 16 ) );
else
Integer.parseInt( value );
break;
case "long" : if ( value.startsWith( "0x" ) || value.startsWith( "0X") )
value = String.valueOf( Long.parseLong( value.substring( 2 ), 16 ) );
else
Long.parseLong( value );
break;
case "float" : Float.parseFloat( value ); break;
case "double" : Double.parseDouble( value ); break;
case "date" : if ( value.length() == 10 ) {
if ( ( value.charAt( 0 ) == '1' || value.charAt( 0 ) == '2' ) &&
( value.charAt( 1 ) >= '0' && value.charAt( 1 ) <= '9' ) &&
( value.charAt( 2 ) >= '0' && value.charAt( 2 ) <= '9' ) &&
( value.charAt( 3 ) >= '0' && value.charAt( 3 ) <= '9' ) &&
value.charAt( 4 ) == '-' &&
( value.charAt( 5 ) == '0' || value.charAt( 5 ) == '1' ) &&
( value.charAt( 6 ) >= '0' && value.charAt( 6 ) <= '9' ) &&
value.charAt( 7 ) == '-' &&
( value.charAt( 8 ) >= '0' && value.charAt( 8 ) <= '9' ) &&
( value.charAt( 9 ) >= '0' && value.charAt( 9 ) <= '9' ) )
break;
}
throw new StorageException( "Incorrect value for date data type: " + value );
case "time" : if ( value.length() == 5 ) {
if ( ( value.charAt( 0 ) == '0' || value.charAt( 0 ) == '1' ) &&
( value.charAt( 1 ) >= '0' && value.charAt( 1 ) <= '9' ) &&
value.charAt( 2 ) == ':' &&
( value.charAt( 3 ) >= '0' && value.charAt( 3 ) <= '9' ) &&
( value.charAt( 4 ) >= '0' && value.charAt( 4 ) <= '9' ) )
break;
}
throw new StorageException( "Incorrect value for time data type: " + value );
}
}
catch ( NumberFormatException e ) {
throw new StorageException( "Incorrect value for data type: " + value );
}
keyVals.add( new Pair<String,String>( heading.get( j ), value ) );
}
// insert row in data store
dataStore.Insert( keyVals );
}
}
// Split a character sequence separated line
// line : line to split
// separator : delimiter
public static ArrayList<String> Split( String line, char separator ) {
ArrayList<String> result = new ArrayList<>();
//if empty, return!
if ( line == null && line.isEmpty() ) {
return result;
}
boolean inQuotes = false;
StringBuffer curVal = new StringBuffer();
char[] chars = line.toCharArray();
for ( char ch : chars ) {
// separator found and not in quotes
if ( ch == separator && !inQuotes ) {
result.add( curVal.toString().trim() );
curVal = new StringBuffer();
}
// quote found
else if ( ch == '"' ) {
inQuotes = !inQuotes;
}
// everything else
else
curVal.append( ch );
}
result.add( curVal.toString().trim() );
return result;
}
}