diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/read_file_command.h | 39 | ||||
-rw-r--r-- | src/read_xml_file_command.h | 39 | ||||
-rw-r--r-- | src/utility.h | 10 |
3 files changed, 49 insertions, 39 deletions
diff --git a/src/read_file_command.h b/src/read_file_command.h index 837ec04..5e47c36 100644 --- a/src/read_file_command.h +++ b/src/read_file_command.h @@ -7,17 +7,17 @@ #include "utility.h" -namespace xalan = xalanc_1_11; +namespace InputXSLT { class FunctionReadFile : public xalan::Function { public: virtual xalan::XObjectPtr execute( - xalan::XPathExecutionContext& executionContext, - xalan::XalanNode* context, - const xalan::Function::XObjectArgVectorType& args, + xalan::XPathExecutionContext& executionContext, + xalan::XalanNode* context, + const xalan::Function::XObjectArgVectorType& arguments, const xalan::Locator* locator ) const { - if ( args.size() != 1 ) { + if ( arguments.size() != 1 ) { xalan::XPathExecutionContext::GetAndReleaseCachedString guard( executionContext ); @@ -25,21 +25,21 @@ class FunctionReadFile : public xalan::Function { generalError(executionContext, context, locator); } - xalan::CharVectorType tmpFileName; - std::string fileName; + xalan::CharVectorType fileNameVector; + std::string fileNameString; - args[0]->str().transcode(tmpFileName); + arguments[0]->str().transcode(fileNameVector); std::move( - tmpFileName.begin(), - tmpFileName.end(), - fileName.begin() + fileNameVector.begin(), + fileNameVector.end(), + fileNameString.begin() ); - std::string content(readFile(fileName)); - return executionContext.getXObjectFactory().createString( - xalan::XalanDOMString(content.data()) + xalan::XalanDOMString( + InputXSLT::readFile(fileNameString).data() + ) ); } @@ -47,15 +47,16 @@ class FunctionReadFile : public xalan::Function { return xalan::XalanCopyConstruct(manager, *this); } - protected: + FunctionReadFile& operator=(const FunctionReadFile&) = delete; + bool operator==(const FunctionReadFile&) const = delete; + + private: const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const { result.assign("The read-file() function expects one argument."); return result; } - private: - FunctionReadFile& operator=(const FunctionReadFile&); - bool operator==(const FunctionReadFile&) const; - }; + +} diff --git a/src/read_xml_file_command.h b/src/read_xml_file_command.h index 96b6c5d..8aee39f 100644 --- a/src/read_xml_file_command.h +++ b/src/read_xml_file_command.h @@ -8,17 +8,19 @@ #include "utility.h" -namespace xalan = xalanc_1_11; +#include <memory> + +namespace InputXSLT { class FunctionReadXmlFile : public xalan::Function { public: virtual xalan::XObjectPtr execute( - xalan::XPathExecutionContext& executionContext, - xalan::XalanNode* context, - const xalan::Function::XObjectArgVectorType& args, - const xalan::Locator* locator + xalan::XPathExecutionContext& executionContext, + xalan::XalanNode* context, + const xalan::Function::XObjectArgVectorType& arguments, + const xalan::Locator* locator ) const { - if ( args.size() != 1 ) { + if ( arguments.size() != 1 ) { xalan::XPathExecutionContext::GetAndReleaseCachedString guard( executionContext ); @@ -26,14 +28,14 @@ class FunctionReadXmlFile : public xalan::Function { generalError(executionContext, context, locator); } - if ( this->liaison == nullptr ) { - const_cast<FunctionReadXmlFile*>(this)->liaison = new xalan::XercesParserLiaison(); + if ( !this->parser_ ) { + this->parser_ = std::make_shared<xalan::XercesParserLiaison>(); } - xalan::XSLTInputSource file(args[0]->str()); - return executionContext.getXObjectFactory().createNodeSet( - liaison->parseXMLStream(file) + this->parser_->parseXMLStream( + xalan::XSLTInputSource(arguments[0]->str()) + ) ); } @@ -41,17 +43,18 @@ class FunctionReadXmlFile : public xalan::Function { return xalan::XalanCopyConstruct(manager, *this); } - protected: - xalan::XercesParserLiaison* liaison = nullptr; + FunctionReadXmlFile& operator=(const FunctionReadXmlFile&) = delete; + bool operator==(const FunctionReadXmlFile&) const = delete; + + private: + mutable std::shared_ptr<xalan::XercesParserLiaison> parser_; const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const { - result.assign("The read-file() function expects one argument."); + result.assign("The read-xml-file() function expects one argument."); return result; } - private: - FunctionReadXmlFile& operator=(const FunctionReadXmlFile&); - bool operator==(const FunctionReadXmlFile&) const; - }; + +} diff --git a/src/utility.h b/src/utility.h index 9c88b38..82f52aa 100644 --- a/src/utility.h +++ b/src/utility.h @@ -8,6 +8,8 @@ #include <cstddef> #include <cstdio> +namespace xalan = xalanc_1_11; + namespace { const int OpenFlags = O_RDONLY; @@ -15,6 +17,8 @@ const mode_t OpenMode = S_IRUSR | S_IWUSR; } +namespace InputXSLT { + std::string readFile(const std::string& path) { int descriptor( open(path.data(), OpenFlags, OpenMode) @@ -29,11 +33,11 @@ std::string readFile(const std::string& path) { fstat(descriptor, &info); const std::size_t size(info.st_size); - char* buffer(new char[size]); + char* const buffer(new char[size]); ssize_t readSize(read( descriptor, - reinterpret_cast<void*>(buffer), + static_cast<void*const>(buffer), size )); @@ -50,4 +54,6 @@ std::string readFile(const std::string& path) { } } +} + #endif // UTILITY_H_ |