XMPPParser.m 23.6 KB
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
#import "XMPPParser.h"
#import "XMPPLogging.h"
#import <libxml/parser.h>
#import <libxml/parserInternals.h>

#if TARGET_OS_IPHONE

// These internal functions are ripped out of DDXMLPrivate.h
// If the function signatures of these methods ever change in KissXML
// we won't know until there are runtime crashes. Not great.

// This change is needed because DDXMLPrivate.h was made private in the latest
// release of KissXML to avoid Swift issues with non-modular includes.

@interface DDXMLElement (PrivateAPI)
+ (instancetype)nodeWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)owner;
@end

@interface DDXMLNode (PrivateAPI)
+ (void)detachChild:(xmlNodePtr)child andClean:(BOOL)clean andFixNamespaces:(BOOL)fixNamespaces;
@end

#endif

#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif


#define CHECK_FOR_NULL(value)                       \
    do {                                            \
        if (value == NULL) {                        \
            xmpp_xmlAbortDueToMemoryShortage(ctxt); \
            return;                                 \
        }                                           \
    } while(false)

#if !TARGET_OS_IPHONE
  static void xmpp_recursiveAddChild(NSXMLElement *parent, xmlNodePtr childNode);
#endif

@implementation XMPPParser
{
	#if __has_feature(objc_arc_weak)
	__weak id delegate;
	#else
	__unsafe_unretained id delegate;
	#endif
	dispatch_queue_t delegateQueue;
	
	dispatch_queue_t parserQueue;
	void *xmppParserQueueTag;
	
	BOOL hasReportedRoot;
	unsigned depth;
	
	xmlParserCtxt *parserCtxt;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark iPhone
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#if TARGET_OS_IPHONE

static void xmpp_onDidReadRoot(XMPPParser *parser, xmlNodePtr root)
{
	if (parser->delegateQueue && [parser->delegate respondsToSelector:@selector(xmppParser:didReadRoot:)])
	{
		// We first copy the root node.
		// We do this to allow the delegate to retain and make changes to the reported root
		// without affecting the underlying xmpp parser.
		
		// xmlCopyNode(const xmlNodePtr node, int extended)
		// 
		// node:
		//   the node to copy
		// extended:
		//   if 1 do a recursive copy (properties, namespaces and children when applicable)
		//   if 2 copy properties and namespaces (when applicable)
		
		xmlNodePtr rootCopy = xmlCopyNode(root, 2);
		DDXMLElement *rootCopyWrapper = [DDXMLElement nodeWithElementPrimitive:rootCopy owner:nil];
		
		__strong id theDelegate = parser->delegate;
		
		dispatch_async(parser->delegateQueue, ^{ @autoreleasepool {
			
			[theDelegate xmppParser:parser didReadRoot:rootCopyWrapper];
		}});

		// Note: DDXMLElement will properly free the rootCopy when it's deallocated.
	}
}

static void xmpp_onDidReadElement(XMPPParser *parser, xmlNodePtr child)
{
	// Detach the child from the xml tree.
	// 
	// clean: Nullify next, prev, parent and doc pointers of child.
	// fixNamespaces: Recurse through subtree, and ensure no namespaces are pointing to xmlNs nodes outside the tree.
	//                E.G. in a parent node that will no longer be available after the child is detached.
	// 
	// We don't need to fix namespaces since we used xmpp_xmlSearchNs() to ensure we never created any
	// namespaces outside the subtree of the child in the first place.
	
	[DDXMLNode detachChild:child andClean:YES andFixNamespaces:NO];
	
	DDXMLElement *childWrapper = [DDXMLElement nodeWithElementPrimitive:child owner:nil];
	
	// Note: We want to detach the child from the root even if the delegate method isn't setup.
	// This prevents the doc from growing infinitely large.
	
	if (parser->delegateQueue && [parser->delegate respondsToSelector:@selector(xmppParser:didReadElement:)])
	{
		__strong id theDelegate = parser->delegate;
		
		dispatch_async(parser->delegateQueue, ^{ @autoreleasepool {
			
			[theDelegate xmppParser:parser didReadElement:childWrapper];
		}});
	}
	
	// Note: DDXMLElement will properly free the child when it's deallocated.
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Mac
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#else

static void xmpp_setName(NSXMLElement *element, xmlNodePtr node)
{
	// Remember: The NSString initWithUTF8String raises an exception if passed NULL
	
	if (node->name == NULL)
	{
		[element setName:@""];
		return;
	}
	
	if ((node->ns != NULL) && (node->ns->prefix != NULL))
	{
		// E.g: <deusty:element xmlns:deusty="deusty.com"/>
		
		NSString *prefix = [[NSString alloc] initWithUTF8String:(const char *)node->ns->prefix];
		NSString *name   = [[NSString alloc] initWithUTF8String:(const char *)node->name];
		
		NSString *elementName = [[NSString alloc] initWithFormat:@"%@:%@", prefix, name];
		[element setName:elementName];
		
	}
	else
	{
		NSString *elementName = [[NSString alloc] initWithUTF8String:(const char *)node->name];
		[element setName:elementName];
	}
}

static void xmpp_addNamespaces(NSXMLElement *element, xmlNodePtr node)
{
	// Remember: The NSString initWithUTF8String raises an exception if passed NULL
	
	xmlNsPtr nsNode = node->nsDef;
	while (nsNode != NULL)
	{
		if (nsNode->href == NULL)
		{
			// Namespace doesn't have a value!
		}
		else
		{
			NSXMLNode *ns = [[NSXMLNode alloc] initWithKind:NSXMLNamespaceKind];
			
			if (nsNode->prefix != NULL)
			{
				NSString *nsName = [[NSString alloc] initWithUTF8String:(const char *)nsNode->prefix];
				[ns setName:nsName];
			}
			else
			{
				// Default namespace.
				// E.g: xmlns="deusty.com"
				
				[ns setName:@""];
			}
			
			NSString *nsValue = [[NSString alloc] initWithUTF8String:(const char *)nsNode->href];
			[ns setStringValue:nsValue];
			
			[element addNamespace:ns];
		}
		
		nsNode = nsNode->next;
	}
}

static void xmpp_addChildren(NSXMLElement *element, xmlNodePtr node)
{
	// Remember: The NSString initWithUTF8String raises an exception if passed NULL
	
	xmlNodePtr childNode = node->children;
	while (childNode != NULL)
	{
		if (childNode->type == XML_ELEMENT_NODE)
		{
			xmpp_recursiveAddChild(element, childNode);
		}
		else if (childNode->type == XML_TEXT_NODE)
		{
			if (childNode->content != NULL)
			{
				NSString *value = [[NSString alloc] initWithUTF8String:(const char *)childNode->content];
				[element setStringValue:value];
			}
		}
		
		childNode = childNode->next;
	}
}

static void xmpp_addAttributes(NSXMLElement *element, xmlNodePtr node)
{
	// Remember: The NSString initWithUTF8String raises an exception if passed NULL
	
	xmlAttrPtr attrNode = node->properties;
	while (attrNode != NULL)
	{
		if (attrNode->name == NULL)
		{
			// Attribute doesn't have a name!
		}
		else if (attrNode->children == NULL)
		{
			// Attribute doesn't have a value node!
		}
		else if (attrNode->children->content == NULL)
		{
			// Attribute doesn't have a value!
		}
		else
		{
			NSXMLNode *attr = [[NSXMLNode alloc] initWithKind:NSXMLAttributeKind];
			
			if ((attrNode->ns != NULL) && (attrNode->ns->prefix != NULL))
			{
				// E.g: <element xmlns:deusty="deusty.com" deusty:attr="value"/>
				
				NSString *prefix = [[NSString alloc] initWithUTF8String:(const char *)attrNode->ns->prefix];
				NSString *name   = [[NSString alloc] initWithUTF8String:(const char *)attrNode->name];
				
				NSString *attrName = [[NSString alloc] initWithFormat:@"%@:%@", prefix, name];
				[attr setName:attrName];
				
			}
			else
			{
				NSString *attrName = [[NSString alloc] initWithUTF8String:(const char *)attrNode->name];
				[attr setName:attrName];
			}
			
			NSString *attrValue = [[NSString alloc] initWithUTF8String:(const char *)attrNode->children->content];
			[attr setStringValue:attrValue];
			
			[element addAttribute:attr];
		}
		
		attrNode = attrNode->next;
	}
}

/**
 * Recursively adds all the child elements to the given parent.
 * 
 * Note: This method is almost the same as xmpp_nsxmlFromLibxml, with one important difference.
 * It doen't add any objects to the autorelease pool (xmpp_nsxmlFromLibXml has return value).
**/
static void xmpp_recursiveAddChild(NSXMLElement *parent, xmlNodePtr childNode)
{
	// Remember: The NSString initWithUTF8String raises an exception if passed NULL
	
	NSXMLElement *child = [[NSXMLElement alloc] initWithKind:NSXMLElementKind];
	
	xmpp_setName(child, childNode);
	
	xmpp_addNamespaces(child, childNode);
	
	xmpp_addChildren(child, childNode);
	xmpp_addAttributes(child, childNode);
	
	[parent addChild:child];
}

/**
 * Creates and returns an NSXMLElement from the given node.
 * Use this method after finding the root element, or root.child element.
**/
static NSXMLElement* xmpp_nsxmlFromLibxml(xmlNodePtr rootNode)
{
	// Remember: The NSString initWithUTF8String raises an exception if passed NULL
	
	NSXMLElement *root = [[NSXMLElement alloc] initWithKind:NSXMLElementKind];
	
	xmpp_setName(root, rootNode);
	
	xmpp_addNamespaces(root, rootNode);
	
	xmpp_addChildren(root, rootNode);
	xmpp_addAttributes(root, rootNode);
	
	return root;
}

static void xmpp_onDidReadRoot(XMPPParser *parser, xmlNodePtr root)
{
	if (parser->delegateQueue && [parser->delegate respondsToSelector:@selector(xmppParser:didReadRoot:)])
	{
		NSXMLElement *nsRoot = xmpp_nsxmlFromLibxml(root);
		
		__strong id theDelegate = parser->delegate;
		
		dispatch_async(parser->delegateQueue, ^{ @autoreleasepool {
			
			[theDelegate xmppParser:parser didReadRoot:nsRoot];
		}});
	}
}

static void xmpp_onDidReadElement(XMPPParser *parser, xmlNodePtr child)
{
	if (parser->delegateQueue && [parser->delegate respondsToSelector:@selector(xmppParser:didReadElement:)])
	{
		NSXMLElement *nsChild = xmpp_nsxmlFromLibxml(child);
		
		__strong id theDelegate = parser->delegate;
		
		dispatch_async(parser->delegateQueue, ^{ @autoreleasepool {
		
			[theDelegate xmppParser:parser didReadElement:nsChild];
		}});
	}
	
	// Note: We want to detach the child from the root even if the delegate method isn't setup.
	// This prevents the doc from growing infinitely large.
	
	// Detach and free child to keep memory footprint small
	xmlUnlinkNode(child);
	xmlFreeNode(child);
}

#endif

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Common
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * This method is called at the end of the xmlStartElement method.
 * This allows us to inspect the parser and xml tree, and determine if we need to invoke any delegate methods.
**/
static void xmpp_postStartElement(xmlParserCtxt *ctxt)
{
	XMPPParser *parser = (__bridge XMPPParser *)ctxt->_private;
	parser->depth++;
	
	if (!(parser->hasReportedRoot) && (parser->depth == 1))
	{
		// We've received the full root - report it to the delegate
		
		if (ctxt->myDoc)
		{
			xmlNodePtr root = xmlDocGetRootElement(ctxt->myDoc);
			if (root)
			{
				xmpp_onDidReadRoot(parser, root);
				
				parser->hasReportedRoot = YES;
			}
		}
	}
}

/**
 * This method is called at the end of the xmlEndElement method.
 * This allows us to inspect the parser and xml tree, and determine if we need to invoke any delegate methods.
**/
static void xmpp_postEndElement(xmlParserCtxt *ctxt)
{
	XMPPParser *parser = (__bridge XMPPParser *)ctxt->_private;
	parser->depth--;
	
	if (parser->depth == 1)
	{
		// End of full xmpp element.
		// That is, a child of the root element.
		// Extract the child, and pass it to the delegate.
		
		xmlDocPtr doc = ctxt->myDoc;
		xmlNodePtr root = xmlDocGetRootElement(doc);
		
		xmlNodePtr child = root->children;
		while (child != NULL)
		{
			if (child->type == XML_ELEMENT_NODE)
			{
				xmpp_onDidReadElement(parser, child);
				
				// Exit while loop
				break;
			}
			
			child = child->next;
		}
	}
	else if (parser->depth == 0)
	{
		// End of the root element
		
		if (parser->delegateQueue && [parser->delegate respondsToSelector:@selector(xmppParserDidEnd:)])
		{
			__strong id theDelegate = parser->delegate;
			
			dispatch_async(parser->delegateQueue, ^{ @autoreleasepool {
			
				[theDelegate xmppParserDidEnd:parser];
			}});
		}
	}
}

/**
 * We're screwed...
**/
static void xmpp_xmlAbortDueToMemoryShortage(xmlParserCtxt *ctxt)
{
	XMPPParser *parser = (__bridge XMPPParser *)ctxt->_private;
	
	xmlStopParser(ctxt);
	
	if (parser->delegateQueue && [parser->delegate respondsToSelector:@selector(xmppParser:didFail:)])
	{
		NSString *errMsg = @"Unable to allocate memory in xmpp parser";
		NSDictionary *info = @{NSLocalizedDescriptionKey : errMsg};
		
		NSError *error = [NSError errorWithDomain:@"libxmlErrorDomain" code:1001 userInfo:info];
		
		__strong id theDelegate = parser->delegate;
		
		dispatch_async(parser->delegateQueue, ^{ @autoreleasepool {
			
			[theDelegate xmppParser:parser didFail:error];
		}});
	}
}

/**
 * (Similar to the libxml "xmlSearchNs" method, with one very important difference.)
 * 
 * This method searches for an existing xmlNsPtr in the given node,
 * recursing on the parents but stopping before it reaches the root node of the document.
 * 
 * Why do we skip the root node?
 * Because all nodes are going to be detached from the root node.
 * So it makes no sense to allow them to reference namespaces stored in the root node,
 * since the detach algorithm will be forced to copy the namespaces later anyway.
**/
static xmlNsPtr xmpp_xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace)
{
	xmlNodePtr rootNode = xmlDocGetRootElement(doc);
	
	xmlNodePtr currentNode = node;
	while (currentNode && currentNode != rootNode)
	{
		xmlNsPtr currentNs = currentNode->nsDef;
		while (currentNs)
		{
			if (currentNs->href != NULL)
			{
				if ((currentNs->prefix == NULL) && (nameSpace == NULL))
				{
					return currentNs;
				}
				if ((currentNs->prefix != NULL) && (nameSpace != NULL))
				{
					if (xmlStrEqual(currentNs->prefix, nameSpace))
						return currentNs;
				}
			}
			
			currentNs = currentNs->next;
		}
		
		currentNode = currentNode->parent;
	}
	
	return NULL;
}

/**
 * SAX parser C-style callback.
 * Invoked when a new node element is started.
**/
static void	xmpp_xmlStartElement(void *ctx, const xmlChar  *nodeName,
                                            const xmlChar  *nodePrefix,
                                            const xmlChar  *nodeUri,
                                                      int   nb_namespaces,
                                            const xmlChar **namespaces,
                                                      int   nb_attributes,
                                                      int   nb_defaulted,
                                            const xmlChar **attributes)
{
	int i, j;
	xmlNsPtr lastAddedNs = NULL;
	
	xmlParserCtxt *ctxt = (xmlParserCtxt *)ctx;
	
	// We store the parent node in the context's node pointer.
	// We keep this updated by "pushing" the node in the startElement method,
	// and "popping" the node in the endElement method.
	xmlNodePtr parent = ctxt->node;
	
	// Create the node
	xmlNodePtr newNode = xmlNewDocNode(ctxt->myDoc, NULL, nodeName, NULL);
	CHECK_FOR_NULL(newNode);
	
	// Add the node to the tree
	if (parent == NULL)
	{
		// Root node
		xmlAddChild((xmlNodePtr)ctxt->myDoc, newNode);
	}
	else
	{
		xmlAddChild(parent, newNode);
	}
	
	// Process the namespaces
	for (i = 0, j = 0; j < nb_namespaces; j++)
	{
		// Extract namespace prefix and uri
		const xmlChar *nsPrefix = namespaces[i++];
		const xmlChar *nsUri    = namespaces[i++];
		
		// Create the namespace
		xmlNsPtr newNs = xmlNewNs(NULL, nsUri, nsPrefix);
		CHECK_FOR_NULL(newNs);
		
		// Add namespace to node.
		// Each node has a linked list of nodes (in the nsDef variable).
		// The linked list is forward only.
		// In other words, each ns has a next, but not a prev pointer.
		
		if (newNode->nsDef == NULL)
		{
			newNode->nsDef = newNs;
			lastAddedNs = newNs;
		}
		else
		{
            if(lastAddedNs != NULL)
            {
                lastAddedNs->next = newNs;
            }
            
			lastAddedNs = newNs;
		}
		
		// Is this the namespace for the node?
		
		if (nodeUri && (nodePrefix == nsPrefix))
		{
			// Ex 1: node == <stream:stream xmlns:stream="url"> && newNs == stream:url
			// Ex 2: node == <starttls xmlns="url">             && newNs == null:url
			
			newNode->ns = newNs;
		}
	}
	
	// Search for the node's namespace if it wasn't already found
	if ((nodeUri) && (newNode->ns == NULL))
	{
		newNode->ns = xmpp_xmlSearchNs(ctxt->myDoc, newNode, nodePrefix);
		
		if (newNode->ns == NULL)
		{
			// We use href==NULL in the case of an element creation where the namespace was not defined.
			// 
			// We do NOT use xmlNewNs(newNode, nodeUri, nodePrefix) because that method doesn't properly add
			// the namespace to BOTH nsDef and ns.
			
			xmlNsPtr newNs = xmlNewNs(NULL, nodeUri, nodePrefix);
			CHECK_FOR_NULL(newNs);
			
			if (newNode->nsDef == NULL)
			{
				newNode->nsDef = newNs;
			}
			else if(lastAddedNs != NULL)
			{
				lastAddedNs->next = newNs;
			}
			
			newNode->ns = newNs;
		}
	}
	
	// Process all the attributes
	for (i = 0, j = 0; j < nb_attributes; j++)
	{
		const xmlChar *attrName   = attributes[i++];
		const xmlChar *attrPrefix = attributes[i++];
		const xmlChar *attrUri    = attributes[i++];
		const xmlChar *valueBegin = attributes[i++];
		const xmlChar *valueEnd   = attributes[i++];
		
        // The attribute value might contain character references which need to be decoded.
        // 
        // "Franks &#38; Beans" -> "Franks & Beans"
        
		xmlChar *value = xmlStringLenDecodeEntities(ctxt,                    // the parser context
		                                            valueBegin,              // the input string
		                                      (int)(valueEnd - valueBegin),  // the input string length
		                                           (XML_SUBSTITUTE_REF),     // what to substitue
		                                            0, 0, 0);                // end markers, 0 if none
		CHECK_FOR_NULL(value);
        
		if ((attrPrefix == NULL) && (attrUri == NULL))
		{
			// Normal attribute - no associated namespace
			xmlAttrPtr newAttr = xmlNewProp(newNode, attrName, value);
			CHECK_FOR_NULL(newAttr);
		}
		else
		{
			// Find the namespace for the attribute
			xmlNsPtr attrNs = xmpp_xmlSearchNs(ctxt->myDoc, newNode, attrPrefix);
			
			if (attrNs != NULL)
			{
				xmlAttrPtr newAttr = xmlNewNsProp(newNode, attrNs, attrName, value);
				CHECK_FOR_NULL(newAttr);
			}
			else
			{
				attrNs = xmlNewNs(NULL, NULL, nodePrefix);
				CHECK_FOR_NULL(attrNs);
				
				xmlAttrPtr newAttr = xmlNewNsProp(newNode, attrNs, attrName, value);
				CHECK_FOR_NULL(newAttr);
			}
		}
		
		xmlFree(value);
	}
	
	// Update our parent node pointer
	ctxt->node = newNode;
	
	// Invoke delegate methods if needed
	xmpp_postStartElement(ctxt);
}

/**
 * SAX parser C-style callback.
 * Invoked when characters are found within a node.
**/
static void xmpp_xmlCharacters(void *ctx, const xmlChar *ch, int len)
{
	xmlParserCtxt *ctxt = (xmlParserCtxt *)ctx;
	
	if (ctxt->node != NULL)
	{
		xmlNodePtr textNode = xmlNewTextLen(ch, len);
		
		// xmlAddChild(xmlNodePtr parent, xmlNodePtr cur)
		// 
		// Add a new node to @parent, at the end of the child list
		// merging adjacent TEXT nodes (in which case @cur is freed).
		
		xmlAddChild(ctxt->node, textNode);
	}
}

/**
 * SAX parser C-style callback.
 * Invoked when a new node element is ended.
**/
static void xmpp_xmlEndElement(void *ctx, const xmlChar *localname,
                                          const xmlChar *prefix,
                                          const xmlChar *URI)
{
	xmlParserCtxt *ctxt = (xmlParserCtxt *)ctx;
	
	// Update our parent node pointer
	if (ctxt->node != NULL)
		ctxt->node = ctxt->node->parent;
	
	// Invoke delegate methods if needed
	xmpp_postEndElement(ctxt);
}

- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq
{
	return [self initWithDelegate:aDelegate delegateQueue:dq parserQueue:NULL];
}

- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq parserQueue:(dispatch_queue_t)pq
{
	if ((self = [super init]))
	{
		delegate = aDelegate;
		delegateQueue = dq;
		
		#if !OS_OBJECT_USE_OBJC
		if (delegateQueue)
			dispatch_retain(delegateQueue);
		#endif

		if (pq) {
			parserQueue = pq;
			
			#if !OS_OBJECT_USE_OBJC
			dispatch_retain(parserQueue);
			#endif
		}
		else {
			parserQueue = dispatch_queue_create("xmpp.parser", NULL);
		}
		
		xmppParserQueueTag = &xmppParserQueueTag;
		dispatch_queue_set_specific(parserQueue, xmppParserQueueTag, xmppParserQueueTag, NULL);
		
		hasReportedRoot = NO;
		depth  = 0;
		
		// Create SAX handler
		xmlSAXHandler saxHandler;
		memset(&saxHandler, 0, sizeof(xmlSAXHandler));
		
		saxHandler.initialized = XML_SAX2_MAGIC;
		saxHandler.startElementNs = xmpp_xmlStartElement;
		saxHandler.characters = xmpp_xmlCharacters;
		saxHandler.endElementNs = xmpp_xmlEndElement;
		
		// Create the push parser context
		parserCtxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
		
		// Note: This method copies the saxHandler, so we don't have to keep it around.
		
		// Create the document to hold the parsed elements
		parserCtxt->myDoc = xmlNewDoc(parserCtxt->version);
		
		// Store reference to ourself
		parserCtxt->_private = (__bridge void *)(self);
		
		// Note: The parserCtxt also has a userData variable, but it is used by the DOM building functions.
		// If we put a value there, it actually causes a crash!
		// We need to be sure to use the _private variable which libxml won't touch.
	}
	return self;
}

- (void)dealloc
{
	if (parserCtxt)
	{
		// The xmlFreeParserCtxt method will not free the created document in parserCtxt->myDoc.
		if (parserCtxt->myDoc)
		{
			// Free the created xmlDoc
			xmlFreeDoc(parserCtxt->myDoc);
		}
		
		xmlFreeParserCtxt(parserCtxt);
	}
	
	#if !OS_OBJECT_USE_OBJC
	if (delegateQueue)
		dispatch_release(delegateQueue);
	if (parserQueue)
		dispatch_release(parserQueue);
	#endif
}

- (void)setDelegate:(id)newDelegate delegateQueue:(dispatch_queue_t)newDelegateQueue
{
	#if !OS_OBJECT_USE_OBJC
	if (newDelegateQueue)
		dispatch_retain(newDelegateQueue);
	#endif
	
	dispatch_block_t block = ^{
		
		delegate = newDelegate;
		
		#if !OS_OBJECT_USE_OBJC
		if (delegateQueue)
			dispatch_release(delegateQueue);
		#endif
		
		delegateQueue = newDelegateQueue;
	};
	
	if (dispatch_get_specific(xmppParserQueueTag))
		block();
	else
		dispatch_async(parserQueue, block);
}

- (void)parseData:(NSData *)data
{
	dispatch_block_t block = ^{ @autoreleasepool {
	
		int result = xmlParseChunk(parserCtxt, (const char *)[data bytes], (int)[data length], 0);
		
		if (result == 0)
		{
			if (delegateQueue && [delegate respondsToSelector:@selector(xmppParserDidParseData:)])
			{
				__strong id theDelegate = delegate;
				
				dispatch_async(delegateQueue, ^{ @autoreleasepool {
					
					[theDelegate xmppParserDidParseData:self];
				}});
			}
		}
		else
		{
			if (delegateQueue && [delegate respondsToSelector:@selector(xmppParser:didFail:)])
			{
				NSError *error;
				
				xmlError *xmlErr = xmlCtxtGetLastError(parserCtxt);
				
				if (xmlErr->message)
				{
					NSString *errMsg = [NSString stringWithFormat:@"%s", xmlErr->message];
					NSDictionary *info = @{NSLocalizedDescriptionKey : errMsg};
					
					error = [NSError errorWithDomain:@"libxmlErrorDomain" code:xmlErr->code userInfo:info];
				}
				else
				{
					error = [NSError errorWithDomain:@"libxmlErrorDomain" code:xmlErr->code userInfo:nil];
				}
				
				__strong id theDelegate = delegate;
				
				dispatch_async(delegateQueue, ^{ @autoreleasepool {
					
					[theDelegate xmppParser:self didFail:error];
				}});
			}
		}
	}};
	
	if (dispatch_get_specific(xmppParserQueueTag))
		block();
	else
		dispatch_async(parserQueue, block);
}

@end