diff options
Diffstat (limited to 'xmlserializer')
-rw-r--r-- | xmlserializer/Android.mk | 13 | ||||
-rw-r--r-- | xmlserializer/CMakeLists.txt | 5 | ||||
-rw-r--r-- | xmlserializer/XmlDocSource.cpp | 110 | ||||
-rw-r--r-- | xmlserializer/XmlDocSource.h | 65 | ||||
-rw-r--r-- | xmlserializer/XmlFileDocSink.cpp | 54 | ||||
-rw-r--r-- | xmlserializer/XmlFileDocSink.h | 66 | ||||
-rw-r--r-- | xmlserializer/XmlFileDocSource.cpp | 109 | ||||
-rw-r--r-- | xmlserializer/XmlFileDocSource.h | 104 | ||||
-rw-r--r-- | xmlserializer/XmlMemoryDocSource.cpp | 27 | ||||
-rw-r--r-- | xmlserializer/XmlMemoryDocSource.h | 24 | ||||
-rw-r--r-- | xmlserializer/XmlStreamDocSink.cpp (renamed from xmlserializer/XmlStringDocSink.cpp) | 22 | ||||
-rw-r--r-- | xmlserializer/XmlStreamDocSink.h (renamed from xmlserializer/XmlStringDocSink.h) | 14 | ||||
-rw-r--r-- | xmlserializer/XmlStringDocSource.cpp | 54 | ||||
-rw-r--r-- | xmlserializer/XmlStringDocSource.h | 69 |
14 files changed, 126 insertions, 610 deletions
diff --git a/xmlserializer/Android.mk b/xmlserializer/Android.mk index eedf48b..67b9bf7 100644 --- a/xmlserializer/Android.mk +++ b/xmlserializer/Android.mk @@ -37,10 +37,7 @@ common_src_files := \ XmlDocSource.cpp \ XmlMemoryDocSink.cpp \ XmlMemoryDocSource.cpp \ - XmlStringDocSink.cpp \ - XmlFileDocSink.cpp \ - XmlFileDocSource.cpp \ - XmlStringDocSource.cpp + XmlStreamDocSink.cpp \ common_module := libxmlserializer @@ -66,6 +63,8 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := $(common_src_files) +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) + LOCAL_MODULE := $(common_module) LOCAL_MODULE_OWNER := intel LOCAL_MODULE_TAGS := $(common_module_tags) @@ -79,7 +78,7 @@ LOCAL_STATIC_LIBRARIES := $(common_static_libraries) LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) -include $(BUILD_STATIC_LIBRARY) +include $(BUILD_SHARED_LIBRARY) ############################## # Host build @@ -88,6 +87,8 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := $(common_src_files) +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) + LOCAL_MODULE := $(common_module)_host LOCAL_MODULE_OWNER := intel LOCAL_MODULE_TAGS := $(common_module_tags) @@ -101,5 +102,5 @@ LOCAL_STATIC_LIBRARIES := libxml2 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) -include $(BUILD_HOST_STATIC_LIBRARY) +include $(BUILD_HOST_SHARED_LIBRARY) diff --git a/xmlserializer/CMakeLists.txt b/xmlserializer/CMakeLists.txt index 074bbcc..be068c9 100644 --- a/xmlserializer/CMakeLists.txt +++ b/xmlserializer/CMakeLists.txt @@ -32,10 +32,7 @@ add_library(xmlserializer SHARED XmlDocSource.cpp XmlMemoryDocSink.cpp XmlMemoryDocSource.cpp - XmlStringDocSink.cpp - XmlFileDocSink.cpp - XmlFileDocSource.cpp - XmlStringDocSource.cpp) + XmlStreamDocSink.cpp) include(FindLibXml2) if(NOT LIBXML2_FOUND) diff --git a/xmlserializer/XmlDocSource.cpp b/xmlserializer/XmlDocSource.cpp index 35a8f4e..90a30ac 100644 --- a/xmlserializer/XmlDocSource.cpp +++ b/xmlserializer/XmlDocSource.cpp @@ -31,6 +31,9 @@ #include "XmlDocSource.h" #include <libxml/tree.h> #include <libxml/xmlschemas.h> +#include <libxml/parser.h> +#include <libxml/xinclude.h> +#include <libxml/xmlerror.h> #include <stdlib.h> using std::string; @@ -38,65 +41,30 @@ using std::string; // Schedule for libxml2 library bool CXmlDocSource::_bLibXml2CleanupScheduled; -CXmlDocSource::CXmlDocSource(_xmlDoc *pDoc, _xmlNode *pRootNode, - bool bValidateWithSchema) : +CXmlDocSource::CXmlDocSource(_xmlDoc *pDoc, bool bValidateWithSchema, + _xmlNode *pRootNode) : _pDoc(pDoc), _pRootNode(pRootNode), _strXmlSchemaFile(""), _strRootElementType(""), _strRootElementName(""), - _strNameAttrituteName(""), - _bNameCheck(false), + _strNameAttributeName(""), _bValidateWithSchema(bValidateWithSchema) { init(); } -CXmlDocSource::CXmlDocSource(_xmlDoc *pDoc, +CXmlDocSource::CXmlDocSource(_xmlDoc *pDoc, bool bValidateWithSchema, const string& strXmlSchemaFile, const string& strRootElementType, const string& strRootElementName, - const string& strNameAttrituteName) : + const string& strNameAttributeName) : _pDoc(pDoc), - _pRootNode(NULL), + _pRootNode(xmlDocGetRootElement(pDoc)), _strXmlSchemaFile(strXmlSchemaFile), _strRootElementType(strRootElementType), _strRootElementName(strRootElementName), - _strNameAttrituteName(strNameAttrituteName), - _bNameCheck(true), - _bValidateWithSchema(false) -{ - init(); -} - -CXmlDocSource::CXmlDocSource(_xmlDoc* pDoc, - const string& strXmlSchemaFile, - const string& strRootElementType, - bool bValidateWithSchema) : - _pDoc(pDoc), _pRootNode(NULL), - _strXmlSchemaFile(strXmlSchemaFile), - _strRootElementType(strRootElementType), - _strRootElementName(""), - _strNameAttrituteName(""), - _bNameCheck(false), - _bValidateWithSchema(bValidateWithSchema) -{ - init(); -} - -CXmlDocSource::CXmlDocSource(_xmlDoc *pDoc, - const string& strXmlSchemaFile, - const string& strRootElementType, - const string& strRootElementName, - const string& strNameAttrituteName, - bool bValidateWithSchema) : - _pDoc(pDoc), - _pRootNode(NULL), - _strXmlSchemaFile(strXmlSchemaFile), - _strRootElementType(strRootElementType), - _strRootElementName(strRootElementName), - _strNameAttrituteName(strNameAttrituteName), - _bNameCheck(true), + _strNameAttributeName(strNameAttributeName), _bValidateWithSchema(bValidateWithSchema) { init(); @@ -133,6 +101,18 @@ _xmlDoc* CXmlDocSource::getDoc() const return _pDoc; } +bool CXmlDocSource::isParsable() const +{ + // Check that the doc has been created + return _pDoc != NULL; +} + +bool CXmlDocSource::populate(CXmlSerializingContext& serializingContext) +{ + return validate(serializingContext); + +} + bool CXmlDocSource::validate(CXmlSerializingContext& serializingContext) { // Check that the doc has been created @@ -164,9 +144,9 @@ bool CXmlDocSource::validate(CXmlSerializingContext& serializingContext) return false; } - if (_bNameCheck) { + if (!_strNameAttributeName.empty()) { - string strRootElementNameCheck = getRootElementAttributeString(_strNameAttrituteName); + string strRootElementNameCheck = getRootElementAttributeString(_strNameAttributeName); // Check Root element name attribute (if any) if (!_strRootElementName.empty() && strRootElementNameCheck != _strRootElementName) { @@ -193,11 +173,6 @@ void CXmlDocSource::init() _bLibXml2CleanupScheduled = true; } - - if (!_pRootNode) { - - _pRootNode = xmlDocGetRootElement(_pDoc); - } } bool CXmlDocSource::isInstanceDocumentValid() @@ -264,3 +239,40 @@ void CXmlDocSource::schemaValidityStructuredErrorFunc(void* pUserData, _xmlError puts(pError->message); #endif } + +_xmlDoc* CXmlDocSource::mkXmlDoc(const string& source, bool fromFile, bool xincludes, string& errorMsg) +{ + _xmlDoc* doc = NULL; + if (fromFile) { + doc = xmlReadFile(source.c_str(), NULL, 0); + } else { + doc = xmlReadMemory(source.c_str(), (int)source.size(), "", NULL, 0); + } + + if (doc == NULL) { + errorMsg = "libxml failed to read"; + if (fromFile) { + errorMsg += " \"" + source + "\""; + } + + xmlError* details = xmlGetLastError(); + if (details != NULL) { + errorMsg += ": " + string(details->message); + } + + return NULL; + } + + if (xincludes and (xmlXIncludeProcess(doc) < 0)) { + errorMsg = "libxml failed to resolve XIncludes"; + xmlError* details = xmlGetLastError(); + if (details != NULL) { + errorMsg += ": " + string(details->message); + } + + xmlFreeDoc(doc); + doc = NULL; + } + + return doc; +} diff --git a/xmlserializer/XmlDocSource.h b/xmlserializer/XmlDocSource.h index e7be8f3..e41923e 100644 --- a/xmlserializer/XmlDocSource.h +++ b/xmlserializer/XmlDocSource.h @@ -54,22 +54,7 @@ public: * @param[in] pRootNode a pointer to the root element of the document. * @param[in] bValidateWithSchema a boolean that toggles schema validation */ - CXmlDocSource(_xmlDoc* pDoc, _xmlNode* pRootNode = NULL, bool bValidateWithSchema = false); - - /** - * Constructor - * - * @param[out] pDoc a pointer to the xml document that will be filled by the class - * @param[in] strXmlSchemaFile a std::string containing the path to the schema file - * @param[in] strRootElementType a std::string containing the root element type - * @param[in] strRootElementName a std::string containing the root element name - * @param[in] strNameAttributeName a std::string containing the name of the root name attribute - */ - CXmlDocSource(_xmlDoc* pDoc, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - const std::string& strRootElementName, - const std::string& strNameAttrituteName); + CXmlDocSource(_xmlDoc* pDoc, bool bValidateWithSchema = false, _xmlNode* pRootNode = NULL); /** * Constructor @@ -81,22 +66,11 @@ public: * @param[in] strNameAttributeName a string containing the name of the root name attribute * @param[in] bValidateWithSchema a boolean that toggles schema validation */ - CXmlDocSource(_xmlDoc* pDoc, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - const std::string& strRootElementName, - const std::string& strNameAttrituteName, - bool bValidateWithSchema); - - /** - * Constructor - * - * @param[out] pDoc a pointer to the xml document that will be filled by the class - * @param[in] strXmlSchemaFile a string containing the path to the schema file - * @param[in] strRootElementType a string containing the root element type - */ - CXmlDocSource(_xmlDoc* pDoc, const std::string& strXmlSchemaFile, const std::string& strRootElementType, - bool bValidateWithSchema); + CXmlDocSource(_xmlDoc* pDoc, bool bValidateWithSchema, + const std::string& strXmlSchemaFile = "", + const std::string& strRootElementType = "", + const std::string& strRootElementName = "", + const std::string& strNameAttributeName = ""); /** * Destructor @@ -110,7 +84,7 @@ public: * * @return false if there are any error */ - virtual bool populate(CXmlSerializingContext& serializingContext) = 0; + virtual bool populate(CXmlSerializingContext& serializingContext); /** * Method that returns the root element of the Xml tree. @@ -154,6 +128,24 @@ public: */ virtual bool validate(CXmlSerializingContext& serializingContext); + /** + * Method that checks that the xml document has been correctly parsed. + * + * @return false if any error occurs during the parsing + */ + virtual bool isParsable() const; + + /** + * Helper method for creating an xml document from either a file or a + * string. + * + * @param[in] source either a filename or a string representing an xml document + * @param[in] fromFile true if source is a filename, false if source is an xml + * represents an xml document + * @param[in] xincludes if true, process xincludes tags + * @param[out] errorMsg used as error output + */ + static _xmlDoc* mkXmlDoc(const std::string& source, bool fromFile, bool xincludes, std::string& errorMsg); protected: @@ -210,12 +202,7 @@ private: /** * Element name attribute info */ - std::string _strNameAttrituteName; - - /** - * Boolean that enables the root element name attribute check - */ - bool _bNameCheck; + std::string _strNameAttributeName; /** * Boolean that enables the validation via xsd files diff --git a/xmlserializer/XmlFileDocSink.cpp b/xmlserializer/XmlFileDocSink.cpp deleted file mode 100644 index f73860c..0000000 --- a/xmlserializer/XmlFileDocSink.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2011-2014, Intel Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "XmlFileDocSink.h" -#include <libxml/parser.h> - -#define base CXmlDocSink - -CXmlFileDocSink::CXmlFileDocSink(const std::string& strXmlInstanceFile): - _strXmlInstanceFile(strXmlInstanceFile) -{ -} - -bool CXmlFileDocSink::doProcess(CXmlDocSource& xmlDocSource, - CXmlSerializingContext& serializingContext) -{ - // Write file (formatted) - if (xmlSaveFormatFileEnc(_strXmlInstanceFile.c_str(), - xmlDocSource.getDoc(), "UTF-8", 1) == -1) { - - serializingContext.setError("Could not write file " + _strXmlInstanceFile); - - return false; - } - return true; -} - diff --git a/xmlserializer/XmlFileDocSink.h b/xmlserializer/XmlFileDocSink.h deleted file mode 100644 index 6063e6a..0000000 --- a/xmlserializer/XmlFileDocSink.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2011-2014, Intel Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once -#include "XmlDocSink.h" -#include <string> - -/** - * Sink class that save the content of any CXmlDocSource into a file. - * The file path is defined in the constructor. - */ -class CXmlFileDocSink : public CXmlDocSink -{ -public: - /** - * Constructor - * - * @param[in] strXmlInstanceFile defines the path used to save the file. - */ - CXmlFileDocSink(const std::string& strXmlInstanceFile); - -private: - /** - * Implementation of CXmlDocSink::doProcess() - * Write the content of the xmlDocSource to the file opened in strXmlInstanceFile using - * UTF-8 encoding - * - * @param[in] xmlDocSource is the source containing the Xml document - * @param[out] serializingContext is used as error output - * - * @return false if any error occurs - */ - virtual bool doProcess(CXmlDocSource& xmlDocSource, CXmlSerializingContext& serializingContext); - - /** - * Name of the instance file - */ - std::string _strXmlInstanceFile; -}; diff --git a/xmlserializer/XmlFileDocSource.cpp b/xmlserializer/XmlFileDocSource.cpp deleted file mode 100644 index f980c39..0000000 --- a/xmlserializer/XmlFileDocSource.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2011-2014, Intel Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "XmlFileDocSource.h" -#include <libxml/parser.h> -#include <libxml/xinclude.h> - -#define base CXmlDocSource - -CXmlFileDocSource::CXmlFileDocSource(const std::string& strXmlInstanceFile, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - const std::string& strRootElementName, - const std::string& strNameAttrituteName, - bool bValidateWithSchema) : - base(readFile(strXmlInstanceFile), - strXmlSchemaFile, - strRootElementType, - strRootElementName, - strNameAttrituteName, - bValidateWithSchema), - _strXmlInstanceFile(strXmlInstanceFile) -{ -} - -CXmlFileDocSource::CXmlFileDocSource(const std::string& strXmlInstanceFile, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - bool bValidateWithSchema) : - base(readFile(strXmlInstanceFile), - strXmlSchemaFile, - strRootElementType, - bValidateWithSchema), - _strXmlInstanceFile(strXmlInstanceFile) -{ -} - -bool CXmlFileDocSource::isParsable(CXmlSerializingContext& serializingContext) const -{ - // Check that the doc has been created - if (!_pDoc) { - - serializingContext.setError("Could not parse file " + _strXmlInstanceFile); - - return false; - } - - return true; -} - -bool CXmlFileDocSource::populate(CXmlSerializingContext& serializingContext) -{ - if (!validate(serializingContext)) { - - // Add the file's name in the error message - serializingContext.appendLineToError("File : " + _strXmlInstanceFile); - - return false; - } - - return true; -} - -_xmlDoc* CXmlFileDocSource::readFile(const std::string& strFileName) -{ - // Read xml file - xmlDocPtr pDoc = xmlReadFile(strFileName.c_str(), NULL, 0); - - if (!pDoc) { - - return NULL; - } - // Process file inclusion - // WARNING: this symbol is available if libxml2 has been compiled with LIBXML_XINCLUDE_ENABLED - if (xmlXIncludeProcess(pDoc) < 0) { - - xmlFreeDoc(pDoc); - return NULL; - } - - return pDoc; -} diff --git a/xmlserializer/XmlFileDocSource.h b/xmlserializer/XmlFileDocSource.h deleted file mode 100644 index f7b2e30..0000000 --- a/xmlserializer/XmlFileDocSource.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2011-2014, Intel Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once -#include "XmlDocSource.h" -#include <string> - -/** - * Source class that read a file to get an xml document. - * Its base class will validate the document. - */ -class CXmlFileDocSource : public CXmlDocSource -{ -public: - /** - * Constructor - * - * @param[in] strXmlInstanceFile a string containing the path to the xml file - * @param[in] strXmlSchemaFile a string containing the path to the schema file - * @param[in] strRootElementType a string containing the root element type - * @param[in] strRootElementName a string containing the root element name - * @param[in] strNameAttributeName a string containing the name of the root name attribute - * @param[in] bValidateWithSchema a boolean that toggles schema validation - */ - CXmlFileDocSource(const std::string& strXmlInstanceFile, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - const std::string& strRootElementName, - const std::string& strNameAttrituteName, - bool bValidateWithSchema); - /** - * Constructor - * - * @param[in] strXmlInstanceFile a string containing the path to the xml file - * @param[in] strXmlSchemaFile a string containing the path to the schema file - * @param[in] strRootElementType a string containing the root element type - * @param[in] bValidateWithSchema a boolean that toggles schema validation - */ - CXmlFileDocSource(const std::string& strXmlInstanceFile, const std::string& strXmlSchemaFile, const std::string& strRootElementType, - bool bValidateWithSchema); - - /** - * CXmlDocSource method implementation. - * - * @param[out] serializingContext is used as error output - * - * @return false if any error occurs - */ - virtual bool populate(CXmlSerializingContext& serializingContext); - - /** - * Method that checks that the file exists and is readable. - * - * @param[out] serializingContext is used as error output - * - * @return false if any error occurs during the parsing - */ - virtual bool isParsable(CXmlSerializingContext& serializingContext) const; - -private: - /** - * Read xml file - * - * This function reads an xml file and processes eventual included files - * WARNING: to compile this function, libxml2 has to be compiled with LIBXML_XINCLUDE_ENABLED - * - * @param[in] strFileName the file name - * - * @return a pointer to generated xml document object - */ - static _xmlDoc* readFile(const std::string& strFileName); - - /** - * Instance file - */ - std::string _strXmlInstanceFile; -}; diff --git a/xmlserializer/XmlMemoryDocSource.cpp b/xmlserializer/XmlMemoryDocSource.cpp index 411d98a..2929b79 100644 --- a/xmlserializer/XmlMemoryDocSource.cpp +++ b/xmlserializer/XmlMemoryDocSource.cpp @@ -34,26 +34,17 @@ #define base CXmlDocSource -CXmlMemoryDocSource::CXmlMemoryDocSource(const IXmlSource* pXmlSource, +CXmlMemoryDocSource::CXmlMemoryDocSource(const IXmlSource* pXmlSource, bool bValidateWithSchema, const std::string& strRootElementType, const std::string& strXmlSchemaFile, const std::string& strProduct, - const std::string& strVersion, - bool bValidateWithSchema): - base(xmlNewDoc(BAD_CAST "1.0"), xmlNewNode(NULL, BAD_CAST strRootElementType.c_str()), - bValidateWithSchema), - _pXmlSource(pXmlSource), _strXmlSchemaFile(strXmlSchemaFile), _bWithHeader(true), - _strProduct(strProduct), _strVersion(strVersion) -{ - init(); -} - -CXmlMemoryDocSource::CXmlMemoryDocSource(const IXmlSource* pXmlSource, - const std::string& strRootElementType, - bool bValidateWithSchema): - base(xmlNewDoc(BAD_CAST "1.0"), xmlNewNode(NULL, BAD_CAST strRootElementType.c_str()), - bValidateWithSchema), - _pXmlSource(pXmlSource), _bWithHeader(false) + const std::string& strVersion): + base(xmlNewDoc(BAD_CAST "1.0"), bValidateWithSchema, + xmlNewNode(NULL, BAD_CAST strRootElementType.c_str())), + _pXmlSource(pXmlSource), + _strXmlSchemaFile(strXmlSchemaFile), + _strProduct(strProduct), + _strVersion(strVersion) { init(); } @@ -80,7 +71,7 @@ bool CXmlMemoryDocSource::populate(CXmlSerializingContext& serializingContext) // Create Xml element with the Doc CXmlElement docElement(_pRootNode); - if (_bWithHeader) { + if (!_strXmlSchemaFile.empty()) { // Schema namespace docElement.setAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); diff --git a/xmlserializer/XmlMemoryDocSource.h b/xmlserializer/XmlMemoryDocSource.h index 3266782..bc5fdb5 100644 --- a/xmlserializer/XmlMemoryDocSource.h +++ b/xmlserializer/XmlMemoryDocSource.h @@ -50,20 +50,11 @@ public: * @param[in] strVersion a string containing the version number * @param[in] bValidateWithSchema a boolean that toggles schema validation */ - CXmlMemoryDocSource(const IXmlSource* pXmlSource, const std::string& strRootElementType, - const std::string& strXmlSchemaFile, const std::string& strProduct, - const std::string& strVersion, - bool bValidateWithSchema); - - /** - * Constructor - * - * @param[in] pXmlSource a pointer to a parameter-framework structure that can generate - * an xml description of itself - * @param[in] strRootElementType a string containing the root element type - * @param[in] bValidateWithSchema a boolean that toggles schema validation - */ - CXmlMemoryDocSource(const IXmlSource* pXmlSource, const std::string& strRootElementType, bool bValidateWithSchema); + CXmlMemoryDocSource(const IXmlSource* pXmlSource, bool bValidateWithSchema, + const std::string& strRootElementType, + const std::string& strXmlSchemaFile = "", + const std::string& strProduct = "", + const std::string& strVersion = ""); /** * Implementation of CXmlDocSource::populate() method. @@ -91,11 +82,6 @@ private: */ std::string _strXmlSchemaFile; - /** - * Boolean used to specify if a header should be added in the Xml Doc - */ - bool _bWithHeader; - // Product and version info std::string _strProduct; std::string _strVersion; diff --git a/xmlserializer/XmlStringDocSink.cpp b/xmlserializer/XmlStreamDocSink.cpp index caec545..f1a2524 100644 --- a/xmlserializer/XmlStringDocSink.cpp +++ b/xmlserializer/XmlStreamDocSink.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2014, Intel Corporation + * Copyright (c) 2015, Intel Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -27,36 +27,34 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "XmlStringDocSink.h" +#include "XmlStreamDocSink.h" #include <libxml/parser.h> #define base CXmlDocSink -CXmlStringDocSink::CXmlStringDocSink(std::string& strResult): - _strResult(strResult) +CXmlStreamDocSink::CXmlStreamDocSink(std::ostream& output): + _output(output) { } -bool CXmlStringDocSink::doProcess(CXmlDocSource& xmlDocSource, +bool CXmlStreamDocSink::doProcess(CXmlDocSource& xmlDocSource, CXmlSerializingContext& serializingContext) { - (void)serializingContext; - - xmlChar* pcDumpedDoc = NULL; + xmlChar* dumpedDoc = NULL; int iSize; - xmlDocDumpFormatMemoryEnc(xmlDocSource.getDoc(), &pcDumpedDoc, &iSize, "UTF-8", 1); + xmlDocDumpFormatMemoryEnc(xmlDocSource.getDoc(), &dumpedDoc, &iSize, "UTF-8", 1); - if (!pcDumpedDoc) { + if (!dumpedDoc) { serializingContext.setError("Unable to encode XML document in memory"); return false; } - _strResult.append((const char*)pcDumpedDoc); + _output << static_cast<unsigned char*>(dumpedDoc); - xmlFree(pcDumpedDoc); + xmlFree(dumpedDoc); return true; } diff --git a/xmlserializer/XmlStringDocSink.h b/xmlserializer/XmlStreamDocSink.h index 22fa3a1..08b81cb 100644 --- a/xmlserializer/XmlStringDocSink.h +++ b/xmlserializer/XmlStreamDocSink.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2014, Intel Corporation + * Copyright (c) 2015, Intel Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -29,7 +29,7 @@ */ #pragma once -#include <string> +#include <ostream> #include "XmlDocSink.h" #include "XmlSource.h" @@ -37,14 +37,14 @@ * Sink class that writes the content of any CXmlDocSource into a std::string. * A reference to an empty std::string is given in the constructor. */ -class CXmlStringDocSink : public CXmlDocSink +class CXmlStreamDocSink : public CXmlDocSink { public: /** Constructor * - * @param[out] strResult a reference to a std::string that will be filled by the doProcess method + * @param[out] output a reference to a ostream that will be filled by the doProcess method */ - CXmlStringDocSink(std::string& strResult); + CXmlStreamDocSink(std::ostream& output); private: /** Implementation of CXmlDocSink::doProcess() @@ -58,8 +58,8 @@ private: virtual bool doProcess(CXmlDocSource& xmlDocSource, CXmlSerializingContext& serializingContext); /** - * Result std::string containing the XML informations + * Result ostream containing the XML informations */ - std::string& _strResult; + std::ostream& _output; }; diff --git a/xmlserializer/XmlStringDocSource.cpp b/xmlserializer/XmlStringDocSource.cpp deleted file mode 100644 index 8d420b6..0000000 --- a/xmlserializer/XmlStringDocSource.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2011-2014, Intel Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "XmlStringDocSource.h" -#include <libxml/parser.h> - -#define base CXmlDocSource - -CXmlStringDocSource::CXmlStringDocSource(const std::string& strXmlInput, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - const std::string& strRootElementName, - const std::string& strNameAttrituteName, - bool bValidateWithSchema) : - base(xmlReadMemory(strXmlInput.c_str(), (int)strXmlInput.size(), "", NULL, 0), - strXmlSchemaFile, - strRootElementType, - strRootElementName, - strNameAttrituteName, - bValidateWithSchema) -{ -} - -bool CXmlStringDocSource::populate(CXmlSerializingContext &serializingContext) -{ - return validate(serializingContext); -} diff --git a/xmlserializer/XmlStringDocSource.h b/xmlserializer/XmlStringDocSource.h deleted file mode 100644 index 06a0337..0000000 --- a/xmlserializer/XmlStringDocSource.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2011-2014, Intel Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once -#include "XmlDocSource.h" -#include <string> - -/** - * Source class that get an xml document from a std::string. - * Its base class will check the validity of the document. - */ -class CXmlStringDocSource : public CXmlDocSource -{ -public: - /** - * Constructor - * - * @param[in] strXmlInput a string containing an xml description - * @param[in] strXmlSchemaFile a string containing the path to the schema file - * @param[in] strRootElementType a string containing the root element type - * @param[in] strRootElementName a string containing the root element name - * @param[in] strNameAttributeName a string containing the name of the root name attribute - * @param[in] bValidateWithSchema a boolean that toggles schema validation - */ - CXmlStringDocSource(const std::string& strXmlInput, - const std::string& strXmlSchemaFile, - const std::string& strRootElementType, - const std::string& strRootElementName, - const std::string& strNameAttrituteName, - bool bValidateWithSchema); - - /** - * CXmlDocSource method implementation. - * - * @param[out] serializingContext is used as error output - * - * @return false if any error occurs - */ - virtual bool populate(CXmlSerializingContext& serializingContext); -}; - - |