blob: 4000758d8ce535816b4fab4380337e45169a54be [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "XmlFileIncluderElement.h"
32#include "XmlParser.h"
33#include "XmlElementSerializingContext.h"
34#include "ElementLibrary.h"
35#include <assert.h>
36
37#define base CKindElement
38
39CXmlFileIncluderElement::CXmlFileIncluderElement(const string& strName, const string& strKind) : base(strName, strKind)
40{
41}
42
43// From IXmlSink
44bool CXmlFileIncluderElement::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
45{
46 // Context
47 CXmlElementSerializingContext& elementSerializingContext = static_cast<CXmlElementSerializingContext&>(serializingContext);
48
49 // Parse included document
50 string strPath = xmlElement.getAttributeString("Path");
51
52 // Relative path?
53 if (strPath[0] != '/') {
54
55 strPath = elementSerializingContext.getXmlFolder() + "/" + strPath;
56 }
57
58 // Instantiate parser
59 string strIncludedElementType = getIncludedElementType();
60
61 CXmlParser parser(strPath, elementSerializingContext.getXmlSchemaPathFolder() + "/" + strIncludedElementType + ".xsd", strIncludedElementType, elementSerializingContext);
62
63 if (!parser.open()) {
64
65 return false;
66 }
67
68 // Get top level element
69 CXmlElement childElement;
70
71 parser.getRootElement(childElement);
72
73 // Create child element
74 CElement* pChild = elementSerializingContext.getElementLibrary()->createElement(childElement);
75
76 if (pChild) {
77
78 // Store created child!
79 getParent()->addChild(pChild);
80 } else {
81
82 elementSerializingContext.setError("Unable to create XML element " + childElement.getPath());
83
84 return false;
85 }
86
87 if (!parser.parse(pChild)) {
88
89 return false;
90 }
91
92 // Detach from parent
93 getParent()->removeChild(this);
94
95 // Self destroy
96 delete this;
97
98 return true;
99}
100
101// Element type
102string CXmlFileIncluderElement::getIncludedElementType() const
103{
104 string strKind = getKind();
105
106 int iPosToRemoveFrom = strKind.rfind("Include", -1);
107
108 assert(iPosToRemoveFrom != -1);
109
110 return strKind.substr(0, iPosToRemoveFrom);
111}