1326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron/*
29368eea42a1afb01dd44110582f997115b50e742François Gaffie * Copyright (c) 2011-2015, Intel Corporation
3b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * All rights reserved.
4326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron *
5b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Redistribution and use in source and binary forms, with or without modification,
6b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * are permitted provided that the following conditions are met:
7326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron *
8b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 1. Redistributions of source code must retain the above copyright notice, this
9b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * list of conditions and the following disclaimer.
10b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
11b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 2. Redistributions in binary form must reproduce the above copyright notice,
12b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * this list of conditions and the following disclaimer in the documentation and/or
13b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * other materials provided with the distribution.
14b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
15b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 3. Neither the name of the copyright holder nor the names of its contributors
16b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * may be used to endorse or promote products derived from this software without
17b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * specific prior written permission.
18b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
19b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2968a912857707864bbaaff9808717813105072a6ePatrick Benavoli */
30326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron
3168a912857707864bbaaff9808717813105072a6ePatrick Benavoli#pragma once
3268a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "XmlElement.h"
3368a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "XmlSerializingContext.h"
349368eea42a1afb01dd44110582f997115b50e742François Gaffie
359368eea42a1afb01dd44110582f997115b50e742François Gaffie#include "NonCopyable.hpp"
369368eea42a1afb01dd44110582f997115b50e742François Gaffie
37cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron#include <string>
3868a912857707864bbaaff9808717813105072a6ePatrick Benavoli
3968a912857707864bbaaff9808717813105072a6ePatrick Benavolistruct _xmlDoc;
4068a912857707864bbaaff9808717813105072a6ePatrick Benavolistruct _xmlNode;
41cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baronstruct _xmlError;
4268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
43cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron/**
44cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron  * The CXmlDocSource is used by CXmlDocSink.
45cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron  * The interaction between the xml source and xml sink is defined
46cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron  * in the process method of CXmlDocSink. One can subclass CXmlDocSource
47cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron  * for different purposes by implementing the populate method and then
48cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron  * use it with any existing implementation of CXmlDocSink.
49cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron  */
509368eea42a1afb01dd44110582f997115b50e742François Gaffieclass CXmlDocSource : private utility::NonCopyable
5168a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
5268a912857707864bbaaff9808717813105072a6ePatrick Benavolipublic:
53cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
54cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Constructor
55cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
56cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @param[out] pDoc a pointer to the xml document that will be filled by the class
57cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @param[in] pRootNode a pointer to the root element of the document.
583141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek      * @param[in] bValidateWithSchema a boolean that toggles schema validation
59cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
609368eea42a1afb01dd44110582f997115b50e742François Gaffie    CXmlDocSource(_xmlDoc *pDoc, bool bValidateWithSchema = false, _xmlNode *pRootNode = NULL);
61cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
62cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
63cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Constructor
64cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
65cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @param[out] pDoc a pointer to the xml document that will be filled by the class
66cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @param[in] strRootElementType a string containing the root element type
673141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek      * @param[in] strRootElementName a string containing the root element name
683141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek      * @param[in] strNameAttributeName a string containing the name of the root name attribute
693141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek      * @param[in] bValidateWithSchema a boolean that toggles schema validation
70cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
719368eea42a1afb01dd44110582f997115b50e742François Gaffie    CXmlDocSource(_xmlDoc *pDoc, bool bValidateWithSchema,
729368eea42a1afb01dd44110582f997115b50e742François Gaffie                  const std::string &strRootElementType = "",
739368eea42a1afb01dd44110582f997115b50e742François Gaffie                  const std::string &strRootElementName = "",
749368eea42a1afb01dd44110582f997115b50e742François Gaffie                  const std::string &strNameAttributeName = "");
75cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
76cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
77cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Destructor
78cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
79326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron    virtual ~CXmlDocSource();
8068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
81cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
82cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Method called by the CXmlDocSink::process method.
83cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
84cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @param[out] serializingContext is used as error output
85cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
86cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @return false if there are any error
87cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
889368eea42a1afb01dd44110582f997115b50e742François Gaffie    virtual bool populate(CXmlSerializingContext &serializingContext);
8968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
90cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
91cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Method that returns the root element of the Xml tree.
92cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
93cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @param[out] xmlRootElement a reference to the CXmleElement destination
94cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
959368eea42a1afb01dd44110582f997115b50e742François Gaffie    void getRootElement(CXmlElement &xmlRootElement) const;
96cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
97cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
98cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Getter method.
99cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
100cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @return the root element's name
101cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
102d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalve    std::string getRootElementName() const;
103cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
1049368eea42a1afb01dd44110582f997115b50e742François Gaffie    /** Get the Schemas' base (folder) URI
1059368eea42a1afb01dd44110582f997115b50e742François Gaffie     */
1069368eea42a1afb01dd44110582f997115b50e742François Gaffie    std::string getSchemaBaseUri();
1079368eea42a1afb01dd44110582f997115b50e742François Gaffie
1089368eea42a1afb01dd44110582f997115b50e742François Gaffie    /** Set the Schema's base (folder) URI
1099368eea42a1afb01dd44110582f997115b50e742François Gaffie     *
1109368eea42a1afb01dd44110582f997115b50e742François Gaffie     * The schema for validating the XML document will be searched for in that
1119368eea42a1afb01dd44110582f997115b50e742François Gaffie     * folder.
1129368eea42a1afb01dd44110582f997115b50e742François Gaffie     *
1139368eea42a1afb01dd44110582f997115b50e742François Gaffie     * @param[in] uri The Schemas' base URI
1149368eea42a1afb01dd44110582f997115b50e742François Gaffie     */
1159368eea42a1afb01dd44110582f997115b50e742François Gaffie    void setSchemaBaseUri(const std::string &uri);
1169368eea42a1afb01dd44110582f997115b50e742François Gaffie
117cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
118cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Getter method.
119cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Method that returns the root element's attribute with name matching strAttributeName.
120cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
121d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalve      * @param[in] strAttributeName is a std::string used to find the corresponding attribute
122cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
123cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @return the value of the root's attribute named as strAttributeName
124cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
1259368eea42a1afb01dd44110582f997115b50e742François Gaffie    std::string getRootElementAttributeString(const std::string &strAttributeName) const;
12668a912857707864bbaaff9808717813105072a6ePatrick Benavoli
127cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
128cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Getter method.
129cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Method that returns the xmlDoc contained in the Source.
130cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * (Can be used in a Doc Sink)
131cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
132cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @return the document _pDoc
133cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
1349368eea42a1afb01dd44110582f997115b50e742François Gaffie    _xmlDoc *getDoc() const;
135cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
136a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    /**
137a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    * Method that checks that the xml document has been correctly parsed.
138a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    *
139a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    * @return false if any error occurs during the parsing
140a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    */
141a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    virtual bool isParsable() const;
142a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi
143a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    /**
1449368eea42a1afb01dd44110582f997115b50e742François Gaffie     * Helper method to build final URI from base and relative uri/path
1459368eea42a1afb01dd44110582f997115b50e742François Gaffie     *
1469368eea42a1afb01dd44110582f997115b50e742François Gaffie     * base = "/path/to/file.xml"
1479368eea42a1afb01dd44110582f997115b50e742François Gaffie     *  - uri                   - returned value
1489368eea42a1afb01dd44110582f997115b50e742François Gaffie     *  .                       /path/to/
1499368eea42a1afb01dd44110582f997115b50e742François Gaffie     *  file.xsd                /path/to/file.xsd
1509368eea42a1afb01dd44110582f997115b50e742François Gaffie     *  ../from/file.xsd        /path/from/file.xsd
1519368eea42a1afb01dd44110582f997115b50e742François Gaffie     *  /path2/file.xsd         /path2/file.xsd
1529368eea42a1afb01dd44110582f997115b50e742François Gaffie     *
1539368eea42a1afb01dd44110582f997115b50e742François Gaffie     * @param[in] base uri, if base is directory, it must contains separator last
1549368eea42a1afb01dd44110582f997115b50e742François Gaffie     * @param[in] relative uri
1559368eea42a1afb01dd44110582f997115b50e742François Gaffie    *
1569368eea42a1afb01dd44110582f997115b50e742François Gaffie    * @return new made URI if succeeded relative input otherwise
1579368eea42a1afb01dd44110582f997115b50e742François Gaffie     */
1589368eea42a1afb01dd44110582f997115b50e742François Gaffie    static std::string mkUri(const std::string &base, const std::string &relative);
1599368eea42a1afb01dd44110582f997115b50e742François Gaffie
1609368eea42a1afb01dd44110582f997115b50e742François Gaffie    /**
161a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     * Helper method for creating an xml document from either a file or a
162a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     * string.
163a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     *
164a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     * @param[in] source either a filename or a string representing an xml document
165a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     * @param[in] fromFile true if source is a filename, false if source is an xml
166a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     *            represents an xml document
167a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     * @param[in] xincludes if true, process xincludes tags
1689368eea42a1afb01dd44110582f997115b50e742François Gaffie     * @param[in] serializingContext will receive any serialization error
169a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi     */
1709368eea42a1afb01dd44110582f997115b50e742François Gaffie    static _xmlDoc *mkXmlDoc(const std::string &source, bool fromFile, bool xincludes,
1719368eea42a1afb01dd44110582f997115b50e742François Gaffie                             CXmlSerializingContext &serializingContext);
17268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
173326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baronprotected:
174cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
175cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Doc
176cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
1779368eea42a1afb01dd44110582f997115b50e742François Gaffie    _xmlDoc *_pDoc;
17868a912857707864bbaaff9808717813105072a6ePatrick Benavoli
179cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
180cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Root node
181cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
1829368eea42a1afb01dd44110582f997115b50e742François Gaffie    _xmlNode *_pRootNode;
183cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
184cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baronprivate:
185cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /** Method that check the validity of the document with the xsd file.
186cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      *
187cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * @return true if document is valid, false if any error occures
188cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
189cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    bool isInstanceDocumentValid();
1909368eea42a1afb01dd44110582f997115b50e742François Gaffie    std::string getSchemaUri() const;
191cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
192cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
193cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Element type info
194cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
195d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalve    std::string _strRootElementType;
196cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
197cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
198cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Element name info
199cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
200d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalve    std::string _strRootElementName;
201cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron
202cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron    /**
203cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      * Element name attribute info
204cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron      */
205a9be2d378b7ad84e679a48efa81f42fb54f85d9aJean-Michel Trivi    std::string _strNameAttributeName;
2063141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek
2073141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek    /**
2083141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek      * Boolean that enables the validation via xsd files
2093141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek      */
2103141891778a0564887d25b8b06b898daf3c1c9b5Mattijs Korpershoek    bool _bValidateWithSchema;
2119368eea42a1afb01dd44110582f997115b50e742François Gaffie
2129368eea42a1afb01dd44110582f997115b50e742François Gaffie    std::string _schemaBaseUri;
21368a912857707864bbaaff9808717813105072a6ePatrick Benavoli};
214