diff options
author | Adrian Kummerlaender | 2014-06-15 15:14:46 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2014-06-15 15:14:46 +0200 |
commit | 32c65970263c65022f5278b568c07b63c3d5d64b (patch) | |
tree | 2efa72b5bc2288daec5d8a5c7ec0e8f7fcaa1365 | |
parent | 58d3e1b8d03a10594e13658b50545fd714c04f6d (diff) | |
download | InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.gz InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.bz2 InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.lz InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.xz InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.tar.zst InputXSLT-32c65970263c65022f5278b568c07b63c3d5d64b.zip |
Merged "read-xml-file" and "read-file" into "read-file"
* FunctionReadFile is now able to distinguish between XML files and plain text files
** it selects the appropriate course of action automatically
** reading the file as XML into the DOM or reading it as a string
* the current selection criteria is the file extension
** I am thinking about trying to import every file into the DOM and using the result state of that action as selection criteria
* Updated README.md and test cases accordingly
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | src/function/read_file.cc | 53 | ||||
-rw-r--r-- | src/function/read_xml_file.cc | 86 | ||||
-rw-r--r-- | src/function/read_xml_file.h | 27 | ||||
-rw-r--r-- | src/plattform_guard.cc | 7 | ||||
-rw-r--r-- | test/common/test.md (renamed from test/external_text_formatter/test.md) | 0 | ||||
-rw-r--r-- | test/common/test.xml (renamed from test/common/test.txt) | 0 | ||||
-rw-r--r-- | test/read_file/reference.xml | 35 | ||||
-rw-r--r-- | test/read_file/transformation.xsl | 2 | ||||
-rw-r--r-- | test/read_xml_file/transformation.xsl | 2 | ||||
-rw-r--r-- | test/transform/transformation.xsl | 2 |
12 files changed, 83 insertions, 139 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aeec10..1df6b5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,6 @@ set( src/plattform_guard.cc src/transformation_facade.cc src/function/read_file.cc - src/function/read_xml_file.cc src/function/read_directory.cc src/function/transform.cc src/function/external_text_formatter.cc @@ -8,8 +8,7 @@ Contrary to popular opinion I actually like XSLT as a content transformation lan ## Current features: -- external `read-file` function for read-only access to text files -- external `read-xml-file` function for read-only access to XML files +- external `read-file` function for read-only access to both plain and xml files - external `read-directory` function for read-only directory traversal - external `transform` function for executing transformations inside transformations - external `external-text-formatter` function for executing text formatters and capturing their XML output @@ -18,8 +17,8 @@ Contrary to popular opinion I actually like XSLT as a content transformation lan The `test` directory contains black-box test cases for every external function provided by this application which may be used as basic usage examples. -- [`InputXSLT:read-file`](test/read_file/transformation.xsl) -- [`InputXSLT:read-xml-file`](test/read_xml_file/transformation.xsl) +- [`InputXSLT:read-file` (plain)](test/read_file/transformation.xsl) +- [`InputXSLT:read-file` (xml)](test/read_xml_file/transformation.xsl) - [`InputXSLT:read-directory`](test/read_directory/transformation.xsl) - [`InputXSLT:transform`](test/transform/transformation.xsl) - [`InputXSLT:external-text-formatter`](test/external_text_formatter/transformation.xsl) (requires [markdown.pl](http://daringfireball.net/projects/markdown/)) diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 22e96d7..7603ad1 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -3,6 +3,8 @@ #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMElement.hpp> +#include <xercesc/parsers/XercesDOMParser.hpp> +#include <xercesc/framework/LocalFileInputSource.hpp> #include "boost/filesystem/fstream.hpp" @@ -11,7 +13,28 @@ namespace { -inline std::string readFile(const boost::filesystem::path& filePath) { +inline bool isXmlFile(const boost::filesystem::path& filePath) { + return filePath.extension() == ".xml"; +} + +inline xercesc::DOMNode* readXmlFile( + const boost::filesystem::path& filePath, + xercesc::DOMDocument* const domDocument +) { + const xercesc::LocalFileInputSource file( + *InputXSLT::XercesStringGuard<XMLCh>(filePath.string().data()) + ); + + xercesc::XercesDOMParser parser; + parser.parse(file); + + return domDocument->importNode( + parser.getDocument()->getDocumentElement(), + true + ); +} + +inline std::string readPlainFile(const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); return std::string( @@ -53,11 +76,33 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument( result.setAttribute("path", filePath.string()); if ( boost::filesystem::is_regular_file(filePath) ) { - result.setAttribute("result", "success"); + try { + if ( isXmlFile(filePath) ) { + result.setAttribute("type", "xml"); + + result.setContent( + readXmlFile(filePath.string(), domDocument) + ); + } else { + result.setAttribute("type", "plain"); + + result.setContent( + readPlainFile(filePath) + ); + } + + result.setAttribute("result", "success"); + } + catch ( const xercesc::DOMException& exception ) { + result.setAttribute("result", "error"); - result.setContent(readFile(filePath)); + result.setValueNode( + "error", + *XercesStringGuard<char>(exception.msg) + ); + } } else { - result.setAttribute("result", "success"); + result.setAttribute("result", "error"); } return domDocument; diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc deleted file mode 100644 index e4d256d..0000000 --- a/src/function/read_xml_file.cc +++ /dev/null @@ -1,86 +0,0 @@ -#include "read_xml_file.h" - -#include <xercesc/dom/DOMDocument.hpp> -#include <xercesc/dom/DOMImplementation.hpp> -#include <xercesc/dom/DOMElement.hpp> -#include <xercesc/parsers/XercesDOMParser.hpp> -#include <xercesc/framework/LocalFileInputSource.hpp> - -#include "support/xerces_string_guard.h" -#include "support/dom/result_node_facade.h" - -namespace { - -inline xercesc::DOMNode* importDocumentElement( - const std::string& filePath, - xercesc::DOMDocument* const domDocument -) { - const xercesc::LocalFileInputSource file( - *InputXSLT::XercesStringGuard<XMLCh>(filePath.data()) - ); - - xercesc::XercesDOMParser parser; - parser.parse(file); - - return domDocument->importNode( - parser.getDocument()->getDocumentElement(), - true - ); -} - -} - -namespace InputXSLT { - -xercesc::DOMDocument* FunctionReadXmlFile::constructDocument( - const FilesystemContext& fsContext, - std::string rawPath -) { - boost::filesystem::path filePath(fsContext.resolve(rawPath)); - - if ( !(boost::filesystem::exists(filePath) && - boost::filesystem::is_regular_file(filePath)) ) { - if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) { - filePath = *resolvedPath; - } - } - - 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 ( boost::filesystem::is_regular_file(filePath) ) { - try { - result.setContent( - importDocumentElement(filePath.string(), domDocument) - ); - - result.setAttribute("result", "success"); - } - catch ( const xercesc::DOMException& exception ) { - result.setAttribute("result", "error"); - - result.setValueNode( - "error", - *XercesStringGuard<char>(exception.msg) - ); - } - } else { - result.setAttribute("result", "error"); - } - - return domDocument; -} - -} diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h deleted file mode 100644 index 7fe949a..0000000 --- a/src/function/read_xml_file.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_ -#define INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_ - -#include "base.h" - -namespace InputXSLT { - -class FunctionReadXmlFile : public FunctionBase< - FunctionReadXmlFile, - std::string -> { - public: - using FunctionBase::FunctionBase; - - protected: - friend FunctionBase; - - xercesc::DOMDocument* constructDocument( - const FilesystemContext&, - std::string - ); - -}; - -} - -#endif // INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_ diff --git a/src/plattform_guard.cc b/src/plattform_guard.cc index d787adf..e232d2b 100644 --- a/src/plattform_guard.cc +++ b/src/plattform_guard.cc @@ -7,7 +7,6 @@ #include "common.h" #include "function/read_file.h" -#include "function/read_xml_file.h" #include "function/read_directory.h" #include "function/transform.h" #include "function/external_text_formatter.h" @@ -31,12 +30,6 @@ PlattformGuard::PlattformGuard(const std::vector<std::string>& path): xalan::XalanTransformer::installExternalFunctionGlobal( customNamespace, - xalan::XalanDOMString("read-xml-file"), - InputXSLT::FunctionReadXmlFile(&this->include_resolver_) - ); - - xalan::XalanTransformer::installExternalFunctionGlobal( - customNamespace, xalan::XalanDOMString("read-directory"), InputXSLT::FunctionReadDirectory(&this->include_resolver_) ); diff --git a/test/external_text_formatter/test.md b/test/common/test.md index 9402e4f..9402e4f 100644 --- a/test/external_text_formatter/test.md +++ b/test/common/test.md diff --git a/test/common/test.txt b/test/common/test.xml index 046a6ef..046a6ef 100644 --- a/test/common/test.txt +++ b/test/common/test.xml diff --git a/test/read_file/reference.xml b/test/read_file/reference.xml index 0bca6f7..4500216 100644 --- a/test/read_file/reference.xml +++ b/test/read_file/reference.xml @@ -1,9 +1,30 @@ <?xml version="1.0" encoding="UTF-8"?> -<test_case><?xml version="1.0"?> -<tester> - <eintrag>Hello 1</eintrag> - <eintrag>Hello 2</eintrag> - <eintrag>Hello 3</eintrag> - <eintrag>Hello 4</eintrag> -</tester> +<test_case># Markdown Test + +__Lorem ipsum__ dolor sit amet, _consectetur_ adipisicing elit, sed do `eiusmod` tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + +> Duis aute irure dolor in reprehenderit in voluptate +> velit esse cillum dolore eu fugiat nulla pariatur. + +Excepteur sint **occaecat** cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +* Test 1 +* Test 2 +* Test 3 +* Test 4 + +Test Test Test + + template < + typename Target, + std::size_t Index = 0, + typename Current = std::tuple<>, + enable_if<Index == std::tuple_size<Target>::value> = 0 + > + inline Target construct( + const xalan::XPathExecutionContext::XObjectArgVectorType&, + Current&& current + ) { + return current; + } </test_case> diff --git a/test/read_file/transformation.xsl b/test/read_file/transformation.xsl index a968f35..53a815c 100644 --- a/test/read_file/transformation.xsl +++ b/test/read_file/transformation.xsl @@ -10,7 +10,7 @@ <xsl:import href="[testcase.xsl]"/> <xsl:template name="implementation"> - <xsl:variable name="result" select="InputXSLT:read-file('test.txt')"/> + <xsl:variable name="result" select="InputXSLT:read-file('test.md')"/> <xsl:choose> <xsl:when test="$result/self::file/@result = 'success'"> diff --git a/test/read_xml_file/transformation.xsl b/test/read_xml_file/transformation.xsl index ae54099..98ae358 100644 --- a/test/read_xml_file/transformation.xsl +++ b/test/read_xml_file/transformation.xsl @@ -10,7 +10,7 @@ <xsl:import href="[testcase.xsl]"/> <xsl:template name="implementation"> - <xsl:variable name="result" select="InputXSLT:read-xml-file('test.txt')"/> + <xsl:variable name="result" select="InputXSLT:read-file('test.xml')"/> <xsl:choose> <xsl:when test="$result/self::file/@result = 'success'"> diff --git a/test/transform/transformation.xsl b/test/transform/transformation.xsl index 6bc759d..0c6173d 100644 --- a/test/transform/transformation.xsl +++ b/test/transform/transformation.xsl @@ -40,7 +40,7 @@ <xsl:choose> <xsl:when test="xalan:nodeset($result)/transformation/@result = 'success'"> <xsl:copy-of select=" - InputXSLT:read-xml-file('test_actual.xml')/test_case/transform_test/* + InputXSLT:read-file('test_actual.xml')/test_case/transform_test/* "/> </xsl:when> <xsl:otherwise> |