Files @ f43d1a4680a9
Branch filter:

Location: hot67beta/libraries/domit/xml_saxy_parser.php

ethanzonca@localhost.localdomain
menubar 0 to 10
  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
<?php
/**
* SAXY is a non-validating, but lightweight and fast SAX parser for PHP, modelled on the Expat parser
* @package saxy-xmlparser
* @subpackage saxy-xmlparser-main
* @version 1.0
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <johnkarl@nbnet.nb.ca>
* @link http://www.engageinteractive.com/saxy/ SAXY Home Page
* SAXY is Free Software
**/

if (!defined('SAXY_INCLUDE_PATH')) {
	define('SAXY_INCLUDE_PATH', (dirname(__FILE__) . "/"));
}

/** current version of SAXY */
define ('SAXY_VERSION', '1.0');

/** default XML namespace */
define ('SAXY_XML_NAMESPACE', 'http://www.w3.org/xml/1998/namespace');

/** saxy parse state, before prolog is encountered */
define('SAXY_STATE_PROLOG_NONE', 0);
/** saxy parse state, in processing instruction */
define('SAXY_STATE_PROLOG_PROCESSINGINSTRUCTION', 1);
/** saxy parse state, an exclamation mark has been encountered */
define('SAXY_STATE_PROLOG_EXCLAMATION', 2);
/** saxy parse state, in DTD */
define('SAXY_STATE_PROLOG_DTD', 3);
/** saxy parse state, an inline DTD */
define('SAXY_STATE_PROLOG_INLINEDTD', 4);
/** saxy parse state, a comment */
define('SAXY_STATE_PROLOG_COMMENT', 5);
/** saxy parse state, processing main document */
define('SAXY_STATE_PARSING', 6);
/** saxy parse state, processing comment in main document */
define('SAXY_STATE_PARSING_COMMENT', 7);

//SAXY error codes; same as EXPAT error codes
/** no error */
define('SAXY_XML_ERROR_NONE', 0);
/** out of memory error */
define('SAXY_XML_ERROR_NO_MEMORY', 1);
/** syntax error */
define('SAXY_XML_ERROR_SYNTAX', 2);
/** no elements in document */
define('SAXY_XML_ERROR_NO_ELEMENTS', 3);
/** invalid token encountered error */
define('SAXY_XML_ERROR_INVALID_TOKEN', 4);
/** unclosed token error */
define('SAXY_XML_ERROR_UNCLOSED_TOKEN', 5);
/** partial character error */
define('SAXY_XML_ERROR_PARTIAL_CHAR', 6);
/** mismatched tag error */
define('SAXY_XML_ERROR_TAG_MISMATCH', 7);
/** duplicate attribute error */
define('SAXY_XML_ERROR_DUPLICATE_ATTRIBUTE', 8);
/** junk after document element error */
define('SAXY_XML_ERROR_JUNK_AFTER_DOC_ELEMENT', 9);
/** parameter enitity reference error */
define('SAXY_XML_ERROR_PARAM_ENTITY_REF', 10);
/** undefined entity error */
define('SAXY_XML_ERROR_UNDEFINED_ENTITY', 11);
/** recursive entity error */
define('SAXY_XML_ERROR_RECURSIVE_ENTITY_REF', 12);
/** asynchronous entity error */
define('SAXY_XML_ERROR_ASYNC_ENTITY', 13);
/** bad character reference error */
define('SAXY_XML_ERROR_BAD_CHAR_REF', 14);
/** binary entity reference error */
define('SAXY_XML_ERROR_BINARY_ENTITY_REF', 15);
/** attribute external entity error */
define('SAXY_XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF', 16);
/** misplaced processing instruction error */
define('SAXY_XML_ERROR_MISPLACED_XML_PI', 17);
/** unknown encoding error */
define('SAXY_XML_ERROR_UNKNOWN_ENCODING', 18);
/** incorrect encoding error */
define('SAXY_XML_ERROR_INCORRECT_ENCODING', 19);
/** unclosed CDATA Section error */
define('SAXY_XML_ERROR_UNCLOSED_CDATA_SECTION', 20);
/** external entity handling error */
define('SAXY_XML_ERROR_EXTERNAL_ENTITY_HANDLING', 21);

require_once(SAXY_INCLUDE_PATH . 'xml_saxy_shared.php');

/**
* The SAX Parser class
*
* @package saxy-xmlparser
* @subpackage saxy-xmlparser-main
* @author John Heinstein <johnkarl@nbnet.nb.ca>
*/
class SAXY_Parser extends SAXY_Parser_Base {
    /** @var int The current error number */
	var $errorCode = SAXY_XML_ERROR_NONE;
	/** @var Object A reference to the DocType event handler */
	var $DTDHandler = null;
	/** @var Object A reference to the Comment event handler */
	var $commentHandler = null;
	/** @var Object A reference to the Processing Instruction event handler */
	var $processingInstructionHandler = null;
	/** @var Object A reference to the Start Namespace Declaration event handler */
	var $startNamespaceDeclarationHandler = null;
	/** @var Object A reference to the End Namespace Declaration event handler */
	var $endNamespaceDeclarationHandler = null;
	/** @var boolean True if SAXY takes namespaces into consideration when parsing element tags */
	var $isNamespaceAware = false;
	/** @var array An indexed array containing associative arrays of namespace prefixes mapped to their namespace URIs */
	var $namespaceMap = array();
	/** @var array A stack used to determine when an end namespace event should be fired */
	var $namespaceStack = array();
	/** @var array A track used to track the uri of the current default namespace */
	var $defaultNamespaceStack = array();
	/** @var array A stack containing tag names of unclosed elements */
	var $elementNameStack = array();

	/**
	* Constructor for SAX parser
	*/
	function SAXY_Parser() {
		$this->SAXY_Parser_Base();
		$this->state = SAXY_STATE_PROLOG_NONE;
	} //SAXY_Parser

	/**
	* Sets a reference to the handler for the DocType event
	* @param mixed A reference to the DocType handler
	*/
	function xml_set_doctype_handler($handler) {
		$this->DTDHandler =& $handler;
	} //xml_set_doctype_handler

	/**
	* Sets a reference to the handler for the Comment event
	* @param mixed A reference to the Comment handler
	*/
	function xml_set_comment_handler($handler) {
		$this->commentHandler =& $handler;
	} //xml_set_comment_handler

	/**
	* Sets a reference to the handler for the Processing Instruction event
	* @param mixed A reference to the Processing Instruction handler
	*/
	function xml_set_processing_instruction_handler($handler) {
		$this->processingInstructionHandler =& $handler;
	} //xml_set_processing_instruction_handler

	/**
	* Sets a reference to the handler for the Start Namespace Declaration event
	* @param mixed A reference to the Start Namespace Declaration handler
	*/
	function xml_set_start_namespace_decl_handler($handler) {
		$this->startNamespaceDeclarationHandler =& $handler;
	} //xml_set_start_namespace_decl_handler

	/**
	* Sets a reference to the handler for the End Namespace Declaration event
	* @param mixed A reference to the Start Namespace Declaration handler
	*/
	function xml_set_end_namespace_decl_handler($handler) {
		$this->endNamespaceDeclarationHandler =& $handler;
	} //xml_set_end_namespace_decl_handler

	/**
	* Specifies whether SAXY is namespace sensitive
	* @param boolean True if SAXY is namespace aware
	*/
	function setNamespaceAwareness($isNamespaceAware) {
		$this->isNamespaceAware =& $isNamespaceAware;
	} //setNamespaceAwareness

	/**
	* Returns the current version of SAXY
	* @return Object The current version of SAXY
	*/
	function getVersion() {
		return SAXY_VERSION;
	} //getVersion

	/**
	* Processes the xml prolog, doctype, and any other nodes that exist outside of the main xml document
	* @param string The xml text to be processed
	* @return string The preprocessed xml text
	*/
	function preprocessXML($xmlText) {
		//strip prolog
		$xmlText = trim($xmlText);
		$startChar = -1;
		$total = strlen($xmlText);

		for ($i = 0; $i < $total; $i++) {
//			$currentChar = $xmlText{$i};
			$currentChar = substr($xmlText, $i, 1);

			switch ($this->state) {
				case SAXY_STATE_PROLOG_NONE:
					if ($currentChar == '<') {
						$nextChar = $xmlText{($i + 1)};

						if ($nextChar == '?')  {
							$this->state = SAXY_STATE_PROLOG_PROCESSINGINSTRUCTION;
							$this->charContainer = '';
						}
						else if ($nextChar == '!') {
							$this->state = SAXY_STATE_PROLOG_EXCLAMATION;
							$this->charContainer .= $currentChar;
							break;
						}
						else {
							$this->charContainer = '';
							$startChar  = $i;
							$this->state = SAXY_STATE_PARSING;
							return (substr($xmlText, $startChar));
						}
					}

					break;

				case SAXY_STATE_PROLOG_EXCLAMATION:
					if ($currentChar == 'D') {
						$this->state = SAXY_STATE_PROLOG_DTD;
						$this->charContainer .= $currentChar;
					}
					else if ($currentChar == '-') {
						$this->state = SAXY_STATE_PROLOG_COMMENT;
						$this->charContainer = '';
					}
					else {
						//will trap ! and add it
						$this->charContainer .= $currentChar;
					}

					break;

				case SAXY_STATE_PROLOG_PROCESSINGINSTRUCTION:
					if ($currentChar == '>') {
						$this->state = SAXY_STATE_PROLOG_NONE;
						$this->parseProcessingInstruction($this->charContainer);
						$this->charContainer = '';
					}
					else {
						$this->charContainer .= $currentChar;
					}

					break;

				case SAXY_STATE_PROLOG_COMMENT:
					if ($currentChar == '>') {
						$this->state = SAXY_STATE_PROLOG_NONE;
						$this->parseComment($this->charContainer);
						$this->charContainer = '';
					}
					else if ($currentChar == '-') {
						if ((($xmlText{($i + 1)} == '-')  && ($xmlText{($i + 2)} == '>')) ||
							($xmlText{($i + 1)} == '>') ||
							(($xmlText{($i - 1)} == '-')  && ($xmlText{($i - 2)}== '!')) ){
							//do nothing
						}
						else {
							$this->charContainer .= $currentChar;
						}
					}
					else {
						$this->charContainer .= $currentChar;
					}

					break;

				case SAXY_STATE_PROLOG_DTD:
					if ($currentChar == '[') {
						$this->charContainer .= $currentChar;
						$this->state = SAXY_STATE_PROLOG_INLINEDTD;
					}
					else if ($currentChar == '>') {
						$this->state = SAXY_STATE_PROLOG_NONE;

						if ($this->DTDHandler != null) {
							$this->fireDTDEvent($this->charContainer . $currentChar);
						}

						$this->charContainer = '';
					}
					else {
						$this->charContainer .= $currentChar;
					}

					break;

				case SAXY_STATE_PROLOG_INLINEDTD:
					$previousChar = $xmlText{($i - 1)};

					if (($currentChar == '>') && ($previousChar == ']')){
						$this->state = SAXY_STATE_PROLOG_NONE;

						if ($this->DTDHandler != null) {
							$this->fireDTDEvent($this->charContainer . $currentChar);
						}

						$this->charContainer = '';
					}
					else {
						$this->charContainer .= $currentChar;
					}

					break;

			}
		}
	} //preprocessXML

	/**
	* The controlling method for the parsing process
	* @param string The xml text to be processed
	* @return boolean True if parsing is successful
	*/
	function parse ($xmlText) {
		$xmlText = $this->preprocessXML($xmlText);
		$total = strlen($xmlText);

		for ($i = 0; $i < $total; $i++) {
//			$currentChar = $xmlText{$i};
			$currentChar = substr($xmlText, $i, 1);

			switch ($this->state) {
				case SAXY_STATE_PARSING:
					switch ($currentChar) {
						case '<':
							if (substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) {
								$this->charContainer .= $currentChar;
							}
							else {
								$this->parseBetweenTags($this->charContainer);
								$this->charContainer = '';
							}
							break;

						case '-':
							if (($xmlText{($i - 1)} == '-') && ($xmlText{($i - 2)} == '!')
								&& ($xmlText{($i - 3)} == '<')) {
								$this->state = SAXY_STATE_PARSING_COMMENT;
								$this->charContainer = '';
							}
							else {
								$this->charContainer .= $currentChar;
							}
							break;

						case '>':
							if ((substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) &&
								!(($this->getCharFromEnd($this->charContainer, 0) == ']') &&
								($this->getCharFromEnd($this->charContainer, 1) == ']'))) {
								$this->charContainer .= $currentChar;
							}
							else {
								$this->parseTag($this->charContainer);
								$this->charContainer = '';
							}
							break;

						default:
							$this->charContainer .= $currentChar;
					}

					break;

				case SAXY_STATE_PARSING_COMMENT:
					switch ($currentChar) {
						case '>':
							if (($xmlText{($i - 1)} == '-') && ($xmlText{($i - 2)} == '-')) {
								$this->fireCommentEvent(substr($this->charContainer, 0,
													(strlen($this->charContainer) - 2)));
								$this->charContainer = '';
								$this->state = SAXY_STATE_PARSING;
							}
							else {
								$this->charContainer .= $currentChar;
							}
							break;

						default:
							$this->charContainer .= $currentChar;
					}

					break;
			}
		}

		return ($this->errorCode == 0);
	} //parse

	/**
	* Parses an element tag
	* @param string The interior text of the element tag
	*/
	function parseTag($tagText) {
		$tagText = trim($tagText);
		$firstChar = $tagText{0};
		$myAttributes = array();

		switch ($firstChar) {
			case '/':
				$tagName = substr($tagText, 1);
				$this->_fireEndElementEvent($tagName);
				break;

			case '!':
				$upperCaseTagText = strtoupper($tagText);

				if (strpos($upperCaseTagText, SAXY_SEARCH_CDATA) !== false) { //CDATA Section
					$total = strlen($tagText);
					$openBraceCount = 0;
					$textNodeText = '';

					for ($i = 0; $i < $total; $i++) {
//						$currentChar = $tagText{$i};
						$currentChar = substr($tagText, $i, 1);

						if (($currentChar == ']') && ($tagText{($i + 1)} == ']')) {
							break;
						}
						else if ($openBraceCount > 1) {
							$textNodeText .= $currentChar;
						}
						else if ($currentChar == '[') { //this won't be reached after the first open brace is found
							$openBraceCount ++;
						}
					}

					if ($this->cDataSectionHandler == null) {
						$this->fireCharacterDataEvent($textNodeText);
					}
					else {
						$this->fireCDataSectionEvent($textNodeText);
					}
				}
				else if (strpos($upperCaseTagText, SAXY_SEARCH_NOTATION) !== false) { //NOTATION node, discard
					return;
				}
				/*
				else if (substr($tagText, 0, 2) == '!-') { //comment node
					if ($this->commentHandler != null) {
						$this->fireCommentEvent(substr($tagText, 3, (strlen($tagText) - 5)));
					}
				}
				*/
				break;

			case '?':
				//Processing Instruction node
				$this->parseProcessingInstruction($tagText);
				break;

			default:
				if ((strpos($tagText, '"') !== false) || (strpos($tagText, "'") !== false)) {
					$total = strlen($tagText);
					$tagName = '';

					for ($i = 0; $i < $total; $i++) {
//						$currentChar = $tagText{$i};
						$currentChar = substr($tagText, $i, 1);

						if (($currentChar == ' ') || ($currentChar == "\t") ||
							($currentChar == "\n") || ($currentChar == "\r") ||
							($currentChar == "\x0B")) {
							$myAttributes = $this->parseAttributes(substr($tagText, $i));
							break;
						}
						else {
							$tagName .= $currentChar;
						}
					}

					if (strrpos($tagText, '/') == (strlen($tagText) - 1)) { //check $tagText, but send $tagName
						$this->_fireStartElementEvent($tagName, $myAttributes);
						$this->_fireEndElementEvent($tagName);
					}
					else {
						$this->_fireStartElementEvent($tagName, $myAttributes);
					}
				}
				else {
					if (strpos($tagText, '/') !== false) {
						$tagText = trim(substr($tagText, 0, (strrchr($tagText, '/') - 1)));
						$this->_fireStartElementEvent($tagText, $myAttributes);
						$this->_fireEndElementEvent($tagText);
					}
					else {
						$this->_fireStartElementEvent($tagText, $myAttributes);
					}
				}
		}
	} //parseTag

 	/**
	* Fires a start element event and pushes the element name onto the elementName stack
	* @param string The start element tag name
	* @param Array The start element attributes
	*/
	function _fireStartElementEvent($tagName, &$myAttributes) {
	    $this->elementNameStack[] = $tagName;

	    if ($this->isNamespaceAware) {
			$this->detectStartNamespaceDeclaration($myAttributes);
			$tagName = $this->expandNamespacePrefix($tagName);

			$this->expandAttributePrefixes($myAttributes);
	    }

	    $this->fireStartElementEvent($tagName, $myAttributes);
	} //_fireStartElementEvent

	/**
	* Expands attribute prefixes to full namespace uri
	* @param Array The start element attributes
	*/
	function expandAttributePrefixes(&$myAttributes) {
	    $arTransform = array();

	    foreach ($myAttributes as $key => $value) {
	        if (strpos($key, 'xmlns') === false) {
	            if (strpos($key, ':') !== false) {
	                $expandedTag = $this->expandNamespacePrefix($key);
	                $arTransform[$key] = $expandedTag;
	            }
	        }
	    }

	    foreach ($arTransform as $key => $value) {
	        $myAttributes[$value] = $myAttributes[$key];
	        unset($myAttributes[$key]);
	    }
	} //expandAttributePrefixes

	/**
	* Expands the namespace prefix (if one exists) to the full namespace uri
	* @param string The tagName with the namespace prefix
	* @return string The tagName, with the prefix expanded to the namespace uri
	*/
	function expandNamespacePrefix($tagName) {
	    $stackLen = count($this->defaultNamespaceStack);
	    $defaultNamespace = $this->defaultNamespaceStack[($stackLen - 1)];

	    $colonIndex = strpos($tagName, ':');

	    if ($colonIndex !== false) {
			$prefix = substr($tagName, 0, $colonIndex);

			if ($prefix != 'xml') {
	        	$tagName = $this->getNamespaceURI($prefix) . substr($tagName, $colonIndex);
			}
			else {
				$tagName = SAXY_XML_NAMESPACE . substr($tagName, $colonIndex);
			}
	    }
	    else if ($defaultNamespace != '') {
	        $tagName = $defaultNamespace . ':' . $tagName;
	    }

	    return $tagName;
	} //expandNamespacePrefix

	/**
	* Searches the namespaceMap for the specified prefix, and returns the full namespace URI
	* @param string The namespace prefix
	* @return string The namespace uri
	*/
	function getNamespaceURI($prefix) {
	    $total = count($this->namespaceMap);
	    $uri = $prefix; //in case uri can't be found, just send back prefix
	                    //should really generate an error, but worry about this later
		//reset($this->namespaceMap);

	    for ($i = ($total - 1); $i >= 0; $i--) {
	        $currMap =& $this->namespaceMap[$i];

	        if (isset($currMap[$prefix])) {
	            $uri = $currMap[$prefix];
	            break;
	        }
	    }

	    return $uri;
	} //getNamespaceURI

	/**
	* Searches the attributes array for an xmlns declaration and fires an event if found
	* @param Array The start element attributes
	*/
	function detectStartNamespaceDeclaration(&$myAttributes) {
	    $namespaceExists = false;
	    $namespaceMapUpper = 0;
	    $userDefinedDefaultNamespace = false;
	    $total = count($myAttributes);

	    foreach ($myAttributes as $key => $value) {
	        if (strpos($key, 'xmlns') !== false) {
	            //add an array to store all namespaces for the current element
	            if (!$namespaceExists) {
					$this->namespaceMap[] = array();
					$namespaceMapUpper = count($this->namespaceMap) - 1;
	            }

				//check for default namespace override, i.e. xmlns='...'
				if (strpos($key, ':') !== false) {
				    $prefix = $namespaceMapKey = substr($key, 6);
				    $this->namespaceMap[$namespaceMapUpper][$namespaceMapKey] = $value;
				}
				else {
				    $prefix = '';
					$userDefinedDefaultNamespace = true;

					//if default namespace '', store in map using key ':'
					$this->namespaceMap[$namespaceMapUpper][':'] = $value;
					$this->defaultNamespaceStack[] = $value;
				}

	            $this->fireStartNamespaceDeclarationEvent($prefix, $value);
	            $namespaceExists = true;

				unset($myAttributes[$key]);
	        }
	    }

	    //store the default namespace (inherited from the parent elements so grab last one)
		if (!$userDefinedDefaultNamespace) {
		    $stackLen = count($this->defaultNamespaceStack);
		    if ($stackLen == 0) {
		        $this->defaultNamespaceStack[] = '';
		    }
		    else {
				$this->defaultNamespaceStack[] =
					$this->defaultNamespaceStack[($stackLen - 1)];
		    }
		}

	    $this->namespaceStack[] = $namespaceExists;
	} //detectStartNamespaceDeclaration

	/**
	* Fires an end element event and pops the element name from the elementName stack
	* @param string The end element tag name
	*/
	function _fireEndElementEvent($tagName) {
	    $lastTagName = array_pop($this->elementNameStack);

		//check for mismatched tag error
		if ($lastTagName != $tagName) {
			$this->errorCode = SAXY_XML_ERROR_TAG_MISMATCH;
		}

		if ($this->isNamespaceAware) {
		    $tagName = $this->expandNamespacePrefix($tagName);
		    $this->fireEndElementEvent($tagName);
			$this->detectEndNamespaceDeclaration();
			$defaultNamespace = array_pop($this->defaultNamespaceStack);
		}
		else {
		    $this->fireEndElementEvent($tagName);
		}
	} //_fireEndElementEvent

	/**
	* Determines whether an end namespace declaration event should be fired
	*/
	function detectEndNamespaceDeclaration() {
	    $isNamespaceEnded = array_pop($this->namespaceStack);

	    if ($isNamespaceEnded) {
			$map = array_pop($this->namespaceMap);

	        foreach ($map as $key => $value) {
	            if ($key == ':') {
					$key = '';
	            }
				$this->fireEndNamespaceDeclarationEvent($key);
	        }
		}
	} //detectEndNamespaceDeclaration

	/**
	* Parses a processing instruction
	* @param string The interior text of the processing instruction
	*/
	function parseProcessingInstruction($data) {
		$endTarget = 0;
		$total = strlen($data);

		for ($x = 2; $x < $total; $x++) {
//			if (trim($data{$x}) == '') {
			if (trim(substr($data, $x, 1)) == '') {
				$endTarget = $x;
				break;
			}
		}

		$target = substr($data, 1, ($endTarget - 1));
		$data = substr($data, ($endTarget + 1), ($total - $endTarget - 2));

		if ($this->processingInstructionHandler != null) {
			$this->fireProcessingInstructionEvent($target, $data);
		}
	} //parseProcessingInstruction

	/**
	* Parses a comment
	* @param string The interior text of the comment
	*/
	function parseComment($data) {
		if ($this->commentHandler != null) {
			$this->fireCommentEvent($data);
		}
	} //parseComment

	/**
	* Fires a doctype event
	* @param string The doctype data
	*/
	function fireDTDEvent($data) {
		call_user_func($this->DTDHandler, $this, $data);
	} //fireDTDEvent

	/**
	* Fires a comment event
	* @param string The text of the comment
	*/
	function fireCommentEvent($data) {
		call_user_func($this->commentHandler, $this, $data);
	} //fireCommentEvent

	/**
	* Fires a processing instruction event
	* @param string The processing instruction data
	*/
	function fireProcessingInstructionEvent($target, $data) {
		call_user_func($this->processingInstructionHandler, $this, $target, $data);
	} //fireProcessingInstructionEvent

	/**
	* Fires a start namespace declaration event
	* @param string The namespace prefix
	* @param string The namespace uri
	*/
	function fireStartNamespaceDeclarationEvent($prefix, $uri) {
		call_user_func($this->startNamespaceDeclarationHandler, $this, $prefix, $uri);
	} //fireStartNamespaceDeclarationEvent

	/**
	* Fires an end namespace declaration event
	* @param string The namespace prefix
	*/
	function fireEndNamespaceDeclarationEvent($prefix) {
		call_user_func($this->endNamespaceDeclarationHandler, $this, $prefix);
	} //fireEndNamespaceDeclarationEvent

	/**
	* Returns the current error code
	* @return int The current error code
	*/
	function xml_get_error_code() {
		return $this->errorCode;
	} //xml_get_error_code

	/**
	* Returns a textual description of the error code
	* @param int The error code
	* @return string The error message
	*/
	function xml_error_string($code) {
		switch ($code) {
		    case SAXY_XML_ERROR_NONE:
		        return "No error";
		        break;
			case SAXY_XML_ERROR_NO_MEMORY:
			    return "Out of memory";
		        break;
			case SAXY_XML_ERROR_SYNTAX:
			    return "Syntax error";
		        break;
			case SAXY_XML_ERROR_NO_ELEMENTS:
			    return "No elements in document";
		        break;
			case SAXY_XML_ERROR_INVALID_TOKEN:
			    return "Invalid token";
		        break;
			case SAXY_XML_ERROR_UNCLOSED_TOKEN:
			    return "Unclosed token";
		        break;
			case SAXY_XML_ERROR_PARTIAL_CHAR:
			    return "Partial character";
		        break;
			case SAXY_XML_ERROR_TAG_MISMATCH:
			    return "Tag mismatch";
		        break;
			case SAXY_XML_ERROR_DUPLICATE_ATTRIBUTE:
			    return "Duplicate attribute";
		        break;
			case SAXY_XML_ERROR_JUNK_AFTER_DOC_ELEMENT:
			    return "Junk encountered after document element";
		        break;
			case SAXY_XML_ERROR_PARAM_ENTITY_REF:
			    return "Parameter entity reference error";
		        break;
			case SAXY_XML_ERROR_UNDEFINED_ENTITY:
			    return "Undefined entity";
		        break;
			case SAXY_XML_ERROR_RECURSIVE_ENTITY_REF:
			    return "Recursive entity reference";
		        break;
			case SAXY_XML_ERROR_ASYNC_ENTITY:
			    return "Asynchronous internal entity found in external entity";
		        break;
			case SAXY_XML_ERROR_BAD_CHAR_REF:
			    return "Bad character reference";
		        break;
			case SAXY_XML_ERROR_BINARY_ENTITY_REF:
				return "Binary entity reference";
		        break;
			case SAXY_XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF:
			    return "Attribute external entity reference";
		        break;
			case SAXY_XML_ERROR_MISPLACED_XML_PI:
			    return "Misplaced processing instruction";
		        break;
			case SAXY_XML_ERROR_UNKNOWN_ENCODING:
			    return "Unknown encoding";
		        break;
			case SAXY_XML_ERROR_INCORRECT_ENCODING:
				return "Incorrect encoding";
		        break;
			case SAXY_XML_ERROR_UNCLOSED_CDATA_SECTION:
			    return "Unclosed CDATA Section";
		        break;
			case SAXY_XML_ERROR_EXTERNAL_ENTITY_HANDLING:
			    return "Problem in external entity handling";
		        break;
			default:
			    return "No definition for error code " . $code;
		        break;
		}
	} //xml_error_string

} //SAXY_Parser
?>