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