From a02dce3c8e6086acbfe9c57c3ee4bb386bfbebc2 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 5 Jul 2014 21:55:48 +0200 Subject: Revamped implementation to support FunctionTransform XalanNode input * FunctionTransform now accepts xalan::XalanNode base xalan::XSLTInputSource instances as input parameter ** such xalan::XSLTInputSource instances were already supported for the stylesheet parameter but not for the input parameter ** e.g. it is now possible to generate a DOM inside a transformation, pass the DOM into a embedded transformation as input, modifiy the output of that transformation, pass this modified DOM into another transformation and so on... you get the point * this required a revamp of TransformationFacade ** it is now a transformation independent wrapper for xalan::XalanTransformer *** as such is was renamed to TransformerFacade ** removed error catching template factory method "try_create" as it is not needed anymore ** "std::basic_ostream&" taking "generate" member method overloads were removed ** the set of "generate" member overloads was reduced to two methods accepting xalan::XSLTInputSource and xalan::FormatterListener as parameters * the core problem first documented in 299d0f6 was solved by transforming the input parameter into a xerces DOM and wrapping this DOM inside a xalan::XercesDOMWrapperParsedSource instance * serialization of the transformation result for file / std::cout output is now performed directly by the ixslt frontend * removed 'stylesheet parameter' parameter from FunctionTransform ** the functionality provided by this is easily replicated using the now possible DOM input parameter ** this was done to simplify the interface and reduce unnecessary feature duplication * updated FunctionTransform test case accordingly * added "generate" template function to ixslt frontend ** wraps TransformerFacade and manages ist input arguments ** handles serialization to stream * this commit marks a important milestone towards the goals defined in 741a70f --- CMakeLists.txt | 3 +- ixslt.cc | 115 ++++++++++++++++---------- src/function/transform.cc | 72 ++++++++--------- src/function/transform.h | 6 +- src/support/stylesheet_parameter_guard.cc | 46 ----------- src/support/stylesheet_parameter_guard.h | 37 --------- src/transformation_facade.cc | 116 --------------------------- src/transformation_facade.h | 80 ------------------ src/transformer_facade.cc | 129 ++++++++++++++++++++++++++++++ src/transformer_facade.h | 46 +++++++++++ test/transform/reference.xml | 4 +- test/transform/test.xsl | 10 +-- test/transform/transformation.xsl | 25 +++--- 13 files changed, 306 insertions(+), 383 deletions(-) delete mode 100644 src/support/stylesheet_parameter_guard.cc delete mode 100644 src/support/stylesheet_parameter_guard.h delete mode 100644 src/transformation_facade.cc delete mode 100644 src/transformation_facade.h create mode 100644 src/transformer_facade.cc create mode 100644 src/transformer_facade.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 73e67b0..9533c63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,14 +23,13 @@ set( set( Sources src/plattform_guard.cc - src/transformation_facade.cc + src/transformer_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 src/support/filesystem_context.cc - src/support/stylesheet_parameter_guard.cc src/support/xalan_string.cc src/support/include_entity_resolver.cc src/support/error/error_multiplexer.cc diff --git a/ixslt.cc b/ixslt.cc index abc7e1a..048f990 100644 --- a/ixslt.cc +++ b/ixslt.cc @@ -1,3 +1,7 @@ +#include +#include +#include + #include "boost/optional.hpp" #include "boost/program_options.hpp" #include @@ -7,16 +11,18 @@ #include #include "plattform_guard.h" -#include "transformation_facade.h" +#include "transformer_facade.h" + +namespace { class WarningGuard { public: - WarningGuard(InputXSLT::TransformationFacade* transformation): - transformation_(transformation) { }; + WarningGuard(InputXSLT::TransformerFacade* transformer): + transformer_(transformer) { }; ~WarningGuard() { InputXSLT::WarningCapacitor::warning_cache_ptr warnings( - this->transformation_->getCachedWarnings() + this->transformer_->getCachedWarnings() ); for ( auto&& warning : *warnings ) { @@ -25,7 +31,7 @@ class WarningGuard { }; private: - InputXSLT::TransformationFacade* const transformation_; + InputXSLT::TransformerFacade* const transformer_; }; @@ -88,6 +94,36 @@ void handleErrors(const InputXSLT::ErrorCapacitor::error_cache& errors) { } } +template +bool generate( + InputXSLT::IncludeEntityResolver* resolver, + std::basic_ostream& target, + Arguments&&... arguments +) { + InputXSLT::TransformerFacade transformer(resolver); + WarningGuard guard(&transformer); + + try { + xalan::XalanStdOutputStream output(target); + xalan::XalanOutputStreamPrintWriter writer(output); + xalan::FormatterToXML formatter(writer); + + formatter.setDoIndent(true); + + transformer.generate( + std::forward(arguments)..., + formatter + ); + + return true; + } + catch (const InputXSLT::ErrorCapacitor::exception& exception) { + handleErrors(*exception); + + return false; + } +} + bool process(const boost::program_options::variables_map& variables) { std::vector includePath; @@ -96,51 +132,48 @@ bool process(const boost::program_options::variables_map& variables) { }; InputXSLT::PlattformGuard plattform(includePath); - InputXSLT::TransformationFacade::ptr transformation{}; - - if ( variables.count("input") ) { - transformation = InputXSLT::TransformationFacade::try_create( - handleErrors, - variables["input"].as().data(), - variables["transformation"].as().data(), - plattform.getEntityResolver() - ); - } else { - transformation = InputXSLT::TransformationFacade::try_create( - handleErrors, - variables["transformation"].as().data(), - plattform.getEntityResolver() - ); - } - if ( transformation ) { - WarningGuard guard(transformation.get()); + if ( variables.count("target") ) { + boost::filesystem::ofstream file( + variables["target"].as() + ); - try { - if ( variables.count("target") ) { - boost::filesystem::ofstream file( - variables["target"].as() + if ( file.is_open() ) { + if ( variables.count("input") ) { + return generate( + plattform.getEntityResolver(), + file, + variables["input"].as().data(), + variables["transformation"].as().data() ); - - if ( file.is_open() ) { - transformation->generate(file); - } else { - return false; - } } else { - transformation->generate(std::cout); + return generate( + plattform.getEntityResolver(), + file, + variables["transformation"].as().data() + ); } - - return true; - } - catch (const InputXSLT::ErrorCapacitor::exception& exception) { - handleErrors(*exception); - + } else { return false; } + } else { + if ( variables.count("input") ) { + return generate( + plattform.getEntityResolver(), + std::cout, + variables["input"].as().data(), + variables["transformation"].as().data() + ); + } else { + return generate( + plattform.getEntityResolver(), + std::cout, + variables["transformation"].as().data() + ); + } } +} - return false; } int main(int argc, char** argv) { diff --git a/src/function/transform.cc b/src/function/transform.cc index 1d22399..e53b55f 100644 --- a/src/function/transform.cc +++ b/src/function/transform.cc @@ -6,7 +6,7 @@ #include #include -#include "transformation_facade.h" +#include "transformer_facade.h" #include "support/xerces_string_guard.h" #include "support/dom/result_node_facade.h" #include "support/error/error_capacitor.h" @@ -15,15 +15,15 @@ namespace { using InputXSLT::ErrorCapacitor; -inline std::function handleErrors( - InputXSLT::ResultNodeFacade& result) { - return [&result](const ErrorCapacitor::error_cache& errors) { - result.setAttribute("result", "error"); +inline void handleErrors( + InputXSLT::ResultNodeFacade& result, + const ErrorCapacitor::error_cache& errors +) { + result.setAttribute("result", "error"); - for ( auto&& error : errors ) { - result.setValueNode("error", error); - } - }; + for ( auto&& error : errors ) { + result.setValueNode("error", error); + } } } @@ -32,8 +32,7 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionTransform::constructDocument( xalan::XSLTInputSource inputSource, - xalan::XSLTInputSource transformationSource, - xalan::XObjectPtr parameterObject + xalan::XSLTInputSource transformationSource ) { xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( @@ -48,37 +47,32 @@ xercesc::DOMDocument* FunctionTransform::constructDocument( ); ResultNodeFacade result(domDocument, rootElement, "transformation"); + TransformerFacade transformer(this->include_resolver_); - if ( auto transformation = TransformationFacade::try_create( - handleErrors(result), - inputSource, - transformationSource, - this->include_resolver_ - ) ) { - try { - xalan::FormatterToXercesDOM formatter( - domDocument, - result.getResultElement() - ); - - transformation->generate( - formatter, - parameterObject - ); - - result.setAttribute("result", "success"); - } - catch (const ErrorCapacitor::exception& exception) { - handleErrors(result)(*exception); - } - - WarningCapacitor::warning_cache_ptr warnings( - transformation->getCachedWarnings() + try { + xalan::FormatterToXercesDOM targetFormatter( + domDocument, + result.getResultElement() ); - for ( auto&& warning : *warnings ) { - result.setValueNode("warning", warning); - } + transformer.generate( + inputSource, + transformationSource, + targetFormatter + ); + + result.setAttribute("result", "success"); + } + catch (const ErrorCapacitor::exception& exception) { + handleErrors(result, *exception); + } + + 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 index 234a1d0..b8e733d 100644 --- a/src/function/transform.h +++ b/src/function/transform.h @@ -10,8 +10,7 @@ namespace InputXSLT { class FunctionTransform : public FunctionBase< FunctionTransform, xalan::XSLTInputSource, - xalan::XSLTInputSource, - xalan::XObjectPtr + xalan::XSLTInputSource > { public: using FunctionBase::FunctionBase; @@ -21,8 +20,7 @@ class FunctionTransform : public FunctionBase< xercesc::DOMDocument* constructDocument( xalan::XSLTInputSource, - xalan::XSLTInputSource, - xalan::XObjectPtr + xalan::XSLTInputSource ); }; diff --git a/src/support/stylesheet_parameter_guard.cc b/src/support/stylesheet_parameter_guard.cc deleted file mode 100644 index 399bccb..0000000 --- a/src/support/stylesheet_parameter_guard.cc +++ /dev/null @@ -1,46 +0,0 @@ -#include "stylesheet_parameter_guard.h" - -namespace InputXSLT { - -StylesheetParameterGuard::StylesheetParameterGuard( - xalan::XalanTransformer& transformer -): transformer_(transformer) { } - -StylesheetParameterGuard::StylesheetParameterGuard( - xalan::XalanTransformer& transformer, - const map& parameters -): transformer_(transformer) { - this->set(parameters); -} - -StylesheetParameterGuard::~StylesheetParameterGuard() { - this->transformer_.clearStylesheetParams(); -} - -void StylesheetParameterGuard::set(const map& parameters) { - for ( auto&& parameter : parameters ) { - this->set(parameter.first, parameter.second); - } -} - -void StylesheetParameterGuard::set( - const std::string& key, - const std::string& value -) { - this->transformer_.setStylesheetParam( - key.data(), - std::string("'" + value + "'").data() - ); -} - -void StylesheetParameterGuard::set( - const std::string& key, - const xalan::XObjectPtr& value -) { - this->transformer_.setStylesheetParam( - key.data(), - value - ); -} - -} diff --git a/src/support/stylesheet_parameter_guard.h b/src/support/stylesheet_parameter_guard.h deleted file mode 100644 index 7fa425e..0000000 --- a/src/support/stylesheet_parameter_guard.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef INPUTXSLT_SRC_SUPPORT_STYLESHEET_PARAMETER_GUARD_H_ -#define INPUTXSLT_SRC_SUPPORT_STYLESHEET_PARAMETER_GUARD_H_ - -#include -#include - -#include -#include - -#include "common.h" - -namespace InputXSLT { - -class StylesheetParameterGuard { - public: - typedef std::unordered_map map; - - explicit StylesheetParameterGuard(xalan::XalanTransformer&); - StylesheetParameterGuard( - xalan::XalanTransformer&, - const map& - ); - - ~StylesheetParameterGuard(); - - void set(const map&); - void set(const std::string&, const std::string&); - void set(const std::string&, const xalan::XObjectPtr&); - - private: - xalan::XalanTransformer& transformer_; - -}; - -} - -#endif // INPUTXSLT_SRC_SUPPORT_STYLESHEET_PARAMETER_GUARD_H_ diff --git a/src/transformation_facade.cc b/src/transformation_facade.cc deleted file mode 100644 index 4017cd0..0000000 --- a/src/transformation_facade.cc +++ /dev/null @@ -1,116 +0,0 @@ -#include "transformation_facade.h" - -#include -#include -#include -#include -#include - -#include "support/xerces_string_guard.h" - -namespace InputXSLT { - -TransformationFacade::TransformationFacade( - xalan::XSLTInputSource transformation, - IncludeEntityResolver* resolver -): - input_{}, - transformation_{}, - transformer_(), - error_multiplexer_(&transformer_), - warning_capacitor_(&error_multiplexer_) { - this->transformer_.setEntityResolver(resolver); - - ErrorCapacitor errorCapacitor(&this->error_multiplexer_); - - std::stringstream dummyStream(""); - - this->transformer_.parseSource( - xalan::XSLTInputSource(dummyStream), - this->input_ - ); - - this->transformer_.compileStylesheet( - transformation, - this->transformation_ - ); - - errorCapacitor.discharge(); -} - -TransformationFacade::TransformationFacade( - xalan::XSLTInputSource input, - xalan::XSLTInputSource transformation, - IncludeEntityResolver* resolver -): - input_{}, - transformation_{}, - transformer_(), - error_multiplexer_(&transformer_), - warning_capacitor_(&error_multiplexer_) { - this->transformer_.setEntityResolver(resolver); - - ErrorCapacitor errorCapacitor(&this->error_multiplexer_); - - this->transformer_.parseSource( - input, - this->input_ - ); - - this->transformer_.compileStylesheet( - transformation, - this->transformation_ - ); - - errorCapacitor.discharge(); -} - -WarningCapacitor::warning_cache_ptr TransformationFacade::getCachedWarnings() { - return this->warning_capacitor_.discharge(); -} - -void TransformationFacade::generate( - std::basic_ostream& targetStream, - const xalan::XObjectPtr& parameter -) { - StylesheetParameterGuard guard(this->transformer_); - guard.set("parameters", parameter); - - this->generate(targetStream); -} - -void TransformationFacade::generate(std::basic_ostream& targetStream) { - StylesheetParameterGuard guard(this->transformer_); - - xalan::XalanStdOutputStream outputStream(targetStream); - xalan::XalanOutputStreamPrintWriter outputWriter(outputStream); - - xalan::FormatterToXML formatter(outputWriter); - formatter.setDoIndent(true); - - this->generate(formatter); -} - -void TransformationFacade::generate( - xalan::FormatterListener& formatter, - const xalan::XObjectPtr& parameter -) { - StylesheetParameterGuard guard(this->transformer_); - guard.set("parameters", parameter); - - this->generate(formatter); -} - -void TransformationFacade::generate(xalan::FormatterListener& target) { - ErrorCapacitor errorCapacitor(&this->error_multiplexer_); - - this->transformer_.transform( - *(this->input_), - this->transformation_, - target - ); - - errorCapacitor.discharge(); -} - -} diff --git a/src/transformation_facade.h b/src/transformation_facade.h deleted file mode 100644 index e4cbc10..0000000 --- a/src/transformation_facade.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef INPUTXSLT_SRC_TRANSFORMATION_FACADE_H_ -#define INPUTXSLT_SRC_TRANSFORMATION_FACADE_H_ - -#include - -#include -#include -#include -#include - -#include "common.h" -#include "support/include_entity_resolver.h" -#include "support/stylesheet_parameter_guard.h" -#include "support/error/error_multiplexer.h" -#include "support/error/error_capacitor.h" -#include "support/error/warning_capacitor.h" - -namespace InputXSLT { - -class TransformationFacade { - public: - typedef std::unique_ptr ptr; - - template - static ptr try_create( - const std::function&, - Arguments&&... - ); - - TransformationFacade( - xalan::XSLTInputSource, - IncludeEntityResolver* - ); - - TransformationFacade( - xalan::XSLTInputSource, - xalan::XSLTInputSource, - IncludeEntityResolver* - ); - - void generate(std::basic_ostream&); - void generate(std::basic_ostream&, const xalan::XObjectPtr&); - - void generate(xalan::FormatterListener&); - void generate(xalan::FormatterListener&, const xalan::XObjectPtr&); - - WarningCapacitor::warning_cache_ptr getCachedWarnings(); - - private: - const xalan::XalanParsedSource* input_; - const xalan::XalanCompiledStylesheet* transformation_; - - xalan::XalanTransformer transformer_; - ErrorMultiplexer error_multiplexer_; - WarningCapacitor warning_capacitor_; - -}; - -template -auto TransformationFacade::try_create( - const std::function& handleErrors, - Arguments&&... arguments -) -> ptr { - try { - return ptr( - new InputXSLT::TransformationFacade( - std::forward(arguments)... - ) - ); - } - catch (const ErrorCapacitor::exception& exception) { - handleErrors(*exception); - - return ptr(); - } -} - -} - -#endif // INPUTXSLT_SRC_TRANSFORMATION_FACADE_H_ diff --git a/src/transformer_facade.cc b/src/transformer_facade.cc new file mode 100644 index 0000000..10c8c7a --- /dev/null +++ b/src/transformer_facade.cc @@ -0,0 +1,129 @@ +#include "transformer_facade.h" + +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "support/xerces_string_guard.h" + +namespace InputXSLT { + +TransformerFacade::TransformerFacade(IncludeEntityResolver* resolver): + transformer_(), + error_multiplexer_(&transformer_), + warning_capacitor_(&error_multiplexer_) { + this->transformer_.setEntityResolver(resolver); +} + +WarningCapacitor::warning_cache_ptr TransformerFacade::getCachedWarnings() { + return this->warning_capacitor_.discharge(); +} + +void TransformerFacade::generate( + const xalan::XSLTInputSource& source, + const xalan::XSLTInputSource& transformation, + xalan::FormatterListener& target +) { + if ( source.getNode() == nullptr ) { + ErrorCapacitor errorCapacitor(&this->error_multiplexer_); + + this->transformer_.transform( + source, + transformation, + target + ); + + errorCapacitor.discharge(); + } else { + this->generate( + source.getNode(), + transformation, + target + ); + } +} + +void TransformerFacade::generate( + const xalan::XSLTInputSource& transformation, + xalan::FormatterListener& target +) { + ErrorCapacitor errorCapacitor(&this->error_multiplexer_); + + xercesc::DOMDocument* const inputDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard("dummy"), + nullptr + ) + ); + + xalan::XercesParserLiaison parserLiaison; + xalan::XercesDOMSupport domSupport(parserLiaison); + + xalan::XercesDOMWrapperParsedSource inputParsedSource( + inputDocument, + parserLiaison, + domSupport + ); + + this->transformer_.transform( + inputParsedSource, + transformation, + target + ); + + inputDocument->release(); + + errorCapacitor.discharge(); +} + +void TransformerFacade::generate( + xalan::XalanNode* const source, + const xalan::XSLTInputSource& transformation, + xalan::FormatterListener& target +) { + ErrorCapacitor errorCapacitor(&this->error_multiplexer_); + + xercesc::DOMDocument* const inputDocument( + xercesc::DOMImplementation::getImplementation()->createDocument() + ); + + xalan::FormatterToXercesDOM inputFormatter( + inputDocument, + inputDocument->getDocumentElement() + ); + + xalan::FormatterTreeWalker walker(inputFormatter); + walker.traverseSubtree(source); + + xalan::XercesParserLiaison parserLiaison; + xalan::XercesDOMSupport domSupport(parserLiaison); + + xalan::XercesDOMWrapperParsedSource inputParsedSource( + inputFormatter.getDocument(), + parserLiaison, + domSupport + ); + + this->transformer_.transform( + inputParsedSource, + transformation, + target + ); + + inputDocument->release(); + + errorCapacitor.discharge(); +} + +} diff --git a/src/transformer_facade.h b/src/transformer_facade.h new file mode 100644 index 0000000..17db094 --- /dev/null +++ b/src/transformer_facade.h @@ -0,0 +1,46 @@ +#ifndef INPUTXSLT_SRC_TRANSFORMER_FACADE_H_ +#define INPUTXSLT_SRC_TRANSFORMER_FACADE_H_ + +#include + +#include "common.h" +#include "support/include_entity_resolver.h" +#include "support/error/error_multiplexer.h" +#include "support/error/error_capacitor.h" +#include "support/error/warning_capacitor.h" + +namespace InputXSLT { + +class TransformerFacade { + public: + TransformerFacade(IncludeEntityResolver*); + + void generate( + const xalan::XSLTInputSource&, + const xalan::XSLTInputSource&, + xalan::FormatterListener& + ); + + void generate( + const xalan::XSLTInputSource&, + xalan::FormatterListener& + ); + + WarningCapacitor::warning_cache_ptr getCachedWarnings(); + + private: + xalan::XalanTransformer transformer_; + ErrorMultiplexer error_multiplexer_; + WarningCapacitor warning_capacitor_; + + void generate( + xalan::XalanNode* const, + const xalan::XSLTInputSource&, + xalan::FormatterListener& + ); + +}; + +} + +#endif // INPUTXSLT_SRC_TRANSFORMER_FACADE_H_ diff --git a/test/transform/reference.xml b/test/transform/reference.xml index e537bbc..1c702e8 100644 --- a/test/transform/reference.xml +++ b/test/transform/reference.xml @@ -1,10 +1,10 @@ - + Hello 1 Hello 2 Hello 3 Hello 4 - + 42 diff --git a/test/transform/test.xsl b/test/transform/test.xsl index 7469f92..a9f04a5 100644 --- a/test/transform/test.xsl +++ b/test/transform/test.xsl @@ -14,16 +14,14 @@ indent="yes" /> - - - - - + + + - + diff --git a/test/transform/transformation.xsl b/test/transform/transformation.xsl index 686bbf5..1002540 100644 --- a/test/transform/transformation.xsl +++ b/test/transform/transformation.xsl @@ -23,12 +23,10 @@ - @@ -36,13 +34,11 @@ - - + - @@ -62,12 +58,21 @@ - [test.xml] + + + + Hello 1 + Hello 2 + Hello 3 + Hello 4 + + + 21 + + + test.xsl test_actual.xml - - 21 - -- cgit v1.2.3