📄 xmlparser.cc
字号:
// back out (*XMLEntityStreamStack.begin())->streamPos = lastStreamPos; return 0; } // critical point XMLString doctypeName; XMLString SystemLiteral; XMLString PubidLiteral; if(!matchProduction05Name((*XMLEntityStreamStack.begin()),doctypeName)) { throw XMLParserException(&XMLEntityStreamStack,"Name (for DTD) expected.", XMLParserException::BAD_XML_ERR); } matchProduction03S((*XMLEntityStreamStack.begin()),0); matchProduction75ExternalID(PubidLiteral,SystemLiteral); matchProduction03S((*XMLEntityStreamStack.begin()),0); // OK, now we can create 'theDocumentType' theDocumentType = myDOMImplementation.createDocumentType(doctypeName,PubidLiteral,SystemLiteral); // now if we have a SystemLiteral we need to create an XMLEntityStream for it, and // descend into it in order to parse the external DTD before processing the // internal subset if(SystemLiteral.length()>0) { if(StandAlone) { throw XMLParserException(&XMLEntityStreamStack,"External DTD not allowed in standalone documents", XMLParserException::BAD_XML_ERR); } XMLEntityStream* newXMLEntityStream = (*XMLEntityStreamStack.begin())->addParameterXMLEntityStream( doctypeName,PubidLiteral,SystemLiteral); XMLEntityStreamStack.push_front(newXMLEntityStream); matchProduction31extSubsetDecl(); if(XMLEntityStreamStack.size()>1) { throw XMLParserException(&XMLEntityStreamStack, "markupdecl|conditionalSect|DeclSep expected", XMLParserException::BAD_XML_ERR); } } // now parse internal subset if(matchProductionFixedString((*XMLEntityStreamStack.begin()),"[")) { bool more2go = 1; while(more2go) { more2go = matchProduction29markupdecl(); if((*XMLEntityStreamStack.begin())!=rootXMLEntityStream) { more2go = more2go|matchProduction61conditionalSect(); } more2go = more2go|matchProduction28aDeclSep(); if(!more2go) { if((*XMLEntityStreamStack.begin())!=rootXMLEntityStream) { if((*XMLEntityStreamStack.begin())->atEnd()) { (*XMLEntityStreamStack.begin()) = (*XMLEntityStreamStack.begin())->parentXMLEntityStream(); } else { throw XMLParserException(&XMLEntityStreamStack, "markupdecl|conditionalSect|DeclSep expected", XMLParserException::BAD_XML_ERR); } more2go=1; } } } if(!matchProductionFixedString((*XMLEntityStreamStack.begin()),"]")) { // not correctly terminated throw XMLParserException(&XMLEntityStreamStack,"']' expected", XMLParserException::BAD_XML_ERR); } matchProduction03S((*XMLEntityStreamStack.begin()),0); } if(!matchProductionFixedString((*XMLEntityStreamStack.begin()),">")) { // not correctly terminated throw XMLParserException(&XMLEntityStreamStack,"'>'expected", XMLParserException::BAD_XML_ERR); } return 1;};// ******************************************************************************bool XMLParser::matchProduction29markupdecl() { // this production is optional if(DEBUGXMLPARSER) { printf("XMLParser::matchProduction29markupdecl\n"); } if(matchProduction45elementdecl()) { return 1; } if(matchProduction52AttlistDecl()) { return 1; } if(matchProduction70EntityDecl()) { return 1; } if(matchProduction82NotationDecl()) { return 1; } if(matchProduction16PI(0)) { return 1; } if(matchProduction15Comment(0)) { return 1; } return 0;};// ******************************************************************************void XMLParser::matchProduction31extSubsetDecl() { if(DEBUGXMLPARSER) { printf("XMLParser::matchProduction31extSubsetDecl\n"); } // this production is optional const unsigned long enteringlevel = XMLEntityStreamStack.size(); while(1) { if(matchProduction29markupdecl()) {} else if(matchProduction61conditionalSect()) {} else if(matchProduction28aDeclSep()) {} else if(XMLEntityStreamStack.size()<=enteringlevel) { return; } else { throw XMLParserException(&XMLEntityStreamStack,"markupdecl|conditionalSect|DeclSep expected", XMLParserException::BAD_XML_ERR); } }};// ******************************************************************************bool XMLParser::matchProduction32SDDecl() { // this production is optional if(DEBUGXMLPARSER) { printf("XMLParser::matchProduction32SDDecl\n"); } StreamPositionStruct lastStreamPos = (*XMLEntityStreamStack.begin())->streamPos; if(matchProduction03S((*XMLEntityStreamStack.begin()),0)==0) { return 0; // there was no white space } if(!matchProductionFixedString((*XMLEntityStreamStack.begin()),"standalone")) { // back out (*XMLEntityStreamStack.begin())->streamPos = lastStreamPos; return 0; } // critical point matchProduction25Eq((*XMLEntityStreamStack.begin())); XMLString SDDecl; lastStreamPos = (*XMLEntityStreamStack.begin())->streamPos; matchProductionQuotedString((*XMLEntityStreamStack.begin()),SDDecl); if(SDDecl=="yes") { StandAlone = 1; } else if(SDDecl=="no") { StandAlone = 0; } else { // isn't an SDDecl (*XMLEntityStreamStack.begin())->streamPos = lastStreamPos; (*XMLEntityStreamStack.begin())->streamPos.columnNumber++; throw XMLParserException(&XMLEntityStreamStack,"'yes' or 'no' expected for StandAlone declaration", XMLParserException::BAD_XML_ERR); } if(DEBUGXMLPARSER) { printf("SDDecl = '%s'\n",SDDecl.c_str()); } return 1;};// ******************************************************************************bool XMLParser::matchProduction39Element( Element* containingElement) { // this production is optional if(DEBUGXMLPARSER) { printf("XMLParser::matchProduction39Element\n"); } const XMLEntityStream *const enteringXMLEntityStream = (*XMLEntityStreamStack.begin()); StreamPositionStruct lastStreamPos = (*XMLEntityStreamStack.begin())->streamPos; XMLString tagName; list <XMLString> myAttributeNamesList; list <XMLString> myAttributeValuesList; Element* newElement; bool isEmptyElement=0; if(matchProduction44EmptyElementTag(&tagName,&myAttributeNamesList,&myAttributeValuesList)) { isEmptyElement=1; } else if(!matchProduction40STag(&tagName,&myAttributeNamesList,&myAttributeValuesList)) { return 0; } if((theDocument==0)&(theDocumentType!=0)) { if(tagName != *theDocumentType->nodeName()) { (*XMLEntityStreamStack.begin())->streamPos = lastStreamPos; (*XMLEntityStreamStack.begin())->streamPos.columnNumber++; sprintf(errormessage, "Expecting root element to be '%s'\n",theDocumentType->nodeName()->c_str()); throw XMLParserException(&XMLEntityStreamStack,errormessage, XMLParserException::BAD_XML_ERR); } } // need to compile a list of local URI bindings list<XMLString> localPrefixes; list<XMLString> localURIs; XMLString defaultURI; list<XMLString>::const_iterator pAttName = myAttributeNamesList.begin(); list<XMLString>::const_iterator pAttValue = myAttributeValuesList.begin(); while(pAttName!=myAttributeNamesList.end()) { if(pAttName->eqxmlns()) { defaultURI = *pAttValue; } else { XMLString attPrefix; XMLString attLocalName; if(pAttName->splitNSName(attPrefix,attLocalName)) { if(attPrefix.eqxmlns()) { localPrefixes.push_back(attLocalName); localURIs.push_back(*pAttValue); } } } pAttName++; pAttValue++; } // now find URI binding for element tagName XMLString prefix; XMLString localName; XMLString namespaceURI; if(tagName.splitNSName(prefix,localName)) { // prefix exists, is it xml:? if(prefix.eqxml()) { if(DEBUGXMLPARSER) { printf("my prefix is xml, will use XML_NAMESPACEURI binding\n"); } namespaceURI = XML_NAMESPACEURI; } if(namespaceURI.length()==0) { // not xml: => look for xmlns:prefix="..." binding // firstly look for local binding if(DEBUGXMLPARSER) { printf("my prefix is not xml, looking for local binding\n"); } list<XMLString>::const_iterator pLocalPrefix=localPrefixes.begin(); list<XMLString>::const_iterator pLocalURIs=localURIs.begin(); while((namespaceURI.length()==0)&(pLocalPrefix!=localPrefixes.end())) { if(*pLocalPrefix==prefix) { namespaceURI=*pLocalURIs; } pLocalPrefix++; pLocalURIs++; } } if((namespaceURI.length()==0)&!(containingElement==0)) { // didn't find one in attributes, look in ancestor elements if(DEBUGXMLPARSER) { printf("my prefix is not xml, looking for binding in ancestor element\n"); } const XMLString* pNamespaceURI=containingElement->lookupNamespaceURI(prefix); if(!(pNamespaceURI==0)) { namespaceURI=*pNamespaceURI; } } if(namespaceURI.length()==0) { // cannot find a URI binding (*XMLEntityStreamStack.begin())->streamPos = lastStreamPos; (*XMLEntityStreamStack.begin())->streamPos.columnNumber++; throw XMLParserException(&XMLEntityStreamStack, "Cannot find internal URI binding for this prefix", XMLParserException::BAD_XML_ERR); } } else { // no prefix => look for default xmlns="..." binding if(!(defaultURI.length()==0)) { // a local default exists if(DEBUGXMLPARSER) { printf("there is a local default URI binding\n"); } namespaceURI=defaultURI; } else if(!(containingElement==0)) { // no local default, try in ancestor elements if(DEBUGXMLPARSER) { printf("looking for default binding in ancestor element\n"); } const XMLString* pNamespaceURI=containingElement->lookupNamespaceURI(""); if(!(pNamespaceURI==0)) { namespaceURI=*pNamespaceURI; } } } if(DEBUGXMLPARSER) { printf("my namespaceURI binding is'%s'\n",namespaceURI.c_str()); } if(theDocument==0) { theDocument = myDOMImplementation.createDocument(namespaceURI,tagName,theDocumentType); newElement = theDocument->documentElement(); } else { if (namespaceURI.length()==0) { newElement = theDocument->createElement(tagName); } else { newElement = theDocument->createElementNS(namespaceURI,tagName); } } if(!(containingElement==0)) { containingElement->appendChild(newElement); } // now add the attributes pAttName = myAttributeNamesList.begin(); pAttValue = myAttributeValuesList.begin(); while(pAttName!=myAttributeNamesList.end()) { if(DEBUGXMLPARSER) { printf("examining attribute %s='%s'\n",pAttName->c_str(),pAttValue->c_str()); } if(pAttName->splitNSName(prefix,localName)) { // attribute has a prefix if(DEBUGXMLPARSER) { printf(" attribute has a prefix\n"); } namespaceURI=""; if(prefix.eqxml()) { namespaceURI = XML_NAMESPACEURI; } else if(prefix.eqxmlns()) { namespaceURI = XMLNS_NAMESPACEURI; } else { // need to look for local binding if(DEBUGXMLPARSER) { printf(" prefix is not xml or xmlns, looking for local binding\n"); } list<XMLString>::const_iterator pLocalPrefix=localPrefixes.begin(); list<XMLString>::const_iterator pLocalURIs=localURIs.begin(); while((namespaceURI.length()==0)&(pLocalPrefix!=localPrefixes.end())) { if(*pLocalPrefix==prefix) { namespaceURI=*pLocalURIs; } pLocalPrefix++; pLocalURIs++; } } if((namespaceURI.length()==0)&!(containingElement==0)) { // look for default in ancestor elements if(DEBUGXMLPARSER) { printf("no local binding found, looking for ancestral binding\n"); } const XMLString* pNamespaceURI=containingElement->lookupNamespaceURI(""); if(!(pNamespaceURI==0)) { namespaceURI=*pNamespaceURI; } } if(namespaceURI.length()==0) { sprintf(errormessage,"Cannot find internal URI binding for prefix '%s'\n",prefix.c_str())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -