Diffstat (limited to 'frontend/beta/js/Clipperz/CSVProcessor.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/CSVProcessor.js | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/CSVProcessor.js b/frontend/beta/js/Clipperz/CSVProcessor.js index ec94206..f429468 100644 --- a/frontend/beta/js/Clipperz/CSVProcessor.js +++ b/frontend/beta/js/Clipperz/CSVProcessor.js | |||
@@ -1,214 +1,212 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2011 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz Community Edition. | 5 | This file is part of Clipperz, the online password manager. |
6 | Clipperz Community Edition is an online password manager. | ||
7 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
8 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
9 | 8 | ||
10 | * Clipperz Community Edition is free software: you can redistribute | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
11 | it and/or modify it under the terms of the GNU Affero General Public | 10 | under the terms of the GNU Affero General Public License as published |
12 | License as published by the Free Software Foundation, either version | 11 | by the Free Software Foundation, either version 3 of the License, or |
13 | 3 of the License, or (at your option) any later version. | 12 | (at your option) any later version. |
14 | 13 | ||
15 | * Clipperz Community Edition is distributed in the hope that it will | 14 | * Clipperz is distributed in the hope that it will be useful, but |
16 | be useful, but WITHOUT ANY WARRANTY; without even the implied | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
19 | 18 | ||
20 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
21 | License along with Clipperz Community Edition. If not, see | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
22 | <http://www.gnu.org/licenses/>. | ||
23 | 21 | ||
24 | */ | 22 | */ |
25 | 23 | ||
26 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | 24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } |
27 | 25 | ||
28 | 26 | ||
29 | Clipperz.CSVProcessor = function(args) { | 27 | Clipperz.CSVProcessor = function(args) { |
30 | args = args || {}; | 28 | args = args || {}; |
31 | 29 | ||
32 | // this._status = undefined; | 30 | // this._status = undefined; |
33 | // this._error_input= undefined; | 31 | // this._error_input= undefined; |
34 | // this._string = undefined; | 32 | // this._string = undefined; |
35 | // this._fields = undefined; | 33 | // this._fields = undefined; |
36 | 34 | ||
37 | this._quoteChar = args['quoteChar'] ||"\042"; | 35 | this._quoteChar = args['quoteChar'] ||"\042"; |
38 | this._eol = args['eol'] ||""; | 36 | this._eol = args['eol'] ||""; |
39 | this._escapeChar = args['escapeChar'] ||"\042"; | 37 | this._escapeChar = args['escapeChar'] ||"\042"; |
40 | this._separatorChar = args['separatorChar'] ||","; | 38 | this._separatorChar = args['separatorChar'] ||","; |
41 | this._binary = args['binary'] ||false; | 39 | this._binary = args['binary'] ||false; |
42 | this._alwaysQuote = args['alwaysQuote'] ||false; | 40 | this._alwaysQuote = args['alwaysQuote'] ||false; |
43 | 41 | ||
44 | return this; | 42 | return this; |
45 | } | 43 | } |
46 | 44 | ||
47 | //============================================================================= | 45 | //============================================================================= |
48 | 46 | ||
49 | Clipperz.CSVProcessor.prototype = MochiKit.Base.update(null, { | 47 | Clipperz.CSVProcessor.prototype = MochiKit.Base.update(null, { |
50 | 48 | ||
51 | //------------------------------------------------------------------------- | 49 | //------------------------------------------------------------------------- |
52 | 50 | ||
53 | 'quoteChar': function() { | 51 | 'quoteChar': function() { |
54 | return this._quoteChar; | 52 | return this._quoteChar; |
55 | }, | 53 | }, |
56 | 54 | ||
57 | //------------------------------------------------------------------------- | 55 | //------------------------------------------------------------------------- |
58 | 56 | ||
59 | 'eol': function() { | 57 | 'eol': function() { |
60 | return this._eol; | 58 | return this._eol; |
61 | }, | 59 | }, |
62 | 60 | ||
63 | //------------------------------------------------------------------------- | 61 | //------------------------------------------------------------------------- |
64 | 62 | ||
65 | 'escapeChar': function() { | 63 | 'escapeChar': function() { |
66 | return this._escapeChar; | 64 | return this._escapeChar; |
67 | }, | 65 | }, |
68 | 66 | ||
69 | //------------------------------------------------------------------------- | 67 | //------------------------------------------------------------------------- |
70 | 68 | ||
71 | 'separatorChar': function() { | 69 | 'separatorChar': function() { |
72 | return this._separatorChar; | 70 | return this._separatorChar; |
73 | }, | 71 | }, |
74 | 72 | ||
75 | 'setSeparatorChar': function(aValue) { | 73 | 'setSeparatorChar': function(aValue) { |
76 | this._separatorChar = aValue; | 74 | this._separatorChar = aValue; |
77 | }, | 75 | }, |
78 | 76 | ||
79 | //------------------------------------------------------------------------- | 77 | //------------------------------------------------------------------------- |
80 | 78 | ||
81 | 'binary': function() { | 79 | 'binary': function() { |
82 | return this._binary; | 80 | return this._binary; |
83 | }, | 81 | }, |
84 | 82 | ||
85 | //------------------------------------------------------------------------- | 83 | //------------------------------------------------------------------------- |
86 | 84 | ||
87 | 'alwaysQuote': function() { | 85 | 'alwaysQuote': function() { |
88 | return this._alwaysQuote; | 86 | return this._alwaysQuote; |
89 | }, | 87 | }, |
90 | 88 | ||
91 | //------------------------------------------------------------------------- | 89 | //------------------------------------------------------------------------- |
92 | /* | 90 | /* |
93 | 'parse': function(aValue) { | 91 | 'parse': function(aValue) { |
94 | var result; | 92 | var result; |
95 | var lines; | 93 | var lines; |
96 | var parameter; | 94 | var parameter; |
97 | 95 | ||
98 | //MochiKit.Logging.logDebug(">>> CSVProcessor.parse"); | 96 | //MochiKit.Logging.logDebug(">>> CSVProcessor.parse"); |
99 | result = []; | 97 | result = []; |
100 | 98 | ||
101 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n* /g, "").replace(/\n$/g, "");; | 99 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n* /g, "").replace(/\n$/g, "");; |
102 | parameter = { | 100 | parameter = { |
103 | line: lines | 101 | line: lines |
104 | } | 102 | } |
105 | 103 | ||
106 | do { | 104 | do { |
107 | var fields; | 105 | var fields; |
108 | 106 | ||
109 | fields = this.parseLine(parameter); | 107 | fields = this.parseLine(parameter); |
110 | 108 | ||
111 | if (fields != null) { | 109 | if (fields != null) { |
112 | result.push(fields); | 110 | result.push(fields); |
113 | } | 111 | } |
114 | 112 | ||
115 | parameter.line = parameter.line.replace(/^\n* /g, "").replace(/\n$/g, ""); | 113 | parameter.line = parameter.line.replace(/^\n* /g, "").replace(/\n$/g, ""); |
116 | 114 | ||
117 | //MochiKit.Logging.logDebug("line: '" + parameter.line + "'"); | 115 | //MochiKit.Logging.logDebug("line: '" + parameter.line + "'"); |
118 | } while (parameter.line != ""); | 116 | } while (parameter.line != ""); |
119 | //MochiKit.Logging.logDebug("--- CSVProcessor.parse - result: " + Clipperz.Base.serializeJSON(result)); | 117 | //MochiKit.Logging.logDebug("--- CSVProcessor.parse - result: " + Clipperz.Base.serializeJSON(result)); |
120 | //MochiKit.Logging.logDebug("<<< CSVProcessor.parse"); | 118 | //MochiKit.Logging.logDebug("<<< CSVProcessor.parse"); |
121 | 119 | ||
122 | return result; | 120 | return result; |
123 | }, | 121 | }, |
124 | */ | 122 | */ |
125 | //------------------------------------------------------------------------- | 123 | //------------------------------------------------------------------------- |
126 | 124 | ||
127 | 'deferredParse_core': function(aContext) { | 125 | 'deferredParse_core': function(aContext) { |
128 | var deferredResult; | 126 | var deferredResult; |
129 | 127 | ||
130 | if (aContext.line == "") { | 128 | if (aContext.line == "") { |
131 | deferredResult = MochiKit.Async.succeed(aContext.result); | 129 | deferredResult = MochiKit.Async.succeed(aContext.result); |
132 | } else { | 130 | } else { |
133 | var fields; | 131 | var fields; |
134 | 132 | ||
135 | fields = this.parseLine(aContext); | 133 | fields = this.parseLine(aContext); |
136 | if (fields != null) { | 134 | if (fields != null) { |
137 | aContext.result.push(fields); | 135 | aContext.result.push(fields); |
138 | } | 136 | } |
139 | 137 | ||
140 | aContext.line = aContext.line.replace(/^\n*/g, "").replace(/\n$/g, ""); | 138 | aContext.line = aContext.line.replace(/^\n*/g, "").replace(/\n$/g, ""); |
141 | 139 | ||
142 | deferredResult = new MochiKit.Async.Deferred(); | 140 | deferredResult = new MochiKit.Async.Deferred(); |
143 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'importProcessorProgressUpdate', {status:'processing', size:aContext.size, progress:(aContext.size - aContext.line.length)}); | 141 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'importProcessorProgressUpdate', {status:'processing', size:aContext.size, progress:(aContext.size - aContext.line.length)}); |
144 | deferredResult.addCallback(MochiKit.Async.wait, 0.2); | 142 | deferredResult.addCallback(MochiKit.Async.wait, 0.2); |
145 | deferredResult.addCallback(MochiKit.Base.method(this, 'deferredParse_core')) | 143 | deferredResult.addCallback(MochiKit.Base.method(this, 'deferredParse_core')) |
146 | deferredResult.callback(aContext); | 144 | deferredResult.callback(aContext); |
147 | } | 145 | } |
148 | 146 | ||
149 | return deferredResult; | 147 | return deferredResult; |
150 | }, | 148 | }, |
151 | 149 | ||
152 | //......................................................................... | 150 | //......................................................................... |
153 | 151 | ||
154 | 'deferredParse': function(aValue) { | 152 | 'deferredParse': function(aValue) { |
155 | var deferredResult; | 153 | var deferredResult; |
156 | var lines; | 154 | var lines; |
157 | var context; | 155 | var context; |
158 | 156 | ||
159 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n*/g, "").replace(/\n$/g, ""); | 157 | lines = aValue.replace(/\r?\n/g, "\n").replace(/^\n*/g, "").replace(/\n$/g, ""); |
160 | 158 | ||
161 | context = { | 159 | context = { |
162 | line: lines, | 160 | line: lines, |
163 | size: lines.length, | 161 | size: lines.length, |
164 | result: [] | 162 | result: [] |
165 | } | 163 | } |
166 | 164 | ||
167 | deferredResult = new MochiKit.Async.Deferred(); | 165 | deferredResult = new MochiKit.Async.Deferred(); |
168 | deferredResult.addCallback(MochiKit.Base.method(this, 'deferredParse_core')); | 166 | deferredResult.addCallback(MochiKit.Base.method(this, 'deferredParse_core')); |
169 | deferredResult.callback(context); | 167 | deferredResult.callback(context); |
170 | 168 | ||
171 | return deferredResult; | 169 | return deferredResult; |
172 | }, | 170 | }, |
173 | 171 | ||
174 | //------------------------------------------------------------------------- | 172 | //------------------------------------------------------------------------- |
175 | 173 | ||
176 | 'parseLine': function(aParameter) { | 174 | 'parseLine': function(aParameter) { |
177 | var result; | 175 | var result; |
178 | var palatable; | 176 | var palatable; |
179 | var line; | 177 | var line; |
180 | var processedField; | 178 | var processedField; |
181 | 179 | ||
182 | result = []; | 180 | result = []; |
183 | 181 | ||
184 | do { | 182 | do { |
185 | processedField = this.parseField(aParameter); | 183 | processedField = this.parseField(aParameter); |
186 | if (processedField != null) { | 184 | if (processedField != null) { |
187 | result.push(processedField) | 185 | result.push(processedField) |
188 | }; | 186 | }; |
189 | } while (processedField != null); | 187 | } while (processedField != null); |
190 | 188 | ||
191 | return result; | 189 | return result; |
192 | }, | 190 | }, |
193 | 191 | ||
194 | //------------------------------------------------------------------------- | 192 | //------------------------------------------------------------------------- |
195 | 193 | ||
196 | 'parseField': function(aParameter) { | 194 | 'parseField': function(aParameter) { |
197 | var result; | 195 | var result; |
198 | 196 | ||
199 | var inQuotes; | 197 | var inQuotes; |
200 | var validRegExp; | 198 | var validRegExp; |
201 | var singleQuoteBeginRegexp; | 199 | var singleQuoteBeginRegexp; |
202 | var escapedQuoteBeginRegexp; | 200 | var escapedQuoteBeginRegexp; |
203 | var singleQuoteCommaEndRegexp; | 201 | var singleQuoteCommaEndRegexp; |
204 | var singleQuoteNewLineEndRegexp; | 202 | var singleQuoteNewLineEndRegexp; |
205 | var commaBeginRegexp; | 203 | var commaBeginRegexp; |
206 | var newlineRegexp; | 204 | var newlineRegexp; |
207 | 205 | ||
208 | 206 | ||
209 | singleQuoteBeginRegexp = new RegExp("^" + '\\' + this.quoteChar()); | 207 | singleQuoteBeginRegexp = new RegExp("^" + '\\' + this.quoteChar()); |
210 | escapedQuoteBeginRegexp = new RegExp("^" + '\\' + this.escapeChar() + '\\' + this.quoteChar()); | 208 | escapedQuoteBeginRegexp = new RegExp("^" + '\\' + this.escapeChar() + '\\' + this.quoteChar()); |
211 | singleQuoteCommaEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + '\\' + this.separatorChar()); | 209 | singleQuoteCommaEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + '\\' + this.separatorChar()); |
212 | singleQuoteNewLineEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + "\n"); | 210 | singleQuoteNewLineEndRegexp= new RegExp("^" + '\\' + this.quoteChar() + "\n"); |
213 | commaBeginRegexp = new RegExp("^" + '\\' + this.separatorChar()); | 211 | commaBeginRegexp = new RegExp("^" + '\\' + this.separatorChar()); |
214 | newlineRegexp = new RegExp("^\n"); | 212 | newlineRegexp = new RegExp("^\n"); |