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