From a3fe119214fa61b571326348f3ed23ec7ffd77e6 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 13 Jul 2014 21:46:39 +0200 Subject: Improved xalan::XalanCompiledStylesheet lifetime management * added custom deleter for xalan::XalanCompiledStylesheet specific std::unique_ptr specialization * added previously missing check for successful stylesheet compilation --- src/transformer_facade.cc | 100 ++++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/transformer_facade.cc b/src/transformer_facade.cc index 5910bc7..431b9f8 100644 --- a/src/transformer_facade.cc +++ b/src/transformer_facade.cc @@ -49,6 +49,44 @@ std::unique_ptr augmentFormatterToXML( ); } +class compiled_stylesheet_deleter { + public: + compiled_stylesheet_deleter(xalan::XalanTransformer* transformer): + transformer_(transformer) { } + + void operator()(const xalan::XalanCompiledStylesheet* transformation) { + this->transformer_->destroyStylesheet(transformation); + } + + private: + xalan::XalanTransformer* const transformer_; + +}; + +typedef std::unique_ptr< + const xalan::XalanCompiledStylesheet, + compiled_stylesheet_deleter +> CompiledStylesheetPtr; + +CompiledStylesheetPtr compileStylesheet( + xalan::XalanTransformer* const transformer, + const xalan::XSLTInputSource& transformation +) { + const xalan::XalanCompiledStylesheet* compiled{}; + + if ( transformer->compileStylesheet(transformation, compiled) == 0 ) { + return CompiledStylesheetPtr( + compiled, + compiled_stylesheet_deleter(transformer) + ); + } else { + return CompiledStylesheetPtr( + nullptr, + compiled_stylesheet_deleter(transformer) + ); + } +} + } namespace InputXSLT { @@ -159,42 +197,36 @@ void TransformerFacade::dispatchTransformer( const xalan::XSLTInputSource& transformation, xalan::FormatterListener& target ) { - const xalan::XalanCompiledStylesheet* compiledTransformation{}; - - this->transformer_.compileStylesheet( - transformation, - compiledTransformation - ); - - switch ( target.getOutputFormat() ) { - case xalan::FormatterListener::eFormat::OUTPUT_METHOD_XML: { - auto formatter = augmentFormatterToXML( - compiledTransformation, - target - ); - - this->transformer_.transform( - source, - compiledTransformation, - *formatter - ); - - break; - } - default: { - this->transformer_.transform( - source, - compiledTransformation, - target - ); - - break; + if ( auto compiledTransformation = compileStylesheet( + &(this->transformer_), + transformation + ) ) { + switch ( target.getOutputFormat() ) { + case xalan::FormatterListener::eFormat::OUTPUT_METHOD_XML: { + auto formatter = augmentFormatterToXML( + compiledTransformation.get(), + target + ); + + this->transformer_.transform( + source, + compiledTransformation.get(), + *formatter + ); + + break; + } + default: { + this->transformer_.transform( + source, + compiledTransformation.get(), + target + ); + + break; + } } } - - this->transformer_.destroyStylesheet( - compiledTransformation - ); } } -- cgit v1.2.3