From 266b408ee53b9bc7db2b1a5c92e62adfd2af6def Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 26 Jun 2014 20:26:32 +0200 Subject: Implemented basic external "write-file" function * accepts a path parameter and the content to be written * removed target parameter form FunctionTransform ** transformation result is now returned as a string *** nodeset return value is planned ** e.g. writing the result to the fs is optional and has to be achieved using FunctionWriteFile * changed "transform" test case accordingly * this marks a paradigm shift and is the continuation of the changes described in 741a70f ** InputXSLT now also implements a output function * added basic io error handling to FunctionReadFile --- src/function/read_file.cc | 60 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 19 deletions(-) (limited to 'src/function/read_file.cc') diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 96e677f..f27a38e 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -6,7 +6,8 @@ #include #include -#include "boost/filesystem/fstream.hpp" +#include +#include #include "support/xerces_string_guard.h" #include "support/dom/result_node_facade.h" @@ -18,7 +19,7 @@ inline bool isXmlFile(const boost::filesystem::path& filePath) { filePath.extension() == ".xsl"; } -inline xercesc::DOMNode* readXmlFile( +boost::optional readXmlFile( const boost::filesystem::path& filePath, xercesc::DOMDocument* const domDocument ) { @@ -29,19 +30,32 @@ inline xercesc::DOMNode* readXmlFile( xercesc::XercesDOMParser parser; parser.parse(file); - return domDocument->importNode( - parser.getDocument()->getDocumentElement(), - true - ); + if ( parser.getErrorCount() == 0 ) { + return boost::make_optional( + domDocument->importNode( + parser.getDocument()->getDocumentElement(), + true + ) + ); + } else { + return boost::optional(); + } } -inline std::string readPlainFile(const boost::filesystem::path& filePath) { +boost::optional readPlainFile( + const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); - return std::string( - (std::istreambuf_iterator(file)), - (std::istreambuf_iterator()) - ); + if ( file.is_open() ) { + return boost::make_optional( + std::string( + (std::istreambuf_iterator(file)), + (std::istreambuf_iterator()) + ) + ); + } else { + return boost::optional(); + } } } @@ -70,18 +84,26 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument( if ( isXmlFile(filePath) ) { result.setAttribute("type", "xml"); - result.setContent( - readXmlFile(filePath.string(), domDocument) - ); + if ( auto importedNode = readXmlFile(filePath, domDocument) ) { + result.setContent(*importedNode); + + result.setAttribute("result", "success"); + } else { + result.setAttribute("result", "error"); + } } else { result.setAttribute("type", "plain"); - result.setContent( - readPlainFile(filePath) - ); - } + if ( auto plainContent = readPlainFile(filePath) ) { + result.setContent(*plainContent); - result.setAttribute("result", "success"); + result.setAttribute("result", "success"); + } else { + result.setAttribute("result", "error"); + } + + result.setAttribute("result", "success"); + } } catch ( const xercesc::DOMException& exception ) { result.setAttribute("result", "error"); -- cgit v1.2.3