xsddomparser.cpp

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

CPP
530
字号
                fScanner->getURIText(urlId), elemDecl.getBaseName());        }    }    else {        elem = createElementNSNode(0, elemDecl.getBaseName());    }    DOMElementImpl *elemImpl = (DOMElementImpl *) elem;    for (unsigned int index = 0; index < attrCount; ++index)    {        const XMLAttr* oneAttrib = attrList.elementAt(index);        unsigned int attrURIId = oneAttrib->getURIId();        const XMLCh* namespaceURI = 0;        //for xmlns=...        if (XMLString::equals(oneAttrib->getName(), XMLUni::fgXMLNSString))            attrURIId = fScanner->getXMLNSNamespaceId();        //TagName has a prefix        if (attrURIId != fScanner->getEmptyNamespaceId())            namespaceURI = fScanner->getURIText(attrURIId); //get namespaceURI        //  revisit.  Optimize to init the named node map to the        //            right size up front.        DOMAttrImpl *attr = (DOMAttrImpl *)            fDocument->createAttributeNS(namespaceURI, oneAttrib->getQName());        attr->setValue(oneAttrib -> getValue());        DOMNode* remAttr = elemImpl->setAttributeNodeNS(attr);        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());    }    // set up the default attributes    if (elemDecl.hasAttDefs())	{        XMLAttDefList* defAttrs = &elemDecl.getAttDefList();        XMLAttDef* attr = 0;        DOMAttrImpl * insertAttr = 0;        while (defAttrs->hasMoreElements())        {            attr = &defAttrs->nextElement();            const XMLAttDef::DefAttTypes defType = attr->getDefaultType();            if ((defType == XMLAttDef::Default)            ||  (defType == XMLAttDef::Fixed))            {                // 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))                    uriId = fScanner->getXMLNSNamespaceId();                //TagName has a prefix                if (uriId != fScanner->getEmptyNamespaceId())                    namespaceURI = fScanner->getURIText(uriId);                insertAttr = (DOMAttrImpl *) fDocument->createAttributeNS(                    namespaceURI, qualifiedName);                DOMAttr* remAttr = elemImpl->setDefaultAttributeNodeNS(insertAttr);                if (remAttr)                    remAttr->release();                if (attr->getValue() != 0)                {                    insertAttr->setValue(attr->getValue());                    insertAttr->setSpecified(false);                }            }            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 XSDDOMParser::endElement( const XMLElementDecl& elemDecl                             , const unsigned int                             , const bool                             , const XMLCh* const){    if(fAnnotationDepth > -1)    {        if (fInnerAnnotationDepth == fDepth)        {            fInnerAnnotationDepth = -1;            endAnnotationElement(elemDecl, false);	    }        else if (fAnnotationDepth == fDepth)         {            fAnnotationDepth = -1;            endAnnotationElement(elemDecl, true);        }        else         {   // inside a child of annotation            endAnnotationElement(elemDecl, false);            fDepth--;            return;        }    }	fDepth--;    fCurrentNode   = fCurrentParent;    fCurrentParent = fNodeStack->pop();    // If we've hit the end of content, clear the flag    if (fNodeStack->empty())        fWithinElement = false;}void XSDDOMParser::docCharacters(  const   XMLCh* const    chars                              , const unsigned int    length                              , const bool            cdataSection){    // Ignore chars outside of content    if (!fWithinElement)        return;    if (fInnerAnnotationDepth == -1)    {        if (!((ReaderMgr*) fScanner->getReaderMgr())->getCurrentReader()->isAllSpaces(chars, length))        {            ReaderMgr::LastExtEntityInfo lastInfo;            fScanner->getReaderMgr()->getLastExtEntityInfo(lastInfo);            fXSLocator.setValues(lastInfo.systemId, lastInfo.publicId, lastInfo.lineNumber, lastInfo.colNumber);            fXSDErrorReporter.emitError(XMLValid::NonWSContent, XMLUni::fgValidityDomain, &fXSLocator);        }    }    // when it's within either of the 2 annotation subelements, characters are    // allowed and we need to store them.    else if (cdataSection == true)    {        fAnnotationBuf.append(XMLUni::fgCDataStart);        fAnnotationBuf.append(chars, length);        fAnnotationBuf.append(XMLUni::fgCDataEnd);    }    else    {        for(unsigned int i = 0; i < length; i++ )        {            if(chars[i] == chAmpersand)            {                fAnnotationBuf.append(chAmpersand);                fAnnotationBuf.append(XMLUni::fgAmp);                fAnnotationBuf.append(chSemiColon);            }            else if (chars[i] == chOpenAngle)            {                fAnnotationBuf.append(chAmpersand);                fAnnotationBuf.append(XMLUni::fgLT);                fAnnotationBuf.append(chSemiColon);            }            else {                fAnnotationBuf.append(chars[i]);            }        }    }}void XSDDOMParser::docComment(const XMLCh* const comment){    if (fAnnotationDepth > -1)    {        fAnnotationBuf.append(XMLUni::fgCommentString);        fAnnotationBuf.append(comment);        fAnnotationBuf.append(chDash);        fAnnotationBuf.append(chDash);        fAnnotationBuf.append(chCloseAngle);    }}void XSDDOMParser::startEntityReference(const XMLEntityDecl&){}void XSDDOMParser::endEntityReference(const XMLEntityDecl&){}void XSDDOMParser::ignorableWhitespace( const XMLCh* const chars                                      , const unsigned int length                                      , const bool){    // Ignore chars before the root element    if (!fWithinElement || !fIncludeIgnorableWhitespace)        return;    if (fAnnotationDepth > -1)        fAnnotationBuf.append(chars, length);}// ---------------------------------------------------------------------------//  XSDDOMParser: Implementation of the XMLErrorReporter interface// ---------------------------------------------------------------------------void XSDDOMParser::error(const   unsigned int                code                         , const XMLCh* const                msgDomain                         , const XMLErrorReporter::ErrTypes  errType                         , const XMLCh* const                errorText                         , const XMLCh* const                systemId                         , const XMLCh* const                publicId                         , const XMLSSize_t                  lineNum                         , const XMLSSize_t                  colNum){    if (errType >= XMLErrorReporter::ErrType_Fatal)        fSawFatal = true;    if (fUserErrorReporter)        fUserErrorReporter->error(code, msgDomain, errType, errorText,                                  systemId, publicId, lineNum, colNum);}InputSource* XSDDOMParser::resolveEntity(const XMLCh* const publicId,                                         const XMLCh* const systemId,                                         const XMLCh* const baseURI){    if (fUserEntityHandler)        return fUserEntityHandler->resolveEntity(publicId, systemId, baseURI);    return 0;}InputSource*XSDDOMParser::resolveEntity(XMLResourceIdentifier* resourceIdentifier){    if (fUserEntityHandler)        return fUserEntityHandler->resolveEntity(resourceIdentifier);    return 0;}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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