aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dummy/in.xml7
-rw-r--r--dummy/test.txt1
-rw-r--r--dummy/transform.xsl31
-rw-r--r--src/read_file_command.h60
-rw-r--r--src/utility.h48
-rw-r--r--test.cc37
6 files changed, 184 insertions, 0 deletions
diff --git a/dummy/in.xml b/dummy/in.xml
new file mode 100644
index 0000000..7b512bc
--- /dev/null
+++ b/dummy/in.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<items>
+ <item>Test1</item>
+ <item>Test2</item>
+ <item>Test3</item>
+ <item>Test4</item>
+</items>
diff --git a/dummy/test.txt b/dummy/test.txt
new file mode 100644
index 0000000..cd08755
--- /dev/null
+++ b/dummy/test.txt
@@ -0,0 +1 @@
+Hello world!
diff --git a/dummy/transform.xsl b/dummy/transform.xsl
new file mode 100644
index 0000000..dca15a1
--- /dev/null
+++ b/dummy/transform.xsl
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0"
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+ xmlns:external="http://ExternalFunction.xalan-c++.xml.apache.org">
+
+<xsl:output method="xml"
+ doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
+ doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
+ omit-xml-declaration="yes"
+ encoding="UTF-8"
+ indent="yes" />
+
+<xsl:template match="/">
+<html>
+ <head>
+ </head>
+ <body>
+ <xsl:apply-templates select="items/item" />
+ </body>
+</html>
+</xsl:template>
+
+<xsl:template match="items/item">
+ <div id="{.}">
+ <xsl:value-of select="external:read-file('dummy/test.txt')" />
+ </div>
+</xsl:template>
+
+</xsl:stylesheet>
+
+
diff --git a/src/read_file_command.h b/src/read_file_command.h
new file mode 100644
index 0000000..88c20bf
--- /dev/null
+++ b/src/read_file_command.h
@@ -0,0 +1,60 @@
+#include <xalanc/Include/PlatformDefinitions.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+#include <xalanc/XalanTransformer/XalanTransformer.hpp>
+#include <xalanc/XPath/XObjectFactory.hpp>
+#include <xalanc/XPath/Function.hpp>
+
+#include "utility.h"
+
+XALAN_USING_XALAN(Function)
+XALAN_USING_XALAN(Locator)
+XALAN_USING_XALAN(XPathExecutionContext)
+XALAN_USING_XALAN(XalanDOMString)
+XALAN_USING_XALAN(XalanNode)
+XALAN_USING_XALAN(XObjectPtr)
+XALAN_USING_XALAN(MemoryManager)
+XALAN_USING_XALAN(XalanCopyConstruct)
+
+class FunctionFileRead : public Function {
+ public:
+ virtual XObjectPtr execute(
+ XPathExecutionContext& executionContext,
+ XalanNode* context,
+ const XObjectArgVectorType& args,
+ const Locator* locator
+ ) const {
+ if ( args.size() != 1 ) {
+ XPathExecutionContext::GetAndReleaseCachedString guard(executionContext);
+
+ generalError(executionContext, context, locator);
+ }
+
+ xalanc_1_11::CharVectorType tmpFileName;
+ std::string fileName;
+
+ args[0]->str().transcode(tmpFileName);
+ std::move(tmpFileName.begin(), tmpFileName.end(), fileName.begin());
+
+ std::string content(readFile(fileName));
+
+ return executionContext.getXObjectFactory().createString(
+ XalanDOMString(content.data())
+ );
+ }
+
+ virtual FunctionFileRead* clone(MemoryManager& manager) const {
+ return XalanCopyConstruct(manager, *this);
+ }
+
+ protected:
+ const XalanDOMString& getError(XalanDOMString& result) const {
+ result.assign("The read-file() function expects one argument.");
+
+ return result;
+ }
+
+ private:
+ FunctionFileRead& operator=(const FunctionFileRead&);
+ bool operator==(const FunctionFileRead&) const;
+
+};
diff --git a/src/utility.h b/src/utility.h
new file mode 100644
index 0000000..7050cee
--- /dev/null
+++ b/src/utility.h
@@ -0,0 +1,48 @@
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <cstddef>
+#include <cstdio>
+
+namespace {
+
+const int OpenFlags = O_RDONLY;
+const mode_t OpenMode = S_IRUSR | S_IWUSR;
+
+}
+
+std::string readFile(const std::string& path) {
+ int descriptor(
+ open(path.data(), OpenFlags, OpenMode)
+ );
+
+ if ( descriptor == -1 ) {
+ close(descriptor);
+
+ return "io error";
+ } else {
+ struct stat info;
+ fstat(descriptor, &info);
+ const std::size_t size(info.st_size);
+
+ char* buffer(new char[size]);
+
+ ssize_t readSize(read(
+ descriptor,
+ reinterpret_cast<void*>(buffer),
+ size
+ ));
+
+ close(descriptor);
+
+ std::string content(
+ buffer,
+ readSize
+ );
+
+ delete[] buffer;
+
+ return content;
+ }
+}
diff --git a/test.cc b/test.cc
new file mode 100644
index 0000000..c922abf
--- /dev/null
+++ b/test.cc
@@ -0,0 +1,37 @@
+#include <xalanc/Include/PlatformDefinitions.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+#include <xalanc/XalanTransformer/XalanTransformer.hpp>
+#include <xalanc/XSLT/XSLTInputSource.hpp>
+
+#include "src/read_file_command.h"
+
+int main() {
+ xercesc::XMLPlatformUtils::Initialize();
+ xalanc_1_11::XalanTransformer::initialize();
+
+ const xalanc_1_11::XalanDOMString customNamespace(
+ "http://ExternalFunction.xalan-c++.xml.apache.org"
+ );
+
+ xalanc_1_11::XalanTransformer transformer;
+
+ transformer.installExternalFunction(
+ customNamespace,
+ xalanc_1_11::XalanDOMString("read-file"),
+ FunctionFileRead()
+ );
+
+ xalanc_1_11::XSLTInputSource input("dummy/in.xml");
+ xalanc_1_11::XSLTInputSource tranformation("dummy/transform.xsl");
+ xalanc_1_11::XSLTResultTarget output("dummy/out.xml");
+
+ int result(transformer.transform(
+ input,
+ tranformation,
+ output
+ ));
+
+ xalanc_1_11::XalanTransformer::terminate();
+
+ return result;
+}