author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
commit | ef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff) | |
tree | c403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/beta/js/Clipperz/CSVProcessor.js | |
parent | 597ecfbc0249d83e1b856cbd558340c01237a360 (diff) | |
download | clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2 |
First version of the newly restructured repository
Diffstat (limited to 'frontend/beta/js/Clipperz/CSVProcessor.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/CSVProcessor.js | 348 |
1 files changed, 348 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/CSVProcessor.js b/frontend/beta/js/Clipperz/CSVProcessor.js new file mode 100644 index 0000000..164b02e --- a/dev/null +++ b/frontend/beta/js/Clipperz/CSVProcessor.js | |||
@@ -0,0 +1,348 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | |||
31 | |||
32 | Clipperz.CSVProcessor = function(args) { | ||
33 | args = args || {}; | ||
34 | |||
35 | // this._status = undefined; | ||
36 | // this._error_input= undefined; | ||
37 | // this._string = undefined; | ||
38 | // this._fields = undefined; | ||
39 | |||
40 | this._quoteChar = args['quoteChar'] ||"\042"; | ||
41 | this._eol = args['eol'] ||""; | ||
42 | this._escapeChar = args['escapeChar'] ||"\042"; | ||
43 | this._separatorChar = args['separatorChar'] ||","; | ||
44 | this._binary = args['binary'] ||false; | ||
45 | this._alwaysQuote = args['alwaysQuote'] ||false; | ||
46 | |||
47 | return this; | ||
48 | } | ||
49 | |||
50 | //============================================================================= | ||
51 | |||
52 | Clipperz.CSVProcessor.prototype = MochiKit.Base.update(null, { | ||
53 | |||
54 | //------------------------------------------------------------------------- | ||
55 | |||
56 | 'quoteChar': function() { | ||
57 | return this._quoteChar; | ||
58 | }, | ||
59 | |||
60 | //------------------------------------------------------------------------- | ||
61 | |||
62 | 'eol': function() { | ||
63 | return this._eol; | ||
64 | }, | ||
65 | |||
66 | //------------------------------------------------------------------------- | ||
67 | |||
68 | 'escapeChar': function() { | ||
69 | return this._escapeChar; | ||
70 | }, | ||
71 | |||
72 | //------------------------------------------------------------------------- | ||
73 | |||
74 | 'separatorChar': function() { | ||
75 | return this._separatorChar; | ||
76 | }, | ||
77 | |||
78 | 'setSeparatorChar': function(aValue) { | ||
79 | this._separatorChar = aValue; | ||
80 | }, | ||
81 | |||
82 | //------------------------------------------------------------------------- | ||
83 | |||
84 | 'binary': function() { | ||
85 | return this._binary; | ||
86 | }, | ||
87 | |||
88 | //------------------------------------------------------------------------- | ||
89 | |||
90 | 'alwaysQuote': function() { | ||
91 | return this._alwaysQuote; | ||
92 | }, | ||
93 | |||
94 | //------------------------------------------------------------------------- | ||
95 | /* | ||
96 | 'parse': function(aValue) { | ||
97 | var result; | ||
98 | var lines; | ||
99 | var parameter; | ||
100 | |||
101 | //MochiKit.Logging.logDebug(">>> CSVProcessor.parse"); | ||
102 | result = []; | ||
103 | |||
104 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n* /g, "").replace(/\n$/g, "");; | ||
105 | parameter = { | ||
106 | line: lines | ||
107 | } | ||
108 | |||
109 | do { | ||
110 | var fields; | ||
111 | |||
112 | fields = this.parseLine(parameter); | ||
113 | |||
114 | if (fields != null) { | ||
115 | result.push(fields); | ||
116 | } | ||
117 | |||
118 | parameter.line = parameter.line.replace(/^\n* /g, "").replace(/\n$/g, ""); | ||
119 | |||
120 | //MochiKit.Logging.logDebug("line: '" + parameter.line + "'"); | ||
121 | } while (parameter.line != ""); | ||
122 | //MochiKit.Logging.logDebug("--- CSVProcessor.parse - result: " + Clipperz.Base.serializeJSON(result)); | ||
123 | //MochiKit.Logging.logDebug("<<< CSVProcessor.parse"); | ||
124 | |||
125 | return result; | ||
126 | }, | ||
127 | */ | ||
128 | //------------------------------------------------------------------------- | ||
129 | |||
130 | 'deferredParse_core': function(aContext) { | ||
131 | var deferredResult; | ||
132 | |||
133 | if (aContext.line == "") { | ||
134 | deferredResult = MochiKit.Async.succeed(aContext.result); | ||
135 | } else { | ||
136 | var fields; | ||
137 | |||
138 | fields = this.parseLine(aContext); | ||
139 | if (fields != null) { | ||
140 | aContext.result.push(fields); | ||
141 | } | ||
142 | |||
143 | aContext.line = aContext.line.replace(/^\n*/g, "").replace(/\n$/g, ""); | ||
144 | |||
145 | deferredResult = new MochiKit.Async.Deferred(); | ||
146 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'importProcessorProgressUpdate', {status:'processing', size:aContext.size, progress:(aContext.size - aContext.line.length)}); | ||
147 | deferredResult.addCallback(MochiKit.Async.wait, 0.2); | ||
148 | deferredResult.addCallback(MochiKit.Base.method(this, 'deferredParse_core')) | ||
149 | deferredResult.callback(aContext); | ||
150 | } | ||
151 | |||
152 | return deferredResult; | ||
153 | }, | ||
154 | |||
155 | //......................................................................... | ||
156 | |||
157 | 'deferredParse': function(aValue) { | ||
158 | var deferredResult; | ||
159 | var lines; | ||
160 | var context; | ||
161 | |||
162 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n*/g, "").replace(/\n$/g, ""); | ||
163 | |||
164 | context = { | ||
165 | line: lines, | ||
166 | size: lines.length, | ||
167 | result: [] | ||
168 | } | ||
169 | |||
170 | deferredResult = new MochiKit.Async.Deferred(); | ||
171 | deferredResult.addCallback(MochiKit.Base.method(this, 'deferredParse_core')); | ||
172 | deferredResult.callback(context); | ||
173 | |||
174 | return deferredResult; | ||
175 | }, | ||
176 | |||
177 | //------------------------------------------------------------------------- | ||
178 | |||
179 | 'parseLine': function(aParameter) { | ||
180 | var result; | ||
181 | var palatable; | ||
182 | var line; | ||
183 | var processedField; | ||
184 | |||
185 | result = []; | ||
186 | |||
187 | do { | ||
188 | processedField = this.parseField(aParameter); | ||
189 | if (processedField != null) { | ||
190 | result.push(processedField) | ||
191 | }; | ||
192 | } while (processedField != null); | ||
193 | |||
194 | return result; | ||
195 | }, | ||
196 | |||
197 | //------------------------------------------------------------------------- | ||
198 | |||
199 | 'parseField': function(aParameter) { | ||
200 | var result; | ||
201 | |||
202 | var inQuotes; | ||
203 | var validRegExp; | ||
204 | var singleQuoteBeginRegexp; | ||
205 | var escapedQuoteBeginRegexp; | ||
206 | var singleQuoteCommaEndRegexp; | ||
207 | var singleQuoteNewLineEndRegexp; | ||
208 | var commaBeginRegexp; | ||
209 | var newlineRegexp; | ||
210 | |||
211 | |||
212 | singleQuoteBeginRegexp = new RegExp("^" + '\\' + this.quoteChar()); | ||
213 | escapedQuoteBeginRegexp = new RegExp("^" + '\\' + this.escapeChar() + '\\' + this.quoteChar()); | ||
214 | singleQuoteCommaEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + '\\' + this.separatorChar()); | ||
215 | singleQuoteNewLineEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + "\n"); | ||
216 | commaBeginRegexp = new RegExp("^" + '\\' + this.separatorChar()); | ||
217 | newlineRegexp = new RegExp("^\n"); | ||
218 | |||
219 | inQuotes = false; | ||
220 | |||
221 | //MochiKit.Logging.logDebug("#################################### '" + aParameter.line + "'"); | ||
222 | if (aParameter.line == "") { | ||
223 | if (aParameter.isThereAnEmptyFinalField == true) { | ||
224 | aParameter.isThereAnEmptyFinalField = false; | ||
225 | result = ""; | ||
226 | } else { | ||
227 | result = null; | ||
228 | } | ||
229 | } else { | ||
230 | if (this.binary()) { | ||
231 | validRegexp = /^./; | ||
232 | // validRegexp = /^[^\\]/; | ||
233 | } else { | ||
234 | validRegexp = /^[\t\040-\176]/; | ||
235 | } | ||
236 | |||
237 | try { | ||
238 | var done; | ||
239 | |||
240 | done = false; | ||
241 | result = ""; | ||
242 | |||
243 | while (!done) { | ||
244 | if (aParameter.line.length < 1) { | ||
245 | //MochiKit.Logging.logDebug("---> 1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
246 | if (inQuotes == true) { | ||
247 | //MochiKit.Logging.logDebug("---> 1.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
248 | throw new Error("CSV Parsing error; end of string, missing closing double-quote..."); | ||
249 | } else { | ||
250 | //MochiKit.Logging.logDebug("---> 1.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
251 | done = true; | ||
252 | } | ||
253 | } else if (escapedQuoteBeginRegexp.test(aParameter.line)) { | ||
254 | //MochiKit.Logging.logDebug("---> 2.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
255 | result += this.quoteChar(); | ||
256 | aParameter.line = aParameter.line.substr(2, aParameter.line.length - 1); | ||
257 | //MochiKit.Logging.logDebug("<--- 2.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
258 | } else if (singleQuoteBeginRegexp.test(aParameter.line)) { | ||
259 | //MochiKit.Logging.logDebug("---> 3: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
260 | if (inQuotes == true) { | ||
261 | if (aParameter.line.length == 1) { | ||
262 | //MochiKit.Logging.logDebug("---> 3.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
263 | aParameter.line = ''; | ||
264 | done = true; | ||
265 | } else if (singleQuoteCommaEndRegexp.test(aParameter.line)) { | ||
266 | //MochiKit.Logging.logDebug("---> 3.3: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
267 | aParameter.line = aParameter.line.substr(2, aParameter.line.length - 1); | ||
268 | done = true; | ||
269 | //MochiKit.Logging.logDebug("<--- 3.3: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
270 | } else if (singleQuoteNewLineEndRegexp.test(aParameter.line)) { | ||
271 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
272 | done = true; | ||
273 | } else { | ||
274 | throw new Error("CSV Parsing error; double-quote, followed by undesirable character (bad character sequence)... " + aParameter.line); | ||
275 | } | ||
276 | } else { | ||
277 | //MochiKit.Logging.logDebug("---> 4: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
278 | if (result == "") { | ||
279 | //MochiKit.Logging.logDebug("---> 4.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
280 | inQuotes = true; | ||
281 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
282 | //MochiKit.Logging.logDebug("<--- 4.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
283 | } else { | ||
284 | throw new Error("CSV Parsing error; double-quote, outside of double-quotes (bad character sequence)..."); | ||
285 | } | ||
286 | } | ||
287 | } else if (commaBeginRegexp.test(aParameter.line)) { | ||
288 | //MochiKit.Logging.logDebug("---> 5: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
289 | if (inQuotes) { | ||
290 | //MochiKit.Logging.logDebug("---> 5.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
291 | result += aParameter.line.substr(0 ,1); | ||
292 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
293 | //MochiKit.Logging.logDebug("<--- 5.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
294 | } else { | ||
295 | //MochiKit.Logging.logDebug("---> 5.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
296 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
297 | if (newlineRegexp.test(aParameter.line) || aParameter.line == "") { | ||
298 | //MochiKit.Logging.logDebug("######"); | ||
299 | aParameter.isThereAnEmptyFinalField = true; | ||
300 | }; | ||
301 | done = true; | ||
302 | //MochiKit.Logging.logDebug("<--- 5.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
303 | } | ||
304 | } else if (validRegexp.test(aParameter.line)) { | ||
305 | //MochiKit.Logging.logDebug("---> 6: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
306 | result += aParameter.line.substr(0, 1); | ||
307 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
308 | //MochiKit.Logging.logDebug("<--- 6: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
309 | } else if (newlineRegexp.test(aParameter.line)) { | ||
310 | if (inQuotes == true) { | ||
311 | result += aParameter.line.substr(0 ,1); | ||
312 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
313 | } else { | ||
314 | if (result == "") { | ||
315 | if (aParameter.isThereAnEmptyFinalField == true) { | ||
316 | aParameter.isThereAnEmptyFinalField = false; | ||
317 | } else { | ||
318 | result = null; | ||
319 | } | ||
320 | } | ||
321 | |||
322 | done = true; | ||
323 | } | ||
324 | } else { | ||
325 | throw new Error("CSV Parsing error; an undesirable character... '" + aParameter.line.substr(0,1) + "'"); | ||
326 | } | ||
327 | } | ||
328 | } catch(exception) { | ||
329 | MochiKit.Logging.logError(exception.message); | ||
330 | // result = null; | ||
331 | throw exception; | ||
332 | } | ||
333 | } | ||
334 | |||
335 | //if (result != null) { | ||
336 | //MochiKit.Logging.logDebug("<=== result: '" + result.replace(/\n/g, "\\n") + "'"); | ||
337 | //} else { | ||
338 | //MochiKit.Logging.logDebug("<=== result: NULL"); | ||
339 | //} | ||
340 | |||
341 | return result; | ||
342 | }, | ||
343 | |||
344 | //------------------------------------------------------------------------- | ||
345 | __syntaxFix__: "syntax fix" | ||
346 | }); | ||
347 | |||
348 | |||