| David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 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. |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "XmlFileIncluderElement.h" |
| Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 31 | #include "XmlFileDocSource.h" |
| 32 | #include "XmlMemoryDocSink.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 33 | #include "XmlElementSerializingContext.h" |
| 34 | #include "ElementLibrary.h" |
| Kevin Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 35 | #include "AutoLog.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 36 | #include <assert.h> |
| 37 | |
| 38 | #define base CKindElement |
| Mattijs Korpershoek | cce85f6 | 2014-04-08 14:10:03 +0200 | [diff] [blame^] | 39 | CXmlFileIncluderElement::CXmlFileIncluderElement(const string& strName, |
| 40 | const string& strKind, |
| 41 | bool bValidateWithSchemas) : base(strName, |
| 42 | strKind) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 43 | { |
| Mattijs Korpershoek | cce85f6 | 2014-04-08 14:10:03 +0200 | [diff] [blame^] | 44 | _bValidateSchemasOnStart = bValidateWithSchemas; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // From IXmlSink |
| 48 | bool 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 Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 64 | { |
| 65 | // Open a log section titled with loading file path |
| 66 | CAutoLog autolog(this, "Loading " + strPath); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 67 | |
| Kevin Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 68 | // Use a doc source that load data from a file |
| Mattijs Korpershoek | cce85f6 | 2014-04-08 14:10:03 +0200 | [diff] [blame^] | 69 | string strPathToXsdFile = elementSerializingContext.getXmlSchemaPathFolder() + "/" + |
| 70 | strIncludedElementType + ".xsd"; |
| 71 | |
| 72 | CXmlFileDocSource fileDocSource(strPath, |
| 73 | strPathToXsdFile, |
| 74 | strIncludedElementType, |
| 75 | _bValidateSchemasOnStart); |
| Frédéric Boisnard | a409daa | 2012-10-18 18:20:03 +0200 | [diff] [blame] | 76 | |
| Kevin Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 77 | 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 Boisnard | a409daa | 2012-10-18 18:20:03 +0200 | [diff] [blame] | 108 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 109 | // Detach from parent |
| 110 | getParent()->removeChild(this); |
| 111 | |
| 112 | // Self destroy |
| 113 | delete this; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | // Element type |
| 119 | string 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 | } |