blob: 195342565314d3b3e717c088b1f50bd761f8c0c4 [file] [log] [blame]
Georges-Henri Baron326a31d2012-06-28 12:05:09 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * INTEL CONFIDENTIAL
Georges-Henri Baroncec86c12012-09-04 17:30:28 +02003 * Copyright © 2013 Intel
Patrick Benavoli68a91282011-08-31 11:23:23 +02004 * Corporation All Rights Reserved.
Georges-Henri Baron326a31d2012-06-28 12:05:09 +02005 *
Patrick Benavoli68a91282011-08-31 11:23:23 +02006 * The source code contained or described herein and all documents related to
7 * the source code ("Material") are owned by Intel Corporation or its suppliers
8 * or licensors. Title to the Material remains with Intel Corporation or its
9 * suppliers and licensors. The Material contains trade secrets and proprietary
10 * and confidential information of Intel or its suppliers and licensors. The
11 * Material is protected by worldwide copyright and trade secret laws and
12 * treaty provisions. No part of the Material may be used, copied, reproduced,
13 * modified, published, uploaded, posted, transmitted, distributed, or
14 * disclosed in any way without Intel’s prior express written permission.
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020015 *
Patrick Benavoli68a91282011-08-31 11:23:23 +020016 * No license under any patent, copyright, trade secret or other intellectual
17 * property right is granted to or conferred upon you by disclosure or delivery
18 * of the Materials, either expressly, by implication, inducement, estoppel or
19 * otherwise. Any license under such intellectual property rights must be
20 * express and approved by Intel in writing.
Patrick Benavoli68a91282011-08-31 11:23:23 +020021 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020022
Patrick Benavoli68a91282011-08-31 11:23:23 +020023#pragma once
Patrick Benavoli68a91282011-08-31 11:23:23 +020024#include "XmlElement.h"
25#include "XmlSerializingContext.h"
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020026#include <string>
Patrick Benavoli68a91282011-08-31 11:23:23 +020027
28struct _xmlDoc;
29struct _xmlNode;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020030struct _xmlError;
Patrick Benavoli68a91282011-08-31 11:23:23 +020031
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020032/**
33 * The CXmlDocSource is used by CXmlDocSink.
34 * The interaction between the xml source and xml sink is defined
35 * in the process method of CXmlDocSink. One can subclass CXmlDocSource
36 * for different purposes by implementing the populate method and then
37 * use it with any existing implementation of CXmlDocSink.
38 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020039class CXmlDocSource
Patrick Benavoli68a91282011-08-31 11:23:23 +020040{
41public:
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020042 /**
43 * Constructor
44 *
45 * @param[out] pDoc a pointer to the xml document that will be filled by the class
46 * @param[in] pRootNode a pointer to the root element of the document.
47 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020048 CXmlDocSource(_xmlDoc* pDoc, _xmlNode* pRootNode = NULL);
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020049
50 /**
51 * Constructor
52 *
53 * @param[out] pDoc a pointer to the xml document that will be filled by the class
54 * @param[in] strXmlSchemaFile a string containing the path to the schema file
55 * @param[in] strRootElementType a string containing the root element type
56 * @param[in] strRootElementName a string containing the root element name
57 * @param[in] strNameAttributeName a string containing the name of the root name attribute
58 */
59 CXmlDocSource(_xmlDoc* pDoc,
60 const string& strXmlSchemaFile,
61 const string& strRootElementType,
62 const string& strRootElementName,
63 const string& strNameAttrituteName);
64
65 /**
66 * Constructor
67 *
68 * @param[out] pDoc a pointer to the xml document that will be filled by the class
69 * @param[in] strXmlSchemaFile a string containing the path to the schema file
70 * @param[in] strRootElementType a string containing the root element type
71 */
72 CXmlDocSource(_xmlDoc* pDoc, const string& strXmlSchemaFile, const string& strRootElementType);
73
74 /**
75 * Destructor
76 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020077 virtual ~CXmlDocSource();
Patrick Benavoli68a91282011-08-31 11:23:23 +020078
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020079 /**
80 * Method called by the CXmlDocSink::process method.
81 *
82 * @param[out] serializingContext is used as error output
83 *
84 * @return false if there are any error
85 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020086 virtual bool populate(CXmlSerializingContext& serializingContext) = 0;
Patrick Benavoli68a91282011-08-31 11:23:23 +020087
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020088 /**
89 * Method that returns the root element of the Xml tree.
90 *
91 * @param[out] xmlRootElement a reference to the CXmleElement destination
92 */
Patrick Benavoli68a91282011-08-31 11:23:23 +020093 void getRootElement(CXmlElement& xmlRootElement) const;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +020094
95 /**
96 * Getter method.
97 *
98 * @return the root element's name
99 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100 string getRootElementName() const;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200101
102 /**
103 * Getter method.
104 * Method that returns the root element's attribute with name matching strAttributeName.
105 *
106 * @param[in] strAttributeName is a string used to find the corresponding attribute
107 *
108 * @return the value of the root's attribute named as strAttributeName
109 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200110 string getRootElementAttributeString(const string& strAttributeName) const;
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200111
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200112 /**
113 * Getter method.
114 * Method that returns the xmlDoc contained in the Source.
115 * (Can be used in a Doc Sink)
116 *
117 * @return the document _pDoc
118 */
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200119 _xmlDoc* getDoc() const;
120
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200121 /**
122 * Method that validates the Xml doc contained in pDoc
123 *
124 * @param[out] serializingContext is used as error output
125 *
126 * @return false if any error occurs
127 */
128 virtual bool validate(CXmlSerializingContext& serializingContext);
129
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200130
Patrick Benavoli68a91282011-08-31 11:23:23 +0200131protected:
Patrick Benavoli68a91282011-08-31 11:23:23 +0200132
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200133 /**
134 * Doc
135 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200136 _xmlDoc* _pDoc;
137
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200138 /**
139 * Root node
140 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200141 _xmlNode* _pRootNode;
142
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200143 /**
144 * libxml2 library cleanup
145 */
Patrick Benavoli68a91282011-08-31 11:23:23 +0200146 static bool _bLibXml2CleanupScheduled;
Georges-Henri Baroncec86c12012-09-04 17:30:28 +0200147
148private:
149
150 /**
151 * Method that initializes class internal attributes in constructor
152 */
153 void init();
154
155 /** Method that check the validity of the document with the xsd file.
156 *
157 * @return true if document is valid, false if any error occures
158 */
159 bool isInstanceDocumentValid();
160
161 /** Validity error display method
162 *
163 * @param[in] pUserData pointer to the data to validate
164 * @param[out] pError is the xml error output
165 */
166 static void schemaValidityStructuredErrorFunc(void* pUserData, _xmlError* pError);
167
168 /**
169 * Schema file
170 */
171 string _strXmlSchemaFile;
172
173 /**
174 * Element type info
175 */
176 string _strRootElementType;
177
178 /**
179 * Element name info
180 */
181 string _strRootElementName;
182
183 /**
184 * Element name attribute info
185 */
186 string _strNameAttrituteName;
187
188 /**
189 * Boolean that enables the root element name attribute check
190 */
191 bool _bNameCheck;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200192};