diff options
Diffstat (limited to 'src/function')
-rw-r--r-- | src/function/read_directory.h | 2 | ||||
-rw-r--r-- | src/function/read_file.cc | 60 | ||||
-rw-r--r-- | src/function/read_file.h | 2 | ||||
-rw-r--r-- | src/function/transform.cc | 16 | ||||
-rw-r--r-- | src/function/transform.h | 4 | ||||
-rw-r--r-- | src/function/write_file.cc | 62 | ||||
-rw-r--r-- | src/function/write_file.h | 28 |
7 files changed, 143 insertions, 31 deletions
diff --git a/src/function/read_directory.h b/src/function/read_directory.h index 41976d5..898132a 100644 --- a/src/function/read_directory.h +++ b/src/function/read_directory.h @@ -1,7 +1,7 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_ #define INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_ -#include "boost/filesystem.hpp" +#include <boost/filesystem.hpp> #include "base.h" diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 96e677f..f27a38e 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -6,7 +6,8 @@ #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/framework/LocalFileInputSource.hpp> -#include "boost/filesystem/fstream.hpp" +#include <boost/optional.hpp> +#include <boost/filesystem/fstream.hpp> #include "support/xerces_string_guard.h" #include "support/dom/result_node_facade.h" @@ -18,7 +19,7 @@ inline bool isXmlFile(const boost::filesystem::path& filePath) { filePath.extension() == ".xsl"; } -inline xercesc::DOMNode* readXmlFile( +boost::optional<xercesc::DOMNode*> readXmlFile( const boost::filesystem::path& filePath, xercesc::DOMDocument* const domDocument ) { @@ -29,19 +30,32 @@ inline xercesc::DOMNode* readXmlFile( xercesc::XercesDOMParser parser; parser.parse(file); - return domDocument->importNode( - parser.getDocument()->getDocumentElement(), - true - ); + if ( parser.getErrorCount() == 0 ) { + return boost::make_optional( + domDocument->importNode( + parser.getDocument()->getDocumentElement(), + true + ) + ); + } else { + return boost::optional<xercesc::DOMNode*>(); + } } -inline std::string readPlainFile(const boost::filesystem::path& filePath) { +boost::optional<std::string> readPlainFile( + const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); - return std::string( - (std::istreambuf_iterator<char>(file)), - (std::istreambuf_iterator<char>()) - ); + if ( file.is_open() ) { + return boost::make_optional( + std::string( + (std::istreambuf_iterator<char>(file)), + (std::istreambuf_iterator<char>()) + ) + ); + } else { + return boost::optional<std::string>(); + } } } @@ -70,18 +84,26 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument( if ( isXmlFile(filePath) ) { result.setAttribute("type", "xml"); - result.setContent( - readXmlFile(filePath.string(), domDocument) - ); + if ( auto importedNode = readXmlFile(filePath, domDocument) ) { + result.setContent(*importedNode); + + result.setAttribute("result", "success"); + } else { + result.setAttribute("result", "error"); + } } else { result.setAttribute("type", "plain"); - result.setContent( - readPlainFile(filePath) - ); - } + if ( auto plainContent = readPlainFile(filePath) ) { + result.setContent(*plainContent); - result.setAttribute("result", "success"); + result.setAttribute("result", "success"); + } else { + result.setAttribute("result", "error"); + } + + result.setAttribute("result", "success"); + } } catch ( const xercesc::DOMException& exception ) { result.setAttribute("result", "error"); diff --git a/src/function/read_file.h b/src/function/read_file.h index 9c2185a..fdd70ac 100644 --- a/src/function/read_file.h +++ b/src/function/read_file.h @@ -1,7 +1,7 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_FILE_H_ #define INPUTXSLT_SRC_FUNCTION_READ_FILE_H_ -#include "boost/filesystem.hpp" +#include <boost/filesystem.hpp> #include "base.h" diff --git a/src/function/transform.cc b/src/function/transform.cc index 3ab6475..9cf806f 100644 --- a/src/function/transform.cc +++ b/src/function/transform.cc @@ -4,6 +4,8 @@ #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> +#include <sstream> + #include "transformation_facade.h" #include "support/xerces_string_guard.h" #include "support/dom/result_node_facade.h" @@ -30,7 +32,6 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionTransform::constructDocument( xalan::XSLTInputSource transformationSource, - boost::filesystem::path targetPath, xalan::XObjectPtr parameterObject ) { xercesc::DOMDocument* const domDocument( @@ -47,22 +48,23 @@ xercesc::DOMDocument* FunctionTransform::constructDocument( ResultNodeFacade result(domDocument, rootNode, "transformation"); - result.setAttribute( - "target", - targetPath.filename().string() - ); - if ( auto transformation = TransformationFacade::try_create( handleErrors(result), transformationSource, this->include_resolver_ ) ) { try { + std::stringstream targetStream; + transformation->generate( - targetPath.string(), + targetStream, parameterObject ); + result.setContent( + targetStream.str() + ); + result.setAttribute("result", "success"); } catch (const ErrorCapacitor::exception& exception) { diff --git a/src/function/transform.h b/src/function/transform.h index 516233f..ee88529 100644 --- a/src/function/transform.h +++ b/src/function/transform.h @@ -3,7 +3,7 @@ #include <xalanc/XSLT/XSLTInputSource.hpp> -#include "boost/filesystem.hpp" +#include <boost/filesystem.hpp> #include "base.h" @@ -12,7 +12,6 @@ namespace InputXSLT { class FunctionTransform : public FunctionBase< FunctionTransform, xalan::XSLTInputSource, - boost::filesystem::path, xalan::XObjectPtr > { public: @@ -23,7 +22,6 @@ class FunctionTransform : public FunctionBase< xercesc::DOMDocument* constructDocument( xalan::XSLTInputSource, - boost::filesystem::path, xalan::XObjectPtr ); diff --git a/src/function/write_file.cc b/src/function/write_file.cc new file mode 100644 index 0000000..39495d8 --- /dev/null +++ b/src/function/write_file.cc @@ -0,0 +1,62 @@ +#include "write_file.h" + +#include <xercesc/dom/DOMDocument.hpp> +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMElement.hpp> + +#include <boost/filesystem.hpp> +#include <boost/filesystem/fstream.hpp> + +#include "support/xerces_string_guard.h" +#include "support/dom/result_node_facade.h" + +namespace { + +bool writeFile( + const boost::filesystem::path& filePath, + const std::string& content +) { + boost::filesystem::ofstream file(filePath); + + if ( file.is_open() ) { + file << content << std::endl; + + return true; + } else { + return false; + } +} + +} + +namespace InputXSLT { + +xercesc::DOMDocument* FunctionWriteFile::constructDocument( + boost::filesystem::path filePath, + std::string content +) { + xercesc::DOMDocument* const domDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard<XMLCh>("content"), + nullptr + ) + ); + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() + ); + + ResultNodeFacade result(domDocument, rootNode, "file"); + result.setAttribute("path", filePath.string()); + + if ( writeFile(filePath, content) ) { + result.setAttribute("result", "success"); + } else { + result.setAttribute("result", "error"); + } + + return domDocument; +} + +} diff --git a/src/function/write_file.h b/src/function/write_file.h new file mode 100644 index 0000000..2260055 --- /dev/null +++ b/src/function/write_file.h @@ -0,0 +1,28 @@ +#ifndef INPUTXSLT_SRC_FUNCTION_WRITE_FILE_H_ +#define INPUTXSLT_SRC_FUNCTION_WRITE_FILE_H_ + +#include "base.h" + +namespace InputXSLT { + +class FunctionWriteFile : public FunctionBase< + FunctionWriteFile, + boost::filesystem::path, + std::string +> { + public: + using FunctionBase::FunctionBase; + + protected: + friend FunctionBase; + + xercesc::DOMDocument* constructDocument( + boost::filesystem::path, + std::string + ); + +}; + +} + +#endif // INPUTXSLT_SRC_FUNCTION_WRITE_FILE_H_ |