From bd24e3fe6335bc776400be6bcb44a0701ecc4133 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 4 May 2014 20:49:37 +0200 Subject: Moved DOM document construction logic into local functions * this change makes the underlying similarity between all currently implemented external functions obvious * they more or less only differ in their document construction logic ** i.e. there is a possibility for a second layer of abstraction between the classes derived from xalan::Function and the actual functionality * added boost::filesystem::path taking overload of the _iterate_ member method to FilesystemGuard ** this overload functions as the _master_ overload that is called by all other overloads which merely perform type conversion and path resolution ** this overload doesn't perform path resolution --- src/function/read_directory.cc | 147 ++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 67 deletions(-) (limited to 'src/function/read_directory.cc') diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index 09f8221..48ffa51 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -8,6 +8,83 @@ #include "support/xerces_string_guard.h" #include "support/filesystem_context.h" +namespace { + +using InputXSLT::XercesStringGuard; + +xercesc::DOMDocument* constructDocument( + const InputXSLT::FilesystemContext& fsContext, + const boost::filesystem::path& directoryPath +) { + xercesc::DOMDocument* const domDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard("content"), + nullptr + ) + ); + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() + ); + + if ( boost::filesystem::is_directory(directoryPath) ) { + fsContext.iterate( + directoryPath, + [&domDocument, &rootNode](const boost::filesystem::path& p) { + xercesc::DOMElement* const itemNode( + domDocument->createElement(*XercesStringGuard("result")) + ); + + switch ( boost::filesystem::status(p).type() ) { + case boost::filesystem::regular_file: { + itemNode->setAttribute( + *XercesStringGuard("type"), + *XercesStringGuard("file") + ); + + break; + }; + case boost::filesystem::directory_file: { + itemNode->setAttribute( + *XercesStringGuard("type"), + *XercesStringGuard("directory") + ); + + break; + }; + default: { + itemNode->setAttribute( + *XercesStringGuard("type"), + *XercesStringGuard("misc") + ); + + break; + }; + } + + xercesc::DOMText* const textNode( + domDocument->createTextNode( + *XercesStringGuard(p.filename().string()) + ) + ); + + itemNode->appendChild(textNode); + rootNode->appendChild(itemNode); + }); + } else { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard("error")) + ); + + rootNode->appendChild(resultNode); + } + + return domDocument; +} + +} + namespace InputXSLT { FunctionReadDirectory::FunctionReadDirectory(): @@ -19,10 +96,10 @@ xalan::XObjectPtr FunctionReadDirectory::execute( const xalan::XObjectPtr argument, const xalan::Locator* locator ) const { - const FilesystemContext fs_context(locator); + const FilesystemContext fsContext(locator); const boost::filesystem::path directoryPath( - fs_context.resolve(argument->str()) + fsContext.resolve(argument->str()) ); DomDocumentCache::optional_item optionalCachedDocument( @@ -30,73 +107,9 @@ xalan::XObjectPtr FunctionReadDirectory::execute( ); if ( !optionalCachedDocument.first ) { - xercesc::DOMDocument* const domDocument( - xercesc::DOMImplementation::getImplementation()->createDocument( - nullptr, - *XercesStringGuard("content"), - nullptr - ) - ); - - xercesc::DOMNode* const rootNode( - domDocument->getDocumentElement() - ); - - if ( boost::filesystem::is_directory(directoryPath) ) { - fs_context.iterate( - argument->str(), - [&domDocument, &rootNode](const boost::filesystem::path& p) { - xercesc::DOMElement* const itemNode( - domDocument->createElement(*XercesStringGuard("result")) - ); - - switch ( boost::filesystem::status(p).type() ) { - case boost::filesystem::regular_file: { - itemNode->setAttribute( - *XercesStringGuard("type"), - *XercesStringGuard("file") - ); - - break; - }; - case boost::filesystem::directory_file: { - itemNode->setAttribute( - *XercesStringGuard("type"), - *XercesStringGuard("directory") - ); - - break; - }; - default: { - itemNode->setAttribute( - *XercesStringGuard("type"), - *XercesStringGuard("misc") - ); - - break; - }; - } - - xercesc::DOMText* const textNode( - domDocument->createTextNode( - *XercesStringGuard(p.filename().string()) - ) - ); - - itemNode->appendChild(textNode); - rootNode->appendChild(itemNode); - }); - } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard("error")) - ); - - rootNode->appendChild(resultNode); - } - optionalCachedDocument = this->document_cache_->create( directoryPath.string(), - domDocument + constructDocument(fsContext, directoryPath) ); } -- cgit v1.2.3