Newer
Older
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
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 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) 2001, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/*
* $Id$
*/
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMDocumentType.hpp>
#include <xercesc/dom/DOMException.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMNamedNodeMap.hpp>
#include <xercesc/dom/DOMNode.hpp>
#include "DOMDocumentImpl.hpp"
#include "DOMCasts.hpp"
#include "DOMDocumentTypeImpl.hpp"
#include "DOMAttrImpl.hpp"
#include "DOMAttrNSImpl.hpp"
#include "DOMCDATASectionImpl.hpp"
#include "DOMCommentImpl.hpp"
#include "DOMDeepNodeListImpl.hpp"
#include "DOMDocumentFragmentImpl.hpp"
#include "DOMElementImpl.hpp"
#include "XSDElementNSImpl.hpp"
#include "DOMEntityImpl.hpp"
#include "DOMEntityReferenceImpl.hpp"
#include "DOMNamedNodeMapImpl.hpp"
#include "DOMNotationImpl.hpp"
#include "DOMProcessingInstructionImpl.hpp"
#include "DOMTextImpl.hpp"
#include "DOMStringPool.hpp"
#include "DOMTreeWalkerImpl.hpp"
#include "DOMNodeIteratorImpl.hpp"
#include "DOMNodeIDMap.hpp"
#include "DOMRangeImpl.hpp"
#include <xercesc/internal/XMLReader.hpp>
#include <xercesc/util/HashPtr.hpp>
//revisit. These can go away once all of the include files above are really there.
class DOMTreeWalker;
class DOMNodeFilter;
//
// Constructors. Warning - be very careful with the ordering of initialization
// of the heap. Ordering depends on the order of declaration
// in the .hpp file, not on the order of initializers here
// in the constructor. The heap declaration can not be
// first - fNode and fParent must be first for the casting
// functions in DOMCasts to work correctly. This means that
// fNode and fParent constructors used here can not
// allocate.
//
DOMDocumentImpl::DOMDocumentImpl()
: fNode(this),
fParent(this),
fCurrentBlock(0),
fFreePtr(0),
fFreeBytesRemaining(0),
fDocType(0),
fDocElement(0),
fNamePool(0),
fIterators(0L),
fTreeWalkers(0L),
fNodeIDMap(0),
fRanges(0),
fChanges(0),
fNodeListPool(0),
fActualEncoding(0),
fEncoding(0),
fVersion(0),
fStandalone(false),
{
fNamePool = new (this) DOMStringPool(257, this);
};
//DOM Level 2
DOMDocumentImpl::DOMDocumentImpl(const XMLCh *fNamespaceURI,
const XMLCh *qualifiedName,
DOMDocumentType *doctype)
: fNode(this),
fParent(this),
fCurrentBlock(0),
fFreePtr(0),
fFreeBytesRemaining(0),
fDocType(0),
fDocElement(0),
fNamePool(0),
fIterators(0L),
fTreeWalkers(0L),
fNodeIDMap(0),
fRanges(0),
fChanges(0),
fNodeListPool(0),
fActualEncoding(0),
fEncoding(0),
fVersion(0),
fStandalone(false),
{
fNamePool = new (this) DOMStringPool(257, this);
try {
setDocumentType(doctype);
appendChild(createElementNS(fNamespaceURI, qualifiedName)); //root element
}
catch (...) {
this->deleteHeap();
throw;
}
}
void DOMDocumentImpl::setDocumentType(DOMDocumentType *doctype)
{
// New doctypes can be created either with the factory methods on DOMImplementation, in
// which case ownerDocument will be 0, or with methods on DocumentImpl, in which case
// ownerDocument will be set, but the DocType won't yet be a child of the document.
if (doctype->getOwnerDocument() != 0 && doctype->getOwnerDocument() != this)
throw DOMException( //one doctype can belong to only one DOMDocumentImpl
DOMException::WRONG_DOCUMENT_ERR, 0);
DOMDocumentTypeImpl* doctypeImpl = (DOMDocumentTypeImpl*) doctype;
doctypeImpl->setOwnerDocument(this);
// The doctype can not have any Entities or Notations yet, because they can not
// be created except through factory methods on a document.
// revisit. What if this doctype is already a child of the document?
}
DOMDocumentImpl::~DOMDocumentImpl()
{
// Clean up the fNodeListPool
if (fNodeListPool)
fNodeListPool->cleanup();
// Clean up the RefVector
if (fIterators)
fIterators->cleanup();
if (fTreeWalkers)
fTreeWalkers->cleanup();
if (fRanges)
fRanges->cleanup();
if (fUserDataTable)
fUserDataTable->cleanup();
// Delete the heap for this document. This uncerimoniously yanks the storage
// out from under all of the nodes in the document. Destructors are NOT called.
this->deleteHeap();
};
DOMNode *DOMDocumentImpl::cloneNode(bool deep) const {
// Note: the cloned document node goes on the system heap. All other
// nodes added to the new document will go on that document's heap,
// but we need to construct the document first, before its heap exists.
DOMDocumentImpl *newdoc = new DOMDocumentImpl();
// then the children by _importing_ them
if (deep)
for (DOMNode *n = this->getFirstChild(); n != 0; n = n->getNextSibling()) {
newdoc->appendChild(newdoc->importNode(n, true, true));
}
fNode.callUserDataHandlers(DOMUserDataHandler::NODE_CLONED, this, newdoc);
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
return newdoc;
};
const XMLCh * DOMDocumentImpl::getNodeName() const {
static const XMLCh nam[] = // "#document"
{chPound, chLatin_d, chLatin_o, chLatin_c, chLatin_u, chLatin_m, chLatin_e, chLatin_n, chLatin_t, 0};
return nam;
}
short DOMDocumentImpl::getNodeType() const {
return DOMNode::DOCUMENT_NODE;
};
// even though ownerDocument refers to this in this implementation
// the DOM Level 2 spec says it must be 0, so make it appear so
DOMDocument * DOMDocumentImpl::getOwnerDocument() const {
return 0;
}
DOMAttr *DOMDocumentImpl::createAttribute(const XMLCh *nam)
{
if(!isXMLName(nam))
throw DOMException(DOMException::INVALID_CHARACTER_ERR,0);
return new (this) DOMAttrImpl(this,nam);
};
DOMCDATASection *DOMDocumentImpl::createCDATASection(const XMLCh *data) {
return new (this) DOMCDATASectionImpl(this,data);
};
DOMComment *DOMDocumentImpl::createComment(const XMLCh *data)
{
return new (this) DOMCommentImpl(this, data);
};
DOMDocumentFragment *DOMDocumentImpl::createDocumentFragment()
{
return new (this) DOMDocumentFragmentImpl(this);
};
DOMDocumentType *DOMDocumentImpl::createDocumentType(const XMLCh *nam)
{
if (!isXMLName(nam))
throw DOMException(
DOMException::INVALID_CHARACTER_ERR, 0);
return new (this) DOMDocumentTypeImpl(this, nam);
};
DOMDocumentType *
DOMDocumentImpl::createDocumentType(const XMLCh *qualifiedName,
const XMLCh *publicId,
const XMLCh *systemId)
{
if (!isXMLName(qualifiedName))
throw DOMException(
DOMException::INVALID_CHARACTER_ERR, 0);
return new (this) DOMDocumentTypeImpl(this, qualifiedName, publicId, systemId);
};
DOMElement *DOMDocumentImpl::createElement(const XMLCh *tagName)
{
if(!isXMLName(tagName))
throw DOMException(DOMException::INVALID_CHARACTER_ERR,0);
return new (this) DOMElementImpl(this,tagName);
};
DOMElement *DOMDocumentImpl::createElementNoCheck(const XMLCh *tagName)
{
return new (this) DOMElementImpl(this, tagName);
};
DOMEntity *DOMDocumentImpl::createEntity(const XMLCh *nam)
{
if (!isXMLName(nam))
throw DOMException(
DOMException::INVALID_CHARACTER_ERR, 0);
return new (this) DOMEntityImpl(this, nam);
};
DOMEntityReference *DOMDocumentImpl::createEntityReference(const XMLCh *nam)
{
if (!isXMLName(nam))
throw DOMException(
DOMException::INVALID_CHARACTER_ERR, 0);
return new (this) DOMEntityReferenceImpl(this, nam);
};
DOMNotation *DOMDocumentImpl::createNotation(const XMLCh *nam)
{
if (!isXMLName(nam))
throw DOMException(
DOMException::INVALID_CHARACTER_ERR, 0);
return new (this) DOMNotationImpl(this, nam);
};
DOMProcessingInstruction *DOMDocumentImpl::createProcessingInstruction(
const XMLCh *target, const XMLCh *data)
{
if(!isXMLName(target))
throw DOMException(DOMException::INVALID_CHARACTER_ERR,0);
return new (this) DOMProcessingInstructionImpl(this,target,data);
};
DOMText *DOMDocumentImpl::createTextNode(const XMLCh *data)
{
return new (this) DOMTextImpl(this,data);
};
DOMNodeIterator* DOMDocumentImpl::createNodeIterator (
DOMNode *root, unsigned long whatToShow, DOMNodeFilter* filter, bool entityReferenceExpansion)
{
// Create the node iterator implementation object.
// Add it to the vector of fIterators that must be synchronized when a node is deleted.
// The vector of fIterators is kept in the "owner document" if there is one. If there isn't one, I assume that root is the
// owner document.
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
DOMNodeIteratorImpl* iter = new (this) DOMNodeIteratorImpl(root, whatToShow, filter, entityReferenceExpansion);
DOMDocument* doc = root->getOwnerDocument();
DOMDocumentImpl* impl;
if (doc != 0) {
impl = (DOMDocumentImpl *) doc;
}
else
impl = (DOMDocumentImpl *) root;
if (impl->fIterators == 0L) {
impl->fIterators = new (this) NodeIterators(1, false);
impl->fIterators->addElement(iter);
}
return iter;
}
DOMTreeWalker* DOMDocumentImpl::createTreeWalker (DOMNode *root, unsigned long whatToShow, DOMNodeFilter* filter, bool entityReferenceExpansion)
{
// See notes for createNodeIterator...
DOMTreeWalkerImpl* twi = new (this) DOMTreeWalkerImpl(root, whatToShow, filter, entityReferenceExpansion);
DOMDocument* doc = root->getOwnerDocument();
DOMDocumentImpl* impl;
if ( doc != 0) {
impl = (DOMDocumentImpl *) doc;
}
else
impl = (DOMDocumentImpl *) root;
if (impl->fTreeWalkers == 0L) {
impl->fTreeWalkers = new (this) TreeWalkers(1, false);
impl->fTreeWalkers->addElement(twi);
}
return twi;
}
DOMDocumentType *DOMDocumentImpl::getDoctype() const
{
return fDocType;
};
DOMElement *DOMDocumentImpl::getDocumentElement() const
{
return fDocElement;
};
DOMNodeList *DOMDocumentImpl::getElementsByTagName(const XMLCh *tagname) const
{
// cast off the const of this because we will update the fNodeListPool
return ((DOMDocumentImpl*)this)->getDeepNodeList(this,tagname);
};
DOMImplementation *DOMDocumentImpl::getImplementation() const {
return DOMImplementation::getImplementation();
}
DOMNode *DOMDocumentImpl::insertBefore(DOMNode *newChild, DOMNode *refChild)
{
// Only one such child permitted
if(
(newChild->getNodeType() == DOMNode::ELEMENT_NODE && fDocElement!=0)
||
(newChild->getNodeType() == DOMNode::DOCUMENT_TYPE_NODE && fDocType!=0)
)
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR,0);
fParent.insertBefore(newChild,refChild);
// If insert succeeded, cache the kid appropriately
if(newChild->getNodeType() == DOMNode::ELEMENT_NODE)
fDocElement=(DOMElement *)newChild;
else if(newChild->getNodeType() == DOMNode::DOCUMENT_TYPE_NODE)
fDocType=(DOMDocumentType *)newChild;
return newChild;
};
bool DOMDocumentImpl::isXMLName(const XMLCh *s)
{
// revist. This function probably already exists in the scanner.
if (!XMLReader::isFirstNameChar(s[0]))
return false;
const XMLCh *p;
for (p=s+1; *p!=0; p++)
{
if (!XMLReader::isNameChar(*p))
return false;
}
return true;
};
DOMNode *DOMDocumentImpl::removeChild(DOMNode *oldChild)
{
fParent.removeChild(oldChild);
// If remove succeeded, un-cache the kid appropriately
if(oldChild->getNodeType() == DOMNode::ELEMENT_NODE)
fDocElement=0;
else if(oldChild->getNodeType() == DOMNode::DOCUMENT_TYPE_NODE)
fDocType=0;
return oldChild;
};
void DOMDocumentImpl::setNodeValue(const XMLCh *x)
{
fNode.setNodeValue(x);
};
//Introduced in DOM Level 2
DOMNode *DOMDocumentImpl::importNode(DOMNode *source, bool deep)
{
}
DOMElement *DOMDocumentImpl::createElementNS(const XMLCh *fNamespaceURI,
{
if(!isXMLName(qualifiedName))
throw DOMException(DOMException::INVALID_CHARACTER_ERR,0);
//XMLCh * pooledTagName = this->fNamePool->getPooledString(qualifiedName);
return new (this) DOMElementNSImpl(this, fNamespaceURI, qualifiedName);
}
DOMElement *DOMDocumentImpl::createElementNS(const XMLCh *fNamespaceURI,
const XMLCh *qualifiedName,
Tinny Ng
committed
const XMLSSize_t lineNo,
const XMLSSize_t columnNo)
{
if(!isXMLName(qualifiedName))
throw DOMException(DOMException::INVALID_CHARACTER_ERR,0);
return new (this) XSDElementNSImpl(this, fNamespaceURI, qualifiedName, lineNo, columnNo);
}
DOMAttr *DOMDocumentImpl::createAttributeNS(const XMLCh *fNamespaceURI,
{
if(!isXMLName(qualifiedName))
throw DOMException(DOMException::INVALID_CHARACTER_ERR,0);
return new (this) DOMAttrNSImpl(this, fNamespaceURI, qualifiedName);
}
DOMNodeList *DOMDocumentImpl::getElementsByTagNameNS(const XMLCh *fNamespaceURI,
{
// cast off the const of this because we will update the fNodeListPool
return ((DOMDocumentImpl*)this)->getDeepNodeList(this, fNamespaceURI, fLocalName);
}
DOMElement *DOMDocumentImpl::getElementById(const XMLCh *elementId) const
{
if (fNodeIDMap == 0)
return 0;
DOMAttr *theAttr = fNodeIDMap->find(elementId);
if (theAttr == 0)
return theAttr->getOwnerElement();
}
//Return the index > 0 of ':' in the given qualified name qName="prefix:localName".
//Return 0 if there is no ':', or -1 if qName is malformed such as ":abcd" or "abcd:".
int DOMDocumentImpl::indexofQualifiedName(const XMLCh * qName)
{
int qNameLen = XMLString::stringLen(qName);
int index = -1, count = 0;
for (int i = 0; i < qNameLen; ++i) {
if (qName[i] == chColon) {
index = i;
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
}
}
if (qNameLen == 0 || count > 1 || index == 0 || index == qNameLen-1)
return -1;
return count == 0 ? 0 : index;
}
DOMRange* DOMDocumentImpl::createRange()
{
DOMRangeImpl* range = new (this) DOMRangeImpl(this);
if (fRanges == 0L) {
fRanges = new (this) Ranges(1, false);
}
fRanges->addElement(range);
return range;
}
Ranges* DOMDocumentImpl::getRanges() const
{
return fRanges;
}
void DOMDocumentImpl::removeRange(DOMRangeImpl* range)
{
if (fRanges != 0) {
Tinny Ng
committed
XMLSize_t sz = fRanges->size();
if (sz !=0) {
Tinny Ng
committed
for (XMLSize_t i =0; i<sz; i++) {
if (fRanges->elementAt(i) == range) {
fRanges->removeElementAt(i);
break;
}
}
}
}
}
/** Uses the kidOK lookup table to check whether the proposed
tree structure is legal.
????? It feels like there must be a more efficient solution,
but for the life of me I can't think what it would be.
*/
bool DOMDocumentImpl::isKidOK(DOMNode *parent, DOMNode *child)
{
static int kidOK[14];
if (kidOK[DOMNode::ATTRIBUTE_NODE] == 0)
{
kidOK[DOMNode::DOCUMENT_NODE] =
1 << DOMNode::ELEMENT_NODE |
1 << DOMNode::PROCESSING_INSTRUCTION_NODE |
1 << DOMNode::COMMENT_NODE |
kidOK[DOMNode::DOCUMENT_FRAGMENT_NODE] =
kidOK[DOMNode::ENTITY_NODE] =
kidOK[DOMNode::ENTITY_REFERENCE_NODE] =
kidOK[DOMNode::ELEMENT_NODE] =
1 << DOMNode::ELEMENT_NODE |
1 << DOMNode::PROCESSING_INSTRUCTION_NODE |
1 << DOMNode::COMMENT_NODE |
1 << DOMNode::TEXT_NODE |
1 << DOMNode::CDATA_SECTION_NODE |
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
kidOK[DOMNode::ATTRIBUTE_NODE] =
1 << DOMNode::TEXT_NODE |
1 << DOMNode::ENTITY_REFERENCE_NODE;
kidOK[DOMNode::PROCESSING_INSTRUCTION_NODE] =
kidOK[DOMNode::COMMENT_NODE] =
kidOK[DOMNode::TEXT_NODE] =
kidOK[DOMNode::CDATA_SECTION_NODE] =
kidOK[DOMNode::NOTATION_NODE] =
0;
};
int p=parent->getNodeType();
int ch = child->getNodeType();
return (kidOK[p] & 1<<ch) != 0;
}
void DOMDocumentImpl::changed()
{
fChanges++;
}
int DOMDocumentImpl::changes() const{
return fChanges;
};
//
// Delegation for functions inherited from DOMNode
//
DOMNode* DOMDocumentImpl::appendChild(DOMNode *newChild) {return insertBefore(newChild, 0); };
DOMNamedNodeMap* DOMDocumentImpl::getAttributes() const {return fNode.getAttributes (); };
DOMNodeList* DOMDocumentImpl::getChildNodes() const {return fParent.getChildNodes (); };
DOMNode* DOMDocumentImpl::getFirstChild() const {return fParent.getFirstChild (); };
DOMNode* DOMDocumentImpl::getLastChild() const {return fParent.getLastChild (); };
const XMLCh* DOMDocumentImpl::getLocalName() const {return fNode.getLocalName (); };
const XMLCh* DOMDocumentImpl::getNamespaceURI() const {return fNode.getNamespaceURI (); };
DOMNode* DOMDocumentImpl::getNextSibling() const {return fNode.getNextSibling (); };
const XMLCh* DOMDocumentImpl::getNodeValue() const {return fNode.getNodeValue (); };
const XMLCh* DOMDocumentImpl::getPrefix() const {return fNode.getPrefix (); };
DOMNode* DOMDocumentImpl::getParentNode() const {return fNode.getParentNode (); };
DOMNode* DOMDocumentImpl::getPreviousSibling() const {return fNode.getPreviousSibling (); };
bool DOMDocumentImpl::hasChildNodes() const {return fParent.hasChildNodes (); };
void DOMDocumentImpl::normalize() {fParent.normalize (); };
DOMNode* DOMDocumentImpl::replaceChild(DOMNode *newChild, DOMNode *oldChild)
{return fParent.replaceChild (newChild, oldChild); };
bool DOMDocumentImpl::isSupported(const XMLCh *feature, const XMLCh *version) const
{return fNode.isSupported (feature, version); };
void DOMDocumentImpl::setPrefix(const XMLCh *prefix) {fNode.setPrefix(prefix); };
bool DOMDocumentImpl::hasAttributes() const {return fNode.hasAttributes(); };
void* DOMDocumentImpl::setUserData(const XMLCh* key, void* data, DOMUserDataHandler* handler)
{return fNode.setUserData(key, data, handler); };
void* DOMDocumentImpl::getUserData(const XMLCh* key) const {return fNode.getUserData(key); };
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
//-----------------------------------------------------------------------
//
// Per Document Heap and Heap Helper functions
//
// revisit - this stuff should be a class of its own, rather than
// just lying around naked in DocumentImpl.
//
//-----------------------------------------------------------------------
XMLCh * DOMDocumentImpl::cloneString(const XMLCh *src)
{
if (!src) return 0;
size_t len = XMLString::stringLen(src);
len = (len + 1) * sizeof(XMLCh);
len = (len % 4) + len;
XMLCh *newStr = (XMLCh *)this->allocate(len);
XMLString::copyString(newStr, src);
return newStr;
}
const XMLCh * DOMDocumentImpl::getPooledString(const XMLCh *src)
{
return this->fNamePool->getPooledString(src);
}
static const int kHeapAllocSize = 0x10000; // The chunk size to allocate from the
// system allocator.
static const int kMaxSubAllocationSize = 4096; // Any request for more bytes
// than this will be handled by
// allocating directly with system.
void * DOMDocumentImpl::allocate(size_t amount)
{
size_t sizeOfPointer = sizeof(void *);
if (amount%sizeOfPointer!=0)
amount = amount + (sizeOfPointer - (amount % sizeOfPointer));
// If the request is for a largish block, hand it off to the system
// allocator. The block still must be linked into the list of
// allocated blocks so that it will be deleted when the time comes.
if (amount > kMaxSubAllocationSize)
{
void* newBlock = 0;
try {
newBlock = new char[amount + sizeOfPointer];
}
catch (...) {
ThrowXML(RuntimeException, XMLExcepts::Out_Of_Memory);
}
if (!newBlock)
ThrowXML(RuntimeException, XMLExcepts::Out_Of_Memory);
if (fCurrentBlock)
{
*(void **)newBlock = *(void **)fCurrentBlock;
*(void **)fCurrentBlock = newBlock;
}
else
{
fCurrentBlock = newBlock;
fFreePtr = 0;
fFreeBytesRemaining = 0;
}
void *retPtr = (char *)newBlock + sizeOfPointer;
return retPtr;
}
// It's a normal (sub-allocatable) request.
if (amount > fFreeBytesRemaining)
{
// Request doesn't fit in the current block.
// Get a new one from the system allocator.
void* newBlock = 0;
try {
newBlock = new char[kHeapAllocSize];
}
catch (...) {
ThrowXML(RuntimeException, XMLExcepts::Out_Of_Memory);
}
if (!newBlock)
ThrowXML(RuntimeException, XMLExcepts::Out_Of_Memory);
*(void **)newBlock = fCurrentBlock;
fCurrentBlock = newBlock;
fFreePtr = (char *)newBlock + sizeOfPointer;
fFreeBytesRemaining = kHeapAllocSize - sizeOfPointer;
}
void *retPtr = fFreePtr;
fFreePtr += amount;
fFreeBytesRemaining -= amount;
return retPtr;
}
void DOMDocumentImpl::deleteHeap()
{
void *block = fCurrentBlock;
while (fCurrentBlock != 0)
{
void *nextBlock = *(void **)fCurrentBlock;
delete [] fCurrentBlock;
fCurrentBlock = nextBlock;
}
}
DOMNodeList *DOMDocumentImpl::getDeepNodeList(const DOMNode *rootNode, const XMLCh *tagName)
{
if(!fNodeListPool) {
fNodeListPool = new (this) DOMDeepNodeListPool<DOMDeepNodeListImpl>(109, false);
}
DOMDeepNodeListImpl* retList = fNodeListPool->getByKey(rootNode, tagName, 0);
if (!retList) {
int id = fNodeListPool->put((void*) rootNode, (XMLCh*) tagName, 0, new (this) DOMDeepNodeListImpl(rootNode, tagName));
retList = fNodeListPool->getById(id);
}
return retList;
}
DOMNodeList *DOMDocumentImpl::getDeepNodeList(const DOMNode *rootNode, //DOM Level 2
const XMLCh *namespaceURI,
const XMLCh *localName)
{
if(!fNodeListPool) {
fNodeListPool = new (this) DOMDeepNodeListPool<DOMDeepNodeListImpl>(109, false);
}
DOMDeepNodeListImpl* retList = fNodeListPool->getByKey(rootNode, localName, namespaceURI);
if (!retList) {
// the pool will adopt the DOMDeepNodeListImpl
int id = fNodeListPool->put((void*) rootNode, (XMLCh*) localName, (XMLCh*) namespaceURI, new (this) DOMDeepNodeListImpl(rootNode, namespaceURI, localName));
retList = fNodeListPool->getById(id);
}
return retList;
}
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
//Introduced in DOM Level 3
const XMLCh* DOMDocumentImpl::getActualEncoding() const {
return fActualEncoding;
}
void DOMDocumentImpl::setActualEncoding(const XMLCh* actualEncoding){
fActualEncoding = cloneString(actualEncoding);
}
const XMLCh* DOMDocumentImpl::getEncoding() const {
return fEncoding;
}
void DOMDocumentImpl::setEncoding(const XMLCh* encoding){
fEncoding = cloneString(encoding);
}
bool DOMDocumentImpl::getStandalone() const{
return fStandalone;
}
void DOMDocumentImpl::setStandalone(bool standalone){
fStandalone = standalone;
}
const XMLCh* DOMDocumentImpl::getVersion() const {
return fVersion;
}
void DOMDocumentImpl::setVersion(const XMLCh* version){
fVersion = cloneString(version);
}
const XMLCh* DOMDocumentImpl::getDocumentURI() const {
return fDocumentURI;
}
void DOMDocumentImpl::setDocumentURI(const XMLCh* documentURI){
fDocumentURI = cloneString(documentURI);
}
//Introduced in DOM Level 2
DOMNode *DOMDocumentImpl::importNode(DOMNode *source, bool deep, bool cloningDoc)
{
DOMNode *newnode=0;
bool oldErrorCheckingFlag = errorChecking;
switch (source->getNodeType())
{
case DOMNode::ELEMENT_NODE :
{
DOMElement *newelement;
if (source->getLocalName() == 0)
newelement = createElement(source->getNodeName());
else
newelement = createElementNS(source->getNamespaceURI(),
source->getNodeName());
DOMNamedNodeMap *srcattr=source->getAttributes();
if(srcattr!=0)
for(XMLSize_t i=0;i<srcattr->getLength();++i)
{
DOMAttr *attr = (DOMAttr *) srcattr->item(i);
if (attr -> getSpecified()) { // not a default attribute
DOMAttr *nattr = (DOMAttr *) importNode(attr, true, false);
if (attr -> getLocalName() == 0)
newelement->setAttributeNode(nattr);
else
newelement->setAttributeNodeNS(nattr);
// if the imported attribute is of ID type, register the new node in fNodeIDMap
if (castToNodeImpl(attr)->isIdAttr()) {
castToNodeImpl(nattr)->isIdAttr(true);
if (!fNodeIDMap)
fNodeIDMap = new (this) DOMNodeIDMap(500, this);
fNodeIDMap->add((DOMAttr*)nattr);
}
newnode=newelement;
}
break;
case DOMNode::ATTRIBUTE_NODE :
if (source->getLocalName() == 0)
newnode = createAttribute(source->getNodeName());
else
newnode = createAttributeNS(source->getNamespaceURI(),
source->getNodeName());
deep = true;
// Kids carry value
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
break;
case DOMNode::TEXT_NODE :
newnode = createTextNode(source->getNodeValue());
break;
case DOMNode::CDATA_SECTION_NODE :
newnode = createCDATASection(source->getNodeValue());
break;
case DOMNode::ENTITY_REFERENCE_NODE :
{
DOMEntityReferenceImpl* newentityRef = (DOMEntityReferenceImpl*)createEntityReference(source->getNodeName());
newnode=newentityRef;
errorChecking = false;
newentityRef->setReadOnly(false, true); //allow deep import temporarily
}
break;
case DOMNode::ENTITY_NODE :
{
DOMEntity *srcentity=(DOMEntity *)source;
DOMEntityImpl *newentity = (DOMEntityImpl *)createEntity(source->getNodeName());
newentity->setPublicId(srcentity->getPublicId());
newentity->setSystemId(srcentity->getSystemId());
newentity->setNotationName(srcentity->getNotationName());
// Kids carry additional value
newnode=newentity;
castToNodeImpl(newentity)->setReadOnly(false, true);// allow deep import temporarily
}
break;
case DOMNode::PROCESSING_INSTRUCTION_NODE :
newnode = createProcessingInstruction(source->getNodeName(),
source->getNodeValue());
break;
case DOMNode::COMMENT_NODE :
newnode = createComment(source->getNodeValue());
break;
case DOMNode::DOCUMENT_TYPE_NODE :
{
// unless this is used as part of cloning a Document
// forbid it for the sake of being compliant to the DOM spec
if (!cloningDoc)
throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0);
DOMDocumentType *srcdoctype = (DOMDocumentType *)source;
DOMDocumentType *newdoctype = (DOMDocumentType *)
createDocumentType(srcdoctype->getNodeName(),
srcdoctype->getPublicId(),