dommemtest.cpp

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

CPP
1,486
字号
        EXCEPTION_TEST(attr->setPrefix(X("xml")), DOMException::NAMESPACE_ERR);        //Special case for xmlns:a where namespaceURI must be xmlURI        attr = doc->createAttributeNS(X("http://www.w3.org/2000/xmlns/"), X("foo:a"));        attr->setPrefix(X("xmlns"));        attr = doc->createAttributeNS(X("http://nsa"), X("foo:a"));        EXCEPTION_TEST(attr->setPrefix(X("xmlns")), DOMException::NAMESPACE_ERR);        //Special case for xmlns where no prefix can be set        attr = doc->createAttributeNS(X("http://www.w3.org/2000/xmlns/"), X("xmlns"));        EXCEPTION_TEST(attr->setPrefix(X("foo")), DOMException::NAMESPACE_ERR);        EXCEPTION_TEST(attr->setPrefix(X("xmlns")), DOMException::NAMESPACE_ERR);        //Also an attribute can not have a prefix with namespaceURI == null or ""        attr = doc->createAttributeNS(XMLUni::fgZeroLenString, X("a"));        EXCEPTION_TEST(attr->setPrefix(X("foo")), DOMException::NAMESPACE_ERR);        attr = doc->createAttributeNS(0, X("a"));        EXCEPTION_TEST(attr->setPrefix(X("foo")), DOMException::NAMESPACE_ERR);        //Only prefix of Element and Attribute can be changed        EXCEPTION_TEST(doc->setPrefix(X("foo")), DOMException::NAMESPACE_ERR);        //Prefix of readonly Attribute can not be changed.        //However, there is no way to create such DOMAttribute for testing yet.        // release the document, the documentType (dt) still has the owner, and thus no need to release        doc->release();    }    //    //  getElementsByTagName*    //    {        // Set up an initial (root element only) document.        //        DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));        DOMDocumentType* dt = impl->createDocumentType(X("foo:docName"), X("pubId"), X("http://sysId"));        DOMDocument* doc = impl->createDocument(X("http://document.namespace"), X("foo:docName"), dt);        DOMElement* rootEl = doc->getDocumentElement();        //        // Populate the document        //        DOMElement* ela = doc->createElementNS(X("http://nsa"), X("a:ela"));        rootEl->appendChild(ela);        DOMElement* elb = doc->createElementNS(X("http://nsb"), X("elb"));        rootEl->appendChild(elb);        DOMElement* elc = doc->createElementNS(XMLUni::fgZeroLenString, X("elc"));        rootEl->appendChild(elc);        DOMElement* eld = doc->createElementNS(X("http://nsa"), X("d:ela"));        rootEl->appendChild(eld);        DOMElement* ele = doc->createElementNS(X("http://nse"), X("elb"));        rootEl->appendChild(ele);        //        // Access with DOM Level 1 getElementsByTagName        //        DOMNodeList* nl;        nl = doc->getElementsByTagName(X("a:ela"));        TASSERT(nl->getLength() == 1);        TASSERT(nl->item(0) == ela);        nl = doc->getElementsByTagName(X("elb"));        TASSERT(nl->getLength() == 2);        TASSERT(nl->item(0) == elb);        TASSERT(nl->item(1) == ele);        nl = doc->getElementsByTagName(X("d:ela"));        TASSERT(nl->getLength() == 1);        TASSERT(nl->item(0) == eld);        //        //  Access with DOM Level 2 getElementsByTagNameNS        //        nl = doc->getElementsByTagNameNS(XMLUni::fgZeroLenString, X("elc"));        TASSERT(nl->getLength() == 1);        TASSERT(nl->item(0) == elc);        nl = doc->getElementsByTagNameNS(0, X("elc"));        TASSERT(nl->getLength() == 1);        TASSERT(nl->item(0) == elc);        nl = doc->getElementsByTagNameNS(X("http://nsa"), X("ela"));        TASSERT(nl->getLength() == 2);        TASSERT(nl->item(0) == ela);        TASSERT(nl->item(1) == eld);        nl = doc->getElementsByTagNameNS(XMLUni::fgZeroLenString, X("elb"));        TASSERT(nl->getLength() == 0);        nl = doc->getElementsByTagNameNS(X("http://nsb"), X("elb"));        TASSERT(nl->getLength() == 1);        TASSERT(nl->item(0) == elb);        nl = doc->getElementsByTagNameNS(X("*"), X("elb"));        TASSERT(nl->getLength() == 2);        TASSERT(nl->item(0) == elb);        TASSERT(nl->item(1) == ele);        nl = doc->getElementsByTagNameNS(X("http://nsa"), X("*"));        TASSERT(nl->getLength() == 2);        TASSERT(nl->item(0) == ela);        TASSERT(nl->item(1) == eld);        nl = doc->getElementsByTagNameNS(X("*"), X("*"));        TASSERT(nl->getLength() == 6);     // Gets the document root element, plus 5 more        TASSERT(nl->item(6) == 0);        // TASSERT(nl->item(-1) == 0);        nl = rootEl->getElementsByTagNameNS(X("*"), X("*"));        TASSERT(nl->getLength() == 5);        nl = doc->getElementsByTagNameNS(X("http://nsa"), X("d:ela"));        TASSERT(nl->getLength() == 0);        //        // Node lists are Live        //        nl = doc->getElementsByTagNameNS(X("*"), X("*"));        DOMNodeList* nla = ela->getElementsByTagNameNS(X("*"), X("*"));        TASSERT(nl->getLength() == 6);        TASSERT(nla->getLength() == 0);        DOMNode* rem = rootEl->removeChild(elc);        rem->release();        TASSERT(nl->getLength() == 5);        TASSERT(nla->getLength() == 0);        ela->appendChild(elc);        TASSERT(nl->getLength() == 6);        TASSERT(nla->getLength() == 1);        // release the document, the documentType (dt) still has the owner, and thus no need to release        doc->release();    }   //    // Attributes and NamedNodeMaps.    //    {        // Set up an initial (root element only) document.        //        DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));        DOMDocumentType* dt = impl->createDocumentType(X("foo:docName"), X("pubId"), X("http://sysId"));        DOMDocument* doc = impl->createDocument(X("http://document.namespace"), X("foo:docName"), dt);        DOMElement* rootEl = doc->getDocumentElement();        //        // Create a set of attributes and hang them on the root element.        //        DOMAttr* attra = doc->createAttributeNS(X("http://nsa"), X("a:attra"));        DOMNode* rem = rootEl->setAttributeNodeNS(attra);        if (rem)            rem->release();        //        // Check that the attribute nodes were created with the correct properties.        //        TASSERT(!XMLString::compareString(attra->getNodeName(), X("a:attra")));        TASSERT(!XMLString::compareString(attra->getNamespaceURI(), X("http://nsa")));        TASSERT(!XMLString::compareString(attra->getLocalName(), X("attra")));        TASSERT(!XMLString::compareString(attra->getName(), X("a:attra")));        TASSERT(attra->getNodeType() == DOMNode::ATTRIBUTE_NODE);        TASSERT(!XMLString::compareString(attra->getNodeValue(), XMLUni::fgZeroLenString));        TASSERT(!XMLString::compareString(attra->getPrefix(), X("a")));        TASSERT(attra->getSpecified() == true);        TASSERT(!XMLString::compareString(attra->getValue(), XMLUni::fgZeroLenString));        //        // Create a set of attributes and hang them on the root element.        //        DOMAttr* attrb = doc->createAttributeNS(X("http://nsb"), X("attrb"));        rem = rootEl->setAttributeNodeNS(attrb);        if (rem)            rem->release();        DOMAttr* attrc = doc->createAttributeNS(XMLUni::fgZeroLenString, X("attrc"));        rem = rootEl->setAttributeNodeNS(attrc);        if (rem)            rem->release();        // this one will replace the attra        DOMAttr* attrd = doc->createAttributeNS(X("http://nsa"), X("d:attra"));        rem = rootEl->setAttributeNodeNS(attrd);        TASSERT(attra->getOwnerElement() == 0);        if (rem)            rem->release();        DOMAttr* attre = doc->createAttributeNS(X("http://nse"), X("attrb"));        rem = rootEl->setAttributeNodeNS(attre);        if (rem)            rem->release();        // Test methods of NamedNodeMap        DOMNamedNodeMap* nnm = rootEl->getAttributes();        TASSERT(nnm->getLength() == 4);        TASSERT(nnm->getNamedItemNS(X("http://nsa"), X("attra")) == attrd);        TASSERT(nnm->getNamedItemNS(X("http://nsb"), X("attrb")) == attrb);        TASSERT(nnm->getNamedItemNS(XMLUni::fgZeroLenString, X("attra")) == 0);        TASSERT(nnm->getNamedItemNS(X("http://nsa"), X("attrb")) == 0);        TASSERT(nnm->getNamedItemNS(X("http://nse"), X("attrb")) == attre);        TASSERT(nnm->getNamedItemNS(XMLUni::fgZeroLenString, X("attrc")) == attrc);        // Test hasAttributes, hasAttribute, hasAttributeNS        TASSERT(doc->hasAttributes() ==  false);        TASSERT(attrc->hasAttributes() == false);        TASSERT(rootEl->hasAttributes() == true);        TASSERT(rootEl->hasAttribute(X("attrc")) == true);        TASSERT(rootEl->hasAttribute(X("wrong")) == false);        TASSERT(rootEl->hasAttributeNS(X("http://nsa"), X("attra")) == true);        TASSERT(rootEl->hasAttributeNS(X("http://nsa"), X("wrong")) == false);        // release the document, the documentType (dt) still has the owner, and thus no need to release        doc->release();    }    //    //    //}//---------------------------------------------------------------------------------------////   DOMReleaseTests    Test if the release() function////---------------------------------------------------------------------------------------void DOMReleaseTests(){    XMLCh tempStr[4000];    XMLCh tempStr2[4000];    XMLCh tempStr3[4000];    XMLString::transcode("status", tempStr, 3999);    XMLString::transcode("true", tempStr2, 3999);    XMLString::transcode("root", tempStr3, 3999);    //create document    DOMDocument*  cpXMLDocument;    cpXMLDocument = DOMImplementation::getImplementation()->createDocument();    //create root element    DOMElement*   cpRoot = cpXMLDocument->createElement(tempStr3);    //create status attribute    cpRoot->setAttribute(tempStr,tempStr2);    DOMAttr* pAttr = cpRoot->getAttributeNode(tempStr);    //simulate setting the attribute value    //   The setValue and setAttribute should call release internally so that    //   the overall memory usage is not increased    int i = 0;    for(i=0;i<20000;i++)    {        pAttr->setValue(tempStr2);    }    for(i=0;i<20000;i++)    {        //same problem        cpRoot->removeAttribute(tempStr);        cpRoot->setAttribute(tempStr,tempStr2);    }    //simulate changing node value    //   the overall memory usage is not increased    char tempchar[4000];    for(i=0;i<20000;i++)    {        sprintf(tempchar, "time is %i\n",XMLPlatformUtils::getCurrentMillis());        int len = strlen(tempchar);        for (int j = len; j < 4000-len; j++)            tempchar[j] = 'a';        pAttr->setNodeValue(X(tempchar));    }    DOMText*  text = cpXMLDocument->createTextNode(tempStr3);    for(i=0;i<20000;i++)    {        sprintf(tempchar, "time is %i\n",XMLPlatformUtils::getCurrentMillis());        int len = strlen(tempchar);        for (int j = len; j < 4000-len; j++)            tempchar[j] = 'a';        text->setNodeValue(X(tempchar));    }    cpXMLDocument->release();}//---------------------------------------------------------------------------------------////   main////---------------------------------------------------------------------------------------int  mymain(){    try {        XMLPlatformUtils::Initialize();    }    catch (const XMLException& toCatch) {        char *pMessage = XMLString::transcode(toCatch.getMessage());        fprintf(stderr, "Error during XMLPlatformUtils::Initialize(). \n"            "  Message is: %s\n", pMessage);        XMLString::release(&pMessage);        return -1;    }    DOMBasicTests();    DOMNSTests();    DOMReleaseTests();    //    //  Print Final allocation stats for full set of tests    //    XMLPlatformUtils::Terminate();    return 0;};int  main() {   for (int i = 0; i<3; i++)        mymain();    if (errorOccurred) {        printf("Test Failed\n");        return 4;    }    printf("Test Run Successfully\n");    return 0;}

⌨️ 快捷键说明

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