xercesxpath.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,455 行 · 第 1/4 页
CPP
1,455 行
}XercesXPath::~XercesXPath() { cleanUp();}// ---------------------------------------------------------------------------// XercesXPath: Operators// ---------------------------------------------------------------------------bool XercesXPath::operator==(const XercesXPath& other) const { unsigned int locPathSize = fLocationPaths->size(); if (locPathSize != other.fLocationPaths->size()) return false; for (unsigned int i=0; i < locPathSize; i++) { if (*(fLocationPaths->elementAt(i)) != *(other.fLocationPaths->elementAt(i))) return false; } return true;}bool XercesXPath::operator!=(const XercesXPath& other) const { return !operator==(other);}// ---------------------------------------------------------------------------// XercesPath: Helper methods// ---------------------------------------------------------------------------void XercesXPath::cleanUp() { fMemoryManager->deallocate(fExpression);//delete [] fExpression; delete fLocationPaths;}void XercesXPath::checkForSelectedAttributes() { // verify that an attribute is not selected unsigned int locSize = (fLocationPaths) ? fLocationPaths->size() : 0; for (unsigned int i = 0; i < locSize; i++) { XercesLocationPath* locPath = fLocationPaths->elementAt(i); unsigned int stepSize = locPath->getStepSize(); if (stepSize) { if (locPath->getStep(stepSize - 1)->getAxisType() == XercesStep::ATTRIBUTE) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoAttrSelector, fMemoryManager); } } }}void XercesXPath::parseExpression(XMLStringPool* const stringPool, NamespaceScope* const scopeContext) { unsigned int length = XMLString::stringLen(fExpression); if (!length) { return; } ValueVectorOf<int> tokens(16, fMemoryManager); XPathScannerForSchema scanner(stringPool); bool success = scanner.scanExpression(fExpression, 0, length, &tokens); bool firstTokenOfLocationPath=true; unsigned int tokenCount = tokens.size(); RefVectorOf<XercesStep>* stepsVector = new (fMemoryManager) RefVectorOf<XercesStep>(16, true, fMemoryManager); Janitor<RefVectorOf<XercesStep> > janSteps(stepsVector); if (tokenCount) { fLocationPaths = new (fMemoryManager) RefVectorOf<XercesLocationPath>(8, true, fMemoryManager); } for (unsigned int i = 0; i < tokenCount; i++) { int aToken = tokens.elementAt(i); bool isNamespace=false; switch (aToken) { case XercesXPath::EXPRTOKEN_OPERATOR_UNION: { if (i == 0) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoUnionAtStart, fMemoryManager); } int stepsSize = stepsVector->size(); if (stepsSize == 0) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoMultipleUnion, fMemoryManager); } fLocationPaths->addElement(new (fMemoryManager) XercesLocationPath(stepsVector)); janSteps.orphan(); stepsVector = new (fMemoryManager) RefVectorOf<XercesStep>(16, true, fMemoryManager); janSteps.reset(stepsVector); firstTokenOfLocationPath = true; } break; case XercesXPath::EXPRTOKEN_AXISNAME_ATTRIBUTE: { // consume "::" token and drop through i++; } case XercesXPath::EXPRTOKEN_ATSIGN: { // consume QName token if (i == tokenCount - 1) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_MissingAttr, fMemoryManager); } aToken = tokens.elementAt(++i); if (aToken != XercesXPath::EXPRTOKEN_NAMETEST_QNAME && aToken!= XercesXPath::EXPRTOKEN_NAMETEST_ANY && aToken!= XercesXPath::EXPRTOKEN_NAMETEST_NAMESPACE) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_ExpectedToken1, fMemoryManager); } bool isNamespaceAtt=false; switch (aToken) { case XercesXPath::EXPRTOKEN_NAMETEST_ANY: { XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(XercesNodeTest::WILDCARD, fMemoryManager); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::ATTRIBUTE, nodeTest); stepsVector->addElement(step); break; } case XercesXPath::EXPRTOKEN_NAMETEST_NAMESPACE: { isNamespaceAtt = true; } case XercesXPath::EXPRTOKEN_NAMETEST_QNAME: { aToken = tokens.elementAt(++i); const XMLCh* prefix = XMLUni::fgZeroLenString; unsigned int uri = fEmptyNamespaceId; if (scopeContext && aToken != -1) { prefix = stringPool->getValueForId(aToken); uri = scopeContext->getNamespaceForPrefix(prefix); } if (aToken != -1 && scopeContext && uri == fEmptyNamespaceId) { ThrowXMLwithMemMgr1(XPathException, XMLExcepts::XPath_PrefixNoURI, prefix, fMemoryManager); } if (isNamespaceAtt) { // build step XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(prefix, uri, fMemoryManager); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::ATTRIBUTE, nodeTest); stepsVector->addElement(step); break; } aToken = tokens.elementAt(++i); const XMLCh* localPart = stringPool->getValueForId(aToken); QName aQName(prefix, localPart, uri, fMemoryManager); // build step XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(&aQName); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::ATTRIBUTE, nodeTest); stepsVector->addElement(step); break; } } firstTokenOfLocationPath=false; break; } case XercesXPath::EXPRTOKEN_DOUBLE_COLON: { // should never have a bare double colon ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoDoubleColon, fMemoryManager); } case XercesXPath::EXPRTOKEN_AXISNAME_CHILD: { // consume "::" token and drop through i++; if (i == tokenCount - 1) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_ExpectedStep1, fMemoryManager); } firstTokenOfLocationPath=false; break; } case XercesXPath::EXPRTOKEN_NAMETEST_ANY: { XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(XercesNodeTest::WILDCARD, fMemoryManager); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::CHILD, nodeTest); stepsVector->addElement(step); firstTokenOfLocationPath = false; break; } case XercesXPath::EXPRTOKEN_NAMETEST_NAMESPACE: { isNamespace=true; } case XercesXPath::EXPRTOKEN_NAMETEST_QNAME: { // consume QName token aToken = tokens.elementAt(++i); const XMLCh* prefix = XMLUni::fgZeroLenString; unsigned int uri = fEmptyNamespaceId; if (scopeContext && aToken != -1) { prefix = stringPool->getValueForId(aToken); uri = scopeContext->getNamespaceForPrefix(prefix); } if (aToken != -1 && scopeContext && uri == fEmptyNamespaceId) { ThrowXMLwithMemMgr1(XPathException, XMLExcepts::XPath_PrefixNoURI, prefix, fMemoryManager); } if (isNamespace) { // build step XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(prefix, uri, fMemoryManager); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::CHILD, nodeTest); stepsVector->addElement(step); break; } aToken = tokens.elementAt(++i); const XMLCh* localPart = stringPool->getValueForId(aToken); QName aQName(prefix, localPart, uri, fMemoryManager); // build step XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(&aQName); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::CHILD, nodeTest); stepsVector->addElement(step); firstTokenOfLocationPath = false; break; } case XercesXPath::EXPRTOKEN_PERIOD: { // build step XercesNodeTest* nodeTest = new (fMemoryManager) XercesNodeTest(XercesNodeTest::NODE, fMemoryManager); XercesStep* step = new (fMemoryManager) XercesStep(XercesStep::SELF, nodeTest); stepsVector->addElement(step); if (firstTokenOfLocationPath && i+1 < tokenCount) { aToken = tokens.elementAt(i+1); if (aToken == XercesXPath::EXPRTOKEN_OPERATOR_DOUBLE_SLASH){ if (++i == tokenCount - 1) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_ExpectedStep2, fMemoryManager); } if (i+1 < tokenCount) { aToken = tokens.elementAt(i+1); if (aToken == XercesXPath::EXPRTOKEN_OPERATOR_SLASH) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoForwardSlash, fMemoryManager); } } // build step nodeTest = new (fMemoryManager) XercesNodeTest(XercesNodeTest::NODE, fMemoryManager); step = new (fMemoryManager) XercesStep(XercesStep::DESCENDANT, nodeTest); stepsVector->addElement(step); } } firstTokenOfLocationPath=false; break; } case XercesXPath::EXPRTOKEN_OPERATOR_DOUBLE_SLASH: { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoDoubleForwardSlash, fMemoryManager); } case XercesXPath::EXPRTOKEN_OPERATOR_SLASH: { if (i == 0) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoForwardSlashAtStart, fMemoryManager); } // keep on truckin' if (firstTokenOfLocationPath) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoSelectionOfRoot, fMemoryManager); } if (i == tokenCount - 1) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_ExpectedStep3, fMemoryManager); } firstTokenOfLocationPath=false; break; } default: firstTokenOfLocationPath=false; } } int stepsSize = stepsVector->size(); if (stepsSize == 0) { if (!fLocationPaths || fLocationPaths->size() == 0) { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_EmptyExpr, fMemoryManager); } else { ThrowXMLwithMemMgr(XPathException, XMLExcepts::XPath_NoUnionAtEnd, fMemoryManager); } } fLocationPaths->addElement(new (fMemoryManager) XercesLocationPath(stepsVector)); janSteps.orphan();}/*** * Support for Serialization/De-serialization ***/IMPL_XSERIALIZABLE_TOCREATE(XercesXPath)void XercesXPath::serialize(XSerializeEngine& serEng){ if (serEng.isStoring()) { serEng<<fEmptyNamespaceId; serEng.writeString(fExpression); /*** * Serialize RefVectorOf<XercesLocationPath>* fLocationPaths; ***/ XTemplateSerializer::storeObject(fLocationPaths, serEng); } else { serEng>>fEmptyNamespaceId; serEng.readString(fExpression); /*** * Deserialize RefVectorOf<XercesLocationPath>* fLocationPaths; ***/ XTemplateSerializer::loadObject(&fLocationPaths, 8, true, serEng); }}XercesXPath::XercesXPath(MemoryManager* const manager):fEmptyNamespaceId(0),fExpression(0),fLocationPaths(0),fMemoryManager(manager){}// ---------------------------------------------------------------------------// XPathScanner: Constructors and Destructor
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?