blob: 2e97bdbf36292aacef8ac1ed6167cc30ba3ccc0e [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
Mattijs Korpershoekcce85f62014-04-08 14:10:03 +020039CXmlFileIncluderElement::CXmlFileIncluderElement(const string& strName,
40 const string& strKind,
41 bool bValidateWithSchemas) : base(strName,
42 strKind)
Patrick Benavoli68a91282011-08-31 11:23:23 +020043{
Mattijs Korpershoekcce85f62014-04-08 14:10:03 +020044 _bValidateSchemasOnStart = bValidateWithSchemas;
Patrick Benavoli68a91282011-08-31 11:23:23 +020045}
46
47// From IXmlSink
48bool CXmlFileIncluderElement::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
49{
50 // Context
51 CXmlElementSerializingContext& elementSerializingContext = static_cast<CXmlElementSerializingContext&>(serializingContext);
52
53 // Parse included document
54 string strPath = xmlElement.getAttributeString("Path");
55
56 // Relative path?
57 if (strPath[0] != '/') {
58
59 strPath = elementSerializingContext.getXmlFolder() + "/" + strPath;
60 }
61
62 // Instantiate parser
63 string strIncludedElementType = getIncludedElementType();
Kevin Rocard57096bd2012-11-30 11:24:20 +010064 {
65 // Open a log section titled with loading file path
66 CAutoLog autolog(this, "Loading " + strPath);
Patrick Benavoli68a91282011-08-31 11:23:23 +020067
Kevin Rocard57096bd2012-11-30 11:24:20 +010068 // Use a doc source that load data from a file
Mattijs Korpershoekcce85f62014-04-08 14:10:03 +020069 string strPathToXsdFile = elementSerializingContext.getXmlSchemaPathFolder() + "/" +
70 strIncludedElementType + ".xsd";
71
72 CXmlFileDocSource fileDocSource(strPath,
73 strPathToXsdFile,
74 strIncludedElementType,
75 _bValidateSchemasOnStart);
Frédéric Boisnarda409daa2012-10-18 18:20:03 +020076
Kevin Rocard57096bd2012-11-30 11:24:20 +010077 if (!fileDocSource.isParsable(elementSerializingContext)) {
78
79 return false;
80 }
81
82 // Get top level element
83 CXmlElement childElement;
84
85 fileDocSource.getRootElement(childElement);
86
87 // Create child element
88 CElement* pChild = elementSerializingContext.getElementLibrary()->createElement(childElement);
89
90 if (pChild) {
91
92 // Store created child!
93 getParent()->addChild(pChild);
94 } else {
95
96 elementSerializingContext.setError("Unable to create XML element " + childElement.getPath());
97
98 return false;
99 }
100
101 // Use a doc sink that instantiate the structure from the doc source
102 CXmlMemoryDocSink memorySink(pChild);
103
104 if (!memorySink.process(fileDocSource, elementSerializingContext)) {
105
106 return false;
107 }
Frédéric Boisnarda409daa2012-10-18 18:20:03 +0200108 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200109 // Detach from parent
110 getParent()->removeChild(this);
111
112 // Self destroy
113 delete this;
114
115 return true;
116}
117
118// Element type
119string CXmlFileIncluderElement::getIncludedElementType() const
120{
121 string strKind = getKind();
122
123 int iPosToRemoveFrom = strKind.rfind("Include", -1);
124
125 assert(iPosToRemoveFrom != -1);
126
127 return strKind.substr(0, iPosToRemoveFrom);
128}