Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 1 | /* |
David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2011-2014, Intel Corporation |
| 3 | * All rights reserved. |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 4 | * |
David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame^] | 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 7 | * |
David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame^] | 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. |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 30 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 31 | #pragma once |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 32 | #include "XmlElement.h" |
| 33 | #include "XmlSerializingContext.h" |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 34 | #include <string> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 35 | |
| 36 | struct _xmlDoc; |
| 37 | struct _xmlNode; |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 38 | struct _xmlError; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 39 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 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 | */ |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 47 | class CXmlDocSource |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 48 | { |
| 49 | public: |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 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 | */ |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 56 | CXmlDocSource(_xmlDoc* pDoc, _xmlNode* pRootNode = NULL); |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Constructor |
| 60 | * |
| 61 | * @param[out] pDoc a pointer to the xml document that will be filled by the class |
| 62 | * @param[in] strXmlSchemaFile a string containing the path to the schema file |
| 63 | * @param[in] strRootElementType a string containing the root element type |
| 64 | * @param[in] strRootElementName a string containing the root element name |
| 65 | * @param[in] strNameAttributeName a string containing the name of the root name attribute |
| 66 | */ |
| 67 | CXmlDocSource(_xmlDoc* pDoc, |
| 68 | const string& strXmlSchemaFile, |
| 69 | const string& strRootElementType, |
| 70 | const string& strRootElementName, |
| 71 | const string& strNameAttrituteName); |
| 72 | |
| 73 | /** |
| 74 | * Constructor |
| 75 | * |
| 76 | * @param[out] pDoc a pointer to the xml document that will be filled by the class |
| 77 | * @param[in] strXmlSchemaFile a string containing the path to the schema file |
| 78 | * @param[in] strRootElementType a string containing the root element type |
| 79 | */ |
| 80 | CXmlDocSource(_xmlDoc* pDoc, const string& strXmlSchemaFile, const string& strRootElementType); |
| 81 | |
| 82 | /** |
| 83 | * Destructor |
| 84 | */ |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 85 | virtual ~CXmlDocSource(); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 86 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 87 | /** |
| 88 | * Method called by the CXmlDocSink::process method. |
| 89 | * |
| 90 | * @param[out] serializingContext is used as error output |
| 91 | * |
| 92 | * @return false if there are any error |
| 93 | */ |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 94 | virtual bool populate(CXmlSerializingContext& serializingContext) = 0; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 95 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 96 | /** |
| 97 | * Method that returns the root element of the Xml tree. |
| 98 | * |
| 99 | * @param[out] xmlRootElement a reference to the CXmleElement destination |
| 100 | */ |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 101 | void getRootElement(CXmlElement& xmlRootElement) const; |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Getter method. |
| 105 | * |
| 106 | * @return the root element's name |
| 107 | */ |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 108 | string getRootElementName() const; |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Getter method. |
| 112 | * Method that returns the root element's attribute with name matching strAttributeName. |
| 113 | * |
| 114 | * @param[in] strAttributeName is a string used to find the corresponding attribute |
| 115 | * |
| 116 | * @return the value of the root's attribute named as strAttributeName |
| 117 | */ |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 118 | string getRootElementAttributeString(const string& strAttributeName) const; |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 119 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 120 | /** |
| 121 | * Getter method. |
| 122 | * Method that returns the xmlDoc contained in the Source. |
| 123 | * (Can be used in a Doc Sink) |
| 124 | * |
| 125 | * @return the document _pDoc |
| 126 | */ |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 127 | _xmlDoc* getDoc() const; |
| 128 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 129 | /** |
| 130 | * Method that validates the Xml doc contained in pDoc |
| 131 | * |
| 132 | * @param[out] serializingContext is used as error output |
| 133 | * |
| 134 | * @return false if any error occurs |
| 135 | */ |
| 136 | virtual bool validate(CXmlSerializingContext& serializingContext); |
| 137 | |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 138 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 139 | protected: |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 140 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 141 | /** |
| 142 | * Doc |
| 143 | */ |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 144 | _xmlDoc* _pDoc; |
| 145 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 146 | /** |
| 147 | * Root node |
| 148 | */ |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 149 | _xmlNode* _pRootNode; |
| 150 | |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 151 | /** |
| 152 | * libxml2 library cleanup |
| 153 | */ |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 154 | static bool _bLibXml2CleanupScheduled; |
Georges-Henri Baron | cec86c1 | 2012-09-04 17:30:28 +0200 | [diff] [blame] | 155 | |
| 156 | private: |
| 157 | |
| 158 | /** |
| 159 | * Method that initializes class internal attributes in constructor |
| 160 | */ |
| 161 | void init(); |
| 162 | |
| 163 | /** Method that check the validity of the document with the xsd file. |
| 164 | * |
| 165 | * @return true if document is valid, false if any error occures |
| 166 | */ |
| 167 | bool isInstanceDocumentValid(); |
| 168 | |
| 169 | /** Validity error display method |
| 170 | * |
| 171 | * @param[in] pUserData pointer to the data to validate |
| 172 | * @param[out] pError is the xml error output |
| 173 | */ |
| 174 | static void schemaValidityStructuredErrorFunc(void* pUserData, _xmlError* pError); |
| 175 | |
| 176 | /** |
| 177 | * Schema file |
| 178 | */ |
| 179 | string _strXmlSchemaFile; |
| 180 | |
| 181 | /** |
| 182 | * Element type info |
| 183 | */ |
| 184 | string _strRootElementType; |
| 185 | |
| 186 | /** |
| 187 | * Element name info |
| 188 | */ |
| 189 | string _strRootElementName; |
| 190 | |
| 191 | /** |
| 192 | * Element name attribute info |
| 193 | */ |
| 194 | string _strNameAttrituteName; |
| 195 | |
| 196 | /** |
| 197 | * Boolean that enables the root element name attribute check |
| 198 | */ |
| 199 | bool _bNameCheck; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 200 | }; |