diff options
author | Adrian Kummerländer | 2014-04-27 17:24:30 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2014-04-27 17:24:30 +0200 |
commit | 947a8389728ff7d052fa820f598da3c17802f3d1 (patch) | |
tree | c24c7f19ea9b7b6fb6999e1c065b38598ebd27df /src/function | |
parent | 5fde046561caa11a58abd13cf1f469fdce5c53f1 (diff) | |
download | InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.tar InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.tar.gz InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.tar.bz2 InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.tar.lz InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.tar.xz InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.tar.zst InputXSLT-947a8389728ff7d052fa820f598da3c17802f3d1.zip |
Added status information to external read-xml-file function
* XML tree is contained below the _content_ node if read was successful
** status is set to error otherwise
* updated test transformation accordingly
Diffstat (limited to 'src/function')
-rw-r--r-- | src/function/read_directory.cc | 1 | ||||
-rw-r--r-- | src/function/read_file.cc | 4 | ||||
-rw-r--r-- | src/function/read_xml_file.cc | 88 | ||||
-rw-r--r-- | src/function/read_xml_file.h | 6 |
4 files changed, 78 insertions, 21 deletions
diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index 1e7e7d7..6fcb447 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -4,7 +4,6 @@ #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> #include <xercesc/dom/DOMText.hpp> -#include <xercesc/util/XMLString.hpp> #include "support/utility.h" #include "support/xerces_string_guard.h" diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 6f02702..cfbec59 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -4,14 +4,12 @@ #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> #include <xercesc/dom/DOMText.hpp> -#include <xercesc/util/XMLString.hpp> #include "boost/filesystem/fstream.hpp" #include <fstream> #include "support/xerces_string_guard.h" -#include "support/utility.h" namespace InputXSLT { @@ -30,7 +28,7 @@ xalan::XObjectPtr FunctionReadFile::execute( ); DomDocumentCache::item* const cachedDocument( - this->document_cache_->get(xalanToString(argument->str())) + this->document_cache_->get(filePath.string()) ); if ( !cachedDocument->isFinalized() ) { diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index d429f34..52f7735 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -1,16 +1,20 @@ #include "read_xml_file.h" +#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" + namespace InputXSLT { FunctionReadXmlFile::FunctionReadXmlFile(const FilesystemContext& context): fs_context_(context), - parser_() { } - -FunctionReadXmlFile::FunctionReadXmlFile(const FunctionReadXmlFile& src): - fs_context_(src.fs_context_), - parser_() { } + document_cache_(std::make_shared<DomDocumentCache>()) { } xalan::XObjectPtr FunctionReadXmlFile::execute( xalan::XPathExecutionContext& executionContext, @@ -22,19 +26,75 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( this->fs_context_.resolve(argument->str()) ); - if ( boost::filesystem::is_regular_file(filePath) ) { - boost::filesystem::ifstream file(filePath); + DomDocumentCache::item* const cachedDocument( + this->document_cache_->get(filePath.string()) + ); - return executionContext.getXObjectFactory().createNodeSet( - this->parser_.parseXMLStream( - xalan::XSLTInputSource(file) - ) + if ( !cachedDocument->isFinalized() ) { + xercesc::DOMDocument* const domDocument( + cachedDocument->getXercesDocument() ); - } else { - return executionContext.getXObjectFactory().createString( - xalan::XalanDOMString("io error") + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() ); + + if ( boost::filesystem::is_regular_file(filePath) ) { + xercesc::DOMElement* const contentNode( + domDocument->createElement(*XercesStringGuard("content")) + ); + + xercesc::XercesDOMParser parser; + boost::filesystem::ifstream file(filePath); + parser.parse(xalan::XSLTInputSource(file)); + + xercesc::DOMNode* const contentTreeNode( + domDocument->importNode( + parser.getDocument()->getDocumentElement(), + true + ) + ); + + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard("status")) + ); + + xercesc::DOMText* const resultTextNode( + domDocument->createTextNode( + *XercesStringGuard("successful") + ) + ); + + contentNode->appendChild(contentTreeNode); + resultNode->appendChild(resultTextNode); + + rootNode->appendChild(contentNode); + rootNode->appendChild(resultNode); + } else { + xercesc::DOMElement* const resultNode( + domDocument->createElement(*XercesStringGuard("status")) + ); + + xercesc::DOMText* const resultTextNode( + domDocument->createTextNode( + *XercesStringGuard("error") + ) + ); + + resultNode->appendChild(resultTextNode); + rootNode->appendChild(resultNode); + } } + + xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( + executionContext + ); + + nodeList->addNodes( + *cachedDocument->getXalanDocument()->getDocumentElement()->getChildNodes() + ); + + return executionContext.getXObjectFactory().createNodeSet(nodeList); } FunctionReadXmlFile* FunctionReadXmlFile::clone( diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h index 775ec96..1df48fb 100644 --- a/src/function/read_xml_file.h +++ b/src/function/read_xml_file.h @@ -5,19 +5,19 @@ #include <xalanc/XPath/XObjectFactory.hpp> #include <xalanc/XPath/Function.hpp> #include <xalanc/XPath/XObject.hpp> -#include <xalanc/XercesParserLiaison/XercesParserLiaison.hpp> #include <string> +#include <memory> #include "common.h" #include "support/filesystem_context.h" +#include "support/dom/document_cache.h" namespace InputXSLT { class FunctionReadXmlFile : public xalan::Function { public: FunctionReadXmlFile(const FilesystemContext&); - FunctionReadXmlFile(const FunctionReadXmlFile&); virtual xalan::XObjectPtr execute( xalan::XPathExecutionContext&, @@ -33,7 +33,7 @@ class FunctionReadXmlFile : public xalan::Function { private: const FilesystemContext& fs_context_; - mutable xalan::XercesParserLiaison parser_; + std::shared_ptr<DomDocumentCache> document_cache_; const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const; |