aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/function/transform.cc113
-rw-r--r--src/function/transform.h43
-rw-r--r--src/plattform_guard.cc7
3 files changed, 163 insertions, 0 deletions
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 <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMImplementation.hpp>
+#include <xercesc/dom/DOMElement.hpp>
+
+#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<XMLCh>("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<XMLCh>("result"))
+ );
+
+ resultNode->setAttribute(
+ *XercesStringGuard<XMLCh>("name"),
+ *XercesStringGuard<XMLCh>(targetPath.string())
+ );
+
+ rootNode->appendChild(resultNode);
+ } else {
+ xercesc::DOMElement* const resultNode(
+ domDocument->createElement(*XercesStringGuard<XMLCh>("error"))
+ );
+
+ rootNode->appendChild(resultNode);
+ }
+
+ return domDocument;
+}
+
+}
+
+namespace InputXSLT {
+
+FunctionTransform::FunctionTransform():
+ document_cache_(std::make_shared<DomDocumentCache>()) { }
+
+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;
+}
+
+}
diff --git a/src/function/transform.h b/src/function/transform.h
new file mode 100644
index 0000000..41bba05
--- /dev/null
+++ b/src/function/transform.h
@@ -0,0 +1,43 @@
+#ifndef INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_
+#define INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_
+
+#include <xalanc/XalanTransformer/XalanTransformer.hpp>
+#include <xalanc/XPath/XObjectFactory.hpp>
+#include <xalanc/XPath/Function.hpp>
+#include <xalanc/XPath/XObject.hpp>
+
+#include <memory>
+
+#include "common.h"
+#include "support/dom/document_cache.h"
+#include "support/filesystem_context.h"
+
+namespace InputXSLT {
+
+class FunctionTransform : public xalan::Function {
+ public:
+ FunctionTransform();
+
+ virtual xalan::XObjectPtr execute(
+ xalan::XPathExecutionContext&,
+ xalan::XalanNode*,
+ const xalan::XObjectPtr,
+ const xalan::XObjectPtr,
+ const xalan::Locator*
+ ) const;
+
+ virtual FunctionTransform* clone(xalan::MemoryManager&) const;
+
+ FunctionTransform& operator=(const FunctionTransform&) = delete;
+ bool operator==(const FunctionTransform&) const = delete;
+
+ private:
+ std::shared_ptr<DomDocumentCache> document_cache_;
+
+ const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const;
+
+};
+
+}
+
+#endif // INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_
diff --git a/src/plattform_guard.cc b/src/plattform_guard.cc
index 2c3cee4..da3b6d7 100644
--- a/src/plattform_guard.cc
+++ b/src/plattform_guard.cc
@@ -9,6 +9,7 @@
#include "function/read_file.h"
#include "function/read_xml_file.h"
#include "function/read_directory.h"
+#include "function/transform.h"
namespace InputXSLT {
@@ -37,6 +38,12 @@ PlattformGuard::PlattformGuard() {
xalan::XalanDOMString("read-directory"),
InputXSLT::FunctionReadDirectory()
);
+
+ xalan::XalanTransformer::installExternalFunctionGlobal(
+ customNamespace,
+ xalan::XalanDOMString("transform"),
+ InputXSLT::FunctionTransform()
+ );
}
PlattformGuard::~PlattformGuard() {