domparser.cpp

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

CPP
1,464
字号
                attString.appendData(chSpace);                attString.appendData(XMLUni::fgEntitiesString);                break;            case XMLAttDef::NmToken :                attString.appendData(chSpace);                attString.appendData(XMLUni::fgNmTokenString);                break;            case XMLAttDef::NmTokens :                attString.appendData(chSpace);                attString.appendData(XMLUni::fgNmTokensString);                break;            case XMLAttDef::Notation :                attString.appendData(chSpace);                attString.appendData(XMLUni::fgNotationString);                break;            case XMLAttDef::Enumeration :                attString.appendData(chSpace);                //  attString.appendData(XMLUni::fgEnumerationString);                const XMLCh* enumString = attDef.getEnumeration();                int length = XMLString::stringLen(enumString);                if (length > 0) {                    DOMString anotherEnumString;                    anotherEnumString.appendData(chOpenParen );                    for(int i=0; i<length; i++) {                        if (enumString[i] == chSpace)                            anotherEnumString.appendData(chPipe);                        else                            anotherEnumString.appendData(enumString[i]);                    }                    anotherEnumString.appendData(chCloseParen);                    attString.appendData(anotherEnumString);                }                break;            }            //get te default types of the attlist            const XMLAttDef::DefAttTypes def = attDef.getDefaultType();            switch(def)            {            case XMLAttDef::Required :                attString.appendData(chSpace);                attString.appendData(XMLUni::fgRequiredString);                break;            case XMLAttDef::Implied :                attString.appendData(chSpace);                attString.appendData(XMLUni::fgImpliedString);                break;            case XMLAttDef::Fixed :                attString.appendData(chSpace);                attString.appendData(XMLUni::fgFixedString);                break;            }            const XMLCh* defaultValue = attDef.getValue();            if (defaultValue != 0) {                attString.appendData(chSpace);                attString.appendData(chDoubleQuote);                attString.appendData(defaultValue);                attString.appendData(chDoubleQuote);            }            attString.appendData(chCloseAngle);            fDocumentType->internalSubset.appendData(attString);        }    }}void DOMParser::doctypeComment(    const   XMLCh* const    comment){    if (fDocumentType->isIntSubsetReading())    {        if (comment != 0)        {            DOMString comString;            comString.appendData(XMLUni::fgCommentString);            comString.appendData(chSpace);            comString.appendData(comment);            comString.appendData(chSpace);            comString.appendData(chDash);            comString.appendData(chDash);            comString.appendData(chCloseAngle);            fDocumentType->internalSubset.appendData(comString);        }    }}void DOMParser::doctypeDecl(    const   DTDElementDecl& elemDecl    , const XMLCh* const    publicId    , const XMLCh* const    systemId    , const bool            hasIntSubset    , const bool            hasExtSubset){	DOM_DocumentType dt;	dt = fDocument.getImplementation().createDocumentType(elemDecl.getFullName(), publicId, systemId);    fDocumentType = (DocumentTypeImpl*)dt.fImpl;	((DocumentImpl*)fDocument.fImpl)->setDocumentType(fDocumentType);}void DOMParser::doctypePI(    const   XMLCh* const    target    , const XMLCh* const    data){    if (fDocumentType->isIntSubsetReading())	{		//add these chars to internalSubset variable        DOMString pi;        pi.appendData(chOpenAngle);        pi.appendData(chQuestion);        pi.appendData(target);        pi.appendData(chSpace);        pi.appendData(data);        pi.appendData(chQuestion);        pi.appendData(chCloseAngle);		fDocumentType->internalSubset.appendData(pi);	}	}void DOMParser::doctypeWhitespace(    const   XMLCh* const    chars    , const unsigned int    length){    if (fDocumentType->isIntSubsetReading())		fDocumentType->internalSubset.appendData(chars);}void DOMParser::elementDecl(    const   DTDElementDecl& decl    , const bool            isIgnored){    if (fDocumentType->isIntSubsetReading())	{        DOMString elemDecl;        elemDecl.appendData(chOpenAngle);        elemDecl.appendData(chBang);        elemDecl.appendData(XMLUni::fgElemString);        elemDecl.appendData(chSpace);        elemDecl.appendData(decl.getFullName());        //get the ContentSpec information        const XMLCh* contentModel = decl.getFormattedContentModel();        if (contentModel != 0) {            elemDecl.appendData(chSpace);            elemDecl.appendData(contentModel);        }        elemDecl.appendData(chCloseAngle);		fDocumentType->internalSubset.appendData(elemDecl);	}}void DOMParser::endAttList(    const   DTDElementDecl& elemDecl){	// this section sets up default attributes.	// default attribute nodes are stored in a NamedNodeMap DocumentTypeImpl::elements	// default attribute data attached to the document is used to conform to the	// DOM spec regarding creating element nodes & removing attributes with default values	// see DocumentTypeImpl	if (elemDecl.hasAttDefs())	{				XMLAttDefList* defAttrs = &elemDecl.getAttDefList();		XMLAttDef* attr = 0;		AttrImpl* insertAttr = 0;		DOM_Element dom_elem = fDocument.createElement(elemDecl.getFullName());		ElementImpl* elem = (ElementImpl*)(dom_elem.fImpl);        for(unsigned int i=0; i<defAttrs->getAttDefCount(); i++)        {            attr = &defAttrs->getAttDef(i);            if (attr->getValue() != null)            {                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.                    DOMString qualifiedName = attr->getFullName();                    int index = DocumentImpl::indexofQualifiedName(qualifiedName);                    XMLBuffer buf(1023, fMemoryManager);                    static const XMLCh XMLNS[] = {                        chLatin_x, chLatin_m, chLatin_l, chLatin_n, chLatin_s, chNull};                    if (index > 0) {                        // there is prefix                        // map to XML URI for all cases except when prefix == "xmlns"                        DOMString prefix = qualifiedName.substringData(0, index);                        if (prefix.equals(XMLNS))                            buf.append(XMLUni::fgXMLNSURIName);                        else                            buf.append(XMLUni::fgXMLURIName);                    }                    else {                        //   No prefix                        if (qualifiedName.equals(XMLNS))                            buf.append(XMLUni::fgXMLNSURIName);                    }                    insertAttr = new (fMemoryManager) AttrNSImpl((DocumentImpl*)fDocument.fImpl,                       DOMString(buf.getRawBuffer()),     // NameSpaceURI                       qualifiedName);   // qualified name                }                else                {                    // Namespaces is turned off...                    insertAttr = new (fMemoryManager) AttrImpl((DocumentImpl*)fDocument.fImpl, attr->getFullName());                }                insertAttr->setValue(attr->getValue());                // memory leak here                AttrImpl * previousAttr = elem->setAttributeNode(insertAttr);				if ( previousAttr != 0 && previousAttr->nodeRefCount ==0)					NodeImpl::deleteIf(previousAttr);                insertAttr->setSpecified(false);            }        }        ElementImpl *previousElem = (ElementImpl *)                fDocumentType->getElements()->setNamedItem( elem );        //        //  If this new element is replacing an element node that was already        //    in the element named node map, we need to delete the original        //    element node, assuming no-one else was referencing it.        //        if (previousElem != 0 && previousElem->nodeRefCount == 0)            NodeImpl::deleteIf(previousElem);    }}void DOMParser::endIntSubset(){	fDocumentType->intSubsetReading = false;}void DOMParser::endExtSubset(){}void DOMParser::entityDecl(    const   DTDEntityDecl&  entityDecl    , const bool            isPEDecl    , const bool            isIgnored){	EntityImpl* entity = ((DocumentImpl*)fDocument.fImpl)->createEntity(entityDecl.getName());	entity->setPublicId(entityDecl.getPublicId());	entity->setSystemId(entityDecl.getSystemId());	entity->setNotationName(entityDecl.getNotationName());    EntityImpl *previousDef = (EntityImpl *)	    fDocumentType->entities->setNamedItem( entity );    //    //  If this new entity node is replacing an entity node that was already    //    in the entities named node map (happens if documents redefine the    //    predefined entited such as lt), we need to delete the original    //    entitiy node, assuming no-one else was referencing it.    //    if (previousDef != 0 && previousDef->nodeRefCount == 0)    	        NodeImpl::deleteIf(previousDef);	if (fDocumentType->isIntSubsetReading())	{		//add thes chars to internalSubset variable		DOMString entityName;		entityName.appendData(chOpenAngle);        entityName.appendData(chBang);		entityName.appendData(XMLUni::fgEntityString);        entityName.appendData(chSpace);        entityName.appendData(entityDecl.getName());        DOMString id = entity->getPublicId();        if (id != 0) {            entityName.appendData(chSpace);            entityName.appendData(XMLUni::fgPubIDString);            entityName.appendData(chSpace);            entityName.appendData(chDoubleQuote);            entityName.appendData(id);            entityName.appendData(chDoubleQuote);        }        id = entity->getSystemId();        if (id != 0) {            entityName.appendData(chSpace);            entityName.appendData(XMLUni::fgSysIDString);            entityName.appendData(chSpace);            entityName.appendData(chDoubleQuote);            entityName.appendData(id);            entityName.appendData(chDoubleQuote);        }        id = entity->getNotationName();        if (id != 0) {            entityName.appendData(chSpace);            entityName.appendData(XMLUni::fgNDATAString);            entityName.appendData(chSpace);            entityName.appendData(chDoubleQuote);            entityName.appendData(id);            entityName.appendData(chDoubleQuote);        }        id = entityDecl.getValue();        if (id !=0) {            entityName.appendData(chSpace);            entityName.appendData(chDoubleQuote);            entityName.appendData(id);            entityName.appendData(chDoubleQuote);        }        entityName.appendData(chCloseAngle);        fDocumentType->internalSubset.appendData(entityName);    }}void DOMParser::resetDocType(){	fDocumentType = null;}void DOMParser::notationDecl(    const   XMLNotationDecl&    notDecl    , const bool                isIgnored){	NotationImpl* notation = ((DocumentImpl*)fDocument.fImpl)->createNotation(notDecl.getName());	notation->setPublicId(notDecl.getPublicId());	notation->setSystemId(notDecl.getSystemId());    NotationImpl *previousNot = (NotationImpl *)       fDocumentType->notations->setNamedItem( notation );    //    //  If this new notation is replacing a notation node that was already    //    in the notation named node map, we need to delete the original    //    notation node, assuming no-one else was referencing it.    //    if (previousNot != 0 && previousNot->nodeRefCount == 0)        NodeImpl::deleteIf(previousNot);}void DOMParser::startAttList(    const   DTDElementDecl& elemDecl){}void DOMParser::startIntSubset(){	fDocumentType->intSubsetReading = true;}void DOMParser::startExtSubset(){}void DOMParser::TextDecl(    const   XMLCh* const    versionStr    , const XMLCh* const    encodingStr){}// ---------------------------------------------------------------------------//  DOMParser: Grammar preparsing methods// ---------------------------------------------------------------------------Grammar* DOMParser::loadGrammar(const char* const systemId,                                const short grammarType,                                const bool toCache){    // Avoid multiple entrance    if (fParseInProgress)        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);    Grammar* grammar = 0;    try    {        fParseInProgress = true;        grammar = fScanner->loadGrammar(systemId, grammarType, toCache);        fParseInProgress = false;    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        fParseInProgress = false;        throw;    }    return grammar;}Grammar* DOMParser::loadGrammar(const XMLCh* const systemId,                                const short grammarType,                                const bool toCache){    // Avoid multiple entrance    if (fParseInProgress)        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);    Grammar* grammar = 0;    try    {        fParseInProgress = true;        grammar = fScanner->loadGrammar(systemId, grammarType, toCache);        fParseInProgress = false;    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        fParseInProgress = false;        throw;    }    return grammar;}Grammar* DOMParser::loadGrammar(const InputSource& source,                                const short grammarType,                                const bool toCache){    // Avoid multiple entrance    if (fParseInProgress)        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);   Grammar* grammar = 0;    try    {        fParseInProgress = true;        grammar = fScanner->loadGrammar(source, grammarType, toCache);        fParseInProgress = false;    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        fParseInProgress = false;        throw;    }    return grammar;}void DOMParser::resetCachedGrammarPool(){    fGrammarResolver->resetCachedGrammar();}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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