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