domnormalizer.cpp

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

CPP
546
字号
            addOrChangeNamespaceDecl(prefix, uri, ele);            fNSScope->addOrChangeBinding(prefix, uri, fMemoryManager);        }    }    else {        if(ele->getLocalName() == 0) {            error(XMLErrs::DOMLevel1Node, ele);        }        else if(!fNSScope->isValidBinding(XMLUni::fgZeroLenString, XMLUni::fgZeroLenString)) {            addOrChangeNamespaceDecl(XMLUni::fgZeroLenString, XMLUni::fgZeroLenString, ele);            fNSScope->addOrChangeBinding(XMLUni::fgZeroLenString, XMLUni::fgZeroLenString, fMemoryManager);        }    }    //fix up non ns attrs    len = attrMap->getLength();    // hp aCC complains this i is a redefinition of the i on line 283    for(int j = 0; j < len; j++) {        DOMAttr *at = (DOMAttr*)attrMap->item(j);        const XMLCh *uri = at->getNamespaceURI();                const XMLCh* prefix = at->getPrefix();        if(!XMLString::equals(XMLUni::fgXMLNSURIName, uri)) {            if(uri != 0) {                if(prefix == 0 || !fNSScope->isValidBinding(prefix, uri)) {                                        const XMLCh* newPrefix =  fNSScope->getPrefix(uri);                    if(newPrefix != 0) {                        at->setPrefix(newPrefix);                                                                    }                    else {                        if(prefix != 0 && !fNSScope->getUri(prefix)) {                            fNSScope->addOrChangeBinding(prefix, uri, fMemoryManager);                            addOrChangeNamespaceDecl(prefix, uri, ele);                        }                        else {                            newPrefix = addCustomNamespaceDecl(uri, ele);                            fNSScope->addOrChangeBinding(newPrefix, uri, fMemoryManager);                            at->setPrefix(newPrefix);                        }                    }                }            }            else if(at->getLocalName() == 0) {                error(XMLErrs::DOMLevel1Node, at);            }        }    }}const XMLCh * DOMNormalizer::integerToXMLCh(unsigned int i) const {    XMLCh *buf = (XMLCh*) fMemoryManager->allocate(15 * sizeof(XMLCh));//new XMLCh[15];	XMLCh *pos = buf + sizeof(buf) - sizeof(XMLCh);	*pos = chNull;	do {        switch(i % 10) {        case 0 : *--pos = chDigit_0;break;        case 1 : *--pos = chDigit_1;break;        case 2 : *--pos = chDigit_2;break;        case 3 : *--pos = chDigit_3;break;        case 4 : *--pos = chDigit_4;break;        case 5 : *--pos = chDigit_5;break;        case 6 : *--pos = chDigit_6;break;        case 7 : *--pos = chDigit_7;break;        case 8 : *--pos = chDigit_8;break;        case 9 : *--pos = chDigit_9;break;        default:;        }		i /= 10;	} while (i);    const XMLCh *copy = fDocument->getPooledString(pos);    fMemoryManager->deallocate(buf);//delete[] buf;	return copy;}void DOMNormalizer::addOrChangeNamespaceDecl(const XMLCh* prefix, const XMLCh* uri, DOMElementImpl* element) const {    if (XMLString::equals(prefix, XMLUni::fgZeroLenString)) {        element->setAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString, uri);    } else {        XMLBuffer buf(1023, fMemoryManager);        buf.set(XMLUni::fgXMLNSString);        buf.append(chColon);        buf.append(prefix);        element->setAttributeNS(XMLUni::fgXMLNSURIName, buf.getRawBuffer(), uri);     }}const XMLCh* DOMNormalizer::addCustomNamespaceDecl(const XMLCh* uri, DOMElementImpl *element) const {    XMLBuffer preBuf(1023, fMemoryManager);    preBuf.append(chLatin_N);    preBuf.append(chLatin_S);    preBuf.append(integerToXMLCh(fNewNamespaceCount));    ((DOMNormalizer *)this)->fNewNamespaceCount++;    while(fNSScope->getUri(preBuf.getRawBuffer())) {        preBuf.reset();        preBuf.append(chLatin_N);        preBuf.append(chLatin_S);        preBuf.append(integerToXMLCh(fNewNamespaceCount));        ((DOMNormalizer *)this)->fNewNamespaceCount++;    }        XMLBuffer buf(1023, fMemoryManager);    buf.set(XMLUni::fgXMLNSString);    buf.append(chColon);    buf.append(preBuf.getRawBuffer());    element->setAttributeNS(XMLUni::fgXMLNSURIName, buf.getRawBuffer(), uri);    return element->getAttributeNodeNS(XMLUni::fgXMLNSURIName, preBuf.getRawBuffer())->getLocalName();}int DOMNormalizer::InScopeNamespaces::size() {    return fScopes->size();}DOMNormalizer::InScopeNamespaces::InScopeNamespaces(MemoryManager* const manager): lastScopeWithBindings(0){    fScopes = new (manager) RefVectorOf<Scope>(10, true, manager);}DOMNormalizer::InScopeNamespaces::~InScopeNamespaces() {    delete fScopes;}void DOMNormalizer::InScopeNamespaces::addOrChangeBinding(const XMLCh *prefix, const XMLCh *uri,                                                          MemoryManager* const manager) {    unsigned int s = fScopes->size();    if(!s)        addScope(manager);        Scope *curScope = fScopes->elementAt(s - 1);    curScope->addOrChangeBinding(prefix, uri, manager);    lastScopeWithBindings = curScope;}void DOMNormalizer::InScopeNamespaces::addScope(MemoryManager* const manager) {    Scope *s = new (manager) Scope(lastScopeWithBindings);    fScopes->addElement(s);}void DOMNormalizer::InScopeNamespaces::removeScope() {    lastScopeWithBindings = fScopes->elementAt(fScopes->size() - 1)->fBaseScopeWithBindings;    Scope *s = fScopes->orphanElementAt(fScopes->size() - 1);    delete s;}bool DOMNormalizer::InScopeNamespaces::isValidBinding(const XMLCh* prefix, const XMLCh* uri) const {    const XMLCh* actual = fScopes->elementAt(fScopes->size() - 1)->getUri(prefix);    if(actual == 0 || !XMLString::equals(actual, uri))        return false;    return true;}const XMLCh* DOMNormalizer::InScopeNamespaces::getPrefix(const XMLCh* uri) const {    return fScopes->elementAt(fScopes->size() - 1)->getPrefix(uri);}const XMLCh* DOMNormalizer::InScopeNamespaces::getUri(const XMLCh* prefix) const {    return fScopes->elementAt(fScopes->size() - 1)->getUri(prefix);}DOMNormalizer::InScopeNamespaces::Scope::Scope(Scope *baseScopeWithBindings) : fBaseScopeWithBindings(baseScopeWithBindings), fPrefixHash(0), fUriHash(0){}DOMNormalizer::InScopeNamespaces::Scope::~Scope() {    delete fPrefixHash;    delete fUriHash;}void DOMNormalizer::InScopeNamespaces::Scope::addOrChangeBinding(const XMLCh *prefix, const XMLCh *uri,                                                                 MemoryManager* const manager) {    //initialize and copy forward now we need to    if(!fUriHash) {        fPrefixHash = new (manager) RefHashTableOf<XMLCh>(10, (bool) false, manager);        fUriHash = new (manager) RefHashTableOf<XMLCh>(10, (bool) false, manager);                if(fBaseScopeWithBindings) {            RefHashTableOfEnumerator<XMLCh> preEnumer(fBaseScopeWithBindings->fPrefixHash, false, manager);            while(preEnumer.hasMoreElements()) {                const XMLCh* prefix = (XMLCh*) preEnumer.nextElementKey();                const XMLCh* uri  = fBaseScopeWithBindings->fPrefixHash->get((void*)prefix);                                //have to cast here because otherwise we have delete problems under windows :(                fPrefixHash->put((void *)prefix, (XMLCh*)uri);            }                        RefHashTableOfEnumerator<XMLCh> uriEnumer(fBaseScopeWithBindings->fUriHash, false, manager);            while(uriEnumer.hasMoreElements()) {                const XMLCh* uri = (XMLCh*) uriEnumer.nextElementKey();                const XMLCh* prefix  = fBaseScopeWithBindings->fUriHash->get((void*)uri);                //have to cast here because otherwise we have delete problems under windows :(                fUriHash->put((void *)uri, (XMLCh*)prefix);            }        }    }    const XMLCh *oldUri = fPrefixHash->get(prefix);    if(oldUri) {        fUriHash->removeKey(oldUri);    }    fPrefixHash->put((void *)prefix, (XMLCh*)uri);    fUriHash->put((void *)uri, (XMLCh*)prefix);}const XMLCh* DOMNormalizer::InScopeNamespaces::Scope::getUri(const XMLCh *prefix) const {    const XMLCh* uri = 0;    if(fPrefixHash) {        uri = fPrefixHash->get(prefix);    }    else if(fBaseScopeWithBindings) {        uri = fBaseScopeWithBindings->getUri(prefix);    }        return uri ? uri : 0;}const XMLCh* DOMNormalizer::InScopeNamespaces::Scope::getPrefix(const XMLCh* uri) const {    const XMLCh* prefix = 0;    if(fUriHash) {        prefix = fUriHash->get(uri);    }    else if(fBaseScopeWithBindings) {        prefix = fBaseScopeWithBindings->getPrefix(uri);    }    return prefix ? prefix : 0;}void DOMNormalizer::error(const XMLErrs::Codes code, const DOMNode *node) const{    if (fErrorHandler) {        //  Load the message into alocal and replace any tokens found in        //  the text.        const unsigned int maxChars = 2047;        XMLCh errText[maxChars + 1];        if (!gNormalizerMsgLoader().loadMsg(code, errText, maxChars))        {                // <TBD> Should probably load a default message here        }        DOMErrorImpl domError(code, 0, errText, (void*)node);        if (!fErrorHandler->handleError(domError))            throw (XMLErrs::Codes) code;    }}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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