#include "read_file.h" #include #include #include #include #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.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&, const boost::filesystem::path& filePath ) { 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) ) { xercesc::DOMElement* const resultNode( domDocument->createElement(*XercesStringGuard("result")) ); resultNode->setAttribute( *XercesStringGuard("name"), *XercesStringGuard(filePath.filename().string()) ); xercesc::DOMText* const resultTextNode( domDocument->createTextNode( *XercesStringGuard(readFile(filePath)) ) ); resultNode->appendChild(resultTextNode); rootNode->appendChild(resultNode); } else { xercesc::DOMElement* const resultNode( domDocument->createElement(*XercesStringGuard("error")) ); rootNode->appendChild(resultNode); } return domDocument; } }