1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.data.CsvStore");
|
|
|
14 |
dojo.require("dojo.data.core.RemoteStore");
|
|
|
15 |
dojo.require("dojo.lang.assert");
|
|
|
16 |
dojo.declare("dojo.data.CsvStore", dojo.data.core.RemoteStore, {_setupQueryRequest:function (result, requestKw) {
|
|
|
17 |
var serverQueryUrl = this._serverQueryUrl ? this._serverQueryUrl : "";
|
|
|
18 |
var queryUrl = result.query ? result.query : "";
|
|
|
19 |
requestKw.url = serverQueryUrl + queryUrl;
|
|
|
20 |
requestKw.method = "get";
|
|
|
21 |
}, _resultToQueryData:function (serverResponseData) {
|
|
|
22 |
var csvFileContentString = serverResponseData;
|
|
|
23 |
var arrayOfArrays = this._getArrayOfArraysFromCsvFileContents(csvFileContentString);
|
|
|
24 |
var arrayOfObjects = this._getArrayOfObjectsFromArrayOfArrays(arrayOfArrays);
|
|
|
25 |
var remoteStoreData = this._getRemoteStoreDataFromArrayOfObjects(arrayOfObjects);
|
|
|
26 |
return remoteStoreData;
|
|
|
27 |
}, _setupSaveRequest:function (saveKeywordArgs, requestKw) {
|
|
|
28 |
}, _getArrayOfArraysFromCsvFileContents:function (csvFileContents) {
|
|
|
29 |
dojo.lang.assertType(csvFileContents, String);
|
|
|
30 |
var lineEndingCharacters = new RegExp("\r\n|\n|\r");
|
|
|
31 |
var leadingWhiteSpaceCharacters = new RegExp("^\\s+", "g");
|
|
|
32 |
var trailingWhiteSpaceCharacters = new RegExp("\\s+$", "g");
|
|
|
33 |
var doubleQuotes = new RegExp("\"\"", "g");
|
|
|
34 |
var arrayOfOutputRecords = [];
|
|
|
35 |
var arrayOfInputLines = csvFileContents.split(lineEndingCharacters);
|
|
|
36 |
for (var i in arrayOfInputLines) {
|
|
|
37 |
var singleLine = arrayOfInputLines[i];
|
|
|
38 |
if (singleLine.length > 0) {
|
|
|
39 |
var listOfFields = singleLine.split(",");
|
|
|
40 |
var j = 0;
|
|
|
41 |
while (j < listOfFields.length) {
|
|
|
42 |
var space_field_space = listOfFields[j];
|
|
|
43 |
var field_space = space_field_space.replace(leadingWhiteSpaceCharacters, "");
|
|
|
44 |
var field = field_space.replace(trailingWhiteSpaceCharacters, "");
|
|
|
45 |
var firstChar = field.charAt(0);
|
|
|
46 |
var lastChar = field.charAt(field.length - 1);
|
|
|
47 |
var secondToLastChar = field.charAt(field.length - 2);
|
|
|
48 |
var thirdToLastChar = field.charAt(field.length - 3);
|
|
|
49 |
if ((firstChar == "\"") && ((lastChar != "\"") || ((lastChar == "\"") && (secondToLastChar == "\"") && (thirdToLastChar != "\"")))) {
|
|
|
50 |
if (j + 1 === listOfFields.length) {
|
|
|
51 |
return null;
|
|
|
52 |
}
|
|
|
53 |
var nextField = listOfFields[j + 1];
|
|
|
54 |
listOfFields[j] = field_space + "," + nextField;
|
|
|
55 |
listOfFields.splice(j + 1, 1);
|
|
|
56 |
} else {
|
|
|
57 |
if ((firstChar == "\"") && (lastChar == "\"")) {
|
|
|
58 |
field = field.slice(1, (field.length - 1));
|
|
|
59 |
field = field.replace(doubleQuotes, "\"");
|
|
|
60 |
}
|
|
|
61 |
listOfFields[j] = field;
|
|
|
62 |
j += 1;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
arrayOfOutputRecords.push(listOfFields);
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
return arrayOfOutputRecords;
|
|
|
69 |
}, _getArrayOfObjectsFromArrayOfArrays:function (arrayOfArrays) {
|
|
|
70 |
dojo.lang.assertType(arrayOfArrays, Array);
|
|
|
71 |
var arrayOfItems = [];
|
|
|
72 |
if (arrayOfArrays.length > 1) {
|
|
|
73 |
var arrayOfKeys = arrayOfArrays[0];
|
|
|
74 |
for (var i = 1; i < arrayOfArrays.length; ++i) {
|
|
|
75 |
var row = arrayOfArrays[i];
|
|
|
76 |
var item = {};
|
|
|
77 |
for (var j in row) {
|
|
|
78 |
var value = row[j];
|
|
|
79 |
var key = arrayOfKeys[j];
|
|
|
80 |
item[key] = value;
|
|
|
81 |
}
|
|
|
82 |
arrayOfItems.push(item);
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
return arrayOfItems;
|
|
|
86 |
}, _getRemoteStoreDataFromArrayOfObjects:function (arrayOfObjects) {
|
|
|
87 |
dojo.lang.assertType(arrayOfObjects, Array);
|
|
|
88 |
var output = {};
|
|
|
89 |
for (var i = 0; i < arrayOfObjects.length; ++i) {
|
|
|
90 |
var object = arrayOfObjects[i];
|
|
|
91 |
for (var key in object) {
|
|
|
92 |
var value = object[key];
|
|
|
93 |
object[key] = [value];
|
|
|
94 |
}
|
|
|
95 |
output[i] = object;
|
|
|
96 |
}
|
|
|
97 |
return output;
|
|
|
98 |
}, newItem:function (attributes, keywordArgs) {
|
|
|
99 |
dojo.unimplemented("dojo.data.CsvStore.newItem");
|
|
|
100 |
}, deleteItem:function (item) {
|
|
|
101 |
dojo.unimplemented("dojo.data.CsvStore.deleteItem");
|
|
|
102 |
}, setValues:function (item, attribute, values) {
|
|
|
103 |
dojo.unimplemented("dojo.data.CsvStore.setValues");
|
|
|
104 |
}, set:function (item, attribute, value) {
|
|
|
105 |
dojo.unimplemented("dojo.data.CsvStore.set");
|
|
|
106 |
}, unsetAttribute:function (item, attribute) {
|
|
|
107 |
dojo.unimplemented("dojo.data.CsvStore.unsetAttribute");
|
|
|
108 |
}, save:function (keywordArgs) {
|
|
|
109 |
dojo.unimplemented("dojo.data.CsvStore.save");
|
|
|
110 |
}, revert:function () {
|
|
|
111 |
dojo.unimplemented("dojo.data.CsvStore.revert");
|
|
|
112 |
}, isDirty:function (item) {
|
|
|
113 |
dojo.unimplemented("dojo.data.CsvStore.isDirty");
|
|
|
114 |
}});
|
|
|
115 |
|