From 266b408ee53b9bc7db2b1a5c92e62adfd2af6def Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 26 Jun 2014 20:26:32 +0200 Subject: 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 --- CMakeLists.txt | 1 + src/function/read_directory.h | 2 +- src/function/read_file.cc | 60 +++++++++++++++++++++++++------------ src/function/read_file.h | 2 +- src/function/transform.cc | 16 +++++----- src/function/transform.h | 4 +-- src/function/write_file.cc | 62 +++++++++++++++++++++++++++++++++++++++ src/function/write_file.h | 28 ++++++++++++++++++ src/plattform_guard.cc | 7 +++++ test/transform/transformation.xsl | 56 ++++++++++++++++++++++++++++++----- 10 files changed, 199 insertions(+), 39 deletions(-) create mode 100644 src/function/write_file.cc create mode 100644 src/function/write_file.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1df6b5c..73e67b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ set( src/plattform_guard.cc src/transformation_facade.cc src/function/read_file.cc + src/function/write_file.cc src/function/read_directory.cc src/function/transform.cc src/function/external_text_formatter.cc 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 #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 #include -#include "boost/filesystem/fstream.hpp" +#include +#include #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 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(); + } } -inline std::string readPlainFile(const boost::filesystem::path& filePath) { +boost::optional readPlainFile( + const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); - return std::string( - (std::istreambuf_iterator(file)), - (std::istreambuf_iterator()) - ); + if ( file.is_open() ) { + return boost::make_optional( + std::string( + (std::istreambuf_iterator(file)), + (std::istreambuf_iterator()) + ) + ); + } else { + return boost::optional(); + } } } @@ -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 #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 #include +#include + #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 -#include "boost/filesystem.hpp" +#include #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 +#include +#include + +#include +#include + +#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("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" @@ -28,6 +29,12 @@ PlattformGuard::PlattformGuard(const std::vector& path): InputXSLT::FunctionReadFile(&this->include_resolver_) ); + xalan::XalanTransformer::installExternalFunctionGlobal( + customNamespace, + xalan::XalanDOMString("write-file"), + InputXSLT::FunctionWriteFile(&this->include_resolver_) + ); + xalan::XalanTransformer::installExternalFunctionGlobal( customNamespace, xalan::XalanDOMString("read-directory"), diff --git a/test/transform/transformation.xsl b/test/transform/transformation.xsl index 1cd424b..99933ba 100644 --- a/test/transform/transformation.xsl +++ b/test/transform/transformation.xsl @@ -11,24 +11,64 @@ + + + + + + + + + + + + + + + + + + + + - - InputXSLT:transform( - string($transformation), - string($target), - xalan:nodeset($parameters) - ) + + + + + - + + + + + + + + + + - + test.xsl test_actual.xml -- cgit v1.2.3