abstractdomparser.cpp

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

CPP
1,615
字号
    if (fCurrentNode->getNodeType() == DOMNode::TEXT_NODE)    {        DOMText *node = (DOMText *)fCurrentNode;        node->appendData(chars);    }    else    {        DOMTextImpl *node = (DOMTextImpl *)fDocument->createTextNode(chars);        node->setIgnorableWhitespace(true);        fCurrentParent->appendChild(node);        fCurrentNode = node;    }    ncChars[length] = savedChar;}void AbstractDOMParser::resetDocument(){    //    //  The reset methods are called before a new parse event occurs.    //  Reset this parsers state to clear out anything that may be left    //  from a previous use, in particular the DOM document itself.    //    this->reset();}void AbstractDOMParser::startDocument(){    if(fImplementationFeatures == 0)        fDocument = (DOMDocumentImpl *)DOMImplementation::getImplementation()->createDocument(fMemoryManager);    else         fDocument = (DOMDocumentImpl *)DOMImplementationRegistry::getDOMImplementation(fImplementationFeatures)->createDocument(fMemoryManager);    // Just set the document as the current parent and current node    fCurrentParent = fDocument;    fCurrentNode   = fDocument;    // set DOM error checking off    fDocument->setErrorChecking(false);    fDocument->setDocumentURI(fScanner->getLocator()->getSystemId());    fDocument->setActualEncoding(fScanner->getReaderMgr()->getCurrentEncodingStr());}void AbstractDOMParser::endDocument(){    // set DOM error checking back on    fDocument->setErrorChecking(true);    // DOM L2 does not support editing DocumentType nodes    if (fDocumentType && fScanner -> getDoNamespaces())        fDocumentType->setReadOnly(true, true);}void AbstractDOMParser::startElement(const  XMLElementDecl&         elemDecl                             , const unsigned int            urlId                             , const XMLCh* const            elemPrefix                             , const RefVectorOf<XMLAttr>&   attrList                             , const unsigned int            attrCount                             , const bool                    isEmpty                             , const bool                    isRoot){    DOMElement     *elem;    DOMElementImpl *elemImpl;    const XMLCh* namespaceURI = 0;    //get the list for use in the loop    XMLAttDefList* defAttrs = 0;    if(elemDecl.hasAttDefs()) {        defAttrs = &elemDecl.getAttDefList();    }    if (fScanner -> getDoNamespaces()) {    //DOM Level 2, doNamespaces on        if (urlId != fScanner->getEmptyNamespaceId()) {  //TagName has a prefix            namespaceURI = fScanner->getURIText(urlId); //get namespaceURI            if (elemPrefix && *elemPrefix) {                XMLBufBid elemQName(&fBufMgr);                elemQName.set(elemPrefix);                elemQName.append(chColon);                elemQName.append(elemDecl.getBaseName());                elem = createElementNSNode(namespaceURI, elemQName.getRawBuffer());            }            else {                 elem = createElementNSNode(namespaceURI, elemDecl.getBaseName());            }        }         else {            elem = createElementNSNode(namespaceURI, elemDecl.getBaseName());        }        elemImpl = (DOMElementImpl *) elem;    }    else {    //DOM Level 1        elem = fDocument->createElement(elemDecl.getFullName());        elemImpl = (DOMElementImpl *) elem;    }    for (unsigned int index = 0; index < attrCount; ++index) {        const XMLAttr* oneAttrib = attrList.elementAt(index);        DOMAttrImpl *attr = 0;        DOMNode* remAttr = 0;        //  revisit.  Optimize to init the named node map to the        //            right size up front.        if (fScanner -> getDoNamespaces()) {    //DOM Level 2, doNamespaces on            unsigned int attrURIId = oneAttrib -> getURIId();            namespaceURI = 0;                        if (XMLString::equals(oneAttrib -> getName(), XMLUni::fgXMLNSString)) {   //for xmlns=...                attrURIId = fScanner->getXMLNSNamespaceId();                            }            if (attrURIId != fScanner->getEmptyNamespaceId()) {  //TagName has a prefix                namespaceURI = fScanner->getURIText(attrURIId);   //get namespaceURI            }            attr = (DOMAttrImpl *)fDocument->createAttributeNS(namespaceURI, oneAttrib->getQName());            remAttr = elemImpl->setAttributeNodeNS(attr);        }        else {            attr = (DOMAttrImpl *)fDocument->createAttribute(oneAttrib->getName());            remAttr = elemImpl->setAttributeNode(attr);        }        attr->setValue(oneAttrib -> getValue());        if (remAttr)            remAttr->release();        // Attributes of type ID.  If this is one, add it to the hashtable of IDs        //   that is constructed for use by GetElementByID().        //        if (oneAttrib->getType()==XMLAttDef::ID)        {            if (fDocument->fNodeIDMap == 0)                fDocument->fNodeIDMap = new (fDocument) DOMNodeIDMap(500, fDocument);            fDocument->fNodeIDMap->add(attr);            attr->fNode.isIdAttr(true);        }        attr->setSpecified(oneAttrib->getSpecified());        // store DTD validation information        if(fCreateSchemaInfo)        {            switch(oneAttrib->getType())            {            case XMLAttDef::CData:          attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedCDATAAttribute); break;            case XMLAttDef::ID:             attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedIDAttribute); break;            case XMLAttDef::IDRef:          attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedIDREFAttribute); break;            case XMLAttDef::IDRefs:         attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedIDREFSAttribute); break;            case XMLAttDef::Entity:         attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedENTITYAttribute); break;            case XMLAttDef::Entities:       attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedENTITIESAttribute); break;            case XMLAttDef::NmToken:        attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedNMTOKENAttribute); break;            case XMLAttDef::NmTokens:       attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedNMTOKENSAttribute); break;            case XMLAttDef::Notation:       attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedNOTATIONAttribute); break;            case XMLAttDef::Enumeration:    attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedENUMERATIONAttribute); break;            default:                        attr->setTypeInfo(&DOMTypeInfoImpl::g_DtdNotValidatedAttribute); break;            }        }    }    // set up the default attributes	if (defAttrs != 0)	{		        XMLAttDef* attr = 0;        DOMAttrImpl * insertAttr = 0;        for(unsigned int i=0; i<defAttrs->getAttDefCount(); i++)        {            attr = &defAttrs->getAttDef(i);            const XMLAttDef::DefAttTypes defType = attr->getDefaultType();            if ((defType == XMLAttDef::Default)            ||  (defType == XMLAttDef::Fixed))            {                if (fScanner->getDoNamespaces())                {                    // DOM Level 2 wants all namespace declaration attributes                    // to be bound to "http://www.w3.org/2000/xmlns/"                    // So as long as the XML parser doesn't do it, it needs to                    // done here.                    const XMLCh* qualifiedName = attr->getFullName();                    XMLBufBid bbPrefixQName(&fBufMgr);                    XMLBuffer& prefixBuf = bbPrefixQName.getBuffer();                    int colonPos = -1;                    unsigned int uriId = fScanner->resolveQName(qualifiedName, prefixBuf, ElemStack::Mode_Attribute, colonPos);                    const XMLCh* namespaceURI = 0;                    if (XMLString::equals(qualifiedName, XMLUni::fgXMLNSString))    //for xmlns=...                        uriId = fScanner->getXMLNSNamespaceId();                    if (uriId != fScanner->getEmptyNamespaceId()) {  //TagName has a prefix                        namespaceURI = fScanner->getURIText(uriId);                    }                    insertAttr = (DOMAttrImpl *) fDocument->createAttributeNS(namespaceURI,     // NameSpaceURI                                                                              qualifiedName);   // qualified name                    DOMAttr* remAttr = elemImpl->setDefaultAttributeNodeNS(insertAttr);                    if (remAttr)                        remAttr->release();                }                else                {                    // Namespaces is turned off...                    insertAttr = (DOMAttrImpl *) fDocument->createAttribute(attr->getFullName());                    DOMNode* remAttr = elemImpl->setDefaultAttributeNode(insertAttr);                    if (remAttr)                        remAttr->release();                }                //need to do this before the get as otherwise we overwrite any value in the attr                if (attr->getValue() != 0)                {                    insertAttr->setValue(attr->getValue());                    insertAttr->setSpecified(false);                }                // store DTD validation information                if(fCreateSchemaInfo)                {                    switch(attr->getType())                    {                    case XMLAttDef::CData:          insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedCDATAAttribute); break;                    case XMLAttDef::ID:             insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedIDAttribute); break;                    case XMLAttDef::IDRef:          insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedIDREFAttribute); break;                    case XMLAttDef::IDRefs:         insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedIDREFSAttribute); break;                    case XMLAttDef::Entity:         insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedENTITYAttribute); break;                    case XMLAttDef::Entities:       insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedENTITIESAttribute); break;                    case XMLAttDef::NmToken:        insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedNMTOKENAttribute); break;                    case XMLAttDef::NmTokens:       insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedNMTOKENSAttribute); break;                    case XMLAttDef::Notation:       insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedNOTATIONAttribute); break;                    case XMLAttDef::Enumeration:    insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdValidatedENUMERATIONAttribute); break;                    default:                        insertAttr->setTypeInfo(&DOMTypeInfoImpl::g_DtdNotValidatedAttribute); break;                    }                }            }            insertAttr = 0;            attr->reset();        }    }    fCurrentParent->appendChild(elem);    fNodeStack->push(fCurrentParent);    fCurrentParent = elem;    fCurrentNode = elem;    fWithinElement = true;    // If an empty element, do end right now (no endElement() will be called)    if (isEmpty)        endElement(elemDecl, urlId, isRoot, elemPrefix);}void AbstractDOMParser::startEntityReference(const XMLEntityDecl& entDecl){    const XMLCh * entName = entDecl.getName();    DOMNamedNodeMap *entities = fDocumentType->getEntities();    DOMEntityImpl* entity = (DOMEntityImpl*)entities->getNamedItem(entName);    if (entity)        entity->setActualEncoding(fScanner->getReaderMgr()->getCurrentEncodingStr());    fCurrentEntity = entity;    // Following line has been moved up so that erImpl is only declared    // and used if create entity ref flag is true    if (fCreateEntityReferenceNodes == true)    {        DOMEntityReference *er = fDocument->createEntityReferenceByParser(entName);        //set the readOnly flag to false before appending node, will be reset         // in endEntityReference        DOMEntityReferenceImpl *erImpl = (DOMEntityReferenceImpl *) er;        erImpl->setReadOnly(false, true);        fCurrentParent->appendChild(er);        fNodeStack->push(fCurrentParent);        fCurrentParent = er;        fCurrentNode = er;    // this entityRef needs to be stored in Entity map too.    // We'd decide later whether the entity nodes should be created by a    // separated method in parser or not. For now just stick it in if    // the ref nodes are created        if (entity)            entity->setEntityRef(er);    }}void AbstractDOMParser::XMLDecl(const   XMLCh* const version                                , const XMLCh* const encoding                                , const XMLCh* const standalone                                , const XMLCh* const actualEncStr){    fDocument->setStandalone(XMLString::equals(XMLUni::fgYesString, standalone));    fDocument->setVersion(version);    fDocument->setEncoding(encoding);    fDocument->setActualEncoding(actualEncStr);}// ---------------------------------------------------------------------------//  AbstractDOMParser: Helper methods// ---------------------------------------------------------------------------DOMElement* AbstractDOMParser::createElementNSNode(const XMLCh *namespaceURI,                                                   const XMLCh *qualifiedName){    return fDocument->createElementNS(namespaceURI, qualifiedName);}// ---------------------------------------------------------------------------//  AbstractDOMParser: Deprecated methods// ---------------------------------------------------------------------------bool AbstractDOMParser::getDoValidation() const{    //    //  We don't want to tie the public parser classes to the enum used    //  by the scanner, so we use a separate one and map.    //    //  DON'T mix the new and old methods!!    //    const XMLScanner::ValSchemes scheme = fScanner->getValidationScheme();    if (scheme == XMLScanner::Val_Always)        return true;    return false;}void AbstractDOMParser::setDoValidation(const bool newState){    fScanner->setDoValidation    (        newState ? XMLScanner::Val_Always : XMLScanner::Val_Never    );}//doctypehandler interfacesvoid AbstractDOMParser::attDef(    const   DTDElementDecl&     elemDecl    , const DTDAttDef&          attDef    , const bool){	    if (fDocumentType->isIntSubsetReading())    {        if (elemDecl.hasAttDefs())        {            fInternalSubset.append(attDef.getFullName());            // Get the type and display it            const XMLAttDef::AttTypes type = attDef.getType();            switch(type)            {            case XMLAttDef::CData :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgCDATAString);                break;            case XMLAttDef::ID :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgIDString);                break;            case XMLAttDef::IDRef :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgIDRefString);                break;            case XMLAttDef::IDRefs :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgIDRefsString);                break;            case XMLAttDef::Entity :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgEntityString);                break;            case XMLAttDef::Entities :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgEntitiesString);                break;            case XMLAttDef::NmToken :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgNmTokenString);                break;            case XMLAttDef::NmTokens :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgNmTokensString);                break;            case XMLAttDef::Notation :                fInternalSubset.append(chSpace);                fInternalSubset.append(XMLUni::fgNotationString);                break;            case XMLAttDef::Enumeration :                fInternalSubset.append(chSpace);                const XMLCh* enumString = attDef.getEnumeration();                int length = XMLString::stringLen(enumString);                if (length > 0) {

⌨️ 快捷键说明

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