aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_file.cc
diff options
context:
space:
mode:
authorAdrian Kummerlaender2014-06-15 15:14:46 +0200
committerAdrian Kummerlaender2014-06-15 15:14:46 +0200
commit32c65970263c65022f5278b568c07b63c3d5d64b (patch)
tree2efa72b5bc2288daec5d8a5c7ec0e8f7fcaa1365 /src/function/read_file.cc
parent58d3e1b8d03a10594e13658b50545fd714c04f6d (diff)
downloadInputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar
InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.gz
InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.bz2
InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.lz
InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.xz
InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.zst
InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.zip
Merged "read-xml-file" and "read-file" into "read-file"
* FunctionReadFile is now able to distinguish between XML files and plain text files ** it selects the appropriate course of action automatically ** reading the file as XML into the DOM or reading it as a string * the current selection criteria is the file extension ** I am thinking about trying to import every file into the DOM and using the result state of that action as selection criteria * Updated README.md and test cases accordingly
Diffstat (limited to 'src/function/read_file.cc')
-rw-r--r--src/function/read_file.cc53
1 files changed, 49 insertions, 4 deletions
diff --git a/src/function/read_file.cc b/src/function/read_file.cc
index 22e96d7..7603ad1 100644
--- a/src/function/read_file.cc
+++ b/src/function/read_file.cc
@@ -3,6 +3,8 @@
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMElement.hpp>
+#include <xercesc/parsers/XercesDOMParser.hpp>
+#include <xercesc/framework/LocalFileInputSource.hpp>
#include "boost/filesystem/fstream.hpp"
@@ -11,7 +13,28 @@
namespace {
-inline std::string readFile(const boost::filesystem::path& filePath) {
+inline bool isXmlFile(const boost::filesystem::path& filePath) {
+ return filePath.extension() == ".xml";
+}
+
+inline xercesc::DOMNode* readXmlFile(
+ const boost::filesystem::path& filePath,
+ xercesc::DOMDocument* const domDocument
+) {
+ const xercesc::LocalFileInputSource file(
+ *InputXSLT::XercesStringGuard<XMLCh>(filePath.string().data())
+ );
+
+ xercesc::XercesDOMParser parser;
+ parser.parse(file);
+
+ return domDocument->importNode(
+ parser.getDocument()->getDocumentElement(),
+ true
+ );
+}
+
+inline std::string readPlainFile(const boost::filesystem::path& filePath) {
boost::filesystem::ifstream file(filePath);
return std::string(
@@ -53,11 +76,33 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument(
result.setAttribute("path", filePath.string());
if ( boost::filesystem::is_regular_file(filePath) ) {
- result.setAttribute("result", "success");
+ try {
+ if ( isXmlFile(filePath) ) {
+ result.setAttribute("type", "xml");
+
+ result.setContent(
+ readXmlFile(filePath.string(), domDocument)
+ );
+ } else {
+ result.setAttribute("type", "plain");
+
+ result.setContent(
+ readPlainFile(filePath)
+ );
+ }
+
+ result.setAttribute("result", "success");
+ }
+ catch ( const xercesc::DOMException& exception ) {
+ result.setAttribute("result", "error");
- result.setContent(readFile(filePath));
+ result.setValueNode(
+ "error",
+ *XercesStringGuard<char>(exception.msg)
+ );
+ }
} else {
- result.setAttribute("result", "success");
+ result.setAttribute("result", "error");
}
return domDocument;