dataschema-debug.js revision 3cd3e1a007f7fd6874030808f97ef48e3a950b40
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * The DataSchema utility provides a common configurable interface for widgets to
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * apply a given schema to a variety of data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @module dataschema
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Base class for the YUI DataSchema utility.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @class DataSchema.Base
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Returns string name.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method toString
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {String} String representation for this object.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major toString: function() {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return "DataSchema.Base";
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Overridable method returns data as-is.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method apply
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param schema {Object} Schema to apply.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data {Object} Data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Schema-parsed data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Applies field parser, if defined
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method parse
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param value {Object} Original value.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param field {Object} Field.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Type-converted value.
4a48635cccc646ac479830fd4df0ee8e10c5bd8djeff.schenk * The DataSchema utility provides a common configurable interface for widgets to
4a48635cccc646ac479830fd4df0ee8e10c5bd8djeff.schenk * apply a given schema to a variety of data.
4a48635cccc646ac479830fd4df0ee8e10c5bd8djeff.schenk * @module dataschema
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * JSON subclass for the YUI DataSchema utility.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @extends DataSchema.Base
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major /////////////////////////////////////////////////////////////////////////////
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // DataSchema.JSON static methods
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major /////////////////////////////////////////////////////////////////////////////
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Returns string name.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method toString
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {String} String representation for this object.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major toString: function() {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Utility function converts JSON locator strings into walkable paths
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method DataSchema.JSON.buildPath
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param locator {String} JSON value locator.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {String[]} Walkable path to data value.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Strip the ["string keys"] and [1] array indexes
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}).
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major replace(/\[(\d+)\]/g,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}).
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel // Validate against problematic characters.
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel if (!/[^\w\.\$@]/.test(locator)) {
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel for (i=path.length-1; i >= 0; --i) {
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel path[i] = keys[parseInt(path[i].substr(1),10)];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.log("Invalid locator: " + locator, "error", SchemaJSON.toString());
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return path;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Utility function to walk a path and return the value located there.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method DataSchema.JSON.getLocationValue
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param path {String[]} Locator path.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data {String} Data to traverse.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Data value at location.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getLocationValue: function (path, data) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major len = path.length;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major for (;i<len;i++) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data = data[path[i]];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Applies a given schema to given JSON data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method apply
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param schema {Object} Schema to apply.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data {Object} JSON data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Schema-parsed data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major apply: function(schema, data) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var data_in = data,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out = {results:[],meta:{}};
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Convert incoming JSON strings
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(!LANG.isObject(data)) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_in = Y.JSON.parse(data);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.error = e;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data_out;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(LANG.isObject(data_in) && schema) {
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk // Parse results data
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(!LANG.isUndefined(schema.resultsLocator)) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out = SchemaJSON._parseResults(schema, data_in, data_out);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Parse meta data
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(!LANG.isUndefined(schema.metaFields)) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.log("JSON data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", SchemaJSON.toString());
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.error = true;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data_out;
e0c96405853179a6ecb19d24834ccebb904f7789Jake Feasel * Schema-parsed list of results from full data
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel * @method _parseResults
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel * @param schema {Object} Schema to parse against.
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel * @param data_in {Object} Data to parse.
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel * @param data_out {Object} In-progress parsed data to update.
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel * @return {Object} Parsed data object.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @protected
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major _parseResults: function(schema, data_in, data_out) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var results = [],
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk if(schema.resultsLocator) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major path = SchemaJSON.buildPath(schema.resultsLocator);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major results = SchemaJSON.getLocationValue(path, data_in);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (results === undefined) {
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk data_out.results = [];
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk error = new Error(this.toString() + " Results retrieval failure");
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk if(LANG.isArray(schema.resultsFields) && LANG.isArray(results)) {
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk data_out = SchemaJSON._getFieldValues(schema.resultsFields, results, data_out);
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk data_out.results = [];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major error = new Error(this.toString() + " Fields retrieval failure");
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major error = new Error(this.toString() + " Results locator failure");
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (error) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.log("JSON data could not be parsed: " + Y.dump(data_in), "error", SchemaJSON.toString());
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.error = error;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data_out;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Get field data values out of list of full results
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method _getFieldValues
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param fields {Array} Fields to find.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_in {Array} Results data to parse.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_out {Object} In-progress parsed data to update.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Parsed data object.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @protected
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major _getFieldValues: function(fields, data_in, data_out) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var results = [],
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major len = fields.length,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major field, key, path, parser,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major simplePaths = [], complexPaths = [], fieldParsers = [],
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major result, record;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // First collect hashes of simple paths, complex paths, and parsers
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major for (i=0; i<len; i++) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major field = fields[i]; // A field can be a simple string or a hash
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major key = field.key || field; // Find the key
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Validate and store locators for later
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major path = SchemaJSON.buildPath(key);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (path.length === 1) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major simplePaths[simplePaths.length] = {key:key, path:path[0]};
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major complexPaths[complexPaths.length] = {key:key, path:path};
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.log("Invalid key syntax: " + key, "warn", SchemaJSON.toString());
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Validate and store parsers for later
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major //TODO: use Y.DataSchema.parse?
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major parser = (LANG.isFunction(field.parser)) ? field.parser : Y.Parsers[field.parser+''];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (parser) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major fieldParsers[fieldParsers.length] = {key:key, parser:parser};
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Traverse list of data_in, creating records of simple fields,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // complex fields, and applying parsers as necessary
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major for (i=data_in.length-1; i>=0; --i) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major record = {};
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major result = data_in[i];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(result) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Cycle through simpleLocators
0e400e45fa5bef7103232d78fc7a54f7390e03d8Jonathan Scudder for (j=simplePaths.length-1; j>=0; --j) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Bug 1777850: The result might be an array instead of object
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major record[simplePaths[j].key] = Y.DataSchema.Base.parse(
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major (LANG.isUndefined(result[simplePaths[j].path]) ?
b0edc4ffcea5267008dfe12ec694dd37a5037840jeff.schenk result[j] : result[simplePaths[j].path]), simplePaths[j]);
b0edc4ffcea5267008dfe12ec694dd37a5037840jeff.schenk // Cycle through complexLocators
b0edc4ffcea5267008dfe12ec694dd37a5037840jeff.schenk for (j=complexPaths.length - 1; j>=0; --j) {
b0edc4ffcea5267008dfe12ec694dd37a5037840jeff.schenk record[complexPaths[j].key] = Y.DataSchema.Base.parse(
57b295f18bd83b350f1e65573667ee2a3bf2ce3cjeff.schenk (SchemaJSON.getLocationValue(complexPaths[j].path, result)), complexPaths[j] );
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Cycle through fieldParsers
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major for (j=fieldParsers.length-1; j>=0; --j) {
0e400e45fa5bef7103232d78fc7a54f7390e03d8Jonathan Scudder key = fieldParsers[j].key;
0e400e45fa5bef7103232d78fc7a54f7390e03d8Jonathan Scudder record[key] = fieldParsers[j].parser(record[key]);
f58c87ece2202b8f85310d8885c7e39a7f435c09Jason Lemay // Safety net
f58c87ece2202b8f85310d8885c7e39a7f435c09Jason Lemay if (LANG.isUndefined(record[key])) {
f58c87ece2202b8f85310d8885c7e39a7f435c09Jason Lemay record[key] = null;
f58c87ece2202b8f85310d8885c7e39a7f435c09Jason Lemay results[i] = record;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.results = results;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data_out;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Parses results data according to schema
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method _parseMeta
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_out {Object} Data to parse.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_in {Object} In-progress parsed data to update.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Schema-parsed meta data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @protected
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington _parseMeta: function(metaFields, data_in, data_out) {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington if(LANG.isObject(metaFields)) {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington var key, path;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington for(key in metaFields) {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington if (metaFields.hasOwnProperty(key)) {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington path = SchemaJSON.buildPath(metaFields[key]);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington if (path && data_in) {
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington data_out.meta[key] = SchemaJSON.getLocationValue(path, data_in);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington data_out.error = new Error(this.toString() + " Meta retrieval failure");
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington return data_out;
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill CunningtonY.DataSchema.JSON = Y.mix(SchemaJSON, Y.DataSchema.Base);
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington}, '@VERSION@' ,{requires:['dataschema-base']});
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * The DataSchema utility provides a common configurable interface for widgets to
1067e734d6cec73ee954f2e43c1bba0e09d267cfPhill Cunnington * apply a given schema to a variety of data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @module dataschema
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Majorvar LANG = Y.Lang,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * XML subclass for the YUI DataSchema utility.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @extends DataSchema.Base
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter MajorSchemaXML = {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major /////////////////////////////////////////////////////////////////////////////
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // DataSchema.XML static methods
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major /////////////////////////////////////////////////////////////////////////////
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Returns string name.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method toString
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {String} String representation for this object.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major toString: function() {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Applies a given schema to given XML data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method apply
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param schema {Object} Schema to apply.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data {XMLDoc} XML document.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Schema-parsed data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major apply: function(schema, data) {
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel var xmldoc = data,
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel data_out = {results:[],meta:{}};
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel if(LANG.isObject(xmldoc) && schema) {
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel // Parse results data
75be86af5f9fab607e4d013c8d65814f338e35d7Jake Feasel data_out = SchemaXML._parseResults(schema, xmldoc, data_out);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // Parse meta data
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out = SchemaXML._parseMeta(schema.metaFields, xmldoc, data_out);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.log("XML data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", SchemaXML.toString());
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.error = true;
4a48635cccc646ac479830fd4df0ee8e10c5bd8djeff.schenk return data_out;
4a48635cccc646ac479830fd4df0ee8e10c5bd8djeff.schenk * Get an XPath-specified value for a given field from an XML node or document.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method _getLocationValue
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param field {String | Object} Field definition.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param context {Object} XML node or document to search within.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Data value or null.
f720a6e4cf0eab17d4a0fc5177e85bb34fbfd74dPhill Cunnington _getLocationValue: function(field, context) {
f720a6e4cf0eab17d4a0fc5177e85bb34fbfd74dPhill Cunnington var locator = field.locator || field.key || field,
f720a6e4cf0eab17d4a0fc5177e85bb34fbfd74dPhill Cunnington xmldoc = context.ownerDocument || context,
f720a6e4cf0eab17d4a0fc5177e85bb34fbfd74dPhill Cunnington result, res, value = null;
f720a6e4cf0eab17d4a0fc5177e85bb34fbfd74dPhill Cunnington // Standards mode
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major if(!LANG.isUndefined(xmldoc.evaluate)) {
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major result = xmldoc.evaluate(locator, context, xmldoc.createNSResolver(!context.ownerDocument ? context.documentElement : context.ownerDocument.documentElement), 0, null);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major while(res = result.iterateNext()) {
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major value = res.textContent;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major xmldoc.setProperty("SelectionLanguage", "XPath");
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major result = context.selectNodes(locator)[0];
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major value = result.value || result.text || null;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return Y.DataSchema.Base.parse(value, field);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Parses results data according to schema
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method _parseMeta
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_out {Object} Data to parse.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_in {Object} In-progress parsed data to update.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Schema-parsed meta data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @protected
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major _parseMeta: function(metaFields, data_in, data_out) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(LANG.isObject(metaFields)) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major xmldoc = data_in.ownerDocument || data_in;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major for(key in metaFields) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (metaFields.hasOwnProperty(key)) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.meta[key] = SchemaXML._getLocationValue(metaFields[key], xmldoc);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data_out;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * Schema-parsed list of results from full data
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @method _parseResults
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param schema {Object} Schema to parse against.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param xmldoc {Object} XML document parse.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @param data_out {Object} In-progress schema-parsed data to update.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @return {Object} Schema-parsed data.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major * @protected
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major _parseResults: function(schema, xmldoc, data_out) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if(schema.resultsLocator && LANG.isArray(schema.resultsFields)) {
98c66589f6124f17c492667c095aefc61dd51eccAlin Brici var nodeList = xmldoc.getElementsByTagName(schema.resultsLocator),
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major fields = schema.resultsFields,
bd563314e425c4bf7675643269f2b4633ad39f42Allan Foster results = [],
bd563314e425c4bf7675643269f2b4633ad39f42Allan Foster node, field, result, i, j;
bd563314e425c4bf7675643269f2b4633ad39f42Allan Foster if(nodeList.length) {
bd563314e425c4bf7675643269f2b4633ad39f42Allan Foster // Loop through each result node
133bed8ec2ccc857a62d6301f67c3ef3d36aa333Peter Major for(i=nodeList.length-1; i>= 0; i--) {
133bed8ec2ccc857a62d6301f67c3ef3d36aa333Peter Major result = {};
133bed8ec2ccc857a62d6301f67c3ef3d36aa333Peter Major node = nodeList[i];
133bed8ec2ccc857a62d6301f67c3ef3d36aa333Peter Major // Find each field value
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major for(j=fields.length-1; j>= 0; j--) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major field = fields[j];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major result[field.key || field] = SchemaXML._getLocationValue(field, node);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major results[i] = result;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.results = results;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major data_out.error = new Error(this.toString() + " Result nodes retrieval failure");
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return data_out;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter MajorY.DataSchema.XML = Y.mix(SchemaXML, Y.DataSchema.Base);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter MajorYUI.add('dataschema', function(Y){}, '@VERSION@' ,{use:['dataschema-base','dataschema-json','dataschema-xml']});