dataschema.js revision 1fad80225c335240e31e0350a65463a0bd7a9b1a
/**
* The DataSchema utility provides a common configurable interface for widgets to
* apply a given schema to a variety of data.
*
* @module dataschema
*/
/**
* Provides the base DataSchema implementation, which can be extended to
* create DataSchemas for specific data formats, such XML, JSON, text and
* arrays.
*
* @module dataschema
* @submodule dataschema-base
*/
/**
* Base class for the YUI DataSchema Utility.
* @class DataSchema.Base
* @static
*/
SchemaBase = {
/**
* Overridable method returns data as-is.
*
* @method apply
* @param schema {Object} Schema to apply.
* @param data {Object} Data.
* @return {Object} Schema-parsed data.
* @static
*/
return data;
},
/**
* Applies field parser, if defined
*
* @method parse
* @param value {Object} Original value.
* @param field {Object} Field.
* @return {Object} Type-converted value.
*/
if(parser) {
}
else {
}
}
return value;
}
};
Y.namespace("Parsers");
/**
* Provides a DataSchema implementation which can be used to work with JSON data.
*
* @module dataschema
* @submodule dataschema-json
*/
/**
* JSON subclass for the DataSchema Utility.
* @class DataSchema.JSON
* @extends DataSchema.Base
* @static
*/
SchemaJSON = {
/////////////////////////////////////////////////////////////////////////////
//
// DataSchema.JSON static methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Utility function converts JSON locator strings into walkable paths
*
* @method DataSchema.JSON.getPath
* @param locator {String} JSON value locator.
* @return {String[]} Walkable path to data value.
* @static
*/
var path = null,
keys = [],
i = 0;
if (locator) {
// Strip the ["string keys"] and [1] array indexes
replace(/\[(['"])(.*?)\1\]/g,
function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}).
replace(/\[(\d+)\]/g,
function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}).
replace(/^\./,''); // remove leading dot
// Validate against problematic characters.
if (!/[^\w\.\$@]/.test(locator)) {
path = locator.split('.');
for (i=path.length-1; i >= 0; --i) {
if (path[i].charAt(0) === '@') {
path[i] = keys[parseInt(path[i].substr(1),10)];
}
}
}
else {
}
}
return path;
},
/**
* Utility function to walk a path and return the value located there.
*
* @method DataSchema.JSON.getLocationValue
* @param path {String[]} Locator path.
* @param data {String} Data to traverse.
* @return {Object} Data value at location.
* @static
*/
getLocationValue: function (path, data) {
var i = 0,
len = path.length;
for (;i<len;i++) {
if(
LANG.isObject(data) &&
(path[i] in data)
) {
data = data[path[i]];
}
else {
data = undefined;
break;
}
}
return data;
},
/**
* Applies a given schema to given JSON data.
*
* @method apply
* @param schema {Object} Schema to apply.
* @param data {Object} JSON data.
* @return {Object} Schema-parsed data.
* @static
*/
apply: function(schema, data) {
var data_in = data,
data_out = {results:[],meta:{}};
// Convert incoming JSON strings
if(!LANG.isObject(data)) {
try {
data_in = Y.JSON.parse(data);
}
catch(e) {
data_out.error = e;
return data_out;
}
}
if(LANG.isObject(data_in) && schema) {
// Parse results data
if(!LANG.isUndefined(schema.resultListLocator)) {
data_out = SchemaJSON._parseResults.call(this, schema, data_in, data_out);
}
// Parse meta data
if(!LANG.isUndefined(schema.metaFields)) {
data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out);
}
}
else {
data_out.error = new Error("JSON schema parse failure");
}
return data_out;
},
/**
* Schema-parsed list of results from full data
*
* @method _parseResults
* @param schema {Object} Schema to parse against.
* @param json_in {Object} JSON to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Parsed data object.
* @static
* @protected
*/
_parseResults: function(schema, json_in, data_out) {
var results = [],
path,
error;
if(schema.resultListLocator) {
path = SchemaJSON.getPath(schema.resultListLocator);
if(path) {
results = SchemaJSON.getLocationValue(path, json_in);
if (results === undefined) {
data_out.results = [];
error = new Error("JSON results retrieval failure");
}
else {
if(LANG.isArray(results)) {
// if no result fields are passed in, then just take the results array whole-hog
// so resultFields don't make sense.
}
else {
}
}
else {
}
}
}
else {
}
if (error) {
}
}
return data_out;
},
/**
* Get field data values out of list of full results
*
* @method _getFieldValues
* @param fields {Array} Fields to find.
* @param array_in {Array} Results to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Parsed data object.
* @static
* @protected
*/
var results = [],
i, j,
// First collect hashes of simple paths, complex paths, and parsers
for (i=0; i<len; i++) {
// Validate and store locators for later
if (path) {
} else {
}
} else {
}
// Validate and store parsers for later
//TODO: use Y.DataSchema.parse?
if (parser) {
}
}
// Traverse list of array_in, creating records of simple fields,
// complex fields, and applying parsers as necessary
record = {};
if(result) {
// Cycle through simpleLocators
// Bug 1777850: The result might be an array instead of object
}
// Cycle through complexLocators
}
// Cycle through fieldParsers
// Safety net
}
}
}
}
return data_out;
},
/**
* Parses results data according to schema
*
* @method _parseMeta
* @param metaFields {Object} Metafields definitions.
* @param json_in {Object} JSON to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Schema-parsed meta data.
* @static
* @protected
*/
for(key in metaFields) {
}
}
}
}
else {
}
return data_out;
}
};
/**
* Provides a DataSchema implementation which can be used to work with XML data.
*
* @module dataschema
* @submodule dataschema-xml
*/
/**
* XML subclass for the DataSchema Utility.
* @class DataSchema.XML
* @extends DataSchema.Base
* @static
*/
SchemaXML = {
/////////////////////////////////////////////////////////////////////////////
//
// DataSchema.XML static methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Applies a given schema to given XML data.
*
* @method apply
* @param schema {Object} Schema to apply.
* @param data {XMLDoc} XML document.
* @return {Object} Schema-parsed data.
* @static
*/
if(xmldoc && xmldoc.nodeType && (9 === xmldoc.nodeType || 1 === xmldoc.nodeType || 11 === xmldoc.nodeType) && schema) {
// Parse results data
// Parse meta data
}
else {
}
return data_out;
},
/**
* Get an XPath-specified value for a given field from an XML node or document.
*
* @method _getLocationValue
* @param field {String | Object} Field definition.
* @param context {Object} XML node or document to search within.
* @return {Object} Data value or null.
* @static
* @protected
*/
try {
}
}
catch(e) {
}
return null;
},
/**
* Fetches the XPath-specified result for a given location in an XML node or document.
*
* @param locator {String} The XPath location.
* @param context {Object} XML node or document to search within.
* @param xmldoc {Object} XML document to resolve namespace.
* @return {Object} Data collection or null.
* @static
* @protected
*/
// Standards mode
return xmldoc.evaluate(locator, context, xmldoc.createNSResolver(context.ownerDocument ? context.ownerDocument.documentElement : context.documentElement), 0, null);
}
// IE mode
else {
var values=[], locatorArray = locator.split(/\b\/\b/), i=0, l=locatorArray.length, location, subloc, m, isNth;
// XPath is supported
try {
// this fixes the IE 5.5+ issue where childnode selectors begin at 0 instead of 1
}
// Fallback for DOM nodes and fragments
catch (e) {
// Iterate over each locator piece
for (; i<l && context; i++) {
location = locatorArray[i];
// grab nth child []
//XPath is 1-based while DOM is 0-based
subloc--;
isNth = true;
}
// grab attribute value @
}
// grab that last instance of tagName
}
// find the last matching location in children
else if (l != i + 1) {
m = -1;
}
}
}
}
if (context) {
// attribute
}
// nth child
else if (isNth) {
}
// all children
else {
}
}
}
// returning a mock-standard object for IE
return {
index: 0,
iterateNext: function() {
this.index += 1;
return result;
},
};
}
},
/**
* Schema-parsed result field.
*
* @method _parseField
* @param field {String | Object} Required. Field definition.
* @param result {Object} Required. Schema parsed data object.
* @param context {Object} Required. XML node or document to search within.
* @static
* @protected
*/
result[field.key] = SchemaXML._parseResults.call(this, field.schema, context, {results:[],meta:{}}).results;
}
else {
}
},
/**
* Parses results data according to schema
*
* @method _parseMeta
* @param xmldoc_in {Object} XML document parse.
* @param data_out {Object} In-progress schema-parsed data to update.
* @return {Object} Schema-parsed data.
* @static
* @protected
*/
var key,
for(key in metaFields) {
}
}
}
return data_out;
},
/**
* Schema-parsed result to add to results list.
*
* @method _parseResult
* @param fields {Array} Required. A collection of field definition.
* @param context {Object} Required. XML node or document to search within.
* @return {Object} Schema-parsed data.
* @static
* @protected
*/
var result = {}, j;
// Find each field value
}
return result;
},
/**
* Schema-parsed list of results from full data
*
* @method _parseResults
* @param schema {Object} Schema to parse against.
* @param context {Object} XML node or document to parse.
* @param data_out {Object} In-progress schema-parsed data to update.
* @return {Object} Schema-parsed data.
* @static
* @protected
*/
results = [],
// loop through each result node
}
}
else {
// loop through the nodelist
i += 1;
}
}
}
else {
}
}
return data_out;
}
};
/**
* Provides a DataSchema implementation which can be used to work with data stored in arrays.
*
* @module dataschema
* @submodule dataschema-array
*/
/**
* Array subclass for the DataSchema Utility.
* @class DataSchema.Array
* @extends DataSchema.Base
* @static
*/
SchemaArray = {
/////////////////////////////////////////////////////////////////////////////
//
// DataSchema.Array static methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Applies a given schema to given Array data.
*
* @method apply
* @param schema {Object} Schema to apply.
* @param data {Object} Array data.
* @return {Object} Schema-parsed data.
* @static
*/
// Parse results data
}
else {
}
}
else {
}
return data_out;
},
/**
* Schema-parsed list of results from full data
*
* @method _parseResults
* @param fields {Array} Schema to parse against.
* @param array_in {Array} Array to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Parsed data object.
* @static
* @protected
*/
var results = [],
result = {};
type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
if(type > 0) {
}
}
else if(type === 0) {
}
else {
//TODO: null or {}?
result = null;
}
}
return data_out;
}
};
/**
* Provides a DataSchema implementation which can be used to work with delimited text data.
*
* @module dataschema
* @submodule dataschema-text
*/
/**
* Text subclass for the DataSchema Utility.
* @class DataSchema.Text
* @extends DataSchema.Base
* @static
*/
SchemaText = {
/////////////////////////////////////////////////////////////////////////////
//
// DataSchema.Text static methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Applies a given schema to given delimited text data.
*
* @method apply
* @param schema {Object} Schema to apply.
* @param data {Object} Text data.
* @return {Object} Schema-parsed data.
* @static
*/
// Parse results data
}
else {
}
return data_out;
},
/**
* Schema-parsed list of results from full data
*
* @method _parseResults
* @param schema {Array} Schema to parse against.
* @param text_in {String} Text to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Parsed data object.
* @static
* @protected
*/
results = [],
// Delete final delimiter at end of string if there
}
// Split into results
result = {};
item = results_in[i];
}
}
}
else {
}
}
return data_out;
}
};
YUI.add('dataschema', function(Y){}, '@VERSION@' ,{use:['dataschema-base','dataschema-json','dataschema-xml','dataschema-array','dataschema-text']});