Subversion Repositories Applications.papyrus

Rev

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