1/*
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.
29 */
30#include "ComponentInstance.h"
31#include "ComponentLibrary.h"
32#include "ComponentType.h"
33#include "Component.h"
34#include "XmlParameterSerializingContext.h"
35
36#define base CTypeElement
37
38CComponentInstance::CComponentInstance(const std::string& strName) : base(strName), _pComponentType(NULL)
39{
40}
41
42std::string CComponentInstance::getKind() const
43{
44    return "Component";
45}
46
47bool CComponentInstance::childrenAreDynamic() const
48{
49    return true;
50}
51
52bool CComponentInstance::getMappingData(const std::string& strKey, const std::string*& pStrValue) const
53{
54    // Try myself first then associated component type
55    return base::getMappingData(strKey, pStrValue) || (_pComponentType && _pComponentType->getMappingData(strKey, pStrValue));
56}
57
58bool CComponentInstance::hasMappingData() const
59{
60    // Try myself first then extended component type
61    return base::hasMappingData() || (_pComponentType && _pComponentType->hasMappingData());
62}
63
64std::string CComponentInstance::getFormattedMapping() const
65{
66    // Try myself first then associated component type
67    std::string strValue = base::getFormattedMapping();
68    if (_pComponentType) {
69
70        strValue += _pComponentType->getFormattedMapping();
71    }
72
73    return strValue;
74}
75
76bool CComponentInstance::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
77{
78    // Context
79    CXmlParameterSerializingContext& parameterBuildContext = static_cast<CXmlParameterSerializingContext&>(serializingContext);
80
81    const CComponentLibrary* pComponentLibrary = parameterBuildContext.getComponentLibrary();
82
83    std::string strComponentType = xmlElement.getAttributeString("Type");
84
85    _pComponentType = pComponentLibrary->getComponentType(strComponentType);
86
87    if (!_pComponentType) {
88
89        serializingContext.setError("Unable to create Component " + xmlElement.getPath() + ". ComponentType " + strComponentType + " not found!");
90
91        return false;
92    }
93    if (_pComponentType == getParent()) {
94
95        serializingContext.setError("Recursive definition of " + _pComponentType->getName() + " due to " + xmlElement.getPath() + " referring to one of its own type.");
96
97        return false;
98    }
99
100    return base::fromXml(xmlElement, serializingContext);
101}
102
103CInstanceConfigurableElement* CComponentInstance::doInstantiate() const
104{
105    return new CComponent(getName(), this);
106}
107
108void CComponentInstance::populate(CElement* pElement) const
109{
110    base::populate(pElement);
111
112    _pComponentType->populate(static_cast<CComponent*>(pElement));
113}
114