#include "read_file.h" #include #include #include #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.h" #include "support/dom/result_node_facade.h" namespace { inline std::string readFile(const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); return std::string( (std::istreambuf_iterator(file)), (std::istreambuf_iterator()) ); } } namespace InputXSLT { xercesc::DOMDocument* FunctionReadFile::constructDocument( const FilesystemContext& fsContext, const FunctionBase::parameter_tuple& parameters ) { const std::string& rawPath = std::get<0>(parameters); boost::filesystem::path filePath(fsContext.resolve(rawPath)); if ( !(boost::filesystem::exists(filePath) && boost::filesystem::is_regular_file(filePath)) ) { if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) { filePath = *resolvedPath; } } xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( nullptr, *XercesStringGuard("content"), nullptr ) ); xercesc::DOMNode* const rootNode( domDocument->getDocumentElement() ); if ( boost::filesystem::is_regular_file(filePath) ) { ResultNodeFacade result(domDocument, rootNode, "result"); result.setAttribute("name", filePath.filename().string()); result.setContent(readFile(filePath)); } else { ResultNodeFacade result(domDocument, rootNode, "error"); } return domDocument; } }