From 947a8389728ff7d052fa820f598da3c17802f3d1 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 27 Apr 2014 17:24:30 +0200 Subject: 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 --- src/function/read_directory.cc | 1 - src/function/read_file.cc | 4 +- src/function/read_xml_file.cc | 88 +++++++++++++++++++++++++++++++++++------- src/function/read_xml_file.h | 6 +-- 4 files changed, 78 insertions(+), 21 deletions(-) (limited to 'src') 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 #include #include -#include #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 #include #include -#include #include "boost/filesystem/fstream.hpp" #include #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 +#include +#include +#include +#include + #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()) { } 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 #include #include -#include #include +#include #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 document_cache_; const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const; -- cgit v1.2.3