aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/function/read_directory.h2
-rw-r--r--src/function/read_file.cc60
-rw-r--r--src/function/read_file.h2
-rw-r--r--src/function/transform.cc16
-rw-r--r--src/function/transform.h4
-rw-r--r--src/function/write_file.cc62
-rw-r--r--src/function/write_file.h28
-rw-r--r--src/plattform_guard.cc7
8 files changed, 150 insertions, 31 deletions
diff --git a/src/function/read_directory.h b/src/function/read_directory.h
index 41976d5..898132a 100644
--- a/src/function/read_directory.h
+++ b/src/function/read_directory.h
@@ -1,7 +1,7 @@
#ifndef INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_
#define INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_
-#include "boost/filesystem.hpp"
+#include <boost/filesystem.hpp>
#include "base.h"
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");
diff --git a/src/function/read_file.h b/src/function/read_file.h
index 9c2185a..fdd70ac 100644
--- a/src/function/read_file.h
+++ b/src/function/read_file.h
@@ -1,7 +1,7 @@
#ifndef INPUTXSLT_SRC_FUNCTION_READ_FILE_H_
#define INPUTXSLT_SRC_FUNCTION_READ_FILE_H_
-#include "boost/filesystem.hpp"
+#include <boost/filesystem.hpp>
#include "base.h"
diff --git a/src/function/transform.cc b/src/function/transform.cc
index 3ab6475..9cf806f 100644
--- a/src/function/transform.cc
+++ b/src/function/transform.cc
@@ -4,6 +4,8 @@
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMElement.hpp>
+#include <sstream>
+
#include "transformation_facade.h"
#include "support/xerces_string_guard.h"
#include "support/dom/result_node_facade.h"
@@ -30,7 +32,6 @@ namespace InputXSLT {
xercesc::DOMDocument* FunctionTransform::constructDocument(
xalan::XSLTInputSource transformationSource,
- boost::filesystem::path targetPath,
xalan::XObjectPtr parameterObject
) {
xercesc::DOMDocument* const domDocument(
@@ -47,22 +48,23 @@ xercesc::DOMDocument* FunctionTransform::constructDocument(
ResultNodeFacade result(domDocument, rootNode, "transformation");
- result.setAttribute(
- "target",
- targetPath.filename().string()
- );
-
if ( auto transformation = TransformationFacade::try_create(
handleErrors(result),
transformationSource,
this->include_resolver_
) ) {
try {
+ std::stringstream targetStream;
+
transformation->generate(
- targetPath.string(),
+ targetStream,
parameterObject
);
+ result.setContent(
+ targetStream.str()
+ );
+
result.setAttribute("result", "success");
}
catch (const ErrorCapacitor::exception& exception) {
diff --git a/src/function/transform.h b/src/function/transform.h
index 516233f..ee88529 100644
--- a/src/function/transform.h
+++ b/src/function/transform.h
@@ -3,7 +3,7 @@
#include <xalanc/XSLT/XSLTInputSource.hpp>
-#include "boost/filesystem.hpp"
+#include <boost/filesystem.hpp>
#include "base.h"
@@ -12,7 +12,6 @@ namespace InputXSLT {
class FunctionTransform : public FunctionBase<
FunctionTransform,
xalan::XSLTInputSource,
- boost::filesystem::path,
xalan::XObjectPtr
> {
public:
@@ -23,7 +22,6 @@ class FunctionTransform : public FunctionBase<
xercesc::DOMDocument* constructDocument(
xalan::XSLTInputSource,
- boost::filesystem::path,
xalan::XObjectPtr
);
diff --git a/src/function/write_file.cc b/src/function/write_file.cc
new file mode 100644
index 0000000..39495d8
--- /dev/null
+++ b/src/function/write_file.cc
@@ -0,0 +1,62 @@
+#include "write_file.h"
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMImplementation.hpp>
+#include <xercesc/dom/DOMElement.hpp>
+
+#include <boost/filesystem.hpp>
+#include <boost/filesystem/fstream.hpp>
+
+#include "support/xerces_string_guard.h"
+#include "support/dom/result_node_facade.h"
+
+namespace {
+
+bool writeFile(
+ const boost::filesystem::path& filePath,
+ const std::string& content
+) {
+ boost::filesystem::ofstream file(filePath);
+
+ if ( file.is_open() ) {
+ file << content << std::endl;
+
+ return true;
+ } else {
+ return false;
+ }
+}
+
+}
+
+namespace InputXSLT {
+
+xercesc::DOMDocument* FunctionWriteFile::constructDocument(
+ boost::filesystem::path filePath,
+ std::string content
+) {
+ 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 ( writeFile(filePath, content) ) {
+ result.setAttribute("result", "success");
+ } else {
+ result.setAttribute("result", "error");
+ }
+
+ return domDocument;
+}
+
+}
diff --git a/src/function/write_file.h b/src/function/write_file.h
new file mode 100644
index 0000000..2260055
--- /dev/null
+++ b/src/function/write_file.h
@@ -0,0 +1,28 @@
+#ifndef INPUTXSLT_SRC_FUNCTION_WRITE_FILE_H_
+#define INPUTXSLT_SRC_FUNCTION_WRITE_FILE_H_
+
+#include "base.h"
+
+namespace InputXSLT {
+
+class FunctionWriteFile : public FunctionBase<
+ FunctionWriteFile,
+ boost::filesystem::path,
+ std::string
+> {
+ public:
+ using FunctionBase::FunctionBase;
+
+ protected:
+ friend FunctionBase;
+
+ xercesc::DOMDocument* constructDocument(
+ boost::filesystem::path,
+ std::string
+ );
+
+};
+
+}
+
+#endif // INPUTXSLT_SRC_FUNCTION_WRITE_FILE_H_
diff --git a/src/plattform_guard.cc b/src/plattform_guard.cc
index e232d2b..cc3c9c0 100644
--- a/src/plattform_guard.cc
+++ b/src/plattform_guard.cc
@@ -7,6 +7,7 @@
#include "common.h"
#include "function/read_file.h"
+#include "function/write_file.h"
#include "function/read_directory.h"
#include "function/transform.h"
#include "function/external_text_formatter.h"
@@ -30,6 +31,12 @@ PlattformGuard::PlattformGuard(const std::vector<std::string>& path):
xalan::XalanTransformer::installExternalFunctionGlobal(
customNamespace,
+ xalan::XalanDOMString("write-file"),
+ InputXSLT::FunctionWriteFile(&this->include_resolver_)
+ );
+
+ xalan::XalanTransformer::installExternalFunctionGlobal(
+ customNamespace,
xalan::XalanDOMString("read-directory"),
InputXSLT::FunctionReadDirectory(&this->include_resolver_)
);