diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/function/read_directory.cc | 147 | ||||
-rw-r--r-- | src/function/read_file.cc | 90 | ||||
-rw-r--r-- | src/function/read_xml_file.cc | 86 | ||||
-rw-r--r-- | src/support/filesystem_context.cc | 11 | ||||
-rw-r--r-- | src/support/filesystem_context.h | 2 |
5 files changed, 186 insertions, 150 deletions
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<XMLCh>("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<XMLCh>("result")) + ); + + switch ( boost::filesystem::status(p).type() ) { + case boost::filesystem::regular_file: { + itemNode->setAttribute( + *XercesStringGuard<XMLCh>("type"), + *XercesStringGuard<XMLCh>("file") + ); + + break; + }; + case boost::filesystem::directory_file: { + itemNode->setAttribute( + *XercesStringGuard<XMLCh>("type"), + *XercesStringGuard<XMLCh>("directory") + ); + + break; + }; + default: { + itemNode->setAttribute( + *XercesStringGuard<XMLCh>("type"), + *XercesStringGuard<XMLCh>("misc") + ); + + break; + }; + } + + xercesc::DOMText* const textNode( + domDocument->createTextNode( + *XercesStringGuard<XMLCh>(p.filename().string()) + ) + ); + + itemNode->appendChild(textNode); + rootNode->appendChild(itemNode); + }); + } else { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard<XMLCh>("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<XMLCh>("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<XMLCh>("result")) - ); - - switch ( boost::filesystem::status(p).type() ) { - case boost::filesystem::regular_file: { - itemNode->setAttribute( - *XercesStringGuard<XMLCh>("type"), - *XercesStringGuard<XMLCh>("file") - ); - - break; - }; - case boost::filesystem::directory_file: { - itemNode->setAttribute( - *XercesStringGuard<XMLCh>("type"), - *XercesStringGuard<XMLCh>("directory") - ); - - break; - }; - default: { - itemNode->setAttribute( - *XercesStringGuard<XMLCh>("type"), - *XercesStringGuard<XMLCh>("misc") - ); - - break; - }; - } - - xercesc::DOMText* const textNode( - domDocument->createTextNode( - *XercesStringGuard<XMLCh>(p.filename().string()) - ) - ); - - itemNode->appendChild(textNode); - rootNode->appendChild(itemNode); - }); - } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); - - rootNode->appendChild(resultNode); - } - optionalCachedDocument = this->document_cache_->create( directoryPath.string(), - domDocument + constructDocument(fsContext, directoryPath) ); } diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 8fcc04b..ba94909 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -12,6 +12,8 @@ namespace { +using InputXSLT::XercesStringGuard; + inline std::string readFile(const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); @@ -21,6 +23,50 @@ inline std::string readFile(const boost::filesystem::path& filePath) { ); } +xercesc::DOMDocument* constructDocument( + const boost::filesystem::path& filePath +) { + xercesc::DOMDocument* const domDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard<XMLCh>("content"), + nullptr + ) + ); + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() + ); + + if ( boost::filesystem::is_regular_file(filePath) ) { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard<XMLCh>("result")) + ); + + resultNode->setAttribute( + *XercesStringGuard<XMLCh>("name"), + *XercesStringGuard<XMLCh>(filePath.filename().string()) + ); + + xercesc::DOMText* const resultTextNode( + domDocument->createTextNode( + *XercesStringGuard<XMLCh>(readFile(filePath)) + ) + ); + + resultNode->appendChild(resultTextNode); + rootNode->appendChild(resultNode); + } else { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard<XMLCh>("error")) + ); + + rootNode->appendChild(resultNode); + } + + return domDocument; +} + } namespace InputXSLT { @@ -34,10 +80,10 @@ xalan::XObjectPtr FunctionReadFile::execute( const xalan::XObjectPtr argument, const xalan::Locator* locator ) const { - const FilesystemContext fs_context(locator); + const FilesystemContext fsContext(locator); const boost::filesystem::path filePath( - fs_context.resolve(argument->str()) + fsContext.resolve(argument->str()) ); DomDocumentCache::optional_item optionalCachedDocument( @@ -45,47 +91,9 @@ xalan::XObjectPtr FunctionReadFile::execute( ); if ( !optionalCachedDocument.first ) { - xercesc::DOMDocument* const domDocument( - xercesc::DOMImplementation::getImplementation()->createDocument( - nullptr, - *XercesStringGuard<XMLCh>("content"), - nullptr - ) - ); - - xercesc::DOMNode* const rootNode( - domDocument->getDocumentElement() - ); - - if ( boost::filesystem::is_regular_file(filePath) ) { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("result")) - ); - - resultNode->setAttribute( - *XercesStringGuard<XMLCh>("name"), - *XercesStringGuard<XMLCh>(filePath.filename().string()) - ); - - xercesc::DOMText* const resultTextNode( - domDocument->createTextNode( - *XercesStringGuard<XMLCh>(readFile(filePath)) - ) - ); - - resultNode->appendChild(resultTextNode); - rootNode->appendChild(resultNode); - } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); - - rootNode->appendChild(resultNode); - } - optionalCachedDocument = this->document_cache_->create( filePath.string(), - domDocument + constructDocument(filePath) ); } diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index 0fa21fe..a4e3e3c 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -13,6 +13,8 @@ namespace { +using InputXSLT::XercesStringGuard; + inline xercesc::DOMNode* importDocumentElement( const boost::filesystem::path& filePath, xercesc::DOMDocument* const domDocument @@ -27,6 +29,48 @@ inline xercesc::DOMNode* importDocumentElement( ); } +xercesc::DOMDocument* constructDocument( + const boost::filesystem::path& filePath +) { + xercesc::DOMDocument* const domDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard<XMLCh>("content"), + nullptr + ) + ); + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() + ); + + if ( boost::filesystem::is_regular_file(filePath) ) { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard<XMLCh>("result")) + ); + + resultNode->setAttribute( + *XercesStringGuard<XMLCh>("name"), + *XercesStringGuard<XMLCh>(filePath.filename().string()) + ); + + xercesc::DOMNode* const resultTreeNode( + importDocumentElement(filePath, domDocument) + ); + + resultNode->appendChild(resultTreeNode); + rootNode->appendChild(resultNode); + } else { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard<XMLCh>("error")) + ); + + rootNode->appendChild(resultNode); + } + + return domDocument; +} + } namespace InputXSLT { @@ -40,10 +84,10 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( const xalan::XObjectPtr argument, const xalan::Locator* locator ) const { - const FilesystemContext fs_context(locator); + const FilesystemContext fsContext(locator); const boost::filesystem::path filePath( - fs_context.resolve(argument->str()) + fsContext.resolve(argument->str()) ); DomDocumentCache::optional_item optionalCachedDocument( @@ -51,45 +95,9 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( ); if ( !optionalCachedDocument.first ) { - xercesc::DOMDocument* const domDocument( - xercesc::DOMImplementation::getImplementation()->createDocument( - nullptr, - *XercesStringGuard<XMLCh>("content"), - nullptr - ) - ); - - xercesc::DOMNode* const rootNode( - domDocument->getDocumentElement() - ); - - if ( boost::filesystem::is_regular_file(filePath) ) { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("result")) - ); - - resultNode->setAttribute( - *XercesStringGuard<XMLCh>("name"), - *XercesStringGuard<XMLCh>(filePath.filename().string()) - ); - - xercesc::DOMNode* const resultTreeNode( - importDocumentElement(filePath, domDocument) - ); - - resultNode->appendChild(resultTreeNode); - rootNode->appendChild(resultNode); - } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); - - rootNode->appendChild(resultNode); - } - optionalCachedDocument = this->document_cache_->create( filePath.string(), - domDocument + constructDocument(filePath) ); } diff --git a/src/support/filesystem_context.cc b/src/support/filesystem_context.cc index 32646be..418d159 100644 --- a/src/support/filesystem_context.cc +++ b/src/support/filesystem_context.cc @@ -36,11 +36,9 @@ boost::filesystem::path FilesystemContext::resolve( } void FilesystemContext::iterate( - const std::string& path, + const boost::filesystem::path& directory, std::function<void(const boost::filesystem::path&)> func ) const { - const boost::filesystem::path directory(this->resolve(path)); - if ( boost::filesystem::is_directory(directory) ) { for ( boost::filesystem::directory_iterator iter(directory); iter != boost::filesystem::directory_iterator(); @@ -54,6 +52,13 @@ void FilesystemContext::iterate( } void FilesystemContext::iterate( + const std::string& path, + std::function<void(const boost::filesystem::path&)> func +) const { + this->iterate(this->resolve(path), func); +} + +void FilesystemContext::iterate( const xalan::XalanDOMString& path, std::function<void(const boost::filesystem::path&)> func ) const { diff --git a/src/support/filesystem_context.h b/src/support/filesystem_context.h index f743c89..05d5862 100644 --- a/src/support/filesystem_context.h +++ b/src/support/filesystem_context.h @@ -20,6 +20,8 @@ class FilesystemContext { boost::filesystem::path resolve(const std::string&) const; boost::filesystem::path resolve(const xalan::XalanDOMString&) const; + void iterate(const boost::filesystem::path&, + std::function<void(const boost::filesystem::path&)>) const; void iterate(const std::string&, std::function<void(const boost::filesystem::path&)>) const; void iterate(const xalan::XalanDOMString&, |