-
Notifications
You must be signed in to change notification settings - Fork 0
/
WS-xmlParser.js
1543 lines (1127 loc) · 47.2 KB
/
WS-xmlParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// =========================================================================
//
// xmldom.js - an XML DOM parser in JavaScript.
//
// This is the classic DOM that has shipped with XML for <SCRIPT>
// since the beginning. For a more standards-compliant DOM, you may
// wish to use the standards-compliant W3C DOM that is included
// with XML for <SCRIPT> versions 3.0 and above
//
// version 3.1
//
// =========================================================================
//
// Copyright (C) 2000 - 2002, 2003 Michael Houghton ([email protected]), Raymond Irving and David Joham ([email protected])
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
// Visit the XML for <SCRIPT> home page at http://xmljs.sourceforge.net
//
// 2008/08/01
// Make it available for Webkit: Zoltan Herczeg, 2008, University of Szeged
// =========================================================================
// =========================================================================
// =========================================================================
// CONSTANTS
// =========================================================================
// =========================================================================
// =========================================================================
//define the characters which constitute whitespace, and quotes
var whitespace = "\n\r\t ";
var quotes = "\"'";
// =========================================================================
// =========================================================================
// =========================================================================
// CONVENIENCE FUNCTIONS
// =========================================================================
// =========================================================================
// =========================================================================
function convertEscapes(str) {
/*******************************************************************************************************************
function: convertEscapes
Author: David Joham <[email protected]>
Description:
Characters such as less-than signs, greater-than signs and ampersands are
illegal in XML syntax and must be escaped before being inserted into the DOM.
This function is a convience function to take those escaped characters and
return them to their original values for processing outside the parser
This XML Parser automagically converts the content of the XML elements to
their non-escaped values when xmlNode.getText() is called for every element
except CDATA.
EXAMPLES:
& == &
< == <
> == >
*********************************************************************************************************************/
// not all Konqueror installations have regex support for some reason. Here's the original code using regexes
// that is probably a little more efficient if it matters to you
/*
var escAmpRegEx = /&/g;
var escLtRegEx = /</g;
var escGtRegEx = />/g;
str = str.replace(escAmpRegEx, "&");
str = str.replace(escLtRegEx, "<");
str = str.replace(escGtRegEx, ">");
*/
var gt;
//<
gt = -1;
while (str.indexOf("<", gt + 1) > -1) {
var gt = str.indexOf("<", gt + 1);
var newStr = str.substr(0, gt);
newStr += "<";
newStr = newStr + str.substr(gt + 4, str.length);
str = newStr;
}
//>
gt = -1;
while (str.indexOf(">", gt + 1) > -1) {
var gt = str.indexOf(">", gt + 1);
var newStr = str.substr(0, gt);
newStr += ">";
newStr = newStr + str.substr(gt + 4, str.length);
str = newStr;
}
//&
gt = -1;
while (str.indexOf("&", gt + 1) > -1) {
var gt = str.indexOf("&", gt + 1);
var newStr = str.substr(0, gt);
newStr += "&";
newStr = newStr + str.substr(gt + 5, str.length);
str = newStr;
}
return str;
} // end function convertEscapes
function convertToEscapes(str) {
/*******************************************************************************************************************
function: convertToEscapes
Author: David Joham [email protected]
Description:
Characters such as less-than signs, greater-than signs and ampersands are
illegal in XML syntax. This function is a convience function to escape those
characters out to there legal values.
EXAMPLES:
< == <
> == >
& == &
*********************************************************************************************************************/
// not all Konqueror installations have regex support for some reason. Here's the original code using regexes
// that is probably a little more efficient if it matters to you
/*
var escAmpRegEx = /&/g;
var escLtRegEx = /</g;
var escGtRegEx = />/g;
str = str.replace(escAmpRegEx, "&");
str = str.replace(escLtRegEx, "<");
str = str.replace(escGtRegEx, ">");
*/
// start with &
var gt = -1;
while (str.indexOf("&", gt + 1) > -1) {
gt = str.indexOf("&", gt + 1);
var newStr = str.substr(0, gt);
newStr += "&";
newStr = newStr + str.substr(gt + 1, str.length);
str = newStr;
}
// now <
gt = -1;
while (str.indexOf("<", gt + 1) > -1) {
var gt = str.indexOf("<", gt + 1);
var newStr = str.substr(0, gt);
newStr += "<";
newStr = newStr + str.substr(gt + 1, str.length);
str = newStr;
}
//now >
gt = -1;
while (str.indexOf(">", gt + 1) > -1) {
var gt = str.indexOf(">", gt + 1);
var newStr = str.substr(0, gt);
newStr += ">";
newStr = newStr + str.substr(gt + 1, str.length);
str = newStr;
}
return str;
} // end function convertToEscapes
function _displayElement(domElement, strRet) {
/*******************************************************************************************************************
function: _displayElement
Author: [email protected]
Description:
returns the XML string associated with the DOM element passed in
recursively calls itself if child elements are found
*********************************************************************************************************************/
if(domElement==null) {
return;
}
if(!(domElement.nodeType=='ELEMENT')) {
return;
}
var tagName = domElement.tagName;
var tagInfo = "";
tagInfo = "<" + tagName;
// attributes
var attributeList = domElement.getAttributeNames();
for(var intLoop = 0; intLoop < attributeList.length; intLoop++) {
var attribute = attributeList[intLoop];
tagInfo = tagInfo + " " + attribute + "=";
tagInfo = tagInfo + "\"" + domElement.getAttribute(attribute) + "\"";
}
//close the element name
tagInfo = tagInfo + ">";
strRet=strRet+tagInfo;
// children
if(domElement.children!=null) {
var domElements = domElement.children;
for(var intLoop = 0; intLoop < domElements.length; intLoop++) {
var childNode = domElements[intLoop];
if(childNode.nodeType=='COMMENT') {
strRet = strRet + "<!--" + childNode.content + "-->";
}
else if(childNode.nodeType=='TEXT') {
var cont = trim(childNode.content,true,true);
strRet = strRet + childNode.content;
}
else if (childNode.nodeType=='CDATA') {
var cont = trim(childNode.content,true,true);
strRet = strRet + "<![CDATA[" + cont + "]]>";
}
else {
strRet = _displayElement(childNode, strRet);
}
} // end looping through the DOM elements
} // end checking for domElements.children = null
//ending tag
strRet = strRet + "</" + tagName + ">";
return strRet;
} // end function displayElement
function firstWhiteChar(str,pos) {
/*******************************************************************************************************************
function: firstWhiteChar
Author: [email protected] ?
Description:
return the position of the first whitespace character in str after position pos
*********************************************************************************************************************/
if (isEmpty(str)) {
return -1;
}
while(pos < str.length) {
if (whitespace.indexOf(str.charAt(pos))!=-1) {
return pos;
}
else {
pos++;
}
}
return str.length;
} // end function firstWhiteChar
function isEmpty(str) {
/*******************************************************************************************************************
function: isEmpty
Author: [email protected]
Description:
convenience function to identify an empty string
*********************************************************************************************************************/
return (str==null) || (str.length==0);
} // end function isEmpty
function trim(trimString, leftTrim, rightTrim) {
/*******************************************************************************************************************
function: trim
Author: [email protected]
Description:
helper function to trip a string (trimString) of leading (leftTrim)
and trailing (rightTrim) whitespace
*********************************************************************************************************************/
if (isEmpty(trimString)) {
return "";
}
// the general focus here is on minimal method calls - hence only one
// substring is done to complete the trim.
if (leftTrim == null) {
leftTrim = true;
}
if (rightTrim == null) {
rightTrim = true;
}
var left=0;
var right=0;
var i=0;
var k=0;
// modified to properly handle strings that are all whitespace
if (leftTrim == true) {
while ((i<trimString.length) && (whitespace.indexOf(trimString.charAt(i++))!=-1)) {
left++;
}
}
if (rightTrim == true) {
k=trimString.length-1;
while((k>=left) && (whitespace.indexOf(trimString.charAt(k--))!=-1)) {
right++;
}
}
return trimString.substring(left, trimString.length - right);
} // end function trim
// =========================================================================
// =========================================================================
// =========================================================================
// XML DOC FUNCTIONS
// =========================================================================
// =========================================================================
// =========================================================================
function XMLDoc(source, errFn) {
/*******************************************************************************************************************
function: XMLDoc
Author: [email protected]
Description:
a constructor for an XML document
source: the string containing the document
errFn: the (optional) function used to log errors
*********************************************************************************************************************/
// stack for document construction
this.topNode=null;
// set up the properties and methods for this object
this.errFn = errFn; // user defined error functions
this.createXMLNode = _XMLDoc_createXMLNode;
this.error = _XMLDoc_error;
this.getUnderlyingXMLText = _XMLDoc_getUnderlyingXMLText;
this.handleNode = _XMLDoc_handleNode;
this.hasErrors = false; // were errors found during the parse?
this.insertNodeAfter = _XMLDoc_insertNodeAfter;
this.insertNodeInto = _XMLDoc_insertNodeInto;
this.loadXML = _XMLDoc_loadXML;
this.parse = _XMLDoc_parse;
this.parseAttribute = _XMLDoc_parseAttribute;
this.parseDTD = _XMLDoc_parseDTD;
this.parsePI = _XMLDoc_parsePI;
this.parseTag = _XMLDoc_parseTag;
this.removeNodeFromTree = _XMLDoc_removeNodeFromTree;
this.replaceNodeContents = _XMLDoc_replaceNodeContents;
this.selectNode = _XMLDoc_selectNode;
this.selectNodeText = _XMLDoc_selectNodeText;
this.source = source; // the string source of the document
// parse the document
if (this.parse()) {
// we've run out of markup - check the stack is now empty
if (this.topNode!=null) {
return this.error("expected close " + this.topNode.tagName);
}
else {
return true;
}
}
} // end function XMLDoc
function _XMLDoc_createXMLNode(strXML) {
/*******************************************************************************************************************
function: _XMLDoc_createXMLNode
Author: [email protected]
Description:
convienience function to create a new node that inherits
the properties of the document object
*********************************************************************************************************************/
return new XMLDoc(strXML, this.errFn).docNode;
} // end function _XMLDoc_createXMLNode
function _XMLDoc_error(str) {
/*******************************************************************************************************************
function: _XMLDoc_error
Author: [email protected]
Description:
used to log an error in parsing or validating
*********************************************************************************************************************/
this.hasErrors=true;
if(this.errFn){
this.errFn("ERROR: " + str);
}else if(this.onerror){
this.onerror("ERROR: " + str);
}
return 0;
} // end function _XMLDoc_error
function _XMLDoc_getTagNameParams(tag,obj){
/*******************************************************************************************************************
function: _XMLDoc_getTagNameParams
Author: [email protected]
Description:
convienience function for the nodeSearch routines
*********************************************************************************************************************/
var elm=-1,e,s=tag.indexOf('[');
var attr=[];
if(s>=0){
e=tag.indexOf(']');
if(e>=0)elm=tag.substr(s+1,(e-s)-1);
else obj.error('expected ] near '+tag);
tag=tag.substr(0,s);
if(isNaN(elm) && elm!='*'){
attr=elm.substr(1,elm.length-1); // remove @
attr=attr.split('=');
if(attr[1]) { //remove "
s=attr[1].indexOf('"');
attr[1]=attr[1].substr(s+1,attr[1].length-1);
e=attr[1].indexOf('"');
if(e>=0) attr[1]=attr[1].substr(0,e);
else obj.error('expected " near '+tag)
};elm=-1;
}else if(elm=='*') elm=-1;
}
return [tag,elm,attr[0],attr[1]]
} // end function _XMLDoc_getTagNameParams
function _XMLDoc_getUnderlyingXMLText() {
/*******************************************************************************************************************
function: _XMLDoc_getUnderlyingXMLText
Author: [email protected]
Description:
kicks off the process that returns the XML text representation of the XML
document inclusive of any changes made by the manipulation of the DOM
*********************************************************************************************************************/
var strRet = "";
//for now, hardcode the xml version 1 information. When we handle Processing Instructions later, this
//should be looked at again
strRet = strRet + "<?xml version=\"1.0\"?>";
if (this.docNode==null) {
return;
}
strRet = _displayElement(this.docNode, strRet);
return strRet;
} // end function _XMLDoc_getCurrentXMLText
function _XMLDoc_handleNode(current) {
/*******************************************************************************************************************
function: _XMLDoc_handleNode
Author: [email protected]
Description:
adds a markup element to the document
*********************************************************************************************************************/
if ((current.nodeType=='COMMENT') && (this.topNode!=null)) {
return this.topNode.addElement(current);
}
else if ((current.nodeType=='TEXT') || (current.nodeType=='CDATA')) {
// if the current node is a text node:
// if the stack is empty, and this text node isn't just whitespace, we have
// a problem (we're not in a document element)
if(this.topNode==null) {
if (trim(current.content,true,false)=="") {
return true;
}
else {
return this.error("expected document node, found: " + current);
}
}
else {
// otherwise, append this as child to the element at the top of the stack
return this.topNode.addElement(current);
}
}
else if ((current.nodeType=='OPEN') || (current.nodeType=='SINGLE')) {
// if we find an element tag (open or empty)
var success = false;
// if the stack is empty, this node becomes the document node
if(this.topNode==null) {
this.docNode = current;
current.parent = null;
success = true;
}
else {
// otherwise, append this as child to the element at the top of the stack
success = this.topNode.addElement(current);
}
if (success && (current.nodeType!='SINGLE')) {
this.topNode = current;
}
// rename it as an element node
current.nodeType = "ELEMENT";
return success;
}
// if it's a close tag, check the nesting
else if (current.nodeType=='CLOSE') {
// if the stack is empty, it's certainly an error
if (this.topNode==null) {
return this.error("close tag without open: " + current.toString());
}
else {
// otherwise, check that this node matches the one on the top of the stack
if (current.tagName!=this.topNode.tagName) {
return this.error("expected closing " + this.topNode.tagName + ", found closing " + current.tagName);
}
else {
// if it does, pop the element off the top of the stack
this.topNode = this.topNode.getParent();
}
}
}
return true;
} // end function _XMLDoc_handleNode
function _XMLDoc_insertNodeAfter (referenceNode, newNode) {
/*******************************************************************************************************************
function: _XMLDoc_insertNodeAfter
Author: [email protected]
Description:
inserts a new XML node after the reference node;
for example, if we insert the node <tag2>hello</tag2>
after tag1 in the xml <rootnode><tag1></tag1></rootnode>
we will end up with <rootnode><tag1></tag1><tag2>hello</tag2></rootnode>
NOTE: the return value of this function is a new XMLDoc object!!!!
*********************************************************************************************************************/
var parentXMLText = this.getUnderlyingXMLText();
var selectedNodeXMLText = referenceNode.getUnderlyingXMLText();
var originalNodePos = parentXMLText.indexOf(selectedNodeXMLText) + selectedNodeXMLText.length;
var newXML = parentXMLText.substr(0,originalNodePos);
newXML += newNode.getUnderlyingXMLText();
newXML += parentXMLText.substr(originalNodePos);
var newDoc = new XMLDoc(newXML, this.errFn);
return newDoc;
} // end function _XMLDoc_insertNodeAfter
function _XMLDoc_insertNodeInto (referenceNode, insertNode) {
/*******************************************************************************************************************
function: _XMLDoc_insertNodeInto
Author: [email protected]
Description:
inserts a new XML node into the reference node;
for example, if we insert the node <tag2>hello</tag2>
into tag1 in the xml <rootnode><tag1><tag3>foo</tag3></tag1></rootnode>
we will end up with <rootnode><tag1><tag2>hello</tag2><tag3>foo</tag3></tag1></rootnode>
NOTE: the return value of this function is a new XMLDoc object!!!!
*********************************************************************************************************************/
var parentXMLText = this.getUnderlyingXMLText();
var selectedNodeXMLText = referenceNode.getUnderlyingXMLText();
var endFirstTag = selectedNodeXMLText.indexOf(">") + 1;
var originalNodePos = parentXMLText.indexOf(selectedNodeXMLText) + endFirstTag;
var newXML = parentXMLText.substr(0,originalNodePos);
newXML += insertNode.getUnderlyingXMLText();
newXML += parentXMLText.substr(originalNodePos);
var newDoc = new XMLDoc(newXML, this.errFn);
return newDoc;
} // end function _XMLDoc_insertNodeInto
function _XMLDoc_loadXML(source){
/*******************************************************************************************************************
function: _XMLDoc_insertNodeInto
Author: [email protected]
Description:
allows an already existing XMLDoc object to load XML
*********************************************************************************************************************/
this.topNode=null;
this.hasErrors = false;
this.source=source;
// parse the document
return this.parse();
} // end function _XMLDoc_loadXML
function _XMLDoc_parse() {
/*******************************************************************************************************************
function: _XMLDoc_parse
Author: [email protected]
Description:
scans through the source for opening and closing tags
checks that the tags open and close in a sensible order
*********************************************************************************************************************/
var pos = 0;
// set up the arrays used to store positions of < and > characters
err = false;
while(!err) {
var closing_tag_prefix = '';
var chpos = this.source.indexOf('<',pos);
var open_length = 1;
var open;
var close;
if (chpos ==-1) {
break;
}
open = chpos;
// create a text node
var str = this.source.substring(pos, open);
if (str.length!=0) {
err = !this.handleNode(new XMLNode('TEXT',this, str));
}
// handle Programming Instructions - they can't reliably be handled as tags
if (chpos == this.source.indexOf("<?",pos)) {
pos = this.parsePI(this.source, pos + 2);
if (pos==0) {
err=true;
}
continue;
}
// nobble the document type definition
if (chpos == this.source.indexOf("<!DOCTYPE",pos)) {
pos = this.parseDTD(this.source, chpos+ 9);
if (pos==0) {
err=true;
}
continue;
}
// if we found an open comment, we need to ignore angle brackets
// until we find a close comment
if(chpos == this.source.indexOf('<!--',pos)) {
open_length = 4;
closing_tag_prefix = '--';
}
// similarly, if we find an open CDATA, we need to ignore all angle
// brackets until a close CDATA sequence is found
if (chpos == this.source.indexOf('<![CDATA[',pos)) {
open_length = 9;
closing_tag_prefix = ']]';
}
// look for the closing sequence
chpos = this.source.indexOf(closing_tag_prefix + '>',chpos);
if (chpos ==-1) {
return this.error("expected closing tag sequence: " + closing_tag_prefix + '>');
}
close = chpos + closing_tag_prefix.length;
// create a tag node
str = this.source.substring(open+1, close);
var n = this.parseTag(str);
if (n) {
err = !this.handleNode(n);
}
pos = close +1;
// and loop
}
return !err;
} // end function _XMLDoc_parse
function _XMLDoc_parseAttribute(src,pos,node) {
/*******************************************************************************************************************
function: _XMLDoc_parseAttribute
Author: [email protected]
Description:
parse an attribute out of a tag string
*********************************************************************************************************************/
// chew up the whitespace, if any
while ((pos<src.length) && (whitespace.indexOf(src.charAt(pos))!=-1)) {
pos++;
}
// if there's nothing else, we have no (more) attributes - just break out
if (pos >= src.length) {
return pos;
}
var p1 = pos;
while ((pos < src.length) && (src.charAt(pos)!='=')) {
pos++;
}
var msg = "attributes must have values";
// parameters without values aren't allowed.
if(pos >= src.length) {
return this.error(msg);
}
// extract the parameter name
var paramname = trim(src.substring(p1,pos++),false,true);
// chew up whitespace
while ((pos < src.length) && (whitespace.indexOf(src.charAt(pos))!=-1)) {
pos++;
}
// throw an error if we've run out of string
if (pos >= src.length) {
return this.error(msg);
}
msg = "attribute values must be in quotes";
// check for a quote mark to identify the beginning of the attribute value
var quote = src.charAt(pos++);
// throw an error if we didn't find one
if (quotes.indexOf(quote)==-1) {
return this.error(msg);
}
p1 = pos;
while ((pos < src.length) && (src.charAt(pos)!=quote)) {
pos++;
}
// throw an error if we found no closing quote
if (pos >= src.length) {
return this.error(msg);
}
// store the parameter
if (!node.addAttribute(paramname,trim(src.substring(p1,pos++),false,true))) {
return 0;
}
return pos;
} //end function _XMLDoc_parseAttribute
function _XMLDoc_parseDTD(str,pos) {
/*******************************************************************************************************************
function: _XMLDoc_parseDTD
Author: [email protected]
Description:
parse a document type declaration
NOTE: we're just going to discard the DTD
*********************************************************************************************************************/
// we're just going to discard the DTD
var firstClose = str.indexOf('>',pos);
if (firstClose==-1) {
return this.error("error in DTD: expected '>'");
}
var closing_tag_prefix = '';
var firstOpenSquare = str.indexOf('[',pos);
if ((firstOpenSquare!=-1) && (firstOpenSquare < firstClose)) {
closing_tag_prefix = ']';
}
while(true) {
var closepos = str.indexOf(closing_tag_prefix + '>',pos);
if (closepos ==-1) {
return this.error("expected closing tag sequence: " + closing_tag_prefix + '>');
}
pos = closepos + closing_tag_prefix.length +1;
if (str.substring(closepos-1,closepos+2) != ']]>') {
break;
}
}
return pos;
} // end function _XMLDoc_ParseDTD
function _XMLDoc_parsePI(str,pos) {
/*******************************************************************************************************************
function: _XMLDoc_parsePI
Author: [email protected]
Description:
parse a processing instruction
NOTE: we just swallow them up at the moment
*********************************************************************************************************************/
// we just swallow them up
var closepos = str.indexOf('?>',pos);
return closepos + 2;
} // end function _XMLDoc_parsePI
function _XMLDoc_parseTag(src) {
/*******************************************************************************************************************
function: _XMLDoc_parseTag
Author: [email protected]
Description:
parse out a non-text element (incl. CDATA, comments)
handles the parsing of attributes
*********************************************************************************************************************/
// if it's a comment, strip off the packaging, mark it a comment node
// and return it
if (src.indexOf('!--')==0) {
return new XMLNode('COMMENT', this, src.substring(3,src.length-2));
}
// if it's CDATA, do similar
if (src.indexOf('![CDATA[')==0) {
return new XMLNode('CDATA', this, src.substring(8,src.length-2));
}
var n = new XMLNode();
n.doc = this;
if (src.charAt(0)=='/') {
n.nodeType = 'CLOSE';
src = src.substring(1);
}
else {
// otherwise it's an open tag (possibly an empty element)
n.nodeType = 'OPEN';
}
// if the last character is a /, check it's not a CLOSE tag
if (src.charAt(src.length-1)=='/') {
if (n.nodeType=='CLOSE') {
return this.error("singleton close tag");
}
else {
n.nodeType = 'SINGLE';
}
// strip off the last character
src = src.substring(0,src.length-1);
}
// set up the properties as appropriate
if (n.nodeType!='CLOSE') {
n.attributes = new Array();
}
if (n.nodeType=='OPEN') {
n.children = new Array();
}
// trim the whitespace off the remaining content
src = trim(src,true,true);