blob: 9b780e5674162d732559fd2c34946f74f9dc58d8 [file] [log] [blame]
Georges-Henri Baron326a31d2012-06-28 12:05:09 +02001/*
David Wagnerb76c9d62014-02-05 18:30:24 +01002 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
Georges-Henri Baron326a31d2012-06-28 12:05:09 +02004 *
David Wagnerb76c9d62014-02-05 18:30:24 +01005 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
Georges-Henri Baron326a31d2012-06-28 12:05:09 +02007 *
David Wagnerb76c9d62014-02-05 18:30:24 +01008 * 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 Benavoli68a91282011-08-31 11:23:23 +020029 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020030
Patrick Benavoli68a91282011-08-31 11:23:23 +020031#pragma once
Patrick Benavoli68a91282011-08-31 11:23:23 +020032#include "XmlElement.h"
33#include "XmlSerializingContext.h"
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020034#include <string>
Patrick Benavoli68a91282011-08-31 11:23:23 +020035
36struct _xmlDoc;
37struct _xmlNode;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020038struct _xmlError;
Patrick Benavoli68a91282011-08-31 11:23:23 +020039
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020040/**
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 Baron326a31d2012-06-28 12:05:09 +020047class CXmlDocSource
Patrick Benavoli68a91282011-08-31 11:23:23 +020048{
49public:
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020050 /**
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 Baron326a31d2012-06-28 12:05:09 +020056 CXmlDocSource(_xmlDoc* pDoc, _xmlNode* pRootNode = NULL);
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020057
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 Baron326a31d2012-06-28 12:05:09 +020085 virtual ~CXmlDocSource();
Patrick Benavoli68a91282011-08-31 11:23:23 +020086
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020087 /**
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 Baron326a31d2012-06-28 12:05:09 +020094 virtual bool populate(CXmlSerializingContext& serializingContext) = 0;
Patrick Benavoli68a91282011-08-31 11:23:23 +020095
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020096 /**
97 * Method that returns the root element of the Xml tree.
98 *
99 * @param[out] xmlRootElement a reference to the CXmleElement destination
100 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200101 void getRootElement(CXmlElement& xmlRootElement) const;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200102
103 /**
104 * Getter method.
105 *
106 * @return the root element's name
107 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200108 string getRootElementName() const;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200109
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 Benavoli68a91282011-08-31 11:23:23 +0200118 string getRootElementAttributeString(const string& strAttributeName) const;
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200119
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200120 /**
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 Baron326a31d2012-06-28 12:05:09 +0200127 _xmlDoc* getDoc() const;
128
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200129 /**
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 Baron326a31d2012-06-28 12:05:09 +0200138
Patrick Benavoli68a91282011-08-31 11:23:23 +0200139protected:
Patrick Benavoli68a91282011-08-31 11:23:23 +0200140
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200141 /**
142 * Doc
143 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200144 _xmlDoc* _pDoc;
145
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200146 /**
147 * Root node
148 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200149 _xmlNode* _pRootNode;
150
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200151 /**
152 * libxml2 library cleanup
153 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200154 static bool _bLibXml2CleanupScheduled;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200155
156private:
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 Benavoli68a91282011-08-31 11:23:23 +0200200};