From 5a54b5b8150d8bf1ac5db3ac688479b3aca6486d Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 23 Aug 2014 00:19:09 +0200 Subject: Replaced FunctionTransform by making the target of FunctionGenerate optional * if the target parameter is not provided FunctionGenerate now performs exactly the same functionality as FunctionTransform * added "boost::optional" specialization to the XObjectValue class * modified test cases accordingly * modified README.md accordingly --- CMakeLists.txt | 1 - README.md | 7 ++-- src/function/generate.cc | 73 ++++++++++++++++++++++++--------------- src/function/generate.h | 4 +-- src/function/transform.cc | 57 ------------------------------ src/function/transform.h | 31 ----------------- src/platform_guard.cc | 7 ---- src/support/type/xobject_value.cc | 11 ++++++ test/transform/transformation.xsl | 4 +-- 9 files changed, 63 insertions(+), 132 deletions(-) delete mode 100644 src/function/transform.cc delete mode 100644 src/function/transform.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 32d8db2..f933aeb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,6 @@ set( src/function/read_file.cc src/function/write_file.cc src/function/read_directory.cc - src/function/transform.cc src/function/generate.cc src/function/external_command.cc src/support/filesystem_context.cc diff --git a/README.md b/README.md index 0593bb5..b1e0fee 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ Contrary to popular opinion I actually like XSLT as a content transformation lan - external `read-file` function for read-only access to both plain and xml files - external `read-directory` function for read-only directory traversal -- external `transform` function for executing transformations inside transformations -- external `generate` function for executing transformations and committing the result directly to the filesystem +- external `generate` function for executing transformations and optionally committing the result directly to the filesystem - external `external-command` function for executing external commands such as text formatters and capturing their output - external `write-file` function for writing files @@ -22,8 +21,8 @@ The `test` directory contains black-box test cases for every external function p - [`InputXSLT:read-file` (plain)](test/read_file/transformation.xsl) - [`InputXSLT:read-file` (xml)](test/read_xml_file/transformation.xsl) - [`InputXSLT:read-directory`](test/read_directory/transformation.xsl) -- [`InputXSLT:transform`](test/transform/transformation.xsl) -- [`InputXSLT:generate`](test/generate/transformation.xsl) +- [`InputXSLT:generate`](test/transform/transformation.xsl) +- [`InputXSLT:generate` (fs)](test/generate/transformation.xsl) - [`InputXSLT:external-command` (text formatting)](test/external_text_formatter/transformation.xsl) (requires [markdown.pl](http://daringfireball.net/projects/markdown/)) - [`InputXSLT:write-file`](test/write_file/transformation.xsl) diff --git a/src/function/generate.cc b/src/function/generate.cc index f9cd449..b93c2f1 100644 --- a/src/function/generate.cc +++ b/src/function/generate.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -16,53 +17,69 @@ namespace InputXSLT { DomDocumentCache::document_ptr FunctionGenerate::constructDocument( const FilesystemContext&, - xalan::XSLTInputSource inputSource, - xalan::XSLTInputSource transformationSource, - boost::filesystem::path targetPath + xalan::XSLTInputSource inputSource, + xalan::XSLTInputSource transformationSource, + boost::optional targetPath ) const { DomDocumentCache::document_ptr domDocument( DomDocumentCache::createDocument("content") ); - ResultNodeFacade result(domDocument.get(), "generation"); - result.setAttribute("path", targetPath.string()); + ResultNodeFacade result(domDocument.get(), "generation"); + TransformerFacade transformer(this->include_resolver_); - boost::filesystem::create_directories(targetPath.parent_path()); - boost::filesystem::ofstream file(targetPath); + try { + if ( targetPath ) { + result.setAttribute("path", (*targetPath).string()); - if ( file.is_open() ) { - TransformerFacade transformer(this->include_resolver_); + boost::filesystem::create_directories( + (*targetPath).parent_path() + ); + + boost::filesystem::ofstream file(*targetPath); - try { - xalan::XalanStdOutputStream output(file); - xalan::XalanOutputStreamPrintWriter writer(output); - xalan::FormatterToXML targetFormatter(writer); + if ( file.is_open() ) { + xalan::XalanStdOutputStream output(file); + xalan::XalanOutputStreamPrintWriter writer(output); + xalan::FormatterToXML targetFormatter(writer); + + transformer.generate( + inputSource, + transformationSource, + targetFormatter + ); + } else { + result.setAttribute("result", "error"); + } + } else { + xalan::FormatterToXercesDOM targetFormatter( + domDocument.get(), + result.getResultElement() + ); transformer.generate( inputSource, transformationSource, targetFormatter ); - - result.setAttribute("result", "success"); } - catch (const ErrorCapacitor::exception& exception) { - result.setAttribute("result", "error"); - for ( auto&& error : *exception ) { - result.setValueNode("error", error); - } + result.setAttribute("result", "success"); + } + catch (const ErrorCapacitor::exception& exception) { + result.setAttribute("result", "error"); + + for ( auto&& error : *exception ) { + result.setValueNode("error", error); } + } - WarningCapacitor::warning_cache_ptr warnings( - transformer.getCachedWarnings() - ); + WarningCapacitor::warning_cache_ptr warnings( + transformer.getCachedWarnings() + ); - for ( auto&& warning : *warnings ) { - result.setValueNode("warning", warning); - } - } else { - result.setAttribute("result", "error"); + for ( auto&& warning : *warnings ) { + result.setValueNode("warning", warning); } return domDocument; diff --git a/src/function/generate.h b/src/function/generate.h index 333ed37..fa38e3f 100644 --- a/src/function/generate.h +++ b/src/function/generate.h @@ -11,7 +11,7 @@ class FunctionGenerate : public FunctionBase< FunctionGenerate, xalan::XSLTInputSource, xalan::XSLTInputSource, - boost::filesystem::path + boost::optional > { public: using FunctionBase::FunctionBase; @@ -23,7 +23,7 @@ class FunctionGenerate : public FunctionBase< const FilesystemContext&, xalan::XSLTInputSource, xalan::XSLTInputSource, - boost::filesystem::path + boost::optional ) const; }; diff --git a/src/function/transform.cc b/src/function/transform.cc deleted file mode 100644 index 9bd2dae..0000000 --- a/src/function/transform.cc +++ /dev/null @@ -1,57 +0,0 @@ -#include "transform.h" - -#include - -#include "transformer_facade.h" -#include "support/xerces_string_guard.h" -#include "support/dom/result_node_facade.h" -#include "support/error/error_capacitor.h" - -namespace InputXSLT { - -DomDocumentCache::document_ptr FunctionTransform::constructDocument( - const FilesystemContext&, - xalan::XSLTInputSource inputSource, - xalan::XSLTInputSource transformationSource -) const { - DomDocumentCache::document_ptr domDocument( - DomDocumentCache::createDocument("content") - ); - - ResultNodeFacade result(domDocument.get(), "transformation"); - TransformerFacade transformer(this->include_resolver_); - - try { - xalan::FormatterToXercesDOM targetFormatter( - domDocument.get(), - result.getResultElement() - ); - - transformer.generate( - inputSource, - transformationSource, - targetFormatter - ); - - result.setAttribute("result", "success"); - } - catch (const ErrorCapacitor::exception& exception) { - result.setAttribute("result", "error"); - - for ( auto&& error : *exception ) { - result.setValueNode("error", error); - } - } - - WarningCapacitor::warning_cache_ptr warnings( - transformer.getCachedWarnings() - ); - - for ( auto&& warning : *warnings ) { - result.setValueNode("warning", warning); - } - - return domDocument; -} - -} diff --git a/src/function/transform.h b/src/function/transform.h deleted file mode 100644 index 6c8c05d..0000000 --- a/src/function/transform.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_ -#define INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_ - -#include - -#include "base.h" - -namespace InputXSLT { - -class FunctionTransform : public FunctionBase< - FunctionTransform, - xalan::XSLTInputSource, - xalan::XSLTInputSource -> { - public: - using FunctionBase::FunctionBase; - - protected: - friend FunctionBase; - - DomDocumentCache::document_ptr constructDocument( - const FilesystemContext&, - xalan::XSLTInputSource, - xalan::XSLTInputSource - ) const; - -}; - -} - -#endif // INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_ diff --git a/src/platform_guard.cc b/src/platform_guard.cc index f50d976..1449678 100644 --- a/src/platform_guard.cc +++ b/src/platform_guard.cc @@ -9,7 +9,6 @@ #include "function/read_file.h" #include "function/write_file.h" #include "function/read_directory.h" -#include "function/transform.h" #include "function/generate.h" #include "function/external_command.h" @@ -42,12 +41,6 @@ PlatformGuard::PlatformGuard(const std::vector& path): InputXSLT::FunctionReadDirectory(&this->include_resolver_) ); - xalan::XalanTransformer::installExternalFunctionGlobal( - customNamespace, - xalan::XalanDOMString("transform"), - InputXSLT::FunctionTransform(&this->include_resolver_) - ); - xalan::XalanTransformer::installExternalFunctionGlobal( customNamespace, xalan::XalanDOMString("generate"), diff --git a/src/support/type/xobject_value.cc b/src/support/type/xobject_value.cc index 2fe4f9f..959da94 100644 --- a/src/support/type/xobject_value.cc +++ b/src/support/type/xobject_value.cc @@ -51,6 +51,17 @@ boost::filesystem::path XObjectValue::get( } } +template <> +boost::optional +XObjectValue::get>( + const xalan::XObjectPtr& ptr) const { + if ( ptr.null() ) { + return boost::optional(); + } else { + return this->get(ptr); + } +} + template <> xalan::XObjectPtr XObjectValue::get( const xalan::XObjectPtr& ptr) const { diff --git a/test/transform/transformation.xsl b/test/transform/transformation.xsl index 247a4c6..1409db8 100644 --- a/test/transform/transformation.xsl +++ b/test/transform/transformation.xsl @@ -14,7 +14,7 @@ - @@ -40,7 +40,7 @@ - + -- cgit v1.2.3