dataschema-debug.js revision df5496ef3926fd0ea425cd4aa5eb278364c98d5c
308N/A * The DataSchema utility provides a common configurable interface for widgets to 308N/A * apply a given schema to a variety of data. 308N/A * Provides the base DataSchema implementation, which can be extended to 308N/A * create DataSchemas for specific data formats, such XML, JSON, text and 308N/A * @submodule dataschema-base 308N/A * Base class for the YUI DataSchema Utility. 308N/A * @class DataSchema.Base 308N/A * Overridable method returns data as-is. 308N/A * @param schema {Object} Schema to apply. 308N/A * @param data {Object} Data. 308N/A * @return {Object} Schema-parsed data. 308N/A * Applies field parser, if defined 308N/A * @param value {Object} Original value. 308N/A * @param field {Object} Field. 308N/A * @return {Object} Type-converted value. 308N/A Y.
log(
"Could not find parser for field " + Y.
dump(
field),
"warn",
"dataschema-json");
308N/A * Provides a DataSchema implementation which can be used to work with JSON data. 308N/A * @submodule dataschema-json 308N/A * JSON subclass for the DataSchema Utility. 308N/A * @extends DataSchema.Base 308N/A // TODO: I don't think the calls to Base.* need to be done via Base since 308N/A // Base is mixed into SchemaJSON. Investigate for later. 308N/A///////////////////////////////////////////////////////////////////////////// 308N/A///////////////////////////////////////////////////////////////////////////// 308N/A * Utility function converts JSON locator strings into walkable paths 320N/A * @param locator {String} JSON value locator. 308N/A * @return {String[]} Walkable path to data value. // Strip the ["string keys"] and [1] array indexes // TODO: the first two steps can probably be reduced to one with // /\[\s*(['"])?(.*?)\1\s*\]/g, but the array indices would be // stored as strings. This is not likely an issue. replace(/\[\s*([
'"])(.*?)\1\s*\]/g, function (x,$1,$2) {keys[i]=$2;return '.@
'+(i++);}). function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@
'+(i++);}). replace(/^\./,''); // remove leading dot // Validate against problematic characters. // should be safe. I'm not sure what makes a locator invalid. //if (!/[^\w\.\$@]/.test(locator)) { Y.log("Invalid locator: " + locator, "error", "dataschema-json"); * Utility function to walk a path and return the value located there. * @param path {String[]} Locator path. * @param data {String} Data to traverse. * @return {Object} Data value at location. * Applies a given schema to given JSON data. * @param schema {Object} Schema to apply. * @param data {Object} JSON data. * @return {Object} Schema-parsed data. // Convert incoming JSON strings //if (schema.resultListLocator !== undefined) { Y.
log(
"JSON data could not be schema-parsed: " + Y.
dump(
data) +
" " + Y.
dump(
data),
"error",
"dataschema-json");
* Schema-parsed list of results from full data * @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. // Fall back to treat resultListLocator as a simple key // Or if no resultListLocator is supplied, use the input // if no result fields are passed in, then just take // the results array whole-hog Sometimes you're getting // an array of strings, or want the whole object, so // resultFields don't make sense. Y.
log(
"JSON data could not be parsed: " + Y.
dump(
json_in),
"error",
"dataschema-json");
* 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. // First collect hashes of simple paths, complex paths, and parsers field =
fields[i];
// A field can be a simple string or a hash // Validate and store locators for later Y.
log(
"Invalid key syntax: " +
key,
"warn",
"dataschema-json");
// Validate and store parsers for later //TODO: use Y.DataSchema.parse? // Traverse list of array_in, creating records of simple fields, // complex fields, and applying parsers as necessary // Cycle through complexLocators // Fail over keys like "foo.bar" from nested parsing // to single token parsing if a value is found in // Don't try to process the path as complex // Cycle through simpleLocators // Bug 1777850: The result might be an array instead of object // Cycle through fieldParsers * Parses results data according to schema * @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. // TODO: Y.Object + mix() might be better here },
'@VERSION@' ,{
requires:[
'dataschema-base',
'json']});
YUI.
add(
'dataschema-xml',
function(Y) {
* Provides a DataSchema implementation which can be used to work with XML data. * @submodule dataschema-xml * XML subclass for the DataSchema Utility. * @extends DataSchema.Base ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// * Applies a given schema to given XML data. * @param schema {Object} Schema to apply. * @param data {XMLDoc} XML document. * @return {Object} Schema-parsed data. Y.
log(
"XML data could not be schema-parsed: " + Y.
dump(
data) +
" " + Y.
dump(
data),
"error",
"dataschema-xml");
* 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. Y.
log(
'SchemaXML._getLocationValue failed: ' + e.
message);
* 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. // this fixes the IE 5.5+ issue where childnode selectors begin at 0 instead of 1 // Fallback for DOM nodes and fragments // Iterate over each locator piece //XPath is 1-based while DOM is 0-based // grab attribute value @ // grab that last instance of tagName // find the last matching location in children // returning a mock-standard object for IE * Schema-parsed result field. * @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. * Parses results data according to schema * @param xmldoc_in {Object} XML document parse. * @param data_out {Object} In-progress schema-parsed data to update. * @return {Object} Schema-parsed data. * Schema-parsed result to add to results list. * @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. * Schema-parsed list of results from full data * @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. // loop through each result node // loop through the nodelist },
'@VERSION@' ,{
requires:[
'dataschema-base']});
YUI.
add(
'dataschema-array',
function(Y) {
* Provides a DataSchema implementation which can be used to work with data stored in arrays. * @submodule dataschema-array * Array subclass for the DataSchema Utility. * @class DataSchema.Array * @extends DataSchema.Base ///////////////////////////////////////////////////////////////////////////// // DataSchema.Array static methods ///////////////////////////////////////////////////////////////////////////// * Applies a given schema to given Array data. * @param schema {Object} Schema to apply. * @param data {Object} Array data. * @return {Object} Schema-parsed data. Y.
log(
"Schema resultFields property not found: " + Y.
dump(
schema),
"warn",
"dataschema-array");
Y.
log(
"Array data could not be schema-parsed: " + Y.
dump(
data) +
" " + Y.
dump(
data),
"error",
"dataschema-array");
* Schema-parsed list of results from full data * @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. Y.
log(
"Unexpected type while parsing array: " + Y.
dump(
item),
"warn",
"dataschema-array");
},
'@VERSION@' ,{
requires:[
'dataschema-base']});
YUI.
add(
'dataschema-text',
function(Y) {
* Provides a DataSchema implementation which can be used to work with delimited text data. * @submodule dataschema-text * Text subclass for the DataSchema Utility. * @extends DataSchema.Base ///////////////////////////////////////////////////////////////////////////// // DataSchema.Text static methods ///////////////////////////////////////////////////////////////////////////// * Applies a given schema to given delimited text data. * @param schema {Object} Schema to apply. * @param data {Object} Text data. * @return {Object} Schema-parsed data. Y.
log(
"Text data could not be schema-parsed: " + Y.
dump(
data) +
" " + Y.
dump(
data),
"error",
"dataschema-text");
* Schema-parsed list of results from full data * @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. results_in,
fields_in,
result,
item,
fields,
field,
key,
value, i, j,
// Delete final delimiter at end of string if there },
'@VERSION@' ,{
requires:[
'dataschema-base']});
YUI.
add(
'dataschema',
function(Y){},
'@VERSION@' ,{
use:[
'dataschema-base',
'dataschema-json',
'dataschema-xml',
'dataschema-array',
'dataschema-text']});