2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('dataschema-base', function(Y) {
11 * The DataSchema utility provides a common configurable interface for widgets to
12 * apply a given schema to a variety of data.
18 * Provides the base DataSchema implementation, which can be extended to
19 * create DataSchemas for specific data formats, such XML, JSON, text and
23 * @submodule dataschema-base
28 * Base class for the YUI DataSchema Utility.
29 * @class DataSchema.Base
34 * Overridable method returns data as-is.
37 * @param schema {Object} Schema to apply.
38 * @param data {Object} Data.
39 * @return {Object} Schema-parsed data.
42 apply: function(schema, data) {
47 * Applies field parser, if defined
50 * @param value {Object} Original value.
51 * @param field {Object} Field.
52 * @return {Object} Type-converted value.
54 parse: function(value, field) {
56 var parser = (LANG.isFunction(field.parser)) ?
57 field.parser : Y.Parsers[field.parser+''];
59 value = parser.call(this, value);
68 Y.namespace("DataSchema").Base = SchemaBase;
69 Y.namespace("Parsers");
73 }, '3.0.0' ,{requires:['base']});
75 YUI.add('dataschema-json', function(Y) {
78 * Provides a DataSchema implementation which can be used to work with JSON data.
81 * @submodule dataschema-json
85 * JSON subclass for the DataSchema Utility.
86 * @class DataSchema.JSON
87 * @extends DataSchema.Base
94 /////////////////////////////////////////////////////////////////////////////
96 // DataSchema.JSON static methods
98 /////////////////////////////////////////////////////////////////////////////
100 * Utility function converts JSON locator strings into walkable paths
102 * @method DataSchema.JSON.getPath
103 * @param locator {String} JSON value locator.
104 * @return {String[]} Walkable path to data value.
107 getPath: function(locator) {
113 // Strip the ["string keys"] and [1] array indexes
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
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)];
137 * Utility function to walk a path and return the value located there.
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.
145 getLocationValue: function (path, data) {
149 if(!LANG.isUndefined(data[path[i]])) {
150 data = data[path[i]];
161 * Applies a given schema to given JSON data.
164 * @param schema {Object} Schema to apply.
165 * @param data {Object} JSON data.
166 * @return {Object} Schema-parsed data.
169 apply: function(schema, data) {
171 data_out = {results:[],meta:{}};
173 // Convert incoming JSON strings
174 if(!LANG.isObject(data)) {
176 data_in = Y.JSON.parse(data);
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);
191 if(!LANG.isUndefined(schema.metaFields)) {
192 data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out);
196 data_out.error = new Error("JSON schema parse failure");
203 * Schema-parsed list of results from full data
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.
213 _parseResults: function(schema, json_in, data_out) {
218 if(schema.resultListLocator) {
219 path = SchemaJSON.getPath(schema.resultListLocator);
221 results = SchemaJSON.getLocationValue(path, json_in);
222 if (results === undefined) {
223 data_out.results = [];
224 error = new Error("JSON results retrieval failure");
227 if(LANG.isArray(schema.resultFields) && LANG.isArray(results)) {
228 data_out = SchemaJSON._getFieldValues(schema.resultFields, results, data_out);
231 data_out.results = [];
232 error = new Error("JSON Schema fields retrieval failure");
237 error = new Error("JSON Schema results locator failure");
241 data_out.error = error;
249 * Get field data values out of list of full results
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.
259 _getFieldValues: function(fields, array_in, data_out) {
263 field, key, path, parser,
264 simplePaths = [], complexPaths = [], fieldParsers = [],
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
272 // Validate and store locators for later
273 path = SchemaJSON.getPath(key);
275 if (path.length === 1) {
276 simplePaths[simplePaths.length] = {key:key, path:path[0]};
278 complexPaths[complexPaths.length] = {key:key, path:path};
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+''];
287 fieldParsers[fieldParsers.length] = {key:key, parser:parser};
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) {
295 result = array_in[i];
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]);
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] );
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]);
316 if (LANG.isUndefined(record[key])) {
323 data_out.results = results;
328 * Parses results data according to schema
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.
338 _parseMeta: function(metaFields, json_in, data_out) {
339 if(LANG.isObject(metaFields)) {
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);
351 data_out.error = new Error("JSON meta data retrieval failure");
357 Y.DataSchema.JSON = Y.mix(SchemaJSON, Y.DataSchema.Base);
361 }, '3.0.0' ,{requires:['json', 'dataschema-base']});
363 YUI.add('dataschema-xml', function(Y) {
366 * Provides a DataSchema implementation which can be used to work with XML data.
369 * @submodule dataschema-xml
374 * XML subclass for the DataSchema Utility.
375 * @class DataSchema.XML
376 * @extends DataSchema.Base
381 /////////////////////////////////////////////////////////////////////////////
383 // DataSchema.XML static methods
385 /////////////////////////////////////////////////////////////////////////////
387 * Applies a given schema to given XML data.
390 * @param schema {Object} Schema to apply.
391 * @param data {XMLDoc} XML document.
392 * @return {Object} Schema-parsed data.
395 apply: function(schema, data) {
397 data_out = {results:[],meta:{}};
399 if(xmldoc && xmldoc.nodeType && (xmldoc.nodeType === 9 || xmldoc.nodeType === 1 || xmldoc.nodeType === 11) && schema) {
400 // Parse results data
401 data_out = SchemaXML._parseResults(schema, xmldoc, data_out);
404 data_out = SchemaXML._parseMeta(schema.metaFields, xmldoc, data_out);
407 data_out.error = new Error("XML schema parse failure");
414 * Get an XPath-specified value for a given field from an XML node or document.
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.
423 _getLocationValue: function(field, context) {
424 var locator = field.locator || field.key || field,
425 xmldoc = context.ownerDocument || context,
426 result, res, value = null;
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;
438 xmldoc.setProperty("SelectionLanguage", "XPath");
439 result = context.selectNodes(locator)[0];
440 value = result.value || result.text || null;
442 return Y.DataSchema.Base.parse(value, field);
450 * Parses results data according to schema
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.
459 _parseMeta: function(metaFields, xmldoc_in, data_out) {
460 if(LANG.isObject(metaFields)) {
462 xmldoc = xmldoc_in.ownerDocument || xmldoc_in;
464 for(key in metaFields) {
465 if (metaFields.hasOwnProperty(key)) {
466 data_out.meta[key] = SchemaXML._getLocationValue(metaFields[key], xmldoc);
474 * Schema-parsed list of results from full data
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.
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,
489 node, field, result, i, j;
491 if(nodeList.length) {
492 // Loop through each result node
493 for(i=nodeList.length-1; i>= 0; i--) {
497 // Find each field value
498 for(j=fields.length-1; j>= 0; j--) {
500 result[field.key || field] = SchemaXML._getLocationValue(field, node);
505 data_out.results = results;
508 data_out.error = new Error("XML schema result nodes retrieval failure");
515 Y.DataSchema.XML = Y.mix(SchemaXML, Y.DataSchema.Base);
519 }, '3.0.0' ,{requires:['dataschema-base']});
521 YUI.add('dataschema-array', function(Y) {
524 * Provides a DataSchema implementation which can be used to work with data stored in arrays.
527 * @submodule dataschema-array
531 * Array subclass for the DataSchema Utility.
532 * @class DataSchema.Array
533 * @extends DataSchema.Base
540 /////////////////////////////////////////////////////////////////////////////
542 // DataSchema.Array static methods
544 /////////////////////////////////////////////////////////////////////////////
546 * Applies a given schema to given Array data.
549 * @param schema {Object} Schema to apply.
550 * @param data {Object} Array data.
551 * @return {Object} Schema-parsed data.
554 apply: function(schema, data) {
556 data_out = {results:[],meta:{}};
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);
564 data_out.results = data_in;
568 data_out.error = new Error("Array schema parse failure");
575 * Schema-parsed list of results from full data
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.
585 _parseResults: function(fields, array_in, data_out) {
587 result, item, type, field, key, value, i, j;
589 for(i=array_in.length-1; i>-1; i--) {
592 type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
594 for(j=fields.length-1; j>-1; 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);
601 else if(type === 0) {
610 data_out.results = results;
616 Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
620 }, '3.0.0' ,{requires:['dataschema-base']});
622 YUI.add('dataschema-text', function(Y) {
625 * Provides a DataSchema implementation which can be used to work with delimited text data.
628 * @submodule dataschema-text
632 * Text subclass for the DataSchema Utility.
633 * @class DataSchema.Text
634 * @extends DataSchema.Base
642 /////////////////////////////////////////////////////////////////////////////
644 // DataSchema.Text static methods
646 /////////////////////////////////////////////////////////////////////////////
648 * Applies a given schema to given delimited text data.
651 * @param schema {Object} Schema to apply.
652 * @param data {Object} Text data.
653 * @return {Object} Schema-parsed data.
656 apply: function(schema, data) {
658 data_out = {results:[],meta:{}};
660 if(LANG.isString(data_in) && LANG.isString(schema.resultDelimiter)) {
661 // Parse results data
662 data_out = SchemaText._parseResults(schema, data_in, data_out);
665 data_out.error = new Error("Text schema parse failure");
672 * Schema-parsed list of results from full data
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.
682 _parseResults: function(schema, text_in, data_out) {
683 var resultDelim = schema.resultDelimiter,
685 results_in, fields_in, result, item, fields, field, key, value, i, j,
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);
693 // Split into results
694 results_in = text_in.split(schema.resultDelimiter);
696 for(i=results_in.length-1; i>-1; i--) {
698 item = results_in[i];
700 if(LANG.isString(schema.fieldDelimiter)) {
701 fields_in = item.split(schema.fieldDelimiter);
703 if(LANG.isArray(schema.resultFields)) {
704 fields = schema.resultFields;
705 for(j=fields.length-1; j>-1; 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);
720 data_out.results = results;
726 Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base);
730 }, '3.0.0' ,{requires:['dataschema-base']});
734 YUI.add('dataschema', function(Y){}, '3.0.0' ,{use:['dataschema-base','dataschema-json','dataschema-xml','dataschema-array','dataschema-text']});