Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.old.format.Csv");
14
dojo.require("dojo.lang.assert");
15
dojo.data.old.format.Csv = new function () {
16
	this.getArrayStructureFromCsvFileContents = function (csvFileContents) {
17
		dojo.lang.assertType(csvFileContents, String);
18
		var lineEndingCharacters = new RegExp("\r\n|\n|\r");
19
		var leadingWhiteSpaceCharacters = new RegExp("^\\s+", "g");
20
		var trailingWhiteSpaceCharacters = new RegExp("\\s+$", "g");
21
		var doubleQuotes = new RegExp("\"\"", "g");
22
		var arrayOfOutputRecords = [];
23
		var arrayOfInputLines = csvFileContents.split(lineEndingCharacters);
24
		for (var i in arrayOfInputLines) {
25
			var singleLine = arrayOfInputLines[i];
26
			if (singleLine.length > 0) {
27
				var listOfFields = singleLine.split(",");
28
				var j = 0;
29
				while (j < listOfFields.length) {
30
					var space_field_space = listOfFields[j];
31
					var field_space = space_field_space.replace(leadingWhiteSpaceCharacters, "");
32
					var field = field_space.replace(trailingWhiteSpaceCharacters, "");
33
					var firstChar = field.charAt(0);
34
					var lastChar = field.charAt(field.length - 1);
35
					var secondToLastChar = field.charAt(field.length - 2);
36
					var thirdToLastChar = field.charAt(field.length - 3);
37
					if ((firstChar == "\"") && ((lastChar != "\"") || ((lastChar == "\"") && (secondToLastChar == "\"") && (thirdToLastChar != "\"")))) {
38
						if (j + 1 === listOfFields.length) {
39
							return null;
40
						}
41
						var nextField = listOfFields[j + 1];
42
						listOfFields[j] = field_space + "," + nextField;
43
						listOfFields.splice(j + 1, 1);
44
					} else {
45
						if ((firstChar == "\"") && (lastChar == "\"")) {
46
							field = field.slice(1, (field.length - 1));
47
							field = field.replace(doubleQuotes, "\"");
48
						}
49
						listOfFields[j] = field;
50
						j += 1;
51
					}
52
				}
53
				arrayOfOutputRecords.push(listOfFields);
54
			}
55
		}
56
		return arrayOfOutputRecords;
57
	};
58
	this.loadDataProviderFromFileContents = function (dataProvider, csvFileContents) {
59
		dojo.lang.assertType(dataProvider, dojo.data.old.provider.Base);
60
		dojo.lang.assertType(csvFileContents, String);
61
		var arrayOfArrays = this.getArrayStructureFromCsvFileContents(csvFileContents);
62
		if (arrayOfArrays) {
63
			var arrayOfKeys = arrayOfArrays[0];
64
			for (var i = 1; i < arrayOfArrays.length; ++i) {
65
				var row = arrayOfArrays[i];
66
				var item = dataProvider.getNewItemToLoad();
67
				for (var j in row) {
68
					var value = row[j];
69
					var key = arrayOfKeys[j];
70
					item.load(key, value);
71
				}
72
			}
73
		}
74
	};
75
	this.getCsvStringFromResultSet = function (resultSet) {
76
		dojo.unimplemented("dojo.data.old.format.Csv.getCsvStringFromResultSet");
77
		var csvString = null;
78
		return csvString;
79
	};
80
}();
81