From c4fcfa9b39d9c29ecbc3ac1c12b7e5b2beb24a6d Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Mon, 12 May 2014 18:49:24 +0200 Subject: Implemented basic external transform function * "InputXSLT:transform" expects two input arguments and executes the given transformation into the given target file ** this function respresents a important step in the direction of making it possible to write an actual static site generator on top of InputXSLT using XSLT * added basic "transform" test case * updated README.md to include this new function --- src/function/transform.cc | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/function/transform.cc (limited to 'src/function/transform.cc') diff --git a/src/function/transform.cc b/src/function/transform.cc new file mode 100644 index 0000000..9b600d9 --- /dev/null +++ b/src/function/transform.cc @@ -0,0 +1,113 @@ +#include "transform.h" + +#include +#include +#include + +#include "transformation_facade.h" +#include "support/xerces_string_guard.h" + +namespace { + +using InputXSLT::XercesStringGuard; + +xercesc::DOMDocument* constructDocument( + const InputXSLT::FilesystemContext&, + const boost::filesystem::path& transformationPath, + const boost::filesystem::path& targetPath +) { + xercesc::DOMDocument* const domDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard("content"), + nullptr + ) + ); + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() + ); + + InputXSLT::TransformationFacade transformation( + transformationPath.string() + ); + + const int result = transformation.generate( + targetPath.string() + ); + + if ( result == 0 ) { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard("result")) + ); + + resultNode->setAttribute( + *XercesStringGuard("name"), + *XercesStringGuard(targetPath.string()) + ); + + rootNode->appendChild(resultNode); + } else { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard("error")) + ); + + rootNode->appendChild(resultNode); + } + + return domDocument; +} + +} + +namespace InputXSLT { + +FunctionTransform::FunctionTransform(): + document_cache_(std::make_shared()) { } + +xalan::XObjectPtr FunctionTransform::execute( + xalan::XPathExecutionContext& executionContext, + xalan::XalanNode*, + const xalan::XObjectPtr argument1, + const xalan::XObjectPtr argument2, + const xalan::Locator* locator +) const { + const FilesystemContext fsContext(locator); + + xalan::XalanDocument* const domDocument( + this->document_cache_->create( + constructDocument( + fsContext, + fsContext.resolve(argument1->str()), + fsContext.resolve(argument2->str()) + ) + ) + ); + + xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( + executionContext + ); + + nodeList->addNodes( + *domDocument->getDocumentElement()->getChildNodes() + ); + + return executionContext.getXObjectFactory().createNodeSet(nodeList); +} + +FunctionTransform* FunctionTransform::clone( + xalan::MemoryManager& manager) const { + return xalan::XalanCopyConstruct( + manager, + *this + ); +} + +const xalan::XalanDOMString& FunctionTransform::getError( + xalan::XalanDOMString& result) const { + result.assign("The function expects two arguments of type string."); + + return result; +} + +} -- cgit v1.2.3