|
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.0 |
|
6 build: 1549 |
|
7 */ |
|
8 YUI.add('dataschema-xml', function(Y) { |
|
9 |
|
10 /** |
|
11 * Provides a DataSchema implementation which can be used to work with XML data. |
|
12 * |
|
13 * @module dataschema |
|
14 * @submodule dataschema-xml |
|
15 */ |
|
16 var LANG = Y.Lang, |
|
17 |
|
18 /** |
|
19 * XML subclass for the DataSchema Utility. |
|
20 * @class DataSchema.XML |
|
21 * @extends DataSchema.Base |
|
22 * @static |
|
23 */ |
|
24 SchemaXML = { |
|
25 |
|
26 ///////////////////////////////////////////////////////////////////////////// |
|
27 // |
|
28 // DataSchema.XML static methods |
|
29 // |
|
30 ///////////////////////////////////////////////////////////////////////////// |
|
31 /** |
|
32 * Applies a given schema to given XML data. |
|
33 * |
|
34 * @method apply |
|
35 * @param schema {Object} Schema to apply. |
|
36 * @param data {XMLDoc} XML document. |
|
37 * @return {Object} Schema-parsed data. |
|
38 * @static |
|
39 */ |
|
40 apply: function(schema, data) { |
|
41 var xmldoc = data, |
|
42 data_out = {results:[],meta:{}}; |
|
43 |
|
44 if(xmldoc && xmldoc.nodeType && (xmldoc.nodeType === 9 || xmldoc.nodeType === 1 || xmldoc.nodeType === 11) && schema) { |
|
45 // Parse results data |
|
46 data_out = SchemaXML._parseResults(schema, xmldoc, data_out); |
|
47 |
|
48 // Parse meta data |
|
49 data_out = SchemaXML._parseMeta(schema.metaFields, xmldoc, data_out); |
|
50 } |
|
51 else { |
|
52 data_out.error = new Error("XML schema parse failure"); |
|
53 } |
|
54 |
|
55 return data_out; |
|
56 }, |
|
57 |
|
58 /** |
|
59 * Get an XPath-specified value for a given field from an XML node or document. |
|
60 * |
|
61 * @method _getLocationValue |
|
62 * @param field {String | Object} Field definition. |
|
63 * @param context {Object} XML node or document to search within. |
|
64 * @return {Object} Data value or null. |
|
65 * @static |
|
66 * @protected |
|
67 */ |
|
68 _getLocationValue: function(field, context) { |
|
69 var locator = field.locator || field.key || field, |
|
70 xmldoc = context.ownerDocument || context, |
|
71 result, res, value = null; |
|
72 |
|
73 try { |
|
74 // Standards mode |
|
75 if(!LANG.isUndefined(xmldoc.evaluate)) { |
|
76 result = xmldoc.evaluate(locator, context, xmldoc.createNSResolver(!context.ownerDocument ? context.documentElement : context.ownerDocument.documentElement), 0, null); |
|
77 while(res = result.iterateNext()) { |
|
78 value = res.textContent; |
|
79 } |
|
80 } |
|
81 // IE mode |
|
82 else { |
|
83 xmldoc.setProperty("SelectionLanguage", "XPath"); |
|
84 result = context.selectNodes(locator)[0]; |
|
85 value = result.value || result.text || null; |
|
86 } |
|
87 return Y.DataSchema.Base.parse(value, field); |
|
88 |
|
89 } |
|
90 catch(e) { |
|
91 } |
|
92 }, |
|
93 |
|
94 /** |
|
95 * Parses results data according to schema |
|
96 * |
|
97 * @method _parseMeta |
|
98 * @param xmldoc_in {Object} XML document parse. |
|
99 * @param data_out {Object} In-progress schema-parsed data to update. |
|
100 * @return {Object} Schema-parsed data. |
|
101 * @static |
|
102 * @protected |
|
103 */ |
|
104 _parseMeta: function(metaFields, xmldoc_in, data_out) { |
|
105 if(LANG.isObject(metaFields)) { |
|
106 var key, |
|
107 xmldoc = xmldoc_in.ownerDocument || xmldoc_in; |
|
108 |
|
109 for(key in metaFields) { |
|
110 if (metaFields.hasOwnProperty(key)) { |
|
111 data_out.meta[key] = SchemaXML._getLocationValue(metaFields[key], xmldoc); |
|
112 } |
|
113 } |
|
114 } |
|
115 return data_out; |
|
116 }, |
|
117 |
|
118 /** |
|
119 * Schema-parsed list of results from full data |
|
120 * |
|
121 * @method _parseResults |
|
122 * @param schema {Object} Schema to parse against. |
|
123 * @param xmldoc_in {Object} XML document parse. |
|
124 * @param data_out {Object} In-progress schema-parsed data to update. |
|
125 * @return {Object} Schema-parsed data. |
|
126 * @static |
|
127 * @protected |
|
128 */ |
|
129 _parseResults: function(schema, xmldoc_in, data_out) { |
|
130 if(schema.resultListLocator && LANG.isArray(schema.resultFields)) { |
|
131 var nodeList = xmldoc_in.getElementsByTagName(schema.resultListLocator), |
|
132 fields = schema.resultFields, |
|
133 results = [], |
|
134 node, field, result, i, j; |
|
135 |
|
136 if(nodeList.length) { |
|
137 // Loop through each result node |
|
138 for(i=nodeList.length-1; i>= 0; i--) { |
|
139 result = {}; |
|
140 node = nodeList[i]; |
|
141 |
|
142 // Find each field value |
|
143 for(j=fields.length-1; j>= 0; j--) { |
|
144 field = fields[j]; |
|
145 result[field.key || field] = SchemaXML._getLocationValue(field, node); |
|
146 } |
|
147 results[i] = result; |
|
148 } |
|
149 |
|
150 data_out.results = results; |
|
151 } |
|
152 else { |
|
153 data_out.error = new Error("XML schema result nodes retrieval failure"); |
|
154 } |
|
155 } |
|
156 return data_out; |
|
157 } |
|
158 }; |
|
159 |
|
160 Y.DataSchema.XML = Y.mix(SchemaXML, Y.DataSchema.Base); |
|
161 |
|
162 |
|
163 |
|
164 }, '3.0.0' ,{requires:['dataschema-base']}); |