aboutsummaryrefslogtreecommitdiff
path: root/src/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/function')
-rw-r--r--src/function/read_file.cc53
-rw-r--r--src/function/read_xml_file.cc86
-rw-r--r--src/function/read_xml_file.h27
3 files changed, 49 insertions, 117 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;
diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc
deleted file mode 100644
index e4d256d..0000000
--- a/src/function/read_xml_file.cc
+++ /dev/null
@@ -1,86 +0,0 @@
-#include "read_xml_file.h"
-
-#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 "support/xerces_string_guard.h"
-#include "support/dom/result_node_facade.h"
-
-namespace {
-
-inline xercesc::DOMNode* importDocumentElement(
- const std::string& filePath,
- xercesc::DOMDocument* const domDocument
-) {
- const xercesc::LocalFileInputSource file(
- *InputXSLT::XercesStringGuard<XMLCh>(filePath.data())
- );
-
- xercesc::XercesDOMParser parser;
- parser.parse(file);
-
- return domDocument->importNode(
- parser.getDocument()->getDocumentElement(),
- true
- );
-}
-
-}
-
-namespace InputXSLT {
-
-xercesc::DOMDocument* FunctionReadXmlFile::constructDocument(
- const FilesystemContext& fsContext,
- std::string rawPath
-) {
- boost::filesystem::path filePath(fsContext.resolve(rawPath));
-
- if ( !(boost::filesystem::exists(filePath) &&
- boost::filesystem::is_regular_file(filePath)) ) {
- if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) {
- filePath = *resolvedPath;
- }
- }
-
- xercesc::DOMDocument* const domDocument(
- xercesc::DOMImplementation::getImplementation()->createDocument(
- nullptr,
- *XercesStringGuard<XMLCh>("content"),
- nullptr
- )
- );
-
- xercesc::DOMNode* const rootNode(
- domDocument->getDocumentElement()
- );
-
- ResultNodeFacade result(domDocument, rootNode, "file");
- result.setAttribute("path", filePath.string());
-
- if ( boost::filesystem::is_regular_file(filePath) ) {
- try {
- result.setContent(
- importDocumentElement(filePath.string(), domDocument)
- );
-
- result.setAttribute("result", "success");
- }
- catch ( const xercesc::DOMException& exception ) {
- result.setAttribute("result", "error");
-
- result.setValueNode(
- "error",
- *XercesStringGuard<char>(exception.msg)
- );
- }
- } else {
- result.setAttribute("result", "error");
- }
-
- return domDocument;
-}
-
-}
diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h
deleted file mode 100644
index 7fe949a..0000000
--- a/src/function/read_xml_file.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_
-#define INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_
-
-#include "base.h"
-
-namespace InputXSLT {
-
-class FunctionReadXmlFile : public FunctionBase<
- FunctionReadXmlFile,
- std::string
-> {
- public:
- using FunctionBase::FunctionBase;
-
- protected:
- friend FunctionBase;
-
- xercesc::DOMDocument* constructDocument(
- const FilesystemContext&,
- std::string
- );
-
-};
-
-}
-
-#endif // INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_