home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Webmidas / xmldom.js < prev    next >
Text File  |  1999-08-11  |  9KB  |  401 lines

  1. //Copyright Inprise Corporation 1999, all rights reserved, to be used with Borlands Midas-technology only, 
  2. //unless special permission has been given.
  3. var xmldom_vers="1.0";
  4.  
  5. function item(i) {return this[i];}
  6. Array.prototype.item=item;
  7.  
  8. var ParseErr=0;
  9.  
  10. function Document(str)
  11. {
  12. this.borland=1;
  13. this.nodeType=9;
  14. this.parentNode=null;
  15. this.ownerDocument=this;
  16. this.childNodes=new Array();
  17. this.attributes=new Array();
  18. this.removeChild=removeChild;
  19. this.appendChild=appendChild;
  20. this.cloneNode=cloneNode;
  21.  
  22. this.doctype=null;
  23. this.documentElement=null;
  24. this.createElement=createElement;
  25. this.createDocumentFragment=createDocumentFragment;
  26. this.createTextNode=createTextNode;
  27. this.createComment=createComment;
  28. this.createCDATASection=createCDATASection;
  29. this.createProcessingInstruction=createProcessingInstruction;
  30. this.createAttribute=createAttribute;
  31. this.loadXML=loadXML;
  32. if (str) {this.loadXML(str);if (ParseErr) return null;}
  33. return this;
  34. }
  35.  
  36. function createElement(n) {return new Element(this,n);}
  37. function createDocumentFragment() {var df=new Element(this,"");df.nodeType=11;return df;}
  38. function createTextNode(v) {return new CharData(this,v);}
  39. function createComment(d) {var co=new CharData(this,d);co.nodeType=8;return co;}
  40. function createCDATASection(v) {var dat=new CharData(this,v);dat.nodeType=4;return dat;}
  41. function createProcessingInstruction(t,d) {var pro=new CharData(this,d);pro.nodeType=7;pro.target=t;return pro;}
  42.  
  43. function createAttribute(n) {return new Attr(n, 0);}
  44.  
  45. function loadXML(str)
  46. {
  47. ParseErr=0;
  48. ReadDoc(this,str,this.childNodes);this.documentElement=this.childNodes[0];
  49. }
  50.  
  51. function Element(doc,n)
  52. {
  53. this.tagName=n;
  54. this.parentNode=null;
  55. this.ownerDocument=doc;
  56. this.childNodes=new Array();
  57. }
  58.  
  59. new Element(null,0);
  60. Element.prototype.nodeType=1;
  61. Element.prototype.removeChild=removeChild;
  62. Element.prototype.appendChild=appendChild;
  63. Element.prototype.cloneNode=cloneNode;
  64. Element.prototype.setAttribute=setAttribute;
  65. Element.prototype.getAttribute=getAttribute;
  66. Element.prototype.removeAttribute=function(n){this.setAttribute(n,null);}
  67. Element.prototype.attributes=null;
  68. Element.prototype.xmlstr=function(){return XmlStr(this,0,1);}
  69. Element.prototype.parsed=0;
  70.  
  71. function CharData(doc,d)
  72. {
  73. this.parentNode=null;
  74. this.ownerDocument=doc;
  75. this.data=d;
  76. }
  77.  
  78. new CharData(null,0);
  79. CharData.prototype.nodeType=3;
  80.  
  81. function appendChild(p){this.childNodes[this.childNodes.length]=p;}
  82. function removeChild(p)
  83. {
  84. var i,cnt;
  85. cnt= this.childNodes.length;
  86. for(i=0;i<cnt;i++)
  87.  if(this.childNodes[i]==p)
  88.  {
  89.   var j;
  90.   for(j=i+1;j<cnt;j++){this.childNodes[j-1]=this.childNodes[j];}
  91.   this.childNodes[cnt-1]=null;
  92.   this.childNodes.length=cnt-1;
  93.   break;
  94.  }
  95. }
  96.  
  97. function cloneNode(deep)
  98. {
  99. var str=XmlStr(this,0,deep);
  100. var a=new Array();
  101. ReadDoc(this.ownerDocument,str,a);
  102. return a[0];
  103. }
  104.  
  105. function setAttribute(n,v)
  106. {
  107. if(this.attributes!=null&&this.parsed==0){this.attributes=ReadAttrs(this.attributes);this.parsed=1;}
  108. else
  109. if(this.attributes==null){this.attributes=new Array();this.attributes["item"]=null;this.parsed=1;}
  110. this.attributes[n]=v; return;
  111. }
  112.  
  113.  
  114. function getAttribute(n)
  115. {
  116. if(this.attributes==null) return null;
  117. if(this.parsed==0){this.attributes=ReadAttrs(this.attributes);this.parsed=1;}
  118. return this.attributes[n];
  119. }
  120.  
  121. function Attr(n,v) {this.name=n; this.value=v;}
  122.  
  123. function ReadDoc(d,str,c)
  124. {
  125. str=ReadHeader(d,str);
  126. ReadNodes(d,str,null,c);
  127. return ;
  128. }
  129.  
  130. function ReadHeader(d,str)
  131. {
  132. var iBeg=str.indexOf("<");
  133. if(str.substring(iBeg,iBeg+5)=="<?xml")
  134. {
  135.  var iEnd=str.indexOf("?>");
  136.  str=str.substring(str.indexOf("<",iEnd+2),str.length);
  137. }
  138. else
  139. if(iBeg) str=str.substring(iBeg,str.length);
  140. return str;
  141. }
  142.  
  143. function ReadNodes(d,str,p,c)
  144. {
  145. str=ReadWhChar(str);
  146. while(str.length!=0&&ParseErr==0)
  147. {
  148. if(str.charAt(0)=="<")
  149. {
  150.  if(str.charAt(1)=='/')
  151.  {
  152.   var iEnd=str.indexOf(">");
  153.   if(p && iEnd !=-1)
  154.   {
  155.    var Tag=TrimStr(str.substring(2,iEnd));
  156.    if(Tag==p.tagName){str=str.substring(iEnd+1,str.length);return str;}
  157.    if(ParseErr==0){alert("XML-parse error . Expected : "+Tag+"  ,found : "+p.tagName);ParseErr=1;}
  158.    else{str.length=0; return str;}
  159.   }
  160.   else ParseErr=1;
  161.  }
  162.  else
  163.  {
  164.   if(str.charAt(1)=='?'){str=ReadPI(d,str,p,c);}
  165.   else
  166.   {
  167.    if(str.charAt(1)=='!')
  168.    {
  169.     if(str.substring(1,4)=="!--"){str=ReadComment(d,str,p,c);}
  170.     else
  171.     {
  172.      if(str.substring(1,9)=="![CDATA["){str=ReadCData(d,str,p,c);}
  173.      else ParseErr=1;
  174.     }
  175.    }
  176.    else str=ReadElement(d,str,p,c);
  177.   }
  178.  }
  179. }
  180. else str=ReadText(d,str,p,c);
  181. str=ReadWhChar(str);
  182. }
  183. return str;
  184. }
  185.  
  186. function AttrPos(str)
  187. {
  188. var i=str.indexOf("=");
  189. if(i==-1) return 0;
  190. while(str.charAt(i)!=' '&&i>0) i--;
  191. return i;
  192. }
  193.  
  194. function ReadText(d,str,p,c)
  195. {
  196. var iBeg=str.indexOf("<");
  197. var node=null;
  198.  
  199. if(iBeg==-1)
  200.  node=d.createTextNode(ToText(str));
  201.  str="";
  202. }
  203. else
  204. {
  205.  node=d.createTextNode(ToText(str.substring(0,iBeg)));
  206.  str=str.substring(iBeg,str.length);
  207. }
  208. if(p) node.parentNode=p;
  209. c[c.length]=node;
  210. return str;
  211. }
  212.  
  213. function ReadElement(d,str,p,c)
  214. {
  215. var iEnd=str.indexOf(">");
  216. var bEmpty=(str.charAt(iEnd-1)=='/'); 
  217. if(bEmpty) iEnd-=1;
  218. var Tag=str.substring(1,iEnd);
  219. var iBegA=AttrPos(Tag); 
  220.  
  221. var Attrs;
  222. var Name;
  223. if(iBegA){Name=Tag.substring(0,iBegA);Attrs=Tag.substring(iBegA+1,Tag.length);}
  224. else{Name=Tag;Attrs=null;}
  225.  
  226. var el=d.createElement(TrimStr(Name));
  227. el.attributes=Attrs;
  228.  
  229. if(!bEmpty){str=str.substring(iEnd+1,str.length);str=ReadNodes(d,str,el,el.childNodes);}
  230. else{str=str.substring(iEnd+2,str.length);}
  231. el.parentNode=p;
  232. c[c.length]=el;
  233. return str;
  234. }
  235.  
  236. function ReadPI(d,str,p,c)
  237. {
  238. var iEnd=str.indexOf("?>");
  239. var iEndT=str.indexOf(" ");
  240. var t=str.substring(2,iEndT);
  241. var pi=str.substring(iEndT+1,iEnd);
  242. var node=d.createProcessingInstruction(t,pi);
  243. node.parentNode=p;
  244. c[c.length]=node; 
  245. str=str.substring(iEnd+2,str.length);
  246. return str;
  247. }
  248.  
  249. function ReadComment(d,str,p,c)
  250. {
  251. var iEnd=str.indexOf("-->");
  252. var com=str.substring(4,iEnd);
  253. var node=d.createComment(com);
  254. node.parentNode=p;
  255. c[c.length]=node; 
  256. str=str.substring(iEnd+3,str.length);
  257. return str;
  258. }
  259.  
  260. function ReadCData(d,str,p,c)
  261. {
  262. var iEnd=str.indexOf("]]>");
  263. var dat=str.substring(9,iEnd);
  264. var node=d.createCDATASection(dat);
  265. node.parentNode=p;
  266. c[c.length]=node; 
  267. str=str.substring(iEnd+3,str.length);
  268. return str;
  269. }
  270.  
  271. function ReadWhChar(s)
  272. {
  273. var i=0;
  274. while(isWhChar(s.charAt(i))) i++;
  275. if(i!=0) return s.substring(i,s.length);
  276. return s;
  277. }
  278.  
  279. function ReadAttrs(str)
  280. {
  281. var attrs=new Array();
  282. attrs["item"]=null;
  283. while(str.length)
  284. {
  285. var iName=str.indexOf("=");
  286. if(iName==-1) return attrs;
  287. var Name=TrimStr(str.substring(0,iName));
  288. var Val;
  289. var iVal1=str.indexOf("\"");
  290. if(iVal1==-1) return attrs;
  291. var iVal2=str.indexOf("\"",iVal1+1);
  292. if(iVal2==-1) return attrs;
  293. Val=str.substring(iVal1+1,iVal2); 
  294. attrs[Name]=ToText(Val);
  295. str=str.substring(iVal2+1,str.length);
  296. }
  297. return attrs;
  298. }
  299.  
  300. function TrimStr(str)
  301. {
  302. if(str.indexOf(" ")==-1&&str.indexOf("\t")==-1&&str.indexOf("\r")==-1&&str.indexOf("\n")==-1) return str;
  303. var iBeg=0,iEnd=str.length;
  304. while(iBeg<iEnd&&isWhChar(str.charAt(iBeg))) iBeg++;
  305. while(iEnd>iBeg&&isWhChar(str.charAt(iEnd-1))) iEnd--;//Note -1!
  306. if(iBeg==0&&iEnd==str.length) return str;
  307. return str.substring(iBeg,iEnd);
  308. }
  309.  
  310. function isWhChar(ch){if(ch==' '||ch=='\t'||ch=='\r'||ch=='\n')return 1;return 0;}
  311.  
  312. function ToText(str)
  313. {
  314. if(str.indexOf("&")==-1) return str;
  315. var s=new Array();
  316. str+=" ";//Netscape4 bug
  317. s=str.split("<");
  318. str=s.join("<");
  319. s=str.split(">");
  320. str=s.join(">");
  321. s=str.split(""");
  322. str=s.join("\"");
  323. s=str.split("'");
  324. str=s.join("\'");
  325. s=str.split("&");
  326. str=s.join("&");
  327. s=str.split(" ");
  328. str=s.join("\t");
  329.  
  330. s=str.split(" ");
  331. str=s.join("\r\n");
  332. //single new-lines:
  333. s=str.split(" ");
  334. str=s.join("\r\n");
  335. s=str.split(" ");
  336. str=s.join("\r");
  337. s=str.split("\");
  338. str=s.join("\\");
  339. return str.substring(0,str.length-1);
  340. }
  341.  
  342. function FrText(str)
  343. {
  344. var s=new Array();
  345. str=str+" ";//Netscape4 bug !
  346. s=str.split("&");
  347. str=s.join("&");
  348. s=str.split("<");
  349. str=s.join("<");
  350. s=str.split(">");
  351.  
  352. str=s.join(">");
  353. s=str.split("\"");
  354. str=s.join(""");
  355. s=str.split("\'");
  356. str=s.join("'");
  357.  
  358. s=str.split("\\");
  359. str=s.join("\");
  360. s=str.split("\t");
  361. str=s.join(" ");
  362. s=str.split("\n");
  363. str=s.join(" ");
  364. s=str.split("\r");
  365. str=s.join(" ");
  366. return str=str.substring(-1,str.length-1);
  367. }
  368.  
  369. function XmlStr(node,i,deep)
  370. {
  371. var s="";
  372. if(node.nodeType==8){s+=node.data;return s;}
  373. var num=node.childNodes.length;
  374. var empty="";
  375. if(num==0) empty="/";
  376. var tagname=node.tagName;
  377. s+="<"+tagname;
  378. node.getAttribute("a");
  379. var attrs=node.attributes;
  380. if(attrs!=null)
  381. for(var a in attrs){var v=attrs[a];if(v!=null) s+=" "+a+"=\""+FrText(v)+"\"";}
  382. s+=empty+">";
  383. if(num>0)
  384. {
  385.  if(num==1&&node.childNodes.item(0).nodeType==3){s+=FrText(node.childNodes.item(0).data);}
  386.  else
  387.  {var j;
  388.   for(j=0;j<num;j++)
  389.   {
  390.    var child=node.childNodes.item(j);
  391.    if(child.nodeType!=12)
  392.     if(deep>0) s+= XmlStr(child,i+1,deep);
  393.   }
  394.  }
  395.  s+="</"+tagname+">";
  396. }
  397. return s;
  398. }
  399.  
  400.