source: NonGTP/Xerces/xercesc/dom/impl/DOMDocumentImpl.hpp @ 188

Revision 188, 19.5 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1#ifndef DOMDocumentImpl_HEADER_GUARD_
2#define DOMDocumentImpl_HEADER_GUARD_
3
4/*
5 * The Apache Software License, Version 1.1
6 *
7 * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
8 * reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in
19 *    the documentation and/or other materials provided with the
20 *    distribution.
21 *
22 * 3. The end-user documentation included with the redistribution,
23 *    if any, must include the following acknowledgment:
24 *       "This product includes software developed by the
25 *        Apache Software Foundation (http://www.apache.org/)."
26 *    Alternately, this acknowledgment may appear in the software itself,
27 *    if and wherever such third-party acknowledgments normally appear.
28 *
29 * 4. The names "Xerces" and "Apache Software Foundation" must
30 *    not be used to endorse or promote products derived from this
31 *    software without prior written permission. For written
32 *    permission, please contact apache\@apache.org.
33 *
34 * 5. Products derived from this software may not be called "Apache",
35 *    nor may "Apache" appear in their name, without prior written
36 *    permission of the Apache Software Foundation.
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This software consists of voluntary contributions made by many
53 * individuals on behalf of the Apache Software Foundation, and was
54 * originally based on software copyright (c) 2001, International
55 * Business Machines, Inc., http://www.ibm.com .  For more information
56 * on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 */
59
60/*
61 * $Id: DOMDocumentImpl.hpp,v 1.23 2004/01/29 11:44:26 cargilld Exp $
62 */
63
64//
65//  This file is part of the internal implementation of the C++ XML DOM.
66//  It should NOT be included or used directly by application programs.
67//
68//  Applications should include the file <xercesc/dom/DOM.hpp> for the entire
69//  DOM API, or xercesc/dom/DOM*.hpp for individual DOM classes, where the class
70//  name is substituded for the *.
71//
72
73#include <xercesc/util/RefArrayOf.hpp>
74#include <xercesc/util/RefStackOf.hpp>
75#include <xercesc/util/RefHashTableOf.hpp>
76#include <xercesc/util/KeyRefPair.hpp>
77#include <xercesc/dom/DOMDocument.hpp>
78#include <xercesc/dom/DOMUserDataHandler.hpp>
79#include "DOMNodeImpl.hpp"
80#include "DOMParentNode.hpp"
81#include "DOMDeepNodeListPool.hpp"
82
83XERCES_CPP_NAMESPACE_BEGIN
84
85
86class DOMAttrImpl;
87class DOMCDATASectionImpl;
88class DOMCommentImpl;
89class DOMConfiguration;
90class DOMDeepNodeListImpl;
91class DOMDocumentFragmentImpl;
92class DOMDocumentTypeImpl;
93class DOMElementImpl;
94class DOMEntityImpl;
95class DOMEntityReferenceImpl;
96class DOMNotationImpl;
97class DOMProcessingInstructionImpl;
98class DOMTextImpl;
99class DOMNodeIteratorImpl;
100class DOMNormalizer;
101class DOMTreeWalkerImpl;
102class DOMNodeFilter;
103class DOMNodeFilterImpl;
104class DOMImplementation;
105class DOMNodeIDMap;
106class DOMRangeImpl;
107class DOMStringPool;
108class DOMBuffer;
109class MemoryManager;
110class XPathNSResolver;
111class XPathExpression;
112
113typedef RefVectorOf<DOMRangeImpl>        Ranges;
114typedef RefVectorOf<DOMNodeIteratorImpl>     NodeIterators;
115typedef KeyRefPair<void, DOMUserDataHandler> DOMUserDataRecord;
116typedef RefHashTableOf<DOMUserDataRecord> DOMNodeUserDataTable;
117typedef RefStackOf<DOMNode>               DOMNodePtr;
118
119class CDOM_EXPORT DOMDocumentImpl: public XMemory, public DOMDocument {
120public:
121    // -----------------------------------------------------------------------
122    //  data types
123    // -----------------------------------------------------------------------
124    enum NodeObjectType {
125        ATTR_OBJECT                   = 0,
126        ATTR_NS_OBJECT                = 1,
127        CDATA_SECTION_OBJECT          = 2,
128        COMMENT_OBJECT                = 3,
129        DOCUMENT_FRAGMENT_OBJECT      = 4,
130        DOCUMENT_TYPE_OBJECT          = 5,
131        ELEMENT_OBJECT                = 6,
132        ELEMENT_NS_OBJECT             = 7,
133        ENTITY_OBJECT                 = 8,
134        ENTITY_REFERENCE_OBJECT       = 9,
135        NOTATION_OBJECT               = 10,
136        PROCESSING_INSTRUCTION_OBJECT = 11,
137        TEXT_OBJECT                   = 12
138    };
139
140
141    // -----------------------------------------------------------------------
142    //  data
143    // -----------------------------------------------------------------------
144    DOMNodeImpl           fNode;           // Implements common node functionality.
145    DOMParentNode         fParent;         // Implements common parent node functionality
146    DOMNodeIDMap*         fNodeIDMap;     // for use by GetElementsById().
147
148public:
149    DOMDocumentImpl(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
150    DOMDocumentImpl(const XMLCh*     namespaceURI,     //DOM Level 2
151                    const XMLCh*     qualifiedName,
152                    DOMDocumentType* doctype,
153                    MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
154    virtual ~DOMDocumentImpl();
155
156    void                         setDocumentType(DOMDocumentType *doctype);
157
158    // Add all functions that are pure virutal in DOMNODE
159    DOMNODE_FUNCTIONS;
160
161    // Add all functions that are pure virutal in DOMDocument
162    virtual DOMAttr*             createAttribute(const XMLCh *name);
163    virtual DOMCDATASection*     createCDATASection(const XMLCh *data);
164    virtual DOMComment*          createComment(const XMLCh *data);
165    virtual DOMDocumentFragment* createDocumentFragment();
166    virtual DOMDocumentType*     createDocumentType(const XMLCh *name);
167    virtual DOMDocumentType*     createDocumentType(const XMLCh *qName,
168                                                    const XMLCh *publicId,
169                                                    const XMLCh *systemId);
170    virtual DOMElement*          createElement(const XMLCh * tagName);
171    virtual DOMElement*          createElementNoCheck(const XMLCh *tagName);
172    virtual DOMEntity*           createEntity(const XMLCh * name);
173    virtual DOMEntityReference*  createEntityReference(const XMLCh * name);
174    virtual DOMNotation*         createNotation(const XMLCh * name);
175    virtual DOMProcessingInstruction* createProcessingInstruction(const XMLCh * target, const XMLCh * data);
176    virtual DOMText*             createTextNode(const XMLCh * data);
177    virtual DOMDocumentType*     getDoctype() const;
178    virtual DOMElement*          getDocumentElement() const;
179    virtual DOMNodeList*         getElementsByTagName(const XMLCh * tagname) const;
180    virtual DOMImplementation*   getImplementation() const;
181    bool                         isXMLName(const XMLCh * s);
182    virtual DOMNodeIterator*     createNodeIterator(DOMNode *root,
183                                                    unsigned long whatToShow,
184                                                    DOMNodeFilter* filter,
185                                                    bool entityReferenceExpansion);
186    virtual DOMTreeWalker*       createTreeWalker(DOMNode *root,
187                                                  unsigned long whatToShow,
188                                                  DOMNodeFilter* filter,
189                                                  bool entityReferenceExpansion);
190
191
192    virtual DOMRange*            createRange();
193    virtual Ranges*              getRanges() const;  //non-standard api
194    virtual NodeIterators*       getNodeIterators() const;  //non-standard api
195    virtual void                 removeRange(DOMRangeImpl* range); //non-standard api
196    virtual void                 removeNodeIterator(DOMNodeIteratorImpl* nodeIterator); //non-standard api
197
198    virtual const DOMXPathExpression*    createExpression(const XMLCh *expression, const DOMXPathNSResolver *resolver);
199    virtual const DOMXPathNSResolver*    createNSResolver(DOMNode *nodeResolver);
200    virtual void* evaluate(const XMLCh *expression, DOMNode *contextNode, const DOMXPathNSResolver *resolver,
201                           unsigned short type, void* result);
202
203
204    // Extension to be called by the Parser
205    DOMEntityReference*  createEntityReferenceByParser(const XMLCh * name);
206
207
208    //
209    // Functions to keep track of document mutations, so that node list chached
210    //   information can be invalidated.  One global changes counter per document.
211    //
212    virtual void                 changed();
213    virtual int                  changes() const;
214
215    /**
216     * Sets whether the DOM implementation performs error checking
217     * upon operations. Turning off error checking only affects
218     * the following DOM checks:
219     * <ul>
220     * <li>Checking strings to make sure that all characters are
221     *     legal XML characters
222     * <li>Hierarchy checking such as allowed children, checks for
223     *     cycles, etc.
224     * </ul>
225     * <p>
226     * Turning off error checking does <em>not</em> turn off the
227     * following checks:
228     * <ul>
229     * <li>Read only checks
230     * <li>Checks related to DOM events
231     * </ul>
232     */
233    inline void setErrorChecking(bool check) {
234        errorChecking = check;
235    }
236
237    /**
238     * Returns true if the DOM implementation performs error checking.
239     */
240    inline bool getErrorChecking() const {
241        return errorChecking;
242    }
243
244    //Introduced in DOM Level 2
245    virtual DOMNode*             importNode(DOMNode *source, bool deep);
246    virtual DOMElement*          createElementNS(const XMLCh *namespaceURI,
247                                                 const XMLCh *qualifiedName);
248    virtual DOMElement*          createElementNS(const XMLCh *namespaceURI,
249                                                 const XMLCh *qualifiedName,
250                                                 const XMLSSize_t lineNo,
251                                                 const XMLSSize_t columnNo);
252    virtual DOMAttr*             createAttributeNS(const XMLCh *namespaceURI,
253                                                   const XMLCh *qualifiedName);
254    virtual DOMNodeList*         getElementsByTagNameNS(const XMLCh *namespaceURI,
255                                                        const XMLCh *localName) const;
256    virtual DOMElement*          getElementById(const XMLCh *elementId) const;
257
258    //Introduced in DOM Level 3
259    virtual const XMLCh*         getActualEncoding() const;
260    virtual void                 setActualEncoding(const XMLCh* actualEncoding);
261    virtual const XMLCh*         getEncoding() const;
262    virtual void                 setEncoding(const XMLCh* encoding);
263    virtual bool                 getStandalone() const;
264    virtual void                 setStandalone(bool standalone);
265    virtual const XMLCh*         getVersion() const;
266    virtual void                 setVersion(const XMLCh* version);
267    virtual const XMLCh*         getDocumentURI() const;
268    virtual void                 setDocumentURI(const XMLCh* documentURI);
269    virtual bool                 getStrictErrorChecking() const;
270    virtual void                 setStrictErrorChecking(bool strictErrorChecking);
271    virtual DOMNode*             adoptNode(DOMNode* source);
272    virtual void                 normalizeDocument();
273    virtual DOMConfiguration*    getDOMConfiguration() const;
274    virtual void                 setDOMConfiguration(DOMConfiguration *config);
275
276    // helper functions to prevent storing userdata pointers on every node.
277    void*                        setUserData(DOMNodeImpl* n,
278                                            const XMLCh* key,
279                                            void* data,
280                                            DOMUserDataHandler* handler);
281    void*                        getUserData(const DOMNodeImpl* n,
282                                             const XMLCh* key) const;
283    void                         callUserDataHandlers(const DOMNodeImpl* n,
284                                                      DOMUserDataHandler::DOMOperationType operation,
285                                                      const DOMNode* src,
286                                                      const DOMNode* dst) const;
287    void                         transferUserData(DOMNodeImpl* n1, DOMNodeImpl* n2);
288
289    DOMNode*                     renameNode(DOMNode* n,
290                                            const XMLCh* namespaceURI,
291                                            const XMLCh* name);
292
293    //Return the index > 0 of ':' in the given qualified name qName="prefix:localName".
294    //Return 0 if there is no ':', or -1 if qName is malformed such as ":abcd".
295    static  int                  indexofQualifiedName(const XMLCh * qName);
296    static  bool                 isKidOK(DOMNode *parent, DOMNode *child);
297
298    inline DOMNodeIDMap*         getNodeIDMap() {return fNodeIDMap;};
299
300
301    //
302    // Memory Management Functions.  All memory is allocated by and owned by
303    //                               a document, and is not recovered until the
304    //                               document itself is deleted.
305    //
306    void*                        allocate(size_t amount);
307    void*                        allocate(size_t amount, NodeObjectType type);
308    XMLCh*                       cloneString(const XMLCh *src);
309    const XMLCh*                 getPooledString(const XMLCh *src);
310    void                         deleteHeap();
311    void                         release(DOMNode* object, NodeObjectType type);
312    void                         releaseDocNotifyUserData(DOMNode* object);
313    void                         releaseBuffer(DOMBuffer* buffer);
314    DOMBuffer*                   popBuffer();
315    MemoryManager*               getMemoryManager() const;
316
317    // Factory methods for getting/creating node lists.
318    // Because nothing is ever deleted, the implementation caches and recycles
319    //  previously used instances of DOMDeepNodeList
320    //
321    DOMNodeList*                 getDeepNodeList(const DOMNode *rootNode, const XMLCh *tagName);
322    DOMNodeList*                 getDeepNodeList(const DOMNode *rootNode,    //DOM Level 2
323                                                 const XMLCh *namespaceURI,
324                                                 const XMLCh *localName);
325
326private:
327    //Internal helper functions
328    virtual DOMNode*             importNode(DOMNode *source, bool deep, bool cloningNode);
329
330    // -----------------------------------------------------------------------
331    // Unimplemented constructors and operators
332    // -----------------------------------------------------------------------
333    DOMDocumentImpl(const DOMDocumentImpl &);
334    DOMDocumentImpl & operator = (const DOMDocumentImpl &);
335
336private:
337    // -----------------------------------------------------------------------
338    //  data
339    // -----------------------------------------------------------------------
340    // New data introduced in DOM Level 3
341    const XMLCh*          fActualEncoding;
342    const XMLCh*          fEncoding;
343    bool                  fStandalone;
344    const XMLCh*          fVersion;
345    const XMLCh*          fDocumentURI;
346    DOMConfiguration*     fDOMConfiguration;
347   
348    RefHashTableOf<DOMNodeUserDataTable>* fUserDataTable;
349
350
351    // Per-Document heap Variables.
352    //   The heap consists of one or more biggish blocks which are
353    //   sub-allocated for individual allocations of nodes, strings, etc.
354    //   The big blocks form a linked list, allowing them to be located for deletion.
355    //
356    //   There is no provision for deleting suballocated blocks, other than
357    //     deleting the entire heap when the document is deleted.
358    //
359    //   There is no header on individual sub-allocated blocks.
360    //   The header on big blocks consists only of a single back pointer to
361    //    the previously allocated big block (our linked list of big blocks)
362    //
363    //
364    //   revisit - this heap should be encapsulated into its own
365    //                  class, rather than hanging naked on Document.
366    //
367    void*                 fCurrentBlock;
368    char*                 fFreePtr;
369    XMLSize_t             fFreeBytesRemaining;
370
371    // To recycle the DOMNode pointer
372    RefArrayOf<DOMNodePtr>* fRecycleNodePtr;
373
374    // To recycle DOMBuffer pointer
375    RefStackOf<DOMBuffer>* fRecycleBufferPtr;
376
377    // Pool of DOMNodeList for getElementsByTagName
378    DOMDeepNodeListPool<DOMDeepNodeListImpl>* fNodeListPool;
379
380    // Other data
381    DOMDocumentType*      fDocType;
382    DOMElement*           fDocElement;
383    DOMStringPool*        fNamePool;
384    DOMNormalizer*        fNormalizer;
385    Ranges*               fRanges;
386    NodeIterators*        fNodeIterators;
387    MemoryManager*        fMemoryManager;   // configurable memory manager
388
389    int                   fChanges;
390    bool                  errorChecking;    // Bypass error checking.
391
392};
393
394inline MemoryManager* DOMDocumentImpl::getMemoryManager() const
395{
396    return fMemoryManager;
397}
398
399XERCES_CPP_NAMESPACE_END
400
401// ---------------------------------------------------------------------------
402//
403//  Operator new.  Global overloaded version, lets any object be allocated on
404//                 the heap owned by a document.
405//
406// ---------------------------------------------------------------------------
407inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocumentImpl::NodeObjectType type)
408{
409    // revist.  Probably should be a checked cast.
410    void *p = ((XERCES_CPP_NAMESPACE_QUALIFIER DOMDocumentImpl *)doc)->allocate(amt, type);
411    return p;
412}
413
414inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc)
415{
416    // revist.  Probably should be a checked cast.
417    void *p = ((XERCES_CPP_NAMESPACE_QUALIFIER DOMDocumentImpl *)doc)->allocate(amt);
418    return p;
419}
420
421// ---------------------------------------------------------------------------
422//  For DOM:
423//  Bypass compiler warning:
424//    no matching operator delete found; memory will not be freed if initialization throws an exception
425// ---------------------------------------------------------------------------
426#if _MSC_VER >= 1200 /* VC++ 6.0 */
427inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc)
428{
429    return;
430}
431inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocumentImpl::NodeObjectType type)
432{
433    return;
434}
435#endif
436
437#endif
Note: See TracBrowser for help on using the repository browser.