1 /* 2 * Copyright (c) 2011-2014, Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation and/or 13 * other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors 16 * may be used to endorse or promote products derived from this software without 17 * specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #pragma once 32 #include "XmlElement.h" 33 #include "XmlSerializingContext.h" 34 #include <string> 35 36 struct _xmlDoc; 37 struct _xmlNode; 38 struct _xmlError; 39 40 /** 41 * The CXmlDocSource is used by CXmlDocSink. 42 * The interaction between the xml source and xml sink is defined 43 * in the process method of CXmlDocSink. One can subclass CXmlDocSource 44 * for different purposes by implementing the populate method and then 45 * use it with any existing implementation of CXmlDocSink. 46 */ 47 class CXmlDocSource 48 { 49 public: 50 /** 51 * Constructor 52 * 53 * @param[out] pDoc a pointer to the xml document that will be filled by the class 54 * @param[in] pRootNode a pointer to the root element of the document. 55 * @param[in] bValidateWithSchema a boolean that toggles schema validation 56 */ 57 CXmlDocSource(_xmlDoc* pDoc, bool bValidateWithSchema = false, _xmlNode* pRootNode = NULL); 58 59 /** 60 * Constructor 61 * 62 * @param[out] pDoc a pointer to the xml document that will be filled by the class 63 * @param[in] strXmlSchemaFile a string containing the path to the schema file 64 * @param[in] strRootElementType a string containing the root element type 65 * @param[in] strRootElementName a string containing the root element name 66 * @param[in] strNameAttributeName a string containing the name of the root name attribute 67 * @param[in] bValidateWithSchema a boolean that toggles schema validation 68 */ 69 CXmlDocSource(_xmlDoc* pDoc, bool bValidateWithSchema, 70 const std::string& strXmlSchemaFile = "", 71 const std::string& strRootElementType = "", 72 const std::string& strRootElementName = "", 73 const std::string& strNameAttributeName = ""); 74 75 /** 76 * Destructor 77 */ 78 virtual ~CXmlDocSource(); 79 80 /** 81 * Method called by the CXmlDocSink::process method. 82 * 83 * @param[out] serializingContext is used as error output 84 * 85 * @return false if there are any error 86 */ 87 virtual bool populate(CXmlSerializingContext& serializingContext); 88 89 /** 90 * Method that returns the root element of the Xml tree. 91 * 92 * @param[out] xmlRootElement a reference to the CXmleElement destination 93 */ 94 void getRootElement(CXmlElement& xmlRootElement) const; 95 96 /** 97 * Getter method. 98 * 99 * @return the root element's name 100 */ 101 std::string getRootElementName() const; 102 103 /** 104 * Getter method. 105 * Method that returns the root element's attribute with name matching strAttributeName. 106 * 107 * @param[in] strAttributeName is a std::string used to find the corresponding attribute 108 * 109 * @return the value of the root's attribute named as strAttributeName 110 */ 111 std::string getRootElementAttributeString(const std::string& strAttributeName) const; 112 113 /** 114 * Getter method. 115 * Method that returns the xmlDoc contained in the Source. 116 * (Can be used in a Doc Sink) 117 * 118 * @return the document _pDoc 119 */ 120 _xmlDoc* getDoc() const; 121 122 /** 123 * Method that validates the Xml doc contained in pDoc 124 * 125 * @param[out] serializingContext is used as error output 126 * 127 * @return false if any error occurs 128 */ 129 virtual bool validate(CXmlSerializingContext& serializingContext); 130 131 /** 132 * Method that checks that the xml document has been correctly parsed. 133 * 134 * @return false if any error occurs during the parsing 135 */ 136 virtual bool isParsable() const; 137 138 /** 139 * Helper method for creating an xml document from either a file or a 140 * string. 141 * 142 * @param[in] source either a filename or a string representing an xml document 143 * @param[in] fromFile true if source is a filename, false if source is an xml 144 * represents an xml document 145 * @param[in] xincludes if true, process xincludes tags 146 * @param[out] errorMsg used as error output 147 */ 148 static _xmlDoc* mkXmlDoc(const std::string& source, bool fromFile, bool xincludes, std::string& errorMsg); 149 150 protected: 151 152 /** 153 * Doc 154 */ 155 _xmlDoc* _pDoc; 156 157 /** 158 * Root node 159 */ 160 _xmlNode* _pRootNode; 161 162 /** 163 * libxml2 library cleanup 164 */ 165 static bool _bLibXml2CleanupScheduled; 166 167 private: 168 169 /** 170 * Method that initializes class internal attributes in constructor 171 */ 172 void init(); 173 174 /** Method that check the validity of the document with the xsd file. 175 * 176 * @return true if document is valid, false if any error occures 177 */ 178 bool isInstanceDocumentValid(); 179 180 /** Validity error display method 181 * 182 * @param[in] pUserData pointer to the data to validate 183 * @param[out] pError is the xml error output 184 */ 185 static void schemaValidityStructuredErrorFunc(void* pUserData, _xmlError* pError); 186 187 /** 188 * Schema file 189 */ 190 std::string _strXmlSchemaFile; 191 192 /** 193 * Element type info 194 */ 195 std::string _strRootElementType; 196 197 /** 198 * Element name info 199 */ 200 std::string _strRootElementName; 201 202 /** 203 * Element name attribute info 204 */ 205 std::string _strNameAttributeName; 206 207 /** 208 * Boolean that enables the validation via xsd files 209 */ 210 bool _bValidateWithSchema; 211 }; 212