From 261170cc5b6661106877dc2611dab281f4d91348 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 3 May 2014 22:04:16 +0200 Subject: Revamped DomDocumentCache with regard to thread safety * DomDocumentCache::item class now is _finalized_ by default and doesn't perform document instantiation, just lifetime management ** xercesc::DOMDocument is instantiated inside the external function implementations and committed to the document cache for conversion to a xalan document * added mutex with scoped lock to prevent concurrent write access to the std::unordered_map contained withing DomDocumentCache * functionality of the _get_ member method was split into _get_ and _create_ ** added typedef for std::pair specialization type "optional_item" that functions as the return value of _create_ and _get_ * "locator->getSystemId()" was leaking memory as xerces doesn't manage the lifetime of the returned heap-allocated char array ** analog to XMLCh* strings ** transformed XercesStringGuard into template class to be instantiated on either XMLCh or char --- src/function/read_xml_file.cc | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'src/function/read_xml_file.cc') diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index 608e71a..dc98fc7 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -42,7 +42,11 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( ) const { const FilesystemContext fs_context( boost::filesystem::path( - xercesc::XMLString::transcode(locator->getSystemId() + 7) + *XercesStringGuard( + xercesc::XMLString::transcode( + locator->getSystemId() + 7 + ) + ) ).parent_path().string() ); @@ -50,13 +54,17 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( fs_context.resolve(argument->str()) ); - DomDocumentCache::item* const cachedDocument( + DomDocumentCache::optional_item optionalCachedDocument( this->document_cache_->get(filePath.string()) ); - if ( !cachedDocument->isFinalized() ) { + if ( !optionalCachedDocument.first ) { xercesc::DOMDocument* const domDocument( - cachedDocument->getXercesDocument() + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard("content"), + nullptr + ) ); xercesc::DOMNode* const rootNode( @@ -65,12 +73,12 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( if ( boost::filesystem::is_regular_file(filePath) ) { xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard("result")) + domDocument->createElement(*XercesStringGuard("result")) ); resultNode->setAttribute( - *XercesStringGuard("name"), - *XercesStringGuard(filePath.filename().string()) + *XercesStringGuard("name"), + *XercesStringGuard(filePath.filename().string()) ); xercesc::DOMNode* const resultTreeNode( @@ -81,11 +89,16 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( rootNode->appendChild(resultNode); } else { xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard("error")) + domDocument->createElement(*XercesStringGuard("error")) ); rootNode->appendChild(resultNode); } + + optionalCachedDocument = this->document_cache_->create( + filePath.string(), + domDocument + ); } xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( @@ -93,7 +106,9 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( ); nodeList->addNodes( - *cachedDocument->getXalanDocument()->getDocumentElement()->getChildNodes() + *optionalCachedDocument.second->getXalanDocument() + ->getDocumentElement() + ->getChildNodes() ); return executionContext.getXObjectFactory().createNodeSet(nodeList); -- cgit v1.2.3