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/gamma/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/gamma/js/Clipperz/CSVProcessor.js') (more/less context) (show whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/CSVProcessor.js | 349 |
1 files changed, 349 insertions, 0 deletions
diff --git a/frontend/gamma/js/Clipperz/CSVProcessor.js b/frontend/gamma/js/Clipperz/CSVProcessor.js new file mode 100644 index 0000000..d481ba2 --- a/dev/null +++ b/frontend/gamma/js/Clipperz/CSVProcessor.js | |||
@@ -0,0 +1,349 @@ | |||
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 Clipperz.Async.Deferred("CVSProcessor.deferredParse_core"); | ||
146 | // deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'importProcessorProgressUpdate', {status:'processing', size:aContext.size, progress:(aContext.size - aContext.line.length)}); | ||
147 | deferredResult.addCallbackPass(MochiKit.Signal.signal, this, 'importProcessorProgressUpdate', {status:'processing', size:aContext.size, progress:(aContext.size - aContext.line.length)}); | ||
148 | deferredResult.addCallback(MochiKit.Async.wait, 0.2); | ||
149 | deferredResult.addMethod(this, 'deferredParse_core') | ||
150 | deferredResult.callback(aContext); | ||
151 | } | ||
152 | |||
153 | return deferredResult; | ||
154 | }, | ||
155 | |||
156 | //......................................................................... | ||
157 | |||
158 | 'deferredParse': function(aValue) { | ||
159 | var deferredResult; | ||
160 | var lines; | ||
161 | var context; | ||
162 | |||
163 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n*/g, "").replace(/\n$/g, ""); | ||
164 | |||
165 | context = { | ||
166 | line: lines, | ||
167 | size: lines.length, | ||
168 | result: [] | ||
169 | } | ||
170 | |||
171 | deferredResult = new Clipperz.Async.Deferred("CSVProcessor.deferredParse"); | ||
172 | deferredResult.addMethod(this, 'deferredParse_core'); | ||
173 | deferredResult.callback(context); | ||
174 | |||
175 | return deferredResult; | ||
176 | }, | ||
177 | |||
178 | //------------------------------------------------------------------------- | ||
179 | |||
180 | 'parseLine': function(aParameter) { | ||
181 | var result; | ||
182 | var palatable; | ||
183 | var line; | ||
184 | var processedField; | ||
185 | |||
186 | result = []; | ||
187 | |||
188 | do { | ||
189 | processedField = this.parseField(aParameter); | ||
190 | if (processedField != null) { | ||
191 | result.push(processedField) | ||
192 | }; | ||
193 | } while (processedField != null); | ||
194 | |||
195 | return result; | ||
196 | }, | ||
197 | |||
198 | //------------------------------------------------------------------------- | ||
199 | |||
200 | 'parseField': function(aParameter) { | ||
201 | var result; | ||
202 | |||
203 | var inQuotes; | ||
204 | var validRegExp; | ||
205 | var singleQuoteBeginRegexp; | ||
206 | var escapedQuoteBeginRegexp; | ||
207 | var singleQuoteCommaEndRegexp; | ||
208 | var singleQuoteNewLineEndRegexp; | ||
209 | var commaBeginRegexp; | ||
210 | var newlineRegexp; | ||
211 | |||
212 | |||
213 | singleQuoteBeginRegexp = new RegExp("^" + '\\' + this.quoteChar()); | ||
214 | escapedQuoteBeginRegexp = new RegExp("^" + '\\' + this.escapeChar() + '\\' + this.quoteChar()); | ||
215 | singleQuoteCommaEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + '\\' + this.separatorChar()); | ||
216 | singleQuoteNewLineEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + "\n"); | ||
217 | commaBeginRegexp = new RegExp("^" + '\\' + this.separatorChar()); | ||
218 | newlineRegexp = new RegExp("^\n"); | ||
219 | |||
220 | inQuotes = false; | ||
221 | |||
222 | //MochiKit.Logging.logDebug("#################################### '" + aParameter.line + "'"); | ||
223 | if (aParameter.line == "") { | ||
224 | if (aParameter.isThereAnEmptyFinalField == true) { | ||
225 | aParameter.isThereAnEmptyFinalField = false; | ||
226 | result = ""; | ||
227 | } else { | ||
228 | result = null; | ||
229 | } | ||
230 | } else { | ||
231 | if (this.binary()) { | ||
232 | validRegexp = /^./; | ||
233 | // validRegexp = /^[^\\]/; | ||
234 | } else { | ||
235 | validRegexp = /^[\t\040-\176]/; | ||
236 | } | ||
237 | |||
238 | try { | ||
239 | var done; | ||
240 | |||
241 | done = false; | ||
242 | result = ""; | ||
243 | |||
244 | while (!done) { | ||
245 | if (aParameter.line.length < 1) { | ||
246 | //MochiKit.Logging.logDebug("---> 1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
247 | if (inQuotes == true) { | ||
248 | //MochiKit.Logging.logDebug("---> 1.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
249 | throw new Error("CSV Parsing error; end of string, missing closing double-quote..."); | ||
250 | } else { | ||
251 | //MochiKit.Logging.logDebug("---> 1.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
252 | done = true; | ||
253 | } | ||
254 | } else if (escapedQuoteBeginRegexp.test(aParameter.line)) { | ||
255 | //MochiKit.Logging.logDebug("---> 2.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
256 | result += this.quoteChar(); | ||
257 | aParameter.line = aParameter.line.substr(2, aParameter.line.length - 1); | ||
258 | //MochiKit.Logging.logDebug("<--- 2.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
259 | } else if (singleQuoteBeginRegexp.test(aParameter.line)) { | ||
260 | //MochiKit.Logging.logDebug("---> 3: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
261 | if (inQuotes == true) { | ||
262 | if (aParameter.line.length == 1) { | ||
263 | //MochiKit.Logging.logDebug("---> 3.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
264 | aParameter.line = ''; | ||
265 | done = true; | ||
266 | } else if (singleQuoteCommaEndRegexp.test(aParameter.line)) { | ||
267 | //MochiKit.Logging.logDebug("---> 3.3: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
268 | aParameter.line = aParameter.line.substr(2, aParameter.line.length - 1); | ||
269 | done = true; | ||
270 | //MochiKit.Logging.logDebug("<--- 3.3: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
271 | } else if (singleQuoteNewLineEndRegexp.test(aParameter.line)) { | ||
272 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
273 | done = true; | ||
274 | } else { | ||
275 | throw new Error("CSV Parsing error; double-quote, followed by undesirable character (bad character sequence)... " + aParameter.line); | ||
276 | } | ||
277 | } else { | ||
278 | //MochiKit.Logging.logDebug("---> 4: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
279 | if (result == "") { | ||
280 | //MochiKit.Logging.logDebug("---> 4.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
281 | inQuotes = true; | ||
282 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
283 | //MochiKit.Logging.logDebug("<--- 4.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
284 | } else { | ||
285 | throw new Error("CSV Parsing error; double-quote, outside of double-quotes (bad character sequence)..."); | ||
286 | } | ||
287 | } | ||
288 | } else if (commaBeginRegexp.test(aParameter.line)) { | ||
289 | //MochiKit.Logging.logDebug("---> 5: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
290 | if (inQuotes) { | ||
291 | //MochiKit.Logging.logDebug("---> 5.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
292 | result += aParameter.line.substr(0 ,1); | ||
293 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
294 | //MochiKit.Logging.logDebug("<--- 5.1: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
295 | } else { | ||
296 | //MochiKit.Logging.logDebug("---> 5.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
297 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
298 | if (newlineRegexp.test(aParameter.line) || aParameter.line == "") { | ||
299 | //MochiKit.Logging.logDebug("######"); | ||
300 | aParameter.isThereAnEmptyFinalField = true; | ||
301 | }; | ||
302 | done = true; | ||
303 | //MochiKit.Logging.logDebug("<--- 5.2: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
304 | } | ||
305 | } else if (validRegexp.test(aParameter.line)) { | ||
306 | //MochiKit.Logging.logDebug("---> 6: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
307 | result += aParameter.line.substr(0, 1); | ||
308 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
309 | //MochiKit.Logging.logDebug("<--- 6: '" + aParameter.line.replace(/\n/g, "\\n") + "'"); | ||
310 | } else if (newlineRegexp.test(aParameter.line)) { | ||
311 | if (inQuotes == true) { | ||
312 | result += aParameter.line.substr(0 ,1); | ||
313 | aParameter.line = aParameter.line.substr(1, aParameter.line.length - 1); | ||
314 | } else { | ||
315 | if (result == "") { | ||
316 | if (aParameter.isThereAnEmptyFinalField == true) { | ||
317 | aParameter.isThereAnEmptyFinalField = false; | ||
318 | } else { | ||
319 | result = null; | ||
320 | } | ||
321 | } | ||
322 | |||
323 | done = true; | ||
324 | } | ||
325 | } else { | ||
326 | throw new Error("CSV Parsing error; an undesirable character... '" + aParameter.line.substr(0,1) + "'"); | ||
327 | } | ||
328 | } | ||
329 | } catch(exception) { | ||
330 | MochiKit.Logging.logError(exception.message); | ||
331 | // result = null; | ||
332 | throw exception; | ||
333 | } | ||
334 | } | ||
335 | |||
336 | //if (result != null) { | ||
337 | //MochiKit.Logging.logDebug("<=== result: '" + result.replace(/\n/g, "\\n") + "'"); | ||
338 | //} else { | ||
339 | //MochiKit.Logging.logDebug("<=== result: NULL"); | ||
340 | //} | ||
341 | |||
342 | return result; | ||
343 | }, | ||
344 | |||
345 | //------------------------------------------------------------------------- | ||
346 | __syntaxFix__: "syntax fix" | ||
347 | }); | ||
348 | |||
349 | |||