PDA

View Full Version : CSVReader.js



keithgabryelski
21 Sep 2007, 3:56 PM
[This code is superseded by the AGCSVReader.zip attachment in a later message]

A reader for comma separated lists:


Ext.namespace('Ag', 'Ag.data');

Ag.data.Parser = function(data, config) {
this.data = data;
Ext.apply(this, config);
Ag.data.Parser.superclass.constructor.call(this);
};

// token types

Ext.extend(Ag.data.Parser, Ext.util.Observable, {
CharSet : 'latin1',
EscapeChar : null,
FieldDelim : ',',
Language : 'eng-us',
TextQualifier : '"',
RowDelim : '\n',
DOSKludge : true,

parserState : "Looking",
escapedState : null,
currentPosition : 0,
oneBack : null,

getNextCharacter : function() {
if (this.oneBack != null) {
var c = this.oneBack;
this.oneBack = null;
return c;
}
return this.data[this.currentPosition++] || ""
},

// null = EOF
// -1 = end of line
// or string
ReadToken : function() {
var token = null;
while (true) {
if (this.parserState == "EOF") {
return token;
}
var c = this.getNextCharacter();
if (c.length == 0) {
// end of input, are we in the middle of something?
if (this.parserState == "InQuotedString") {
throw "UnexpectedEndOfInputError: End of input in string";
}
this.parserState = "EOF";
continue;
}
if (token == null) {
token = "";
}
if (this.parserState == "Looking") {
if (c == this.FieldDelim) {
// empty start
return token;
}
if (this.DOSKludge && this.RowDelim == '\n' && c == '\r') {
var cc = this.getNextCharacter();
if (cc == '\n') {
c = cc;
} else {
this.oneBack = cc;
}
}
if (c == this.RowDelim) {
return -1;
}
if (c == this.EscapeChar) {
this.escapedState = "InField";
this.parserState = "InEscape";
continue;
}
if (c == this.TextQualifier) {
this.parserState = "InQuotedString";
continue;
}
this.parserState = "InField";
} else if (this.parserState == "InField") {
if (c == this.FieldDelim) {
// still in a field
return token;
}
if (this.DOSKludge && this.RowDelim == '\n' && c == '\r') {
var cc = this.getNextCharacter();
if (cc == '\n') {
c = cc;
} else {
this.oneBack = cc;
}
}
if (c == this.RowDelim) {
this.parserState = "Looking";
this.oneBack = c;
return token;
}
if (c == this.EscapeChar) {
this.escapedState = "InField";
this.parserState = "InEscape";
continue;
}
if (c == this.TextQualifier) {
this.parserState = "InQuotedString";
continue;
}
} else if (this.parserState == "InEscape") {
this.parserState = this.escapedState;
} else if (this.parserState == "InQuotedString") {
if (c == this.EscapeChar) {
this.escapedState = this.parserState;
this.parserState = "InEscape";
continue;
}
if (c == this.TextQualifier) {
this.parserState = "InField";
continue;
}
}
token += c;
}
}
});

Ag.data.CSVReader = function(meta, recordType, config) {
meta = meta || {};
Ext.apply(this, config);
Ag.data.CSVReader.superclass.constructor.call(this, meta, recordType||meta.fields);
};

Ext.extend(Ag.data.CSVReader, Ext.data.DataReader, {
CharSet : 'latin1',
EscapeChar : null,
FieldDelim : ',',
Language : 'eng-us',
TextQualifier : '"',
RowDelim : '\n',
UseHeader : false,

read : function(data) {
return this.readRecords(data.responseText);
},

readRecords : function(data) {
var records = [];

var parser = new Ag.data.Parser(data, { EscapeChar : this.EscapeChar,
FieldDelim : this.FieldDelim,
TextQualifier : this.TextQualifier,
RowDelim : this.RowDelim
});

var eof = false;
var header = null;
while (!eof) {
var row = [];
while (true) {
token = parser.ReadToken();
if (typeof(token) == "string") {
row.push(token);
continue;
}
if (token == null) {
eof = true;
}
break;
}
if (row.length == 0) {
continue;
}
if (this.UseHeader && header == null) {
header = row;
row_index = [];
var fields = this.recordType.prototype.fields;
for (var i = 0; i < fields.length; ++i) {
var field = fields.items[i];
var header_index = -1;
for (var ii = 0; ii < header.length; ++ii) {
var header_name = header[ii];
var sep = header_name.indexOf(':');
if (sep != -1) {
header_name = header_name.substr(0, sep);
}
if (header_name == field.name) {
header_index = ii;
break;
}
}
if (header_index == -1) {
alert("Can't find field: " + field.name);
throw "Field not found: " + field.name;
}
row_index[i] = header_index;
}

} else {
var record = {};
var fields = this.recordType.prototype.fields;
for (var i = 0; i < fields.length; ++i) {
var field = fields.items[i];
record[field.name] = field.convert(row[row_index[i]]);
}
rr = new this.recordType(record);
records.push(rr);
}
}
return {
success : true,
records : records,
totalRecords : records.length
}
}
});



Pax, Ag

galdaka
22 Sep 2007, 4:27 AM
Live example please!

Thanks in advance,

keithgabryelski
28 Sep 2007, 9:22 AM
A few notes:

I have created an example for the CSVReader/parser. There are a few options that can be set in the script below (starting at: CONFIGUATION AREA).

Note, you can pull the data from a string or from a URL (using HTTPProxy).
Paging is done in the paging memory proxy but it gets is data from RecordReader
which just passes the data passed into PagingMemoryProxy. Why did I do this?
because the records are created once not reg-generated so I could use "remoteSort" efficiently.

The high order bits:
Look at the bottom of jtest.html -- if you have your own CSV (or reasonably similar format), modify these:

var CSVDataFromString = true;
var CSVDataURL = 'ForbesMostPowerfulWomen.csv'

var CSVEscapeChar = '\\'; // the special character used to quote other special characters
var CSVFieldDelim = ','; // the field delimiter
var CSVTextQualifier = '"'; // string delimiter (field and row delimiters ignored inside strings)
var CSVRowDelim = '\n'; // the record delimiter
var DataHasHeader = true; // is there a header line?
var ParserDOSKludge = true; // treat \r\n as \n

You can futz with the column model and record meta type (or set the vars
to null and they will be computed from the CSV header).

The parser is quicker than the previous parser but I haven't tested insanely large sets of data.

Pax, Keith

gcsolaroli
8 Oct 2007, 1:20 PM
Hello Keith,

I would be interested in using your CSV parser code, but I can not find any licence note on it.

What kind of licence/usage are you granting for your code?

Thanks in advance.

Best regards

Giulio Cesare

shivsbhaduri
15 Apr 2008, 4:21 AM
Hi,
I tried using you example with EXT 2.0.2 and EXT 1.1.1 but in both the cases it throws up a error "Ext.grid.Grid is not a constructor" .

Based on other articles in the forum it seems like we need to change it to "Ext.grid.GridPanel" but again that throws up error " Ext.grid.Gridview" has no properties.

I'm very new to ExtJS and the CSV Reader function is very critical for my little project.

I would really appreciate if you could help me with the updated jtest.js file which would work with Ext 2.0.2.

Appreciate your help !!

Thanks.....Shiv

ZeusTheTrueGod
21 Apr 2008, 6:53 AM
Hello Keith,

I would be interested in using your CSV parser code, but I can not find any licence note on it.

What kind of licence/usage are you granting for your code?

Thanks in advance.

Best regards

Giulio Cesare
Looks like this is a code to be simple copy pasted
It's stupid to put such simple code under any license, it' same like


/*
License: GPL
...
*/
function INC(arg)
{
return arg++;
}

I always take any found script, remove it 'license' note and use in my projects

JNason
22 Aug 2008, 8:25 AM
I can get this to work with the option of CSVDataFromString = true; where it reads the datastring but not if it is set to false and trying to read the csv file. Is there another setting I'm missing to make it work with the file?

sunilpca
6 Jan 2009, 6:05 AM
Hi,
I tried using you example with EXT 2.0.2 and EXT 1.1.1 but in both the cases it throws up a error "Ext.grid.Grid is not a constructor" .

Based on other articles in the forum it seems like we need to change it to "Ext.grid.GridPanel" but again that throws up error " Ext.grid.Gridview" has no properties.

I'm very new to ExtJS and the CSV Reader function is very critical for my little project.

I would really appreciate if you could help me with the updated jtest.js file which would work with Ext 2.0.2.

Appreciate your help !!

Thanks.....Shiv


Hi, I am also getting the same above problmes can any body help me.

TobNukem
14 Jan 2009, 3:21 AM
Hi, I am also getting the same above problmes can any body help me.

same problem here.... i would appreciate it if somebody could please help