XMLHTTPRequest 的封装--Binder

来源:互联网 发布:vscode golang 跳转 编辑:程序博客网 时间:2024/06/05 07:19
Function.registerNamespace("net.bingosoft.web");
net.bingosoft.web.Binder 
= function() {
 net.bingosoft.web.Binder.initializeBase(
this);
 
 var _container 
= [];
 var _self 
= this;
 
 var _root, _xpath, _maker, _filler, _xmldom, _how;
 
 
this.make  = function(container, root,xpath,maker) {
  _self._setContainer(container);
  
if(!root)throw new Error("bad arguments. need xml root."); 
  
  _root 
= root;
  _xpath 
= xpath || "xpath";
  _maker 
= maker || this.defaultMaker;
  _xmlDom 
= new ActiveXObject("Microsoft.XMLDOM");
  _how 
= "MAKE";
  _xmlDom.loadXML(
"<"+_root+"/>");
  
this._startWalkTree();
  
return _xmlDom;
 }

 
 
this.fill = function(container, xmlDom,xpath,filler) {
  _self._setContainer(container);
  
if(typeof(xmlDom.xml)=="undefined")
   
throw new Error("bad arguments. need data source XMLDOM");
   
  _xmlDom 
= xmlDom;
  _filler 
= filler || this.defaultFiller;
  _root 
= _xmlDom.documentElement.tagName;
  _xpath 
= xpath || "xpath";
  _how 
= "FILL";
  
this._startWalkTree();
  _xmlDom 
= null;
 }

 
 
this._setContainer = function(array){
  
if(!(array instanceof Array))
   
throw new Error("bad arguments, the first arguments should be an array.");
   
  _container 
= array;  
 }

 
 
this._startWalkTree = function() {
  
for(var i=0;i<_container.length;i++{
   var obj 
= _container[i];
   
this._treeWalker(obj,_xmlDom.documentElement);
  }

 }

 
 
this._treeWalker = function(obj,context) {
  
if(typeof(obj.skip) != "undefined" && obj.skip == "true" )
   
return;
  
if(typeof(obj[_xpath]) != "undefined"{
   var node 
= this._iname(obj[_xpath],context);
   
if(node == nullreturn;
   
this._filler(obj,node);
   context 
= node;
  }

  var childs 
= obj.childNodes;
  
if(childs.length > 0{
   
for(var i=0; i<childs.length; i++{
    
this._treeWalker(childs[i],context);
   }

  }

 }

 
 
this._filler = function(htmlNode,xmlNode) {
  
if(_how == "MAKE"{
   _maker(htmlNode,xmlNode);
  }
 else if(_how == "FILL"{
   _filler(htmlNode,xmlNode);
  }

 }

 
 
this._iname = function(xpath,context) {
  
if(xpath.substring(0,1)=="/"{
  
// abslute path
   context = _xmlDom.selectSingleNode("/"+_root);
   xpath 
= xpath.substring(_root.length+2);
  }

  
if(xpath=="")
   
return context; 
   
  
if(_how == "MAKE")
   
return this._createOrUpdate(xpath,context);
  
else if(_how == "FILL")
   
return context.selectSingleNode(xpath);
 }

 
 
this._createOrUpdate = function(xpath,context) {
  var paths 
= xpath.split('/');
  var i 
= 0
  
for(;i<paths.length;i++{
   var reg 
= /^([a-zA-Z0-9]+)([[][@][a-zA-Z]*[=][a-zA-Z0-9]+[]])?$/;
   var arr 
= paths[i].match(reg);  
   var node 
= context.selectSingleNode(paths[i]);
   
if(node==null{
    node 
= _xmlDom.createNode("1",arr[1],"");
    context.appendChild(node);
    
if(arr[2]!=""{
     var t 
= arr[2];
     reg 
= /^[[][@]([a-zA-Z]+)[=]([a-zA-Z0-9]+)[]]$/;
     var attNV 
= t.match(reg);
     var attribute 
= _xmlDom.createNode("2",attNV[1],"");
     
     
if(attNV[2]!="")
      attribute.value 
= attNV[2];
     node.setAttributeNode(attribute);
    }

   }

   context 
= node;
  }

  
return context;
 }

 
 
 
this.defaultMaker = function(htmlNode,xmlNode) {
   var val 
= null;
   
if(typeof(htmlNode.code)!="undefined"{
    val 
= htmlNode.code;
   }
else if(typeof(htmlNode.value)!="undefined"){
    val 
= htmlNode.value;
   }
else if(['TD','SPAN'].contains(htmlNode.tagName.toUpperCase())) {
    val 
= htmlNode.innerText;
   }

   
   
if(val != null){
    xmlNode.text 
= val.trim();
   }

 }

 
 
this.defaultFiller = function(htmlNode,xmlNode) {
   var text 
= xmlNode==null?"":xmlNode.text
   
if(typeof(htmlNode.code)!="undefined"{
    htmlNode.code 
= text ;
   }
 else if(['TD','SPAN'].contains(htmlNode.tagName.toUpperCase())) {
    htmlNode.innerHTML 
= text;
   }
 else if(typeof(htmlNode.value)!="undefined"){
    htmlNode.value 
= text;
   }

 }
 
}

net.bingosoft.web.Binder.registerClass(
"net.bingosoft.web.Binder"nullnull);
var $binder 
= new net.bingosoft.web.Binder();
 
原创粉丝点击