-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
csv.cfc
80 lines (71 loc) · 2.78 KB
/
csv.cfc
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
component extends="base"{
any function getFormatObject( string type="DEFAULT" ){
return library().createJavaObject( "org.apache.commons.csv.CSVFormat" )[ JavaCast( "string", arguments.type ) ];
}
boolean function delimiterIsTab( required string delimiter ){
return ArrayFindNoCase( [ "#Chr( 9 )#", "\t", "tab" ], arguments.delimiter );//CF2016 doesn't support [].FindNoCase( needle )
}
any function getFormat( required string delimiter ){
if( arguments.delimiter.Len() )
return getCsvFormatForDelimiter( arguments.delimiter );
var format = getFormatObject( "RFC4180" );
return format.builder()
.setIgnoreSurroundingSpaces( JavaCast( "boolean", true ) )
.build();
}
array function getColumnNames( required boolean firstRowIsHeader, required array data, required numeric maxColumnCount ){
var result = [];
if( arguments.firstRowIsHeader )
var headerRow = arguments.data[ 1 ];
for( var i=1; i <= arguments.maxColumnCount; i++ ){
if( arguments.firstRowIsHeader && !IsNull( headerRow[ i ] ) && headerRow[ i ].Len() ){
result.Append( headerRow[ i ] );
continue;
}
result.Append( "column#i#" );
}
return result;
}
struct function parseFromString( required string csvString, required boolean trim, required any format ){
if( arguments.trim )
arguments.csvString = arguments.csvString.Trim();
try{
var parser = library().createJavaObject( "org.apache.commons.csv.CSVParser" ).parse( csvString, format );
return dataFromParser( parser );
}
finally{
getFileHelper().closeLocalFileOrStream( local, "parser" );
}
}
struct function parseFromFile( required string path, required boolean trim, required any format ){
getFileHelper()
.throwErrorIFfileNotExists( arguments.path )
.throwErrorIFnotCsvOrTextFile( arguments.path );
return parseFromString( FileRead( arguments.path ), arguments.trim, arguments.format );
}
/* Private */
private any function getCsvFormatForDelimiter( required string delimiter ){
if( delimiterIsTab( arguments.delimiter ) )
return getFormatObject( "TDF" );
var format = getFormatObject( "RFC4180" );
return format.builder()
.setDelimiter( arguments.delimiter )
.setIgnoreSurroundingSpaces( JavaCast( "boolean", true ) )//stop spaces between fields causing problems with embedded lines
.build();
}
private struct function dataFromParser( required any parser ){
var result = {
data: []
,maxColumnCount: 0
};
var recordIterator = arguments.parser.iterator();
while( recordIterator.hasNext() ){
var record = recordIterator.next();
result.maxColumnCount = Max( result.maxColumnCount, record.size() );
//ACF can't handle native arrays in QueryNew()
var values = ( library().getIsACF() )? record.toList(): record.values();
ArrayAppend( result.data, values );
}
return result;
}
}