dommemtest.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,486 行 · 第 1/4 页

CPP
1,486
字号
/* * Copyright 2002,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */////  Various DOM tests.//     This is NOT a complete test of DOM functionality.///* * $Id: DOMMemTest.cpp,v 1.43 2004/09/08 13:57:02 peiyongz Exp $ */#include <stdio.h>#include <string.h>#include <xercesc/dom/DOM.hpp>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/XMLException.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/XMLUniDefs.hpp>XERCES_CPP_NAMESPACE_USEbool errorOccurred = false;#define TASSERT(c) tassert((c), __FILE__, __LINE__)void tassert(bool c, const char *file, int line){    if (!c) {        printf("Failure.  Line %d,   file %s\n", line, file);        errorOccurred = true;    }};#define EXCEPTION_TEST(operation, expected_exception)               \{                                                                   \    try {                                                           \    operation;                                                      \    printf(" Error: no exception thrown at line %d\n", __LINE__);   \    errorOccurred = true;                                           \    }                                                               \    catch (DOMException &e) {                                       \    if (e.code != expected_exception) {                             \        printf(" Wrong exception code: %d at line %d\n", e.code, __LINE__); \        errorOccurred = true;                                       \    }                                                               \    }                                                               \    catch (...)   {                                                 \        printf(" Wrong exception thrown at line %d\n", __LINE__);   \        errorOccurred = true;                                       \    }                                                               \}// ---------------------------------------------------------------------------//  This is a simple class that lets us do easy (though not terribly efficient)//  trancoding of char* data to XMLCh data.// ---------------------------------------------------------------------------class XStr{public :    // -----------------------------------------------------------------------    //  Constructors and Destructor    // -----------------------------------------------------------------------    XStr(const char* const toTranscode)    {        // Call the private transcoding method        fUnicodeForm = XMLString::transcode(toTranscode);    }    ~XStr()    {        XMLString::release(&fUnicodeForm);    }    // -----------------------------------------------------------------------    //  Getter methods    // -----------------------------------------------------------------------    const XMLCh* unicodeForm() const    {        return fUnicodeForm;    }private :    // -----------------------------------------------------------------------    //  Private data members    //    //  fUnicodeForm    //      This is the Unicode XMLCh format of the string.    // -----------------------------------------------------------------------    XMLCh*   fUnicodeForm;};#define X(str) XStr(str).unicodeForm()//---------------------------------------------------------------------------------------////   DOMBasicTests    Basic DOM Level 1 tests////---------------------------------------------------------------------------------------void DOMBasicTests(){    //    //  Test Doc01      Create a new empty document    //    {        //  First precondition, so that lazily created strings do not appear        //  as memory leaks.        DOMDocument*   doc;        doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        doc->release();    }    //    //  Test Doc02      Create one of each kind of node using the    //                  document createXXX methods.    //                  Watch for memory leaks.    //    {        //  Do all operations in a preconditioning step, to force the        //  creation of implementation objects that are set up on first use.        //  Don't watch for leaks in this block (no  / )        DOMDocument* doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   el = doc->createElement(X("Doc02Element"));        DOMDocumentFragment* frag = doc->createDocumentFragment ();        DOMText*  text = doc->createTextNode(X("Doc02TextNode"));        DOMComment* comment = doc->createComment(X("Doc02Comment"));        DOMCDATASection*  cdataSec = doc->createCDATASection(X("Doc02CDataSection"));        DOMDocumentType*  docType = doc->createDocumentType(X("Doc02DocumentType"));        DOMNotation* notation = doc->createNotation(X("Doc02Notation"));        DOMProcessingInstruction* pi = doc->createProcessingInstruction(X("Doc02PITarget"), X("Doc02PIData"));        DOMNodeList*    nodeList = doc->getElementsByTagName(X("*"));        doc->release();    }    //    //  Doc03 - Create a small document tree    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   rootEl = doc->createElement(X("Doc03RootElement"));        doc->appendChild(rootEl);        DOMText*       textNode = doc->createTextNode(X("Doc03 text stuff"));        rootEl->appendChild(textNode);        DOMNodeList*    nodeList = doc->getElementsByTagName(X("*"));        doc->release();    };    //    //  Attr01    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   rootEl  = doc->createElement(X("RootElement"));        doc->appendChild(rootEl);        {            DOMAttr*        attr01  = doc->createAttribute(X("Attr01"));            DOMNode* rem = rootEl->setAttributeNode(attr01);            if (rem)                rem->release();        }        {            DOMAttr* attr02 = doc->createAttribute(X("Attr01"));            DOMNode* rem = rootEl->setAttributeNode(attr02);            if (rem)                rem->release();        }        doc->release();    };    //    //  Attr02    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   rootEl  = doc->createElement(X("RootElement"));        doc->appendChild(rootEl);        DOMAttr*        attr01  = doc->createAttribute(X("Attr02"));        DOMNode* rem = rootEl->setAttributeNode(attr01);        if (rem)            rem->release();        DOMAttr*        attr02 = doc->createAttribute(X("Attr02"));        rem = rootEl->setAttributeNode(attr02);        if (rem)            rem->release();        doc->release();    }    //    //  Attr03    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   rootEl  = doc->createElement(X("RootElement"));        doc->appendChild(rootEl);        DOMAttr*        attr01  = doc->createAttribute(X("Attr03"));        DOMNode* rem = rootEl->setAttributeNode(attr01);        if (rem)            rem->release();        attr01->setValue(X("Attr03Value1"));        attr01->setValue(X("Attr03Value2"));        doc->release();    }    //    //  Attr04    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   rootEl  = doc->createElement(X("RootElement"));        doc->appendChild(rootEl);        DOMAttr*        attr01  = doc->createAttribute(X("Attr04"));        DOMNode* rem = rootEl->setAttributeNode(attr01);        if (rem)            rem->release();        attr01->setValue(X("Attr04Value1"));        DOMNode* value = attr01->getFirstChild();        doc->release();    }    //    //  Text01    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   rootEl  = doc->createElement(X("RootElement"));        doc->appendChild(rootEl);        DOMText*        txt1 = doc->createTextNode(X("Hello Goodbye"));        rootEl->appendChild(txt1);        txt1->splitText(6);        rootEl->normalize();        doc->release();    }    //    //  Notation01    //    {        DOMDocument*       doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMDocumentType*   dt  = doc->createDocumentType(X("DocType_for_Notation01"));        doc->appendChild(dt);        DOMNamedNodeMap* notationMap = dt->getNotations();        DOMNotation*    nt1 = doc->createNotation(X("Notation01"));        DOMNode* rem = notationMap->setNamedItem (nt1);        if (rem)            rem->release();        DOMNode*  abc1 = notationMap->getNamedItem(X("Notation01"));        DOMNotation*    nt2 = (DOMNotation*) abc1;        TASSERT(nt1==nt2);        nt2 = 0;        nt1 = 0;        DOMNode* abc6 = notationMap->getNamedItem(X("Notation01"));        nt2 = (DOMNotation*) abc6;        doc->release();    }    //    //  NamedNodeMap01 - comparison operators.    //    {        DOMNamedNodeMap*    nnm = 0;        TASSERT(nnm == 0);        DOMDocument*       doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        nnm = doc->getAttributes();    // Should be null, because node type                                      //   is not Element.        TASSERT(nnm == 0);        TASSERT(!(nnm != 0));        DOMElement* el = doc->createElement(X("NamedNodeMap01"));        DOMNamedNodeMap* nnm2 = el->getAttributes();    // Should be an empty, but non-null map.        TASSERT(nnm2 != 0);        TASSERT(nnm != nnm2);        nnm = nnm2;        TASSERT(nnm == nnm2);        doc->release();    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?