From 3383e24e396431eed7d4ab9f69ff8ffaf12d4925 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 19 Apr 2014 18:31:15 +0200 Subject: Implemented transformation execution function * InputXSLT::PlattformGuard handles xerces and xalan construction and lifetime * InputXSLT::TransformerGuard instantiates PlattformGuard, configurates external functions ** offers execute method for "executing" a XSL tranformation with empty input --- CMakeLists.txt | 1 + src/plattform_guard.h | 24 +++++++++++++++++ src/transformer_guard.cc | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ src/transformer_guard.h | 29 +++++++++++++++++++++ src/utility.h | 2 +- test.cc | 44 ++++--------------------------- 6 files changed, 127 insertions(+), 40 deletions(-) create mode 100644 src/plattform_guard.h create mode 100644 src/transformer_guard.cc create mode 100644 src/transformer_guard.h diff --git a/CMakeLists.txt b/CMakeLists.txt index aa6310b..26ff8d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable( test test.cc src/utility.cc + src/transformer_guard.cc src/function/read_file.cc src/function/read_xml_file.cc ) diff --git a/src/plattform_guard.h b/src/plattform_guard.h new file mode 100644 index 0000000..9e99fdc --- /dev/null +++ b/src/plattform_guard.h @@ -0,0 +1,24 @@ +#ifndef INPUTXSLT_SRC_PLATTFORM_GUARD_H_ +#define INPUTXSLT_SRC_PLATTFORM_GUARD_H_ + +#include +#include + +namespace xalan = xalanc_1_11; + +namespace InputXSLT { + +struct PlattformGuard { + PlattformGuard() { + xercesc::XMLPlatformUtils::Initialize(); + xalan::XalanTransformer::initialize(); + } + + ~PlattformGuard() { + xalan::XalanTransformer::terminate(); + } +}; + +} + +#endif // INPUTXSLT_SRC_PLATTFORM_GUARD_H_ diff --git a/src/transformer_guard.cc b/src/transformer_guard.cc new file mode 100644 index 0000000..73bcea2 --- /dev/null +++ b/src/transformer_guard.cc @@ -0,0 +1,67 @@ +#include "transformer_guard.h" + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include "function/read_file.h" +#include "function/read_xml_file.h" + +namespace InputXSLT { + +TransformerGuard::TransformerGuard(): + plattform_(), + parser_(), + transformer_() { + const xalan::XalanDOMString customNamespace( + "http://ExternalFunction.xalan-c++.xml.apache.org" + ); + + this->transformer_.installExternalFunction( + customNamespace, + xalan::XalanDOMString("read-file"), + InputXSLT::FunctionReadFile() + ); + + this->transformer_.installExternalFunction( + customNamespace, + xalan::XalanDOMString("read-xml-file"), + InputXSLT::FunctionReadXmlFile() + ); + +} + +int TransformerGuard::execute( + const std::string& transformation, + const std::string& target +) { + xercesc::DOMDocument* inputDom( + xercesc::DOMImplementation::getImplementation()->createDocument() + ); + xalan::XercesDOMSupport domSupport(this->parser_); + + xalan::XercesDOMWrapperParsedSource parsedInput( + inputDom, + this->parser_, + domSupport, + xalan::XalanDOMString("") + ); + + xalan::XSLTInputSource transform(transformation.data()); + xalan::XSLTResultTarget output(target.data()); + + return this->transformer_.transform( + parsedInput, + transform, + output + ); +} + +} diff --git a/src/transformer_guard.h b/src/transformer_guard.h new file mode 100644 index 0000000..381fb94 --- /dev/null +++ b/src/transformer_guard.h @@ -0,0 +1,29 @@ +#ifndef INPUTXSLT_SRC_TRANSFORMER_GUARD_H_ +#define INPUTXSLT_SRC_TRANSFORMER_GUARD_H_ + +#include + +#include +#include + +#include "plattform_guard.h" + +namespace InputXSLT { + +class TransformerGuard { + public: + TransformerGuard(); + + int execute(const std::string&, const std::string&); + + private: + const PlattformGuard plattform_; + mutable xalan::XercesParserLiaison parser_; + + xalan::XalanTransformer transformer_; + +}; + +} + +#endif // INPUTXSLT_SRC_TRANSFORMER_GUARD_H_ diff --git a/src/utility.h b/src/utility.h index b841373..ee39a25 100644 --- a/src/utility.h +++ b/src/utility.h @@ -3,7 +3,7 @@ #include -namespace xalanc_1_11 { }; +namespace xalanc_1_11 { } namespace xalan = xalanc_1_11; namespace InputXSLT { diff --git a/test.cc b/test.cc index cfcf632..09325f7 100644 --- a/test.cc +++ b/test.cc @@ -1,44 +1,10 @@ -#include -#include -#include -#include - -#include "function/read_file.h" -#include "function/read_xml_file.h" +#include "transformer_guard.h" int main() { - xercesc::XMLPlatformUtils::Initialize(); - xalan::XalanTransformer::initialize(); - - const xalan::XalanDOMString customNamespace( - "http://ExternalFunction.xalan-c++.xml.apache.org" - ); - - xalan::XalanTransformer transformer; - - transformer.installExternalFunction( - customNamespace, - xalan::XalanDOMString("read-file"), - InputXSLT::FunctionReadFile() - ); + InputXSLT::TransformerGuard guard; - transformer.installExternalFunction( - customNamespace, - xalan::XalanDOMString("read-xml-file"), - InputXSLT::FunctionReadXmlFile() + return guard.execute( + "../dummy/transform.xsl", + "out.xml" ); - - xalan::XSLTInputSource input("../dummy/in.xml"); - xalan::XSLTInputSource tranformation("../dummy/transform.xsl"); - xalan::XSLTResultTarget output("out.xml"); - - int result(transformer.transform( - input, - tranformation, - output - )); - - xalan::XalanTransformer::terminate(); - - return result; } -- cgit v1.2.3