]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui3/build/dataschema/dataschema-xml.js
Release 6.2.2
[Github/sugarcrm.git] / include / javascript / yui3 / build / dataschema / dataschema-xml.js
1 /*
2  Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3  Code licensed under the BSD License:
4  http://developer.yahoo.com/yui/license.html
5  version: 3.3.0
6  build: 3167
7  */
8 YUI.add('dataschema-xml',function(Y){var LANG=Y.Lang,SchemaXML={apply:function(schema,data){var xmldoc=data,data_out={results:[],meta:{}};if(xmldoc&&xmldoc.nodeType&&(9===xmldoc.nodeType||1===xmldoc.nodeType||11===xmldoc.nodeType)&&schema){data_out=SchemaXML._parseResults.call(this,schema,xmldoc,data_out);data_out=SchemaXML._parseMeta.call(this,schema.metaFields,xmldoc,data_out);}
9 else{data_out.error=new Error("XML schema parse failure");}
10 return data_out;},_getLocationValue:function(field,context){var locator=field.locator||field.key||field,xmldoc=context.ownerDocument||context,result,res,value=null;try{result=SchemaXML._getXPathResult(locator,context,xmldoc);while(res=result.iterateNext()){value=res.textContent||res.value||res.text||res.innerHTML||null;}
11 return Y.DataSchema.Base.parse.call(this,value,field);}
12 catch(e){}
13 return null;},_getXPathResult:function(locator,context,xmldoc){if(!LANG.isUndefined(xmldoc.evaluate)){return xmldoc.evaluate(locator,context,xmldoc.createNSResolver(context.ownerDocument?context.ownerDocument.documentElement:context.documentElement),0,null);}
14 else{var values=[],locatorArray=locator.split(/\b\/\b/),i=0,l=locatorArray.length,location,subloc,m,isNth;try{xmldoc.setProperty("SelectionLanguage","XPath");values=context.selectNodes(locator);}
15 catch(e){for(;i<l&&context;i++){location=locatorArray[i];if((location.indexOf("[")>-1)&&(location.indexOf("]")>-1)){subloc=location.slice(location.indexOf("[")+1,location.indexOf("]"));subloc--;context=context.children[subloc];isNth=true;}
16 else if(location.indexOf("@")>-1){subloc=location.substr(location.indexOf("@"));context=subloc?context.getAttribute(subloc.replace('@','')):context;}
17 else if(-1<location.indexOf("//")){subloc=context.getElementsByTagName(location.substr(2));context=subloc.length?subloc[subloc.length-1]:null;}
18 else if(l!=i+1){for(m=context.childNodes.length-1;0<=m;m-=1){if(location===context.childNodes[m].tagName){context=context.childNodes[m];m=-1;}}}}
19 if(context){if(LANG.isString(context)){values[0]={value:context};}
20 else if(isNth){values[0]={value:context.innerHTML};}
21 else{values=Y.Array(context.childNodes,0,true);}}}
22 return{index:0,iterateNext:function(){if(this.index>=this.values.length){return undefined;}
23 var result=this.values[this.index];this.index+=1;return result;},values:values};}},_parseField:function(field,result,context){if(field.schema){result[field.key]=SchemaXML._parseResults.call(this,field.schema,context,{results:[],meta:{}}).results;}
24 else{result[field.key||field]=SchemaXML._getLocationValue.call(this,field,context);}},_parseMeta:function(metaFields,xmldoc_in,data_out){if(LANG.isObject(metaFields)){var key,xmldoc=xmldoc_in.ownerDocument||xmldoc_in;for(key in metaFields){if(metaFields.hasOwnProperty(key)){data_out.meta[key]=SchemaXML._getLocationValue.call(this,metaFields[key],xmldoc);}}}
25 return data_out;},_parseResult:function(fields,context){var result={},j;for(j=fields.length-1;0<=j;j--){SchemaXML._parseField.call(this,fields[j],result,context);}
26 return result;},_parseResults:function(schema,context,data_out){if(schema.resultListLocator&&LANG.isArray(schema.resultFields)){var xmldoc=context.ownerDocument||context,fields=schema.resultFields,results=[],node,result,nodeList,i=0;if(schema.resultListLocator.match(/^[:\-\w]+$/)){nodeList=context.getElementsByTagName(schema.resultListLocator);for(i=nodeList.length-1;0<=i;i--){results[i]=SchemaXML._parseResult.call(this,fields,nodeList[i]);}}
27 else{nodeList=SchemaXML._getXPathResult(schema.resultListLocator,context,xmldoc);while(node=nodeList.iterateNext()){results[i]=SchemaXML._parseResult.call(this,fields,node);i+=1;}}
28 if(results.length){data_out.results=results;}
29 else{data_out.error=new Error("XML schema result nodes retrieval failure");}}
30 return data_out;}};Y.DataSchema.XML=Y.mix(SchemaXML,Y.DataSchema.Base);},'3.3.0',{requires:['dataschema-base']});