#include "read_file.h" #include #include #include #include #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.h" #include "support/filesystem_context.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 { FunctionReadFile::FunctionReadFile(): document_cache_(std::make_shared()) { } xalan::XObjectPtr FunctionReadFile::execute( xalan::XPathExecutionContext& executionContext, xalan::XalanNode*, const xalan::XObjectPtr argument, const xalan::Locator* locator ) const { const FilesystemContext fs_context(locator); const boost::filesystem::path filePath( fs_context.resolve(argument->str()) ); DomDocumentCache::optional_item optionalCachedDocument( this->document_cache_->get(filePath.string()) ); 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_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); } optionalCachedDocument = this->document_cache_->create( filePath.string(), domDocument ); } xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( executionContext ); nodeList->addNodes( *optionalCachedDocument.second->getXalanDocument() ->getDocumentElement() ->getChildNodes() ); return executionContext.getXObjectFactory().createNodeSet(nodeList); } FunctionReadFile* FunctionReadFile::clone( xalan::MemoryManager& manager) const { return xalan::XalanCopyConstruct(manager, *this); } const xalan::XalanDOMString& FunctionReadFile::getError( xalan::XalanDOMString& result) const { result.assign("The read-file() function expects one argument of type string."); return result; } }