From 29a9fb20b4c8414f2590886e43ae86794a53db89 Mon Sep 17 00:00:00 2001
From: Adrian Kummerländer
Date: Sun, 20 Apr 2014 13:09:32 +0200
Subject: Implemented support for modifying external function base path * one
expects a read-file function to work relative to the directory the
transformation is located and not to the executable's location ** from the
perspective of the user the transformation is the application, not the actual
executable * removed PlattformGuard struct from TransformerGuard (now
TransformerFacade) * TransformerFacade is instatiated with the appropriate
relative working path ** i.e. it will have to be instantiated for every
directory containing one or more transformations
---
CMakeLists.txt | 2 +-
dummy/transform.xsl | 4 +--
src/common.h | 6 ++++
src/function/read_file.cc | 20 ++++++++-----
src/function/read_file.h | 8 +++++-
src/function/read_xml_file.cc | 11 +++++--
src/function/read_xml_file.h | 7 +++--
src/plattform_guard.h | 4 ++-
src/transformer_facade.cc | 66 ++++++++++++++++++++++++++++++++++++++++++
src/transformer_facade.h | 28 ++++++++++++++++++
src/transformer_guard.cc | 67 -------------------------------------------
src/transformer_guard.h | 29 -------------------
test.cc | 8 ++++--
13 files changed, 144 insertions(+), 116 deletions(-)
create mode 100644 src/common.h
create mode 100644 src/transformer_facade.cc
create mode 100644 src/transformer_facade.h
delete mode 100644 src/transformer_guard.cc
delete mode 100644 src/transformer_guard.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 26ff8d1..4421841 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,7 +14,7 @@ add_executable(
test
test.cc
src/utility.cc
- src/transformer_guard.cc
+ src/transformer_facade.cc
src/function/read_file.cc
src/function/read_xml_file.cc
)
diff --git a/dummy/transform.xsl b/dummy/transform.xsl
index 08d005b..803e0f6 100644
--- a/dummy/transform.xsl
+++ b/dummy/transform.xsl
@@ -16,10 +16,10 @@
-
+
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..8643eef
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,6 @@
+#ifndef INPUTXSLT_SRC_COMMON_H_
+#define INPUTXSLT_SRC_COMMON_H_
+
+namespace xalan = xalanc_1_11;
+
+#endif // INPUTXSLT_SRC_COMMON_H_
diff --git a/src/function/read_file.cc b/src/function/read_file.cc
index efc1f61..dcbff25 100644
--- a/src/function/read_file.cc
+++ b/src/function/read_file.cc
@@ -1,7 +1,12 @@
#include "read_file.h"
+#include "utility.h"
+
namespace InputXSLT {
+FunctionReadFile::FunctionReadFile(const std::string& path):
+ path_(path) { }
+
xalan::XObjectPtr FunctionReadFile::execute(
xalan::XPathExecutionContext& executionContext,
xalan::XalanNode* context,
@@ -16,20 +21,21 @@ xalan::XObjectPtr FunctionReadFile::execute(
generalError(executionContext, context, locator);
}
- xalan::CharVectorType fileNameVector;
- std::string fileNameString;
+ xalan::CharVectorType castHelper;
+ arguments[0]->str().transcode(castHelper);
- arguments[0]->str().transcode(fileNameVector);
+ std::string fileName(this->path_);
+ fileName.reserve(fileName.size() + castHelper.size());
std::move(
- fileNameVector.begin(),
- fileNameVector.end(),
- fileNameString.begin()
+ castHelper.begin(),
+ castHelper.end(),
+ fileName.end()
);
return executionContext.getXObjectFactory().createString(
xalan::XalanDOMString(
- InputXSLT::readFile(fileNameString).data()
+ InputXSLT::readFile(fileName).data()
)
);
}
diff --git a/src/function/read_file.h b/src/function/read_file.h
index dbcf3d6..f800c37 100644
--- a/src/function/read_file.h
+++ b/src/function/read_file.h
@@ -8,12 +8,16 @@
#include
#include
-#include "utility.h"
+#include
+
+#include "common.h"
namespace InputXSLT {
class FunctionReadFile : public xalan::Function {
public:
+ FunctionReadFile(const std::string&);
+
virtual xalan::XObjectPtr execute(
xalan::XPathExecutionContext&,
xalan::XalanNode*,
@@ -27,6 +31,8 @@ class FunctionReadFile : public xalan::Function {
bool operator==(const FunctionReadFile&) const = delete;
private:
+ const std::string path_;
+
const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const;
};
diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc
index 616d189..be091a0 100644
--- a/src/function/read_xml_file.cc
+++ b/src/function/read_xml_file.cc
@@ -2,10 +2,12 @@
namespace InputXSLT {
-FunctionReadXmlFile::FunctionReadXmlFile():
+FunctionReadXmlFile::FunctionReadXmlFile(const std::string& path):
+ path_(path),
parser_() { }
-FunctionReadXmlFile::FunctionReadXmlFile(const FunctionReadXmlFile&):
+FunctionReadXmlFile::FunctionReadXmlFile(const FunctionReadXmlFile& src):
+ path_(src.path_),
parser_() { }
xalan::XObjectPtr FunctionReadXmlFile::execute(
@@ -22,9 +24,12 @@ xalan::XObjectPtr FunctionReadXmlFile::execute(
generalError(executionContext, context, locator);
}
+ xalan::XalanDOMString fileName(this->path_.data());
+ fileName.append(arguments[0]->str());
+
return executionContext.getXObjectFactory().createNodeSet(
this->parser_.parseXMLStream(
- xalan::XSLTInputSource(arguments[0]->str())
+ xalan::XSLTInputSource(fileName)
)
);
}
diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h
index 1556764..c755852 100644
--- a/src/function/read_xml_file.h
+++ b/src/function/read_xml_file.h
@@ -9,13 +9,15 @@
#include
#include
-#include "utility.h"
+#include
+
+#include "common.h"
namespace InputXSLT {
class FunctionReadXmlFile : public xalan::Function {
public:
- FunctionReadXmlFile();
+ FunctionReadXmlFile(const std::string&);
FunctionReadXmlFile(const FunctionReadXmlFile&);
virtual xalan::XObjectPtr execute(
@@ -31,6 +33,7 @@ class FunctionReadXmlFile : public xalan::Function {
bool operator==(const FunctionReadXmlFile&) const = delete;
private:
+ const std::string path_;
mutable xalan::XercesParserLiaison parser_;
const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const;
diff --git a/src/plattform_guard.h b/src/plattform_guard.h
index 9e99fdc..fe8b954 100644
--- a/src/plattform_guard.h
+++ b/src/plattform_guard.h
@@ -2,9 +2,11 @@
#define INPUTXSLT_SRC_PLATTFORM_GUARD_H_
#include
+#include
+
#include
-namespace xalan = xalanc_1_11;
+#include "common.h"
namespace InputXSLT {
diff --git a/src/transformer_facade.cc b/src/transformer_facade.cc
new file mode 100644
index 0000000..be2a6a2
--- /dev/null
+++ b/src/transformer_facade.cc
@@ -0,0 +1,66 @@
+#include "transformer_facade.h"
+
+#include
+#include
+
+#include
+#include
+#include
+#include
+
+#include
+#include
+
+#include "function/read_file.h"
+#include "function/read_xml_file.h"
+
+namespace InputXSLT {
+
+TransformerFacade::TransformerFacade(const std::string& path):
+ parser_(),
+ transformer_() {
+ const xalan::XalanDOMString customNamespace(
+ "http://ExternalFunction.xalan-c++.xml.apache.org"
+ );
+
+ this->transformer_.installExternalFunction(
+ customNamespace,
+ xalan::XalanDOMString("read-file"),
+ InputXSLT::FunctionReadFile(path)
+ );
+
+ this->transformer_.installExternalFunction(
+ customNamespace,
+ xalan::XalanDOMString("read-xml-file"),
+ InputXSLT::FunctionReadXmlFile(path)
+ );
+
+}
+
+int TransformerFacade::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_facade.h b/src/transformer_facade.h
new file mode 100644
index 0000000..c11fa78
--- /dev/null
+++ b/src/transformer_facade.h
@@ -0,0 +1,28 @@
+#ifndef INPUTXSLT_SRC_TRANSFORMER_FACADE_H_
+#define INPUTXSLT_SRC_TRANSFORMER_FACADE_H_
+
+#include
+
+#include
+#include
+
+#include "common.h"
+
+namespace InputXSLT {
+
+class TransformerFacade {
+ public:
+ TransformerFacade(const std::string&);
+
+ int execute(const std::string&, const std::string&);
+
+ private:
+ mutable xalan::XercesParserLiaison parser_;
+
+ xalan::XalanTransformer transformer_;
+
+};
+
+}
+
+#endif // INPUTXSLT_SRC_TRANSFORMER_FACADE_H_
diff --git a/src/transformer_guard.cc b/src/transformer_guard.cc
deleted file mode 100644
index 73bcea2..0000000
--- a/src/transformer_guard.cc
+++ /dev/null
@@ -1,67 +0,0 @@
-#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
deleted file mode 100644
index 381fb94..0000000
--- a/src/transformer_guard.h
+++ /dev/null
@@ -1,29 +0,0 @@
-#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/test.cc b/test.cc
index 09325f7..44c2484 100644
--- a/test.cc
+++ b/test.cc
@@ -1,9 +1,11 @@
-#include "transformer_guard.h"
+#include "plattform_guard.h"
+#include "transformer_facade.h"
int main() {
- InputXSLT::TransformerGuard guard;
+ InputXSLT::PlattformGuard plattform;
+ InputXSLT::TransformerFacade transformer("../dummy/");
- return guard.execute(
+ return transformer.execute(
"../dummy/transform.xsl",
"out.xml"
);
--
cgit v1.2.3