From 78d3873061f1a974da4d0ccdcc1778c6a11139e8 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Thu, 24 Apr 2014 22:18:45 +0200 Subject: Added XercesStringGuard scope-guard to manage XMLCh * xercesc requires XMLCh* strings to be hand-allocated and released using the XMLString class * XercesStringGuard works as a scope-guard for XMLCh* string lifetime and greatly simplifies xerces DOM construction --- src/function/read_directory.cc | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'src/function') diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index 22653b6..245f311 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -6,6 +6,8 @@ #include #include +#include "support/xerces_string_guard.h" + namespace InputXSLT { FunctionReadDirectory::FunctionReadDirectory(const FilesystemContext& context): @@ -34,31 +36,27 @@ xalan::XObjectPtr FunctionReadDirectory::execute( this->fs_context_.iterate( arguments[0]->str(), [&domDocument, &rootNode](const boost::filesystem::path& p) { - XMLCh* buffer = xercesc::XMLString::transcode("item"); - xercesc::DOMElement* const itemNode = domDocument->createElement(buffer); - xercesc::XMLString::release(&buffer); - - buffer = xercesc::XMLString::transcode("type"); + xercesc::DOMElement* const itemNode( + domDocument->createElement(*XercesStringGuard("item")) + ); if ( boost::filesystem::is_regular_file(p) ) { - XMLCh* valueBuffer = xercesc::XMLString::transcode("file"); - - itemNode->setAttribute(buffer, valueBuffer); - - xercesc::XMLString::release(&valueBuffer); + itemNode->setAttribute( + *XercesStringGuard("type"), + *XercesStringGuard("file") + ); } else if ( boost::filesystem::is_directory(p) ) { - XMLCh* valueBuffer = xercesc::XMLString::transcode("directory"); - - itemNode->setAttribute(buffer, valueBuffer); - - xercesc::XMLString::release(&valueBuffer); + itemNode->setAttribute( + *XercesStringGuard("type"), + *XercesStringGuard("directory") + ); } - xercesc::XMLString::release(&buffer); - - buffer = xercesc::XMLString::transcode(p.filename().string().data()); - xercesc::DOMText* const textNode = domDocument->createTextNode(buffer); - xercesc::XMLString::release(&buffer); + xercesc::DOMText* const textNode( + domDocument->createTextNode( + *XercesStringGuard(p.filename().string()) + ) + ); itemNode->appendChild(textNode); rootNode->appendChild(itemNode); -- cgit v1.2.3