blob: 693b6fdde3f1af68fd2b7ed9052ba509274b21e6 [file] [log] [blame]
David Wagnerb76c9d62014-02-05 18:30:24 +01001/*
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.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#include "XmlFileIncluderElement.h"
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020031#include "XmlFileDocSource.h"
32#include "XmlMemoryDocSink.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020033#include "XmlElementSerializingContext.h"
34#include "ElementLibrary.h"
Kevin Rocard57096bd2012-11-30 11:24:20 +010035#include "AutoLog.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020036#include <assert.h>
37
38#define base CKindElement
Sebastien Gonzalved9526492014-02-20 22:28:03 +010039CXmlFileIncluderElement::CXmlFileIncluderElement(const std::string& strName,
40 const std::string& strKind,
Patrick Benavoli9ad87f02014-09-20 21:32:54 +020041 bool bValidateWithSchemas)
42 : base(strName, strKind), _bValidateSchemasOnStart(bValidateWithSchemas)
Patrick Benavoli68a91282011-08-31 11:23:23 +020043{
44}
45
46// From IXmlSink
47bool CXmlFileIncluderElement::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
48{
49 // Context
50 CXmlElementSerializingContext& elementSerializingContext = static_cast<CXmlElementSerializingContext&>(serializingContext);
51
52 // Parse included document
Sebastien Gonzalved9526492014-02-20 22:28:03 +010053 std::string strPath = xmlElement.getAttributeString("Path");
Patrick Benavoli68a91282011-08-31 11:23:23 +020054
55 // Relative path?
56 if (strPath[0] != '/') {
57
58 strPath = elementSerializingContext.getXmlFolder() + "/" + strPath;
59 }
60
61 // Instantiate parser
Sebastien Gonzalved9526492014-02-20 22:28:03 +010062 std::string strIncludedElementType = getIncludedElementType();
Kevin Rocard57096bd2012-11-30 11:24:20 +010063 {
64 // Open a log section titled with loading file path
65 CAutoLog autolog(this, "Loading " + strPath);
Patrick Benavoli68a91282011-08-31 11:23:23 +020066
Kevin Rocard57096bd2012-11-30 11:24:20 +010067 // Use a doc source that load data from a file
Sebastien Gonzalved9526492014-02-20 22:28:03 +010068 std::string strPathToXsdFile = elementSerializingContext.getXmlSchemaPathFolder() + "/" +
Mattijs Korpershoekcce85f62014-04-08 14:10:03 +020069 strIncludedElementType + ".xsd";
70
71 CXmlFileDocSource fileDocSource(strPath,
72 strPathToXsdFile,
73 strIncludedElementType,
74 _bValidateSchemasOnStart);
Frédéric Boisnarda409daa2012-10-18 18:20:03 +020075
Kevin Rocard57096bd2012-11-30 11:24:20 +010076 if (!fileDocSource.isParsable(elementSerializingContext)) {
77
78 return false;
79 }
80
81 // Get top level element
82 CXmlElement childElement;
83
84 fileDocSource.getRootElement(childElement);
85
86 // Create child element
87 CElement* pChild = elementSerializingContext.getElementLibrary()->createElement(childElement);
88
89 if (pChild) {
90
91 // Store created child!
92 getParent()->addChild(pChild);
93 } else {
94
95 elementSerializingContext.setError("Unable to create XML element " + childElement.getPath());
96
97 return false;
98 }
99
100 // Use a doc sink that instantiate the structure from the doc source
101 CXmlMemoryDocSink memorySink(pChild);
102
103 if (!memorySink.process(fileDocSource, elementSerializingContext)) {
104
105 return false;
106 }
Frédéric Boisnarda409daa2012-10-18 18:20:03 +0200107 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200108 // Detach from parent
109 getParent()->removeChild(this);
110
111 // Self destroy
112 delete this;
113
114 return true;
115}
116
117// Element type
Sebastien Gonzalved9526492014-02-20 22:28:03 +0100118std::string CXmlFileIncluderElement::getIncludedElementType() const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200119{
Sebastien Gonzalved9526492014-02-20 22:28:03 +0100120 std::string strKind = getKind();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200121
122 int iPosToRemoveFrom = strKind.rfind("Include", -1);
123
124 assert(iPosToRemoveFrom != -1);
125
126 return strKind.substr(0, iPosToRemoveFrom);
127}