|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('dataschema-base', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* The DataSchema utility provides a common configurable interface for widgets to |
|
|
12 |
* apply a given schema to a variety of data. |
|
|
13 |
* |
|
|
14 |
* @module dataschema |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Provides the base DataSchema implementation, which can be extended to |
|
|
19 |
* create DataSchemas for specific data formats, such XML, JSON, text and |
|
|
20 |
* arrays. |
|
|
21 |
* |
|
|
22 |
* @module dataschema |
|
|
23 |
* @submodule dataschema-base |
|
|
24 |
*/ |
|
|
25 |
|
|
|
26 |
var LANG = Y.Lang, |
|
|
27 |
/** |
|
|
28 |
* Base class for the YUI DataSchema Utility. |
|
|
29 |
* @class DataSchema.Base |
|
|
30 |
* @static |
|
|
31 |
*/ |
|
|
32 |
SchemaBase = { |
|
|
33 |
/** |
|
|
34 |
* Overridable method returns data as-is. |
|
|
35 |
* |
|
|
36 |
* @method apply |
|
|
37 |
* @param schema {Object} Schema to apply. |
|
|
38 |
* @param data {Object} Data. |
|
|
39 |
* @return {Object} Schema-parsed data. |
|
|
40 |
* @static |
|
|
41 |
*/ |
|
|
42 |
apply: function(schema, data) { |
|
|
43 |
return data; |
|
|
44 |
}, |
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* Applies field parser, if defined |
|
|
48 |
* |
|
|
49 |
* @method parse |
|
|
50 |
* @param value {Object} Original value. |
|
|
51 |
* @param field {Object} Field. |
|
|
52 |
* @return {Object} Type-converted value. |
|
|
53 |
*/ |
|
|
54 |
parse: function(value, field) { |
|
|
55 |
if(field.parser) { |
|
|
56 |
var parser = (LANG.isFunction(field.parser)) ? |
|
|
57 |
field.parser : Y.Parsers[field.parser+'']; |
|
|
58 |
if(parser) { |
|
|
59 |
value = parser.call(this, value); |
|
|
60 |
} |
|
|
61 |
else { |
|
|
62 |
} |
|
|
63 |
} |
|
|
64 |
return value; |
|
|
65 |
} |
|
|
66 |
}; |
|
|
67 |
|
|
|
68 |
Y.namespace("DataSchema").Base = SchemaBase; |
|
|
69 |
Y.namespace("Parsers"); |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
}, '3.0.0b1' ,{requires:['base']}); |
|
|
74 |
|
|
|
75 |
YUI.add('dataschema-json', function(Y) { |
|
|
76 |
|
|
|
77 |
/** |
|
|
78 |
* Provides a DataSchema implementation which can be used to work with JSON data. |
|
|
79 |
* |
|
|
80 |
* @module dataschema |
|
|
81 |
* @submodule dataschema-json |
|
|
82 |
*/ |
|
|
83 |
|
|
|
84 |
/** |
|
|
85 |
* JSON subclass for the DataSchema Utility. |
|
|
86 |
* @class DataSchema.JSON |
|
|
87 |
* @extends DataSchema.Base |
|
|
88 |
* @static |
|
|
89 |
*/ |
|
|
90 |
var LANG = Y.Lang, |
|
|
91 |
|
|
|
92 |
SchemaJSON = { |
|
|
93 |
|
|
|
94 |
///////////////////////////////////////////////////////////////////////////// |
|
|
95 |
// |
|
|
96 |
// DataSchema.JSON static methods |
|
|
97 |
// |
|
|
98 |
///////////////////////////////////////////////////////////////////////////// |
|
|
99 |
/** |
|
|
100 |
* Utility function converts JSON locator strings into walkable paths |
|
|
101 |
* |
|
|
102 |
* @method DataSchema.JSON.getPath |
|
|
103 |
* @param locator {String} JSON value locator. |
|
|
104 |
* @return {String[]} Walkable path to data value. |
|
|
105 |
* @static |
|
|
106 |
*/ |
|
|
107 |
getPath: function(locator) { |
|
|
108 |
var path = null, |
|
|
109 |
keys = [], |
|
|
110 |
i = 0; |
|
|
111 |
|
|
|
112 |
if (locator) { |
|
|
113 |
// Strip the ["string keys"] and [1] array indexes |
|
|
114 |
locator = locator. |
|
|
115 |
replace(/\[(['"])(.*?)\1\]/g, |
|
|
116 |
function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}). |
|
|
117 |
replace(/\[(\d+)\]/g, |
|
|
118 |
function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}). |
|
|
119 |
replace(/^\./,''); // remove leading dot |
|
|
120 |
|
|
|
121 |
// Validate against problematic characters. |
|
|
122 |
if (!/[^\w\.\$@]/.test(locator)) { |
|
|
123 |
path = locator.split('.'); |
|
|
124 |
for (i=path.length-1; i >= 0; --i) { |
|
|
125 |
if (path[i].charAt(0) === '@') { |
|
|
126 |
path[i] = keys[parseInt(path[i].substr(1),10)]; |
|
|
127 |
} |
|
|
128 |
} |
|
|
129 |
} |
|
|
130 |
else { |
|
|
131 |
} |
|
|
132 |
} |
|
|
133 |
return path; |
|
|
134 |
}, |
|
|
135 |
|
|
|
136 |
/** |
|
|
137 |
* Utility function to walk a path and return the value located there. |
|
|
138 |
* |
|
|
139 |
* @method DataSchema.JSON.getLocationValue |
|
|
140 |
* @param path {String[]} Locator path. |
|
|
141 |
* @param data {String} Data to traverse. |
|
|
142 |
* @return {Object} Data value at location. |
|
|
143 |
* @static |
|
|
144 |
*/ |
|
|
145 |
getLocationValue: function (path, data) { |
|
|
146 |
var i = 0, |
|
|
147 |
len = path.length; |
|
|
148 |
for (;i<len;i++) { |
|
|
149 |
if(!LANG.isUndefined(data[path[i]])) { |
|
|
150 |
data = data[path[i]]; |
|
|
151 |
} |
|
|
152 |
else { |
|
|
153 |
data = undefined; |
|
|
154 |
break; |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
return data; |
|
|
158 |
}, |
|
|
159 |
|
|
|
160 |
/** |
|
|
161 |
* Applies a given schema to given JSON data. |
|
|
162 |
* |
|
|
163 |
* @method apply |
|
|
164 |
* @param schema {Object} Schema to apply. |
|
|
165 |
* @param data {Object} JSON data. |
|
|
166 |
* @return {Object} Schema-parsed data. |
|
|
167 |
* @static |
|
|
168 |
*/ |
|
|
169 |
apply: function(schema, data) { |
|
|
170 |
var data_in = data, |
|
|
171 |
data_out = {results:[],meta:{}}; |
|
|
172 |
|
|
|
173 |
// Convert incoming JSON strings |
|
|
174 |
if(!LANG.isObject(data)) { |
|
|
175 |
try { |
|
|
176 |
data_in = Y.JSON.parse(data); |
|
|
177 |
} |
|
|
178 |
catch(e) { |
|
|
179 |
data_out.error = e; |
|
|
180 |
return data_out; |
|
|
181 |
} |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
if(LANG.isObject(data_in) && schema) { |
|
|
185 |
// Parse results data |
|
|
186 |
if(!LANG.isUndefined(schema.resultListLocator)) { |
|
|
187 |
data_out = SchemaJSON._parseResults(schema, data_in, data_out); |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
// Parse meta data |
|
|
191 |
if(!LANG.isUndefined(schema.metaFields)) { |
|
|
192 |
data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out); |
|
|
193 |
} |
|
|
194 |
} |
|
|
195 |
else { |
|
|
196 |
data_out.error = new Error("JSON schema parse failure"); |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
return data_out; |
|
|
200 |
}, |
|
|
201 |
|
|
|
202 |
/** |
|
|
203 |
* Schema-parsed list of results from full data |
|
|
204 |
* |
|
|
205 |
* @method _parseResults |
|
|
206 |
* @param schema {Object} Schema to parse against. |
|
|
207 |
* @param json_in {Object} JSON to parse. |
|
|
208 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
209 |
* @return {Object} Parsed data object. |
|
|
210 |
* @static |
|
|
211 |
* @protected |
|
|
212 |
*/ |
|
|
213 |
_parseResults: function(schema, json_in, data_out) { |
|
|
214 |
var results = [], |
|
|
215 |
path, |
|
|
216 |
error; |
|
|
217 |
|
|
|
218 |
if(schema.resultListLocator) { |
|
|
219 |
path = SchemaJSON.getPath(schema.resultListLocator); |
|
|
220 |
if(path) { |
|
|
221 |
results = SchemaJSON.getLocationValue(path, json_in); |
|
|
222 |
if (results === undefined) { |
|
|
223 |
data_out.results = []; |
|
|
224 |
error = new Error("JSON results retrieval failure"); |
|
|
225 |
} |
|
|
226 |
else { |
|
|
227 |
if(LANG.isArray(schema.resultFields) && LANG.isArray(results)) { |
|
|
228 |
data_out = SchemaJSON._getFieldValues(schema.resultFields, results, data_out); |
|
|
229 |
} |
|
|
230 |
else { |
|
|
231 |
data_out.results = []; |
|
|
232 |
error = new Error("JSON Schema fields retrieval failure"); |
|
|
233 |
} |
|
|
234 |
} |
|
|
235 |
} |
|
|
236 |
else { |
|
|
237 |
error = new Error("JSON Schema results locator failure"); |
|
|
238 |
} |
|
|
239 |
|
|
|
240 |
if (error) { |
|
|
241 |
data_out.error = error; |
|
|
242 |
} |
|
|
243 |
|
|
|
244 |
} |
|
|
245 |
return data_out; |
|
|
246 |
}, |
|
|
247 |
|
|
|
248 |
/** |
|
|
249 |
* Get field data values out of list of full results |
|
|
250 |
* |
|
|
251 |
* @method _getFieldValues |
|
|
252 |
* @param fields {Array} Fields to find. |
|
|
253 |
* @param array_in {Array} Results to parse. |
|
|
254 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
255 |
* @return {Object} Parsed data object. |
|
|
256 |
* @static |
|
|
257 |
* @protected |
|
|
258 |
*/ |
|
|
259 |
_getFieldValues: function(fields, array_in, data_out) { |
|
|
260 |
var results = [], |
|
|
261 |
len = fields.length, |
|
|
262 |
i, j, |
|
|
263 |
field, key, path, parser, |
|
|
264 |
simplePaths = [], complexPaths = [], fieldParsers = [], |
|
|
265 |
result, record; |
|
|
266 |
|
|
|
267 |
// First collect hashes of simple paths, complex paths, and parsers |
|
|
268 |
for (i=0; i<len; i++) { |
|
|
269 |
field = fields[i]; // A field can be a simple string or a hash |
|
|
270 |
key = field.key || field; // Find the key |
|
|
271 |
|
|
|
272 |
// Validate and store locators for later |
|
|
273 |
path = SchemaJSON.getPath(key); |
|
|
274 |
if (path) { |
|
|
275 |
if (path.length === 1) { |
|
|
276 |
simplePaths[simplePaths.length] = {key:key, path:path[0]}; |
|
|
277 |
} else { |
|
|
278 |
complexPaths[complexPaths.length] = {key:key, path:path}; |
|
|
279 |
} |
|
|
280 |
} else { |
|
|
281 |
} |
|
|
282 |
|
|
|
283 |
// Validate and store parsers for later |
|
|
284 |
//TODO: use Y.DataSchema.parse? |
|
|
285 |
parser = (LANG.isFunction(field.parser)) ? field.parser : Y.Parsers[field.parser+'']; |
|
|
286 |
if (parser) { |
|
|
287 |
fieldParsers[fieldParsers.length] = {key:key, parser:parser}; |
|
|
288 |
} |
|
|
289 |
} |
|
|
290 |
|
|
|
291 |
// Traverse list of array_in, creating records of simple fields, |
|
|
292 |
// complex fields, and applying parsers as necessary |
|
|
293 |
for (i=array_in.length-1; i>=0; --i) { |
|
|
294 |
record = {}; |
|
|
295 |
result = array_in[i]; |
|
|
296 |
if(result) { |
|
|
297 |
// Cycle through simpleLocators |
|
|
298 |
for (j=simplePaths.length-1; j>=0; --j) { |
|
|
299 |
// Bug 1777850: The result might be an array instead of object |
|
|
300 |
record[simplePaths[j].key] = Y.DataSchema.Base.parse( |
|
|
301 |
(LANG.isUndefined(result[simplePaths[j].path]) ? |
|
|
302 |
result[j] : result[simplePaths[j].path]), simplePaths[j]); |
|
|
303 |
} |
|
|
304 |
|
|
|
305 |
// Cycle through complexLocators |
|
|
306 |
for (j=complexPaths.length - 1; j>=0; --j) { |
|
|
307 |
record[complexPaths[j].key] = Y.DataSchema.Base.parse( |
|
|
308 |
(SchemaJSON.getLocationValue(complexPaths[j].path, result)), complexPaths[j] ); |
|
|
309 |
} |
|
|
310 |
|
|
|
311 |
// Cycle through fieldParsers |
|
|
312 |
for (j=fieldParsers.length-1; j>=0; --j) { |
|
|
313 |
key = fieldParsers[j].key; |
|
|
314 |
record[key] = fieldParsers[j].parser(record[key]); |
|
|
315 |
// Safety net |
|
|
316 |
if (LANG.isUndefined(record[key])) { |
|
|
317 |
record[key] = null; |
|
|
318 |
} |
|
|
319 |
} |
|
|
320 |
results[i] = record; |
|
|
321 |
} |
|
|
322 |
} |
|
|
323 |
data_out.results = results; |
|
|
324 |
return data_out; |
|
|
325 |
}, |
|
|
326 |
|
|
|
327 |
/** |
|
|
328 |
* Parses results data according to schema |
|
|
329 |
* |
|
|
330 |
* @method _parseMeta |
|
|
331 |
* @param metaFields {Object} Metafields definitions. |
|
|
332 |
* @param json_in {Object} JSON to parse. |
|
|
333 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
334 |
* @return {Object} Schema-parsed meta data. |
|
|
335 |
* @static |
|
|
336 |
* @protected |
|
|
337 |
*/ |
|
|
338 |
_parseMeta: function(metaFields, json_in, data_out) { |
|
|
339 |
if(LANG.isObject(metaFields)) { |
|
|
340 |
var key, path; |
|
|
341 |
for(key in metaFields) { |
|
|
342 |
if (metaFields.hasOwnProperty(key)) { |
|
|
343 |
path = SchemaJSON.getPath(metaFields[key]); |
|
|
344 |
if (path && json_in) { |
|
|
345 |
data_out.meta[key] = SchemaJSON.getLocationValue(path, json_in); |
|
|
346 |
} |
|
|
347 |
} |
|
|
348 |
} |
|
|
349 |
} |
|
|
350 |
else { |
|
|
351 |
data_out.error = new Error("JSON meta data retrieval failure"); |
|
|
352 |
} |
|
|
353 |
return data_out; |
|
|
354 |
} |
|
|
355 |
}; |
|
|
356 |
|
|
|
357 |
Y.DataSchema.JSON = Y.mix(SchemaJSON, Y.DataSchema.Base); |
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
}, '3.0.0b1' ,{requires:['json', 'dataschema-base']}); |
|
|
362 |
|
|
|
363 |
YUI.add('dataschema-xml', function(Y) { |
|
|
364 |
|
|
|
365 |
/** |
|
|
366 |
* Provides a DataSchema implementation which can be used to work with XML data. |
|
|
367 |
* |
|
|
368 |
* @module dataschema |
|
|
369 |
* @submodule dataschema-xml |
|
|
370 |
*/ |
|
|
371 |
var LANG = Y.Lang, |
|
|
372 |
|
|
|
373 |
/** |
|
|
374 |
* XML subclass for the DataSchema Utility. |
|
|
375 |
* @class DataSchema.XML |
|
|
376 |
* @extends DataSchema.Base |
|
|
377 |
* @static |
|
|
378 |
*/ |
|
|
379 |
SchemaXML = { |
|
|
380 |
|
|
|
381 |
///////////////////////////////////////////////////////////////////////////// |
|
|
382 |
// |
|
|
383 |
// DataSchema.XML static methods |
|
|
384 |
// |
|
|
385 |
///////////////////////////////////////////////////////////////////////////// |
|
|
386 |
/** |
|
|
387 |
* Applies a given schema to given XML data. |
|
|
388 |
* |
|
|
389 |
* @method apply |
|
|
390 |
* @param schema {Object} Schema to apply. |
|
|
391 |
* @param data {XMLDoc} XML document. |
|
|
392 |
* @return {Object} Schema-parsed data. |
|
|
393 |
* @static |
|
|
394 |
*/ |
|
|
395 |
apply: function(schema, data) { |
|
|
396 |
var xmldoc = data, |
|
|
397 |
data_out = {results:[],meta:{}}; |
|
|
398 |
|
|
|
399 |
if(xmldoc && xmldoc.nodeType && xmldoc.nodeType === 9 && schema) { |
|
|
400 |
// Parse results data |
|
|
401 |
data_out = SchemaXML._parseResults(schema, xmldoc, data_out); |
|
|
402 |
|
|
|
403 |
// Parse meta data |
|
|
404 |
data_out = SchemaXML._parseMeta(schema.metaFields, xmldoc, data_out); |
|
|
405 |
} |
|
|
406 |
else { |
|
|
407 |
data_out.error = new Error("XML schema parse failure"); |
|
|
408 |
} |
|
|
409 |
|
|
|
410 |
return data_out; |
|
|
411 |
}, |
|
|
412 |
|
|
|
413 |
/** |
|
|
414 |
* Get an XPath-specified value for a given field from an XML node or document. |
|
|
415 |
* |
|
|
416 |
* @method _getLocationValue |
|
|
417 |
* @param field {String | Object} Field definition. |
|
|
418 |
* @param context {Object} XML node or document to search within. |
|
|
419 |
* @return {Object} Data value or null. |
|
|
420 |
* @static |
|
|
421 |
* @protected |
|
|
422 |
*/ |
|
|
423 |
_getLocationValue: function(field, context) { |
|
|
424 |
var locator = field.locator || field.key || field, |
|
|
425 |
xmldoc = context.ownerDocument || context, |
|
|
426 |
result, res, value = null; |
|
|
427 |
|
|
|
428 |
try { |
|
|
429 |
// Standards mode |
|
|
430 |
if(!LANG.isUndefined(xmldoc.evaluate)) { |
|
|
431 |
result = xmldoc.evaluate(locator, context, xmldoc.createNSResolver(!context.ownerDocument ? context.documentElement : context.ownerDocument.documentElement), 0, null); |
|
|
432 |
while(res = result.iterateNext()) { |
|
|
433 |
value = res.textContent; |
|
|
434 |
} |
|
|
435 |
} |
|
|
436 |
// IE mode |
|
|
437 |
else { |
|
|
438 |
xmldoc.setProperty("SelectionLanguage", "XPath"); |
|
|
439 |
result = context.selectNodes(locator)[0]; |
|
|
440 |
value = result.value || result.text || null; |
|
|
441 |
} |
|
|
442 |
return Y.DataSchema.Base.parse(value, field); |
|
|
443 |
|
|
|
444 |
} |
|
|
445 |
catch(e) { |
|
|
446 |
} |
|
|
447 |
}, |
|
|
448 |
|
|
|
449 |
/** |
|
|
450 |
* Parses results data according to schema |
|
|
451 |
* |
|
|
452 |
* @method _parseMeta |
|
|
453 |
* @param xmldoc_in {Object} XML document parse. |
|
|
454 |
* @param data_out {Object} In-progress schema-parsed data to update. |
|
|
455 |
* @return {Object} Schema-parsed data. |
|
|
456 |
* @static |
|
|
457 |
* @protected |
|
|
458 |
*/ |
|
|
459 |
_parseMeta: function(metaFields, xmldoc_in, data_out) { |
|
|
460 |
if(LANG.isObject(metaFields)) { |
|
|
461 |
var key, |
|
|
462 |
xmldoc = xmldoc_in.ownerDocument || xmldoc_in; |
|
|
463 |
|
|
|
464 |
for(key in metaFields) { |
|
|
465 |
if (metaFields.hasOwnProperty(key)) { |
|
|
466 |
data_out.meta[key] = SchemaXML._getLocationValue(metaFields[key], xmldoc); |
|
|
467 |
} |
|
|
468 |
} |
|
|
469 |
} |
|
|
470 |
return data_out; |
|
|
471 |
}, |
|
|
472 |
|
|
|
473 |
/** |
|
|
474 |
* Schema-parsed list of results from full data |
|
|
475 |
* |
|
|
476 |
* @method _parseResults |
|
|
477 |
* @param schema {Object} Schema to parse against. |
|
|
478 |
* @param xmldoc_in {Object} XML document parse. |
|
|
479 |
* @param data_out {Object} In-progress schema-parsed data to update. |
|
|
480 |
* @return {Object} Schema-parsed data. |
|
|
481 |
* @static |
|
|
482 |
* @protected |
|
|
483 |
*/ |
|
|
484 |
_parseResults: function(schema, xmldoc_in, data_out) { |
|
|
485 |
if(schema.resultListLocator && LANG.isArray(schema.resultFields)) { |
|
|
486 |
var nodeList = xmldoc_in.getElementsByTagName(schema.resultListLocator), |
|
|
487 |
fields = schema.resultFields, |
|
|
488 |
results = [], |
|
|
489 |
node, field, result, i, j; |
|
|
490 |
|
|
|
491 |
if(nodeList.length) { |
|
|
492 |
// Loop through each result node |
|
|
493 |
for(i=nodeList.length-1; i>= 0; i--) { |
|
|
494 |
result = {}; |
|
|
495 |
node = nodeList[i]; |
|
|
496 |
|
|
|
497 |
// Find each field value |
|
|
498 |
for(j=fields.length-1; j>= 0; j--) { |
|
|
499 |
field = fields[j]; |
|
|
500 |
result[field.key || field] = SchemaXML._getLocationValue(field, node); |
|
|
501 |
} |
|
|
502 |
results[i] = result; |
|
|
503 |
} |
|
|
504 |
|
|
|
505 |
data_out.results = results; |
|
|
506 |
} |
|
|
507 |
else { |
|
|
508 |
data_out.error = new Error("XML schema result nodes retrieval failure"); |
|
|
509 |
} |
|
|
510 |
} |
|
|
511 |
return data_out; |
|
|
512 |
} |
|
|
513 |
}; |
|
|
514 |
|
|
|
515 |
Y.DataSchema.XML = Y.mix(SchemaXML, Y.DataSchema.Base); |
|
|
516 |
|
|
|
517 |
|
|
|
518 |
|
|
|
519 |
}, '3.0.0b1' ,{requires:['dataschema-base']}); |
|
|
520 |
|
|
|
521 |
YUI.add('dataschema-array', function(Y) { |
|
|
522 |
|
|
|
523 |
/** |
|
|
524 |
* Provides a DataSchema implementation which can be used to work with data stored in arrays. |
|
|
525 |
* |
|
|
526 |
* @module dataschema |
|
|
527 |
* @submodule dataschema-array |
|
|
528 |
*/ |
|
|
529 |
|
|
|
530 |
/** |
|
|
531 |
* Array subclass for the DataSchema Utility. |
|
|
532 |
* @class DataSchema.Array |
|
|
533 |
* @extends DataSchema.Base |
|
|
534 |
* @static |
|
|
535 |
*/ |
|
|
536 |
var LANG = Y.Lang, |
|
|
537 |
|
|
|
538 |
SchemaArray = { |
|
|
539 |
|
|
|
540 |
///////////////////////////////////////////////////////////////////////////// |
|
|
541 |
// |
|
|
542 |
// DataSchema.Array static methods |
|
|
543 |
// |
|
|
544 |
///////////////////////////////////////////////////////////////////////////// |
|
|
545 |
/** |
|
|
546 |
* Applies a given schema to given Array data. |
|
|
547 |
* |
|
|
548 |
* @method apply |
|
|
549 |
* @param schema {Object} Schema to apply. |
|
|
550 |
* @param data {Object} Array data. |
|
|
551 |
* @return {Object} Schema-parsed data. |
|
|
552 |
* @static |
|
|
553 |
*/ |
|
|
554 |
apply: function(schema, data) { |
|
|
555 |
var data_in = data, |
|
|
556 |
data_out = {results:[],meta:{}}; |
|
|
557 |
|
|
|
558 |
if(LANG.isArray(data_in)) { |
|
|
559 |
if(LANG.isArray(schema.resultFields)) { |
|
|
560 |
// Parse results data |
|
|
561 |
data_out = SchemaArray._parseResults(schema.resultFields, data_in, data_out); |
|
|
562 |
} |
|
|
563 |
else { |
|
|
564 |
data_out.results = data_in; |
|
|
565 |
} |
|
|
566 |
} |
|
|
567 |
else { |
|
|
568 |
data_out.error = new Error("Array schema parse failure"); |
|
|
569 |
} |
|
|
570 |
|
|
|
571 |
return data_out; |
|
|
572 |
}, |
|
|
573 |
|
|
|
574 |
/** |
|
|
575 |
* Schema-parsed list of results from full data |
|
|
576 |
* |
|
|
577 |
* @method _parseResults |
|
|
578 |
* @param fields {Array} Schema to parse against. |
|
|
579 |
* @param array_in {Array} Array to parse. |
|
|
580 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
581 |
* @return {Object} Parsed data object. |
|
|
582 |
* @static |
|
|
583 |
* @protected |
|
|
584 |
*/ |
|
|
585 |
_parseResults: function(fields, array_in, data_out) { |
|
|
586 |
var results = [], |
|
|
587 |
result, item, type, field, key, value, i, j; |
|
|
588 |
|
|
|
589 |
for(i=array_in.length-1; i>-1; i--) { |
|
|
590 |
result = {}; |
|
|
591 |
item = array_in[i]; |
|
|
592 |
type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1; |
|
|
593 |
if(type > 0) { |
|
|
594 |
for(j=fields.length-1; j>-1; j--) { |
|
|
595 |
field = fields[j]; |
|
|
596 |
key = (!LANG.isUndefined(field.key)) ? field.key : field; |
|
|
597 |
value = (!LANG.isUndefined(item[key])) ? item[key] : item[j]; |
|
|
598 |
result[key] = Y.DataSchema.Base.parse(value, field); |
|
|
599 |
} |
|
|
600 |
} |
|
|
601 |
else if(type === 0) { |
|
|
602 |
result = item; |
|
|
603 |
} |
|
|
604 |
else { |
|
|
605 |
//TODO: null or {}? |
|
|
606 |
result = null; |
|
|
607 |
} |
|
|
608 |
results[i] = result; |
|
|
609 |
} |
|
|
610 |
data_out.results = results; |
|
|
611 |
|
|
|
612 |
return data_out; |
|
|
613 |
} |
|
|
614 |
}; |
|
|
615 |
|
|
|
616 |
Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base); |
|
|
617 |
|
|
|
618 |
|
|
|
619 |
|
|
|
620 |
}, '3.0.0b1' ,{requires:['dataschema-base']}); |
|
|
621 |
|
|
|
622 |
YUI.add('dataschema-text', function(Y) { |
|
|
623 |
|
|
|
624 |
/** |
|
|
625 |
* Provides a DataSchema implementation which can be used to work with delimited text data. |
|
|
626 |
* |
|
|
627 |
* @module dataschema |
|
|
628 |
* @submodule dataschema-text |
|
|
629 |
*/ |
|
|
630 |
|
|
|
631 |
/** |
|
|
632 |
* Text subclass for the DataSchema Utility. |
|
|
633 |
* @class DataSchema.Text |
|
|
634 |
* @extends DataSchema.Base |
|
|
635 |
* @static |
|
|
636 |
*/ |
|
|
637 |
|
|
|
638 |
var LANG = Y.Lang, |
|
|
639 |
|
|
|
640 |
SchemaText = { |
|
|
641 |
|
|
|
642 |
///////////////////////////////////////////////////////////////////////////// |
|
|
643 |
// |
|
|
644 |
// DataSchema.Text static methods |
|
|
645 |
// |
|
|
646 |
///////////////////////////////////////////////////////////////////////////// |
|
|
647 |
/** |
|
|
648 |
* Applies a given schema to given delimited text data. |
|
|
649 |
* |
|
|
650 |
* @method apply |
|
|
651 |
* @param schema {Object} Schema to apply. |
|
|
652 |
* @param data {Object} Text data. |
|
|
653 |
* @return {Object} Schema-parsed data. |
|
|
654 |
* @static |
|
|
655 |
*/ |
|
|
656 |
apply: function(schema, data) { |
|
|
657 |
var data_in = data, |
|
|
658 |
data_out = {results:[],meta:{}}; |
|
|
659 |
|
|
|
660 |
if(LANG.isString(data_in) && LANG.isString(schema.resultDelimiter)) { |
|
|
661 |
// Parse results data |
|
|
662 |
data_out = SchemaText._parseResults(schema, data_in, data_out); |
|
|
663 |
} |
|
|
664 |
else { |
|
|
665 |
data_out.error = new Error("Text schema parse failure"); |
|
|
666 |
} |
|
|
667 |
|
|
|
668 |
return data_out; |
|
|
669 |
}, |
|
|
670 |
|
|
|
671 |
/** |
|
|
672 |
* Schema-parsed list of results from full data |
|
|
673 |
* |
|
|
674 |
* @method _parseResults |
|
|
675 |
* @param schema {Array} Schema to parse against. |
|
|
676 |
* @param text_in {String} Text to parse. |
|
|
677 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
678 |
* @return {Object} Parsed data object. |
|
|
679 |
* @static |
|
|
680 |
* @protected |
|
|
681 |
*/ |
|
|
682 |
_parseResults: function(schema, text_in, data_out) { |
|
|
683 |
var resultDelim = schema.resultDelimiter, |
|
|
684 |
results = [], |
|
|
685 |
results_in, fields_in, result, item, fields, field, key, value, i, j, |
|
|
686 |
|
|
|
687 |
// Delete final delimiter at end of string if there |
|
|
688 |
tmpLength = text_in.length-resultDelim.length; |
|
|
689 |
if(text_in.substr(tmpLength) == resultDelim) { |
|
|
690 |
text_in = text_in.substr(0, tmpLength); |
|
|
691 |
} |
|
|
692 |
|
|
|
693 |
// Split into results |
|
|
694 |
results_in = text_in.split(schema.resultDelimiter); |
|
|
695 |
|
|
|
696 |
for(i=results_in.length-1; i>-1; i--) { |
|
|
697 |
result = {}; |
|
|
698 |
item = results_in[i]; |
|
|
699 |
|
|
|
700 |
if(LANG.isString(schema.fieldDelimiter)) { |
|
|
701 |
fields_in = item.split(schema.fieldDelimiter); |
|
|
702 |
|
|
|
703 |
if(LANG.isArray(schema.resultFields)) { |
|
|
704 |
fields = schema.resultFields; |
|
|
705 |
for(j=fields.length-1; j>-1; j--) { |
|
|
706 |
field = fields[j]; |
|
|
707 |
key = (!LANG.isUndefined(field.key)) ? field.key : field; |
|
|
708 |
value = (!LANG.isUndefined(fields_in[key])) ? fields_in[key] : fields_in[j]; |
|
|
709 |
result[key] = Y.DataSchema.Base.parse(value, field); |
|
|
710 |
} |
|
|
711 |
} |
|
|
712 |
|
|
|
713 |
} |
|
|
714 |
else { |
|
|
715 |
result = item; |
|
|
716 |
} |
|
|
717 |
|
|
|
718 |
results[i] = result; |
|
|
719 |
} |
|
|
720 |
data_out.results = results; |
|
|
721 |
|
|
|
722 |
return data_out; |
|
|
723 |
} |
|
|
724 |
}; |
|
|
725 |
|
|
|
726 |
Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base); |
|
|
727 |
|
|
|
728 |
|
|
|
729 |
|
|
|
730 |
}, '3.0.0b1' ,{requires:['dataschema-base']}); |
|
|
731 |
|
|
|
732 |
|
|
|
733 |
|
|
|
734 |
YUI.add('dataschema', function(Y){}, '3.0.0b1' ,{use:['dataschema-base','dataschema-json','dataschema-xml','dataschema-array','dataschema-text']}); |
|
|
735 |
|