aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_file.cc
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-04-26 20:06:17 +0200
committerAdrian Kummerländer2014-04-26 20:06:17 +0200
commiteba5513f82f0e1458543e8646db0317d7211cf96 (patch)
tree67b0238322b10d34c5caf45200aae1cbd548a7b0 /src/function/read_file.cc
parent7e7b112e6c00bcce0b339652748079cf7c9f1430 (diff)
downloadInputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.tar
InputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.tar.gz
InputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.tar.bz2
InputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.tar.lz
InputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.tar.xz
InputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.tar.zst
InputXSLT-eba5513f82f0e1458543e8646db0317d7211cf96.zip
Converted external read-file function output into a node set
* A call to _read-file_ now returns a _content_ and _status_ node ** this enables XSL transformations to easily react to io related errors * fixed bug in path resolution ** boost::filesystem _canonical_ method is throwing and exception when the given path does not exist, this pulled down the whole stack ** replaced call to _canonical_ with call to _absolute_ which does not have this requirement
Diffstat (limited to 'src/function/read_file.cc')
-rw-r--r--src/function/read_file.cc89
1 files changed, 77 insertions, 12 deletions
diff --git a/src/function/read_file.cc b/src/function/read_file.cc
index ac6225a..6f02702 100644
--- a/src/function/read_file.cc
+++ b/src/function/read_file.cc
@@ -1,13 +1,23 @@
#include "read_file.h"
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMImplementation.hpp>
+#include <xercesc/dom/DOMElement.hpp>
+#include <xercesc/dom/DOMText.hpp>
+#include <xercesc/util/XMLString.hpp>
+
#include "boost/filesystem/fstream.hpp"
#include <fstream>
+#include "support/xerces_string_guard.h"
+#include "support/utility.h"
+
namespace InputXSLT {
FunctionReadFile::FunctionReadFile(const FilesystemContext& context):
- fs_context_(context) { }
+ fs_context_(context),
+ document_cache_(std::make_shared<DomDocumentCache>()) { }
xalan::XObjectPtr FunctionReadFile::execute(
xalan::XPathExecutionContext& executionContext,
@@ -19,22 +29,77 @@ xalan::XObjectPtr FunctionReadFile::execute(
this->fs_context_.resolve(argument->str())
);
- if ( boost::filesystem::is_regular_file(filePath) ) {
- boost::filesystem::ifstream file(filePath);
+ DomDocumentCache::item* const cachedDocument(
+ this->document_cache_->get(xalanToString(argument->str()))
+ );
- const std::string fileContent(
- (std::istreambuf_iterator<char>(file)),
- (std::istreambuf_iterator<char>())
+ if ( !cachedDocument->isFinalized() ) {
+ xercesc::DOMDocument* const domDocument(
+ cachedDocument->getXercesDocument()
);
- return executionContext.getXObjectFactory().createString(
- xalan::XalanDOMString(fileContent.data())
- );
- } else {
- return executionContext.getXObjectFactory().createString(
- xalan::XalanDOMString("io error")
+ xercesc::DOMNode* const rootNode(
+ domDocument->getDocumentElement()
);
+
+ if ( boost::filesystem::is_regular_file(filePath) ) {
+ boost::filesystem::ifstream file(filePath);
+
+ const std::string fileContent(
+ (std::istreambuf_iterator<char>(file)),
+ (std::istreambuf_iterator<char>())
+ );
+
+ xercesc::DOMElement* const contentNode(
+ domDocument->createElement(*XercesStringGuard("content"))
+ );
+
+ xercesc::DOMText* const contentTextNode(
+ domDocument->createTextNode(
+ *XercesStringGuard(fileContent)
+ )
+ );
+
+ xercesc::DOMElement* const resultNode(
+ domDocument->createElement(*XercesStringGuard("status"))
+ );
+
+ xercesc::DOMText* const resultTextNode(
+ domDocument->createTextNode(
+ *XercesStringGuard("successful")
+ )
+ );
+
+ contentNode->appendChild(contentTextNode);
+ resultNode->appendChild(resultTextNode);
+
+ rootNode->appendChild(contentNode);
+ rootNode->appendChild(resultNode);
+ } else {
+ xercesc::DOMElement* const resultNode(
+ domDocument->createElement(*XercesStringGuard("status"))
+ );
+
+ xercesc::DOMText* const resultTextNode(
+ domDocument->createTextNode(
+ *XercesStringGuard("error")
+ )
+ );
+
+ resultNode->appendChild(resultTextNode);
+ rootNode->appendChild(resultNode);
+ }
}
+
+ xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList(
+ executionContext
+ );
+
+ nodeList->addNodes(
+ *cachedDocument->getXalanDocument()->getDocumentElement()->getChildNodes()
+ );
+
+ return executionContext.getXObjectFactory().createNodeSet(nodeList);
}
FunctionReadFile* FunctionReadFile::clone(