diff options
author | Adrian Kummerländer | 2014-05-24 13:59:42 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2014-05-24 13:59:42 +0200 |
commit | 085935ddd577b0c65b4330318b5ba20492d93126 (patch) | |
tree | b508590d23a3fe7f3a6813313af2abe17fecdf79 /src | |
parent | afc8eb29c22447fe2bf71a503a5f2d25b4f8a7c7 (diff) | |
download | InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.tar InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.tar.gz InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.tar.bz2 InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.tar.lz InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.tar.xz InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.tar.zst InputXSLT-085935ddd577b0c65b4330318b5ba20492d93126.zip |
Implemented ResultNodeFacade as a DOM node construction helper
* wraps result node construction and appends it to root node on destruction
* offers a simpler interface for common node construction patterns
* simplifies result node construction in all external function implementations
** most noticeable in FunctionReadDirectory
* expanded FunctionReadDirectory result nodes by name, extension, and full-path nodes
Diffstat (limited to 'src')
-rw-r--r-- | src/function/read_directory.cc | 40 | ||||
-rw-r--r-- | src/function/read_file.cc | 29 | ||||
-rw-r--r-- | src/function/read_xml_file.cc | 22 | ||||
-rw-r--r-- | src/function/transform.cc | 18 | ||||
-rw-r--r-- | src/support/dom/result_node_facade.cc | 61 | ||||
-rw-r--r-- | src/support/dom/result_node_facade.h | 35 |
6 files changed, 122 insertions, 83 deletions
diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index 215a59e..a3995ec 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -3,9 +3,9 @@ #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> -#include <xercesc/dom/DOMText.hpp> #include "support/xerces_string_guard.h" +#include "support/dom/result_node_facade.h" namespace InputXSLT { @@ -33,52 +33,34 @@ xercesc::DOMDocument* FunctionReadDirectory::constructDocument( fsContext.iterate( directoryPath, [&domDocument, &rootNode](const boost::filesystem::path& p) { - xercesc::DOMElement* const itemNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("result")) - ); + ResultNodeFacade result(domDocument, rootNode, "result"); switch ( boost::filesystem::status(p).type() ) { case boost::filesystem::regular_file: { - itemNode->setAttribute( - *XercesStringGuard<XMLCh>("type"), - *XercesStringGuard<XMLCh>("file") - ); + result.setAttribute("type", "file"); + result.setValueNode("name", p.stem().string()); + result.setValueNode("extension", p.extension().string()); break; }; case boost::filesystem::directory_file: { - itemNode->setAttribute( - *XercesStringGuard<XMLCh>("type"), - *XercesStringGuard<XMLCh>("directory") - ); + result.setAttribute("type", "directory"); + result.setValueNode("name", p.filename().string()); break; }; default: { - itemNode->setAttribute( - *XercesStringGuard<XMLCh>("type"), - *XercesStringGuard<XMLCh>("misc") - ); + result.setAttribute("type", "misc"); + result.setValueNode("name", p.filename().string()); break; }; } - xercesc::DOMText* const textNode( - domDocument->createTextNode( - *XercesStringGuard<XMLCh>(p.filename().string()) - ) - ); - - itemNode->appendChild(textNode); - rootNode->appendChild(itemNode); + result.setValueNode("full", boost::filesystem::canonical(p).string()); }); } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); - - rootNode->appendChild(resultNode); + ResultNodeFacade result(domDocument, rootNode, "error"); } return domDocument; diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 1a870a6..87423ab 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -3,11 +3,11 @@ #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> -#include <xercesc/dom/DOMText.hpp> #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.h" +#include "support/dom/result_node_facade.h" namespace { @@ -51,29 +51,12 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument( ); if ( boost::filesystem::is_regular_file(filePath) ) { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("result")) - ); - - resultNode->setAttribute( - *XercesStringGuard<XMLCh>("name"), - *XercesStringGuard<XMLCh>(filePath.filename().string()) - ); - - xercesc::DOMText* const resultTextNode( - domDocument->createTextNode( - *XercesStringGuard<XMLCh>(readFile(filePath)) - ) - ); - - resultNode->appendChild(resultTextNode); - rootNode->appendChild(resultNode); - } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); + ResultNodeFacade result(domDocument, rootNode, "result"); - rootNode->appendChild(resultNode); + result.setAttribute("name", filePath.filename().string()); + result.setContent(readFile(filePath)); + } else { + ResultNodeFacade result(domDocument, rootNode, "error"); } return domDocument; diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index 4eb44e5..6d0fd04 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -5,12 +5,12 @@ #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> -#include <xercesc/dom/DOMText.hpp> #include <xercesc/parsers/XercesDOMParser.hpp> #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.h" +#include "support/dom/result_node_facade.h" namespace { @@ -61,27 +61,15 @@ xercesc::DOMDocument* FunctionReadXmlFile::constructDocument( ); if ( boost::filesystem::is_regular_file(filePath) ) { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("result")) - ); + ResultNodeFacade result(domDocument, rootNode, "result"); - resultNode->setAttribute( - *XercesStringGuard<XMLCh>("name"), - *XercesStringGuard<XMLCh>(filePath.filename().string()) - ); + result.setAttribute("name", filePath.filename().string()); - xercesc::DOMNode* const resultTreeNode( + result.setContent( importDocumentElement(filePath, domDocument) ); - - resultNode->appendChild(resultTreeNode); - rootNode->appendChild(resultNode); } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); - - rootNode->appendChild(resultNode); + ResultNodeFacade result(domDocument, rootNode, "error"); } return domDocument; diff --git a/src/function/transform.cc b/src/function/transform.cc index cf6ff09..fd28b34 100644 --- a/src/function/transform.cc +++ b/src/function/transform.cc @@ -6,6 +6,7 @@ #include "transformation_facade.h" #include "support/xerces_string_guard.h" +#include "support/dom/result_node_facade.h" namespace InputXSLT { @@ -43,22 +44,11 @@ xercesc::DOMDocument* FunctionTransform::constructDocument( ); if ( transformation.generate(targetPath, parameterObject) == 0 ) { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("result")) - ); + ResultNodeFacade result(domDocument, rootNode, "result"); - resultNode->setAttribute( - *XercesStringGuard<XMLCh>("name"), - *XercesStringGuard<XMLCh>(targetPath) - ); - - rootNode->appendChild(resultNode); + result.setAttribute("name", targetPath); } else { - xercesc::DOMElement* const resultNode( - domDocument->createElement(*XercesStringGuard<XMLCh>("error")) - ); - - rootNode->appendChild(resultNode); + ResultNodeFacade result(domDocument, rootNode, "error"); } return domDocument; diff --git a/src/support/dom/result_node_facade.cc b/src/support/dom/result_node_facade.cc new file mode 100644 index 0000000..597448b --- /dev/null +++ b/src/support/dom/result_node_facade.cc @@ -0,0 +1,61 @@ +#include "result_node_facade.h" + +#include <xercesc/dom/DOMText.hpp> + +#include "support/xerces_string_guard.h" + +namespace InputXSLT { + +ResultNodeFacade::ResultNodeFacade( + xercesc::DOMDocument* document, + xercesc::DOMNode* node, + const std::string& name +): + dom_document_(document), + result_node_( + dom_document_->createElement(*XercesStringGuard<XMLCh>(name)) + ), + root_node_(node) { } + +ResultNodeFacade::~ResultNodeFacade() { + this->root_node_->appendChild(this->result_node_); +} + +void ResultNodeFacade::setAttribute( + const std::string& name, + const std::string& value +) { + this->result_node_->setAttribute( + *XercesStringGuard<XMLCh>(name), + *XercesStringGuard<XMLCh>(value) + ); +} + +void ResultNodeFacade::setValueNode( + const std::string& name, + const std::string& value +) { + xercesc::DOMElement* const nameNode( + this->dom_document_->createElement(*XercesStringGuard<XMLCh>(name)) + ); + + xercesc::DOMText* const valueNode( + this->dom_document_->createTextNode(*XercesStringGuard<XMLCh>(value)) + ); + + nameNode->appendChild(valueNode); + + this->result_node_->appendChild(nameNode); +} + +void ResultNodeFacade::setContent(const std::string& content) { + this->result_node_->appendChild( + this->dom_document_->createTextNode(*XercesStringGuard<XMLCh>(content)) + ); +} + +void ResultNodeFacade::setContent(xercesc::DOMNode* node) { + this->result_node_->appendChild(node); +} + +} diff --git a/src/support/dom/result_node_facade.h b/src/support/dom/result_node_facade.h new file mode 100644 index 0000000..ccdd850 --- /dev/null +++ b/src/support/dom/result_node_facade.h @@ -0,0 +1,35 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_RESULT_NODE_FACADE_H_ +#define INPUTXSLT_SRC_SUPPORT_RESULT_NODE_FACADE_H_ + +#include <xercesc/dom/DOMDocument.hpp> +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMElement.hpp> + +#include <string> + +namespace InputXSLT { + +class ResultNodeFacade { + public: + ResultNodeFacade( + xercesc::DOMDocument*, + xercesc::DOMNode*, + const std::string& + ); + ~ResultNodeFacade(); + + void setAttribute(const std::string&, const std::string&); + void setValueNode(const std::string&, const std::string&); + void setContent(const std::string&); + void setContent(xercesc::DOMNode*); + + private: + xercesc::DOMDocument* const dom_document_; + xercesc::DOMElement* const result_node_; + xercesc::DOMNode* const root_node_; + +}; + +} + +#endif // INPUTXSLT_SRC_SUPPORT_RESULT_NODE_FACADE_H_ |