aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_file.cc
diff options
context:
space:
mode:
authorAdrian Kummerlaender2014-06-26 20:26:32 +0200
committerAdrian Kummerlaender2014-06-26 20:26:32 +0200
commit266b408ee53b9bc7db2b1a5c92e62adfd2af6def (patch)
treeb84551406742ba892824a5770ae8fb8ec3a6a510 /src/function/read_file.cc
parent7b872121000d4db4026d0c90fcb95a10f1e43694 (diff)
downloadInputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar
InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.gz
InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.bz2
InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.lz
InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.xz
InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.zst
InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.zip
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
Diffstat (limited to 'src/function/read_file.cc')
-rw-r--r--src/function/read_file.cc60
1 files changed, 41 insertions, 19 deletions
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 <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
-#include "boost/filesystem/fstream.hpp"
+#include <boost/optional.hpp>
+#include <boost/filesystem/fstream.hpp>
#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<xercesc::DOMNode*> 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<xercesc::DOMNode*>();
+ }
}
-inline std::string readPlainFile(const boost::filesystem::path& filePath) {
+boost::optional<std::string> readPlainFile(
+ const boost::filesystem::path& filePath) {
boost::filesystem::ifstream file(filePath);
- return std::string(
- (std::istreambuf_iterator<char>(file)),
- (std::istreambuf_iterator<char>())
- );
+ if ( file.is_open() ) {
+ return boost::make_optional(
+ std::string(
+ (std::istreambuf_iterator<char>(file)),
+ (std::istreambuf_iterator<char>())
+ )
+ );
+ } else {
+ return boost::optional<std::string>();
+ }
}
}
@@ -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");