From 0b611b7bd28851fe8096b3d2c121c68e231ada11 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 26 Apr 2014 11:21:10 +0200 Subject: Implemented global DOM document cache * the plan to return XML-nodes from each external function requires a better way to manage the lifetime of many xerces DOM document instances and their support class instances ** this is why DomDocumentCache and DomDocumentCache::item were implemented ** based on std::map so we can easily access the result of old function calls * changed external read-directory function to return the children of the document node instead of the document node itself ** removes unnecessary cruft in function calls ** will make returning status codes alongside the function result more pleasing to the eye * updated test transformation to reflect new features --- src/support/dom/document_cache.cc | 26 ++++++++++++++++++ src/support/dom/document_cache.h | 27 ++++++++++++++++++ src/support/dom/document_cache_item.cc | 50 ++++++++++++++++++++++++++++++++++ src/support/dom/document_cache_item.h | 42 ++++++++++++++++++++++++++++ src/support/dom_document_guard.cc | 45 ------------------------------ src/support/dom_document_guard.h | 36 ------------------------ src/support/filesystem_context.cc | 14 +--------- src/support/utility.h | 18 ++++++++++++ 8 files changed, 164 insertions(+), 94 deletions(-) create mode 100644 src/support/dom/document_cache.cc create mode 100644 src/support/dom/document_cache.h create mode 100644 src/support/dom/document_cache_item.cc create mode 100644 src/support/dom/document_cache_item.h delete mode 100644 src/support/dom_document_guard.cc delete mode 100644 src/support/dom_document_guard.h create mode 100644 src/support/utility.h (limited to 'src/support') diff --git a/src/support/dom/document_cache.cc b/src/support/dom/document_cache.cc new file mode 100644 index 0000000..f814559 --- /dev/null +++ b/src/support/dom/document_cache.cc @@ -0,0 +1,26 @@ +#include "document_cache.h" + +namespace InputXSLT { + +DomDocumentCache::DomDocumentCache(): + map_() { } + +DomDocumentCache::item* DomDocumentCache::get(const std::string& key) { + auto itemIter = this->map_.find(key); + + if ( itemIter == this->map_.end() ) { + auto result = this->map_.emplace( + std::make_pair(key, std::unique_ptr(new item("content"))) + ); + + if ( result.second ) { + return (*(result.first)).second.get(); + } else { + return nullptr; + } + } else { + return (*itemIter).second.get(); + } +} + +} diff --git a/src/support/dom/document_cache.h b/src/support/dom/document_cache.h new file mode 100644 index 0000000..4447a18 --- /dev/null +++ b/src/support/dom/document_cache.h @@ -0,0 +1,27 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_CACHE_H_ +#define INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_CACHE_H_ + +#include +#include +#include + +namespace InputXSLT { + +class DomDocumentCache { + public: + class item; + + DomDocumentCache(); + + item* get(const std::string&); + + private: + std::map> map_; + +}; + +} + +#include "document_cache_item.h" + +#endif // INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_CACHE_H_ diff --git a/src/support/dom/document_cache_item.cc b/src/support/dom/document_cache_item.cc new file mode 100644 index 0000000..8cc1c24 --- /dev/null +++ b/src/support/dom/document_cache_item.cc @@ -0,0 +1,50 @@ +#include "support/dom/document_cache_item.h" + +#include +#include + +#include "support/xerces_string_guard.h" + +namespace InputXSLT { + +DomDocumentCache::item::item(const std::string& rootNode): + parser_(), + dom_support_(parser_), + document_( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard(rootNode), + nullptr + ) + ), + parsed_source_() { } + +DomDocumentCache::item::~item() { + this->document_->release(); +} + +bool DomDocumentCache::item::isFinalized() const { + return static_cast(this->parsed_source_); +} + +xercesc::DOMDocument* DomDocumentCache::item::getXercesDocument() const { + return this->document_; +} + +xalan::XalanDocument* DomDocumentCache::item::getXalanDocument() { + if ( this->parsed_source_ ) { + return this->parsed_source_->getDocument(); + } else { + this->parsed_source_.reset( + new xalan::XercesDOMWrapperParsedSource( + document_, + parser_, + dom_support_ + ) + ); + + return this->parsed_source_->getDocument(); + } +} + +} diff --git a/src/support/dom/document_cache_item.h b/src/support/dom/document_cache_item.h new file mode 100644 index 0000000..1f7152b --- /dev/null +++ b/src/support/dom/document_cache_item.h @@ -0,0 +1,42 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_CACHE_ITEM_H_ +#define INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_CACHE_ITEM_H_ + +#include +#include +#include + +#include + +#include +#include + +#include "common.h" +#include "document_cache.h" + +namespace InputXSLT { + +class DomDocumentCache::item { + public: + ~item(); + + bool isFinalized() const; + + xercesc::DOMDocument* getXercesDocument() const; + xalan::XalanDocument* getXalanDocument(); + + protected: + friend DomDocumentCache; + + item(const std::string&); + + private: + xalan::XercesParserLiaison parser_; + xalan::XercesDOMSupport dom_support_; + xercesc::DOMDocument* const document_; + std::unique_ptr parsed_source_; + +}; + +} + +#endif // INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_CACHE_ITEM_H_ diff --git a/src/support/dom_document_guard.cc b/src/support/dom_document_guard.cc deleted file mode 100644 index a83345f..0000000 --- a/src/support/dom_document_guard.cc +++ /dev/null @@ -1,45 +0,0 @@ -#include "support/dom_document_guard.h" - -#include -#include - -namespace InputXSLT { - -DomDocumentGuard::DomDocumentGuard(const std::string& rootNode): - parser_(), - dom_support_(parser_), - root_node_name_( - xercesc::XMLString::transcode(rootNode.data()) - ), - document_( - xercesc::DOMImplementation::getImplementation()->createDocument( - nullptr, - root_node_name_, - nullptr - ) - ), - parsed_source_() { } - -DomDocumentGuard::~DomDocumentGuard() { - xercesc::XMLString::release(&this->root_node_name_); - - this->document_->release(); -} - -xercesc::DOMDocument* DomDocumentGuard::operator->() { - return this->document_; -} - -xalan::XalanDocument* DomDocumentGuard::finalize() { - this->parsed_source_.reset( - new xalan::XercesDOMWrapperParsedSource( - document_, - parser_, - dom_support_ - ) - ); - - return this->parsed_source_->getDocument(); -} - -} diff --git a/src/support/dom_document_guard.h b/src/support/dom_document_guard.h deleted file mode 100644 index decca01..0000000 --- a/src/support/dom_document_guard.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_GUARD_H_ -#define INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_GUARD_H_ - -#include -#include -#include - -#include - -#include -#include - -#include "common.h" - -namespace InputXSLT { - -class DomDocumentGuard { - public: - DomDocumentGuard(const std::string&); - ~DomDocumentGuard(); - - xercesc::DOMDocument* operator->(); - xalan::XalanDocument* finalize(); - - private: - xalan::XercesParserLiaison parser_; - xalan::XercesDOMSupport dom_support_; - XMLCh* root_node_name_; - xercesc::DOMDocument* const document_; - std::unique_ptr parsed_source_; - -}; - -} - -#endif // INPUTXSLT_SRC_SUPPORT_DOM_DOCUMENT_GUARD_H_ diff --git a/src/support/filesystem_context.cc b/src/support/filesystem_context.cc index baeccf3..9ed9bbf 100644 --- a/src/support/filesystem_context.cc +++ b/src/support/filesystem_context.cc @@ -1,18 +1,6 @@ #include "filesystem_context.h" -namespace { - -inline std::string xalanToString(const xalan::XalanDOMString& text) { - xalan::CharVectorType castHelper; - text.transcode(castHelper); - - return std::string( - castHelper.begin(), - castHelper.end() - 1 - ); -} - -} +#include "support/utility.h" namespace InputXSLT { diff --git a/src/support/utility.h b/src/support/utility.h new file mode 100644 index 0000000..4c63c72 --- /dev/null +++ b/src/support/utility.h @@ -0,0 +1,18 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_UTILITY_H_ +#define INPUTXSLT_SRC_SUPPORT_UTILITY_H_ + +namespace { + +inline std::string xalanToString(const xalan::XalanDOMString& text) { + xalan::CharVectorType castHelper; + text.transcode(castHelper); + + return std::string( + castHelper.begin(), + castHelper.end() - 1 + ); +} + +} + +#endif // INPUTXSLT_SRC_SUPPORT_UTILITY_H_ -- cgit v1.2.3