Skip to content
Snippets Groups Projects
XMLDOMUtil.cpp 23.6 KiB
Newer Older
PeiYong Zhang's avatar
PeiYong Zhang committed
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
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache\@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation, and was
 * originally based on software copyright (c) 1999, International
 * Business Machines, Inc., http://www.ibm.com .  For more information
 * on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

/*
 * $Id$
 */

#include "stdafx.h"
#include "xml4com.h"
#include "XMLDOMElement.h"
#include "XMLDOMAttribute.h"
#include "XMLDOMText.h"
#include "XMLDOMCDATASection.h"
#include "XMLDOMEntityReference.h"
#include "XMLDOMEntity.h"
#include "XMLDOMProcessingInstruction.h"
#include "XMLDOMComment.h"
#include "XMLDOMDocument.h"
#include "XMLDOMDocumentType.h"
#include "XMLDOMDocumentFragment.h"
#include "XMLDOMNotation.h"
#include "XMLDOMXMLDecl.h"
#include "XMLDOMUtil.h"
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLUniDefs.hpp>


const OLECHAR* g_DomNodeName[] =
{	
	OLESTR("invalid"),
	OLESTR("element"),
	OLESTR("attribute"),
	OLESTR("text"),
	OLESTR("cdatasection"),
	OLESTR("entityreference"),
	OLESTR("entity"),
	OLESTR("processinginstruction"),
	OLESTR("comment"),
	OLESTR("document"),
	OLESTR("documenttype"),
	OLESTR("documentfragment"),
	OLESTR("notation")
};

const int g_DomNodeNameSize = sizeof(g_DomNodeName) / sizeof(OLECHAR*);

DOMString GetText(const DOM_Node& node)
{
	DOM_Node::NodeType type = static_cast<DOM_Node::NodeType> (node.getNodeType());
	DOMString val;		

	if (DOM_Node::DOCUMENT_TYPE_NODE		  == type ||
		DOM_Node::NOTATION_NODE				  == type)
		return val;
	
	if (DOM_Node::CDATA_SECTION_NODE		  == type ||
		DOM_Node::COMMENT_NODE				  == type ||
		DOM_Node::PROCESSING_INSTRUCTION_NODE == type ||
		DOM_Node::TEXT_NODE					  == type) {
		val = node.getNodeValue();
		return val;
	}

	DOM_NodeList l =  node.getChildNodes();
	int length = l.getLength();
	if (length > 0) {
		for (int i = 0; i < length; ++i)
			val.appendData(GetText(l.item(i)));
	}
	else {
		val = node.getNodeValue();
	}
	return val;
}



template <class Base>
class CComObjectPool
{
public:
	CComObjectPool(unsigned long poolSize);

	virtual ~CComObjectPool();

	HRESULT WINAPI CreateInstance(Base** pp);

	HRESULT Deactivate(Base* obj);

private:
	Base** m_pool;
	unsigned long m_size;
	unsigned long m_hit;
	unsigned long m_attempt;
	HRESULT Activate(Base* obj);
};




template <class Base>
class CPooledComObject : public Base
{
public:
	typedef Base _BaseClass;
	CPooledComObject(void* = NULL)
	{
		_Module.Lock();
	}
	// Set refcount to 1 to protect destruction
	~CPooledComObject()
	{
		m_dwRef = 1L;
		FinalRelease();
#ifdef _ATL_DEBUG_INTERFACES
		_Module.DeleteNonAddRefThunk(_GetRawUnknown());
#endif
		_Module.Unlock();
	}
	//If InternalAddRef or InternalRelease is undefined then your class
	//doesn't derive from CComObjectRoot
	STDMETHOD_(ULONG, AddRef)() {return InternalAddRef();}
	STDMETHOD_(ULONG, Release)()
	{
		ULONG l = InternalRelease();
		if (l == 0) {
			if(SUCCEEDED(m_pool.Deactivate(this))) {
				FinalRelease();
			}
			else
				delete this;
		}
		return l;
	}
	//if _InternalQueryInterface is undefined then you forgot BEGIN_COM_MAP
	STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject)
	{return _InternalQueryInterface(iid, ppvObject);}
	template <class Q>
	HRESULT STDMETHODCALLTYPE QueryInterface(Q** pp)
	{
		return QueryInterface(__uuidof(Q), (void**)pp);
	}

	static HRESULT WINAPI CreateInstance(Base** pp) {
		return m_pool.CreateInstance(pp);
	}

private:
	static CComObjectPool<Base> m_pool;
};




template <class Base>
CComObjectPool<Base>::CComObjectPool<Base>(unsigned long poolSize) {
	m_pool = NULL;
	m_size = poolSize;
	m_pool = new Base*[m_size];
	for(unsigned long i = 0; i < m_size; i++)
		m_pool[i] = NULL;
	m_hit= 0;
	m_attempt = 0;
}

template <class Base>
CComObjectPool<Base>::~CComObjectPool<Base>() {
	for(unsigned long i = 0; i < m_size; i++) {
		if(m_pool[i]) delete m_pool[i];
	}
	delete [] m_pool;
}

template <class Base>
HRESULT CComObjectPool<Base>::Deactivate(Base* obj) {
	for(unsigned long i = 0; i < m_size; i++) {
		if(m_pool[i] == NULL) {
			m_pool[i] = obj;
			return S_OK;
		}
	}
	return E_FAIL;
}

template <class Base>
HRESULT CComObjectPool<Base>::Activate(Base* p)
{
	p->SetVoid(NULL);
	p->InternalFinalConstructAddRef();
	HRESULT hRes = p->FinalConstruct();
	p->InternalFinalConstructRelease();
	return hRes;
}


template <class Base>
HRESULT WINAPI CComObjectPool<Base>::CreateInstance(Base** pp) {
	ATLASSERT(pp != NULL);
	HRESULT hRes = E_OUTOFMEMORY;
	Base* p = NULL;

	m_attempt++;

	for(unsigned long i = 0; i < m_size; i++) {
		if(m_pool[i]) {
			p = m_pool[i];
			m_pool[i] = NULL;
			hRes = Activate(p);
			if (SUCCEEDED(hRes)) {
				m_hit++;
				break;
			}
			else {
				delete p;
				p = NULL;
			}
		}
	}

	if(FAILED(hRes)) {
		ATLTRY(p = new CPooledComObject<Base>())
		if (p != NULL) {
			hRes = Activate(p);
			if (hRes != S_OK) {
				delete p;
				p = NULL;
			}
		}
	}
	*pp = p;
	return hRes;
}


CComObjectPool<CXMLDOMElement> CPooledComObject<CXMLDOMElement>::m_pool(6);
typedef CPooledComObject<CXMLDOMElement> CPooledXMLDOMElementObj;

CComObjectPool<CXMLDOMAttribute> CPooledComObject<CXMLDOMAttribute>::m_pool(6);
typedef CPooledComObject<CXMLDOMAttribute> CPooledXMLDOMAttributeObj;

CComObjectPool<CXMLDOMText> CPooledComObject<CXMLDOMText>::m_pool(6);
typedef CPooledComObject<CXMLDOMText> CPooledXMLDOMTextObj;

CComObjectPool<CXMLDOMCDATASection> CPooledComObject<CXMLDOMCDATASection>::m_pool(6);
typedef CPooledComObject<CXMLDOMCDATASection> CPooledXMLDOMCDATASectionObj;

CComObjectPool<CXMLDOMEntityReference> CPooledComObject<CXMLDOMEntityReference>::m_pool(6);
typedef CPooledComObject<CXMLDOMEntityReference> CPooledXMLDOMEntityReferenceObj;

CComObjectPool<CXMLDOMEntity> CPooledComObject<CXMLDOMEntity>::m_pool(6);
typedef CPooledComObject<CXMLDOMEntity> CPooledXMLDOMEntityObj;

CComObjectPool<CXMLDOMProcessingInstruction> CPooledComObject<CXMLDOMProcessingInstruction>::m_pool(6);
typedef CPooledComObject<CXMLDOMProcessingInstruction> CPooledXMLDOMProcessingInstructionObj;

CComObjectPool<CXMLDOMComment> CPooledComObject<CXMLDOMComment>::m_pool(6);
typedef CPooledComObject<CXMLDOMComment> CPooledXMLDOMCommentObj;


HRESULT wrapNode(IXMLDOMDocument *pDoc, DOM_Node& node, REFIID iid, LPVOID *pVal)
{
	HRESULT hr = S_OK;
	if (NULL == pVal)
		return E_POINTER;

	*pVal = NULL;
	short type = node.getNodeType();

	// the way we are constructing the wrappers is kind of fishy but oh well...
	// the various IBM DOM wrapper classes don't ever add any members or have
	// any v-tables so what we are doing should be safe.  There isn't any other
	// way as far as I can tell to do this....

	switch(type)
	{
	case DOM_Node::ELEMENT_NODE:
	{
		CXMLDOMElement *pObj = NULL;
		hr = CPooledXMLDOMElementObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->element = *(static_cast<DOM_Element*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::ATTRIBUTE_NODE:
	{
		CXMLDOMAttribute *pObj = NULL;
		hr = CPooledXMLDOMAttributeObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->attr = *(static_cast<DOM_Attr*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::TEXT_NODE:
	{
		CXMLDOMText *pObj = NULL;
		hr = CPooledXMLDOMTextObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->text = *(static_cast<DOM_Text*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::CDATA_SECTION_NODE:
	{
		CXMLDOMCDATASection *pObj = NULL;
		hr = CPooledXMLDOMCDATASectionObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->cdataSection = *(static_cast<DOM_CDATASection*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::ENTITY_REFERENCE_NODE:
	{
		CXMLDOMEntityReference *pObj = NULL;
		hr = CPooledXMLDOMEntityReferenceObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->entityReference = *(static_cast<DOM_EntityReference*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::ENTITY_NODE:
	{
		CXMLDOMEntity *pObj = NULL;
		hr = CPooledXMLDOMEntityObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);
		
		try
		{
			pObj->entity = *(static_cast<DOM_Entity*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::PROCESSING_INSTRUCTION_NODE:
	{
		CXMLDOMProcessingInstruction *pObj = NULL;
		hr = CPooledXMLDOMProcessingInstructionObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->processingInstruction = *(static_cast<DOM_ProcessingInstruction*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::COMMENT_NODE:
	{
		CXMLDOMComment *pObj = NULL;
		hr = CPooledXMLDOMCommentObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->comment = *(static_cast<DOM_Comment*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::DOCUMENT_NODE:
	{
		CXMLDOMDocumentObj *pObj = NULL;
		hr = CXMLDOMDocumentObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->m_Document = *(static_cast<DOM_Document*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::DOCUMENT_TYPE_NODE:
	{
		CXMLDOMDocumentTypeObj *pObj = NULL;
		hr = CXMLDOMDocumentTypeObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->documentType = *(static_cast<DOM_DocumentType*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::DOCUMENT_FRAGMENT_NODE:
	{
		CXMLDOMDocumentFragmentObj *pObj = NULL;
		hr = CXMLDOMDocumentFragmentObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->documentFragment = *(static_cast<DOM_DocumentFragment*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	case DOM_Node::NOTATION_NODE:
	{
		CXMLDOMNotationObj *pObj = NULL;
		hr = CXMLDOMNotationObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->notation = *(static_cast<DOM_Notation*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}

	case DOM_Node::XML_DECL_NODE:
	{
		CXMLDOMXMLDeclObj *pObj = NULL;
		hr = CXMLDOMXMLDeclObj::CreateInstance(&pObj);
		if (S_OK != hr)
			return hr;
	
		pObj->AddRef();
		pObj->SetOwnerDoc(pDoc);

		try
		{
			pObj->xmlDecl = *(static_cast<DOM_XMLDecl*> (&node));
		}
		catch(DOM_DOMException& ex)
		{
			pObj->Release();
			return MakeHRESULT(ex);
		}
		catch(...)
		{
			pObj->Release();
			return E_FAIL;
		}
	
		hr = pObj->QueryInterface(iid, pVal);
		if (S_OK != hr)
			*pVal = NULL;

		pObj->Release();
		break;
	}
	default:
		hr = E_NOTIMPL;
		break;
	}

	return hr;
}


class xmlstream {
public:
	xmlstream() {
		m_length = 0;
		m_alloc = 0;
		m_buffer = 0;
		m_next = 0;
	}

	~xmlstream() {
		delete [] m_buffer;
	}

	xmlstream& operator<<(const XMLCh* other) {
		//
		//   get length of string
		//
		unsigned long len = 0;
		for(const XMLCh* source = other; *source; source++,len++);

		//
		//    append to stream
		//
		append(other,len);
		return *this;
	}

	xmlstream& operator<<(const DOMString& other) {
		append(other.rawBuffer(),other.length());
		return *this;
	}

	xmlstream& operator<<(const XMLCh other) {
		append(&other,1);
		return *this;
	}

	BSTR SysAllocString() {
		if(m_length > 0)
			return SysAllocStringLen(m_buffer,m_length);
		return 0;
	}

private:
	void append(const XMLCh* other,unsigned long length) {
		const XMLCh* source = NULL;

		if(m_length + length > m_alloc) {
			unsigned long chunk = 4096;
			if(length > chunk) chunk += length;
			XMLCh* newbuf = new XMLCh[m_alloc+ chunk];
			m_alloc += chunk;
			m_next = newbuf + m_length;

			//
			//    copy old content into new buffer
			//
			XMLCh* dest = newbuf;
			source = m_buffer;
			for(unsigned long i = 0; i < m_length; i++,dest++,source++) {
				*dest = *source;
			}
			delete [] m_buffer;
			m_buffer = newbuf;
		}

		source = other;
		for(unsigned long i = 0; i < length; i++,source++,m_next++) {
			*m_next = *source;
		}
		m_length += length;
	}

	unsigned long m_length;
	unsigned long m_alloc;
	XMLCh* m_buffer;
	XMLCh* m_next;
};




// ---------------------------------------------------------------------------
//  outputContent
//
//  Write document content from a DOMString to a C++ ostream. Escape the
//  XML special characters (<, &, etc.) unless this is suppressed by the
//  command line option.
// ---------------------------------------------------------------------------
void outputContent(xmlstream& target, const DOMString &toWrite)
{

        int            length = toWrite.length();
        const XMLCh*   chars  = toWrite.rawBuffer();

        int index;
        for (index = 0; index < length; index++)
        {
            switch (chars[index])
            {
            case chAmpersand :
                target << XMLStrL("&amp;");
                break;

            case chOpenAngle :
                target << XMLStrL("&lt;");
                break;

            case chCloseAngle:
                target << XMLStrL("&gt;");
                break;

            case chDoubleQuote :
                target << XMLStrL("&quot;");
                break;

            default:
                // If it is none of the special characters, print it as such
                target << toWrite.substringData(index, 1);
                break;
            }
        }

    return;
}

xmlstream& operator<<(xmlstream& target, const DOM_Node& toWrite)
{
    // Get the name and value out for convenience
    DOMString   nodeName = toWrite.getNodeName();
    DOMString   nodeValue = toWrite.getNodeValue();


	switch (toWrite.getNodeType())
    {
		case DOM_Node::TEXT_NODE:
        {
            outputContent(target, nodeValue);
            break;
        }

        case DOM_Node::PROCESSING_INSTRUCTION_NODE :
        {
            target  << XMLStrL("<?")
                    << nodeName
                    << XMLStrL(' ')
                    << nodeValue
                    << XMLStrL("?>");
            break;
        }

        case DOM_Node::DOCUMENT_NODE :
        {
            //
            //  Bug here:  we need to find a way to get the encoding name
            //  for the default code page on the system where the program
            //  is running, and plug that in for the encoding name.
            //
            //target << "<?xml version='1.0' encoding='ISO-8859-1' ?>\n";
            DOM_Node child = toWrite.getFirstChild();
            while( child != 0)
            {
                target << child;
                child = child.getNextSibling();
            }
            break;
        }

        case DOM_Node::ELEMENT_NODE :
        {
            // Output the element start tag.
            target << XMLStrL('<') << nodeName;

            // Output any attributes on this element
            DOM_NamedNodeMap attributes = toWrite.getAttributes();
            int attrCount = attributes.getLength();
            for (int i = 0; i < attrCount; i++)
            {
                DOM_Node  attribute = attributes.item(i);

                target  << XMLStrL(' ') << attribute.getNodeName()
                        << XMLStrL(" = \"");
                        //  Note that "<" must be escaped in attribute values.
                        outputContent(target, attribute.getNodeValue());
                        target << XMLStrL('"');
            }

            //
            //  Test for the presence of children, which includes both
            //  text content and nested elements.
            //
            DOM_Node child = toWrite.getFirstChild();
            if (child != 0)
            {
                // There are children. Close start-tag, and output children.
                target << XMLStrL(">");
                while( child != 0)
                {
                    target << child;
                    child = child.getNextSibling();
                }

                // Done with children.  Output the end tag.
                target << XMLStrL("</") << nodeName << XMLStrL(">");
            }
            else
            {
                //
                //  There were no children. Output the short form close of
                //  the element start tag, making it an empty-element tag.
                //
                target << XMLStrL("/>");
            }
            break;
        }

        case DOM_Node::ENTITY_REFERENCE_NODE:
        {
            DOM_Node child;
            for (child = toWrite.getFirstChild(); child != 0; child = child.getNextSibling())
                target << child;
            break;
        }

        case DOM_Node::CDATA_SECTION_NODE:
        {
            target << XMLStrL("<![CDATA[") << nodeValue << XMLStrL("]]>");
            break;
        }

        case DOM_Node::COMMENT_NODE:
        {
            target << XMLStrL("<!--") << nodeValue << XMLStrL("-->");
            break;
        }

        case DOM_Node::DOCUMENT_TYPE_NODE:
        {
			DOM_DocumentType doctype = (DOM_DocumentType &)toWrite;;

			target << XMLStrL("<!DOCTYPE ") << nodeName ;
			DOMString id = doctype.getPublicId();
			if (id != 0)
				target << XMLStrL(" PUBLIC \"") << id << XMLStrL("\"");
			id = doctype.getSystemId();
			if (id != 0)