aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-04-24 22:18:45 +0200
committerAdrian Kummerländer2014-04-24 22:18:45 +0200
commit78d3873061f1a974da4d0ccdcc1778c6a11139e8 (patch)
tree26592ea099449a8bd0617ee534990b32ff9bb25b
parentff92d0af44fa454d066f6ee3fe2becd97206b64e (diff)
downloadInputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.tar
InputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.tar.gz
InputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.tar.bz2
InputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.tar.lz
InputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.tar.xz
InputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.tar.zst
InputXSLT-78d3873061f1a974da4d0ccdcc1778c6a11139e8.zip
Added XercesStringGuard scope-guard to manage XMLCh
* xercesc requires XMLCh* strings to be hand-allocated and released using the XMLString class * XercesStringGuard works as a scope-guard for XMLCh* string lifetime and greatly simplifies xerces DOM construction
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/function/read_directory.cc38
-rw-r--r--src/support/xerces_string_guard.cc18
-rw-r--r--src/support/xerces_string_guard.h24
4 files changed, 61 insertions, 20 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ee6e7e3..4d50af0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,7 @@ add_executable(
src/function/read_directory.cc
src/support/filesystem_context.cc
src/support/dom_document_guard.cc
+ src/support/xerces_string_guard.cc
)
target_link_libraries(
diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc
index 22653b6..245f311 100644
--- a/src/function/read_directory.cc
+++ b/src/function/read_directory.cc
@@ -6,6 +6,8 @@
#include <xercesc/dom/DOMText.hpp>
#include <xercesc/util/XMLString.hpp>
+#include "support/xerces_string_guard.h"
+
namespace InputXSLT {
FunctionReadDirectory::FunctionReadDirectory(const FilesystemContext& context):
@@ -34,31 +36,27 @@ xalan::XObjectPtr FunctionReadDirectory::execute(
this->fs_context_.iterate(
arguments[0]->str(),
[&domDocument, &rootNode](const boost::filesystem::path& p) {
- XMLCh* buffer = xercesc::XMLString::transcode("item");
- xercesc::DOMElement* const itemNode = domDocument->createElement(buffer);
- xercesc::XMLString::release(&buffer);
-
- buffer = xercesc::XMLString::transcode("type");
+ xercesc::DOMElement* const itemNode(
+ domDocument->createElement(*XercesStringGuard("item"))
+ );
if ( boost::filesystem::is_regular_file(p) ) {
- XMLCh* valueBuffer = xercesc::XMLString::transcode("file");
-
- itemNode->setAttribute(buffer, valueBuffer);
-
- xercesc::XMLString::release(&valueBuffer);
+ itemNode->setAttribute(
+ *XercesStringGuard("type"),
+ *XercesStringGuard("file")
+ );
} else if ( boost::filesystem::is_directory(p) ) {
- XMLCh* valueBuffer = xercesc::XMLString::transcode("directory");
-
- itemNode->setAttribute(buffer, valueBuffer);
-
- xercesc::XMLString::release(&valueBuffer);
+ itemNode->setAttribute(
+ *XercesStringGuard("type"),
+ *XercesStringGuard("directory")
+ );
}
- xercesc::XMLString::release(&buffer);
-
- buffer = xercesc::XMLString::transcode(p.filename().string().data());
- xercesc::DOMText* const textNode = domDocument->createTextNode(buffer);
- xercesc::XMLString::release(&buffer);
+ xercesc::DOMText* const textNode(
+ domDocument->createTextNode(
+ *XercesStringGuard(p.filename().string())
+ )
+ );
itemNode->appendChild(textNode);
rootNode->appendChild(itemNode);
diff --git a/src/support/xerces_string_guard.cc b/src/support/xerces_string_guard.cc
new file mode 100644
index 0000000..14ad29b
--- /dev/null
+++ b/src/support/xerces_string_guard.cc
@@ -0,0 +1,18 @@
+#include "xerces_string_guard.h"
+
+namespace InputXSLT {
+
+XercesStringGuard::XercesStringGuard(const std::string& src):
+ string_(xercesc::XMLString::transcode(src.data())) { }
+
+XercesStringGuard::~XercesStringGuard() {
+ xercesc::XMLString::release(
+ const_cast<XMLCh**>(&this->string_)
+ );
+}
+
+XMLCh* XercesStringGuard::operator*() {
+ return this->string_;
+}
+
+}
diff --git a/src/support/xerces_string_guard.h b/src/support/xerces_string_guard.h
new file mode 100644
index 0000000..f64a53e
--- /dev/null
+++ b/src/support/xerces_string_guard.h
@@ -0,0 +1,24 @@
+#ifndef INPUTXSLT_SRC_SUPPORT_XERCES_STRING_GUARD_H_
+#define INPUTXSLT_SRC_SUPPORT_XERCES_STRING_GUARD_H_
+
+#include <xercesc/util/XMLString.hpp>
+
+#include <string>
+
+namespace InputXSLT {
+
+class XercesStringGuard {
+ public:
+ XercesStringGuard(const std::string&);
+ ~XercesStringGuard();
+
+ XMLCh* operator*();
+
+ private:
+ XMLCh* const string_;
+
+};
+
+}
+
+#endif // INPUTXSLT_SRC_SUPPORT_XERCES_STRING_GUARD_H_