diff options
author | Adrian Kummerlaender | 2014-06-26 20:26:32 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2014-06-26 20:26:32 +0200 |
commit | 266b408ee53b9bc7db2b1a5c92e62adfd2af6def (patch) | |
tree | b84551406742ba892824a5770ae8fb8ec3a6a510 | |
parent | 7b872121000d4db4026d0c90fcb95a10f1e43694 (diff) | |
download | InputXSLT-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
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/function/read_directory.h | 2 | ||||
-rw-r--r-- | src/function/read_file.cc | 60 | ||||
-rw-r--r-- | src/function/read_file.h | 2 | ||||
-rw-r--r-- | src/function/transform.cc | 16 | ||||
-rw-r--r-- | src/function/transform.h | 4 | ||||
-rw-r--r-- | src/function/write_file.cc | 62 | ||||
-rw-r--r-- | src/function/write_file.h | 28 | ||||
-rw-r--r-- | src/plattform_guard.cc | 7 | ||||
-rw-r--r-- | test/transform/transformation.xsl | 56 |
10 files changed, 199 insertions, 39 deletions
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 <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_) ); 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 @@ -12,23 +12,63 @@ <xsl:template name="transformer"> <xsl:param name="transformation"/> + <xsl:param name="parameters"/> + + <xsl:variable name="stylesheet" select=" + InputXSLT:read-file(string($transformation)) + "/> + + <xsl:choose> + <xsl:when test="$stylesheet/self::file/@result = 'success'"> + <transformation result="success"> + <xsl:copy-of select=" + InputXSLT:transform( + $stylesheet/self::file/*, + xalan:nodeset($parameters) + )/self::transformation/text() + "/> + </transformation> + </xsl:when> + <xsl:otherwise> + <transformation result="error"> + <xsl:copy-of select="$stylesheet/self::file/*"/> + </transformation> + </xsl:otherwise> + </xsl:choose> +</xsl:template> + +<xsl:template name="generator"> + <xsl:param name="transformation"/> <xsl:param name="target"/> <xsl:param name="parameters"/> - <xsl:variable name="command"> - InputXSLT:transform( - string($transformation), - string($target), - xalan:nodeset($parameters) - ) + <xsl:variable name="result"> + <xsl:call-template name="transformer"> + <xsl:with-param name="transformation" select="$transformation"/> + <xsl:with-param name="parameters" select="$parameters"/> + </xsl:call-template> </xsl:variable> - <xsl:copy-of select="dyn:evaluate($command)"/> + <xsl:choose> + <xsl:when test="xalan:nodeset($result)/transformation/@result = 'success'"> + <xsl:variable name="writeResult" select=" + InputXSLT:write-file( + string($target), + xalan:nodeset($result)/transformation/text() + ) + "/> + + <transformation result="success" target="{$target}"/> + </xsl:when> + <xsl:otherwise> + <transformation result="error" target="{$target}"/> + </xsl:otherwise> + </xsl:choose> </xsl:template> <xsl:template name="implementation"> <xsl:variable name="result"> - <xsl:call-template name="transformer"> + <xsl:call-template name="generator"> <xsl:with-param name="transformation">test.xsl</xsl:with-param> <xsl:with-param name="target">test_actual.xml</xsl:with-param> <xsl:with-param name="parameters"> |