|
0
|
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 |
Y.log("XML data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-xml"); |
|
|
53 |
data_out.error = new Error("XML schema parse failure"); |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
return data_out; |
|
|
57 |
}, |
|
|
58 |
|
|
|
59 |
/** |
|
|
60 |
* Get an XPath-specified value for a given field from an XML node or document. |
|
|
61 |
* |
|
|
62 |
* @method _getLocationValue |
|
|
63 |
* @param field {String | Object} Field definition. |
|
|
64 |
* @param context {Object} XML node or document to search within. |
|
|
65 |
* @return {Object} Data value or null. |
|
|
66 |
* @static |
|
|
67 |
* @protected |
|
|
68 |
*/ |
|
|
69 |
_getLocationValue: function(field, context) { |
|
|
70 |
var locator = field.locator || field.key || field, |
|
|
71 |
xmldoc = context.ownerDocument || context, |
|
|
72 |
result, res, value = null; |
|
|
73 |
|
|
|
74 |
try { |
|
|
75 |
// Standards mode |
|
|
76 |
if(!LANG.isUndefined(xmldoc.evaluate)) { |
|
|
77 |
result = xmldoc.evaluate(locator, context, xmldoc.createNSResolver(!context.ownerDocument ? context.documentElement : context.ownerDocument.documentElement), 0, null); |
|
|
78 |
while(res = result.iterateNext()) { |
|
|
79 |
value = res.textContent; |
|
|
80 |
} |
|
|
81 |
} |
|
|
82 |
// IE mode |
|
|
83 |
else { |
|
|
84 |
xmldoc.setProperty("SelectionLanguage", "XPath"); |
|
|
85 |
result = context.selectNodes(locator)[0]; |
|
|
86 |
value = result.value || result.text || null; |
|
|
87 |
} |
|
|
88 |
return Y.DataSchema.Base.parse(value, field); |
|
|
89 |
|
|
|
90 |
} |
|
|
91 |
catch(e) { |
|
|
92 |
} |
|
|
93 |
}, |
|
|
94 |
|
|
|
95 |
/** |
|
|
96 |
* Parses results data according to schema |
|
|
97 |
* |
|
|
98 |
* @method _parseMeta |
|
|
99 |
* @param xmldoc_in {Object} XML document parse. |
|
|
100 |
* @param data_out {Object} In-progress schema-parsed data to update. |
|
|
101 |
* @return {Object} Schema-parsed data. |
|
|
102 |
* @static |
|
|
103 |
* @protected |
|
|
104 |
*/ |
|
|
105 |
_parseMeta: function(metaFields, xmldoc_in, data_out) { |
|
|
106 |
if(LANG.isObject(metaFields)) { |
|
|
107 |
var key, |
|
|
108 |
xmldoc = xmldoc_in.ownerDocument || xmldoc_in; |
|
|
109 |
|
|
|
110 |
for(key in metaFields) { |
|
|
111 |
if (metaFields.hasOwnProperty(key)) { |
|
|
112 |
data_out.meta[key] = SchemaXML._getLocationValue(metaFields[key], xmldoc); |
|
|
113 |
} |
|
|
114 |
} |
|
|
115 |
} |
|
|
116 |
return data_out; |
|
|
117 |
}, |
|
|
118 |
|
|
|
119 |
/** |
|
|
120 |
* Schema-parsed list of results from full data |
|
|
121 |
* |
|
|
122 |
* @method _parseResults |
|
|
123 |
* @param schema {Object} Schema to parse against. |
|
|
124 |
* @param xmldoc_in {Object} XML document parse. |
|
|
125 |
* @param data_out {Object} In-progress schema-parsed data to update. |
|
|
126 |
* @return {Object} Schema-parsed data. |
|
|
127 |
* @static |
|
|
128 |
* @protected |
|
|
129 |
*/ |
|
|
130 |
_parseResults: function(schema, xmldoc_in, data_out) { |
|
|
131 |
if(schema.resultListLocator && LANG.isArray(schema.resultFields)) { |
|
|
132 |
var nodeList = xmldoc_in.getElementsByTagName(schema.resultListLocator), |
|
|
133 |
fields = schema.resultFields, |
|
|
134 |
results = [], |
|
|
135 |
node, field, result, i, j; |
|
|
136 |
|
|
|
137 |
if(nodeList.length) { |
|
|
138 |
// Loop through each result node |
|
|
139 |
for(i=nodeList.length-1; i>= 0; i--) { |
|
|
140 |
result = {}; |
|
|
141 |
node = nodeList[i]; |
|
|
142 |
|
|
|
143 |
// Find each field value |
|
|
144 |
for(j=fields.length-1; j>= 0; j--) { |
|
|
145 |
field = fields[j]; |
|
|
146 |
result[field.key || field] = SchemaXML._getLocationValue(field, node); |
|
|
147 |
} |
|
|
148 |
results[i] = result; |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
data_out.results = results; |
|
|
152 |
} |
|
|
153 |
else { |
|
|
154 |
data_out.error = new Error("XML schema result nodes retrieval failure"); |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
return data_out; |
|
|
158 |
} |
|
|
159 |
}; |
|
|
160 |
|
|
|
161 |
Y.DataSchema.XML = Y.mix(SchemaXML, Y.DataSchema.Base); |
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
}, '3.0.0' ,{requires:['dataschema-base']}); |