| 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 |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 39 | CXmlFileIncluderElement::CXmlFileIncluderElement(const std::string& strName, |
| 40 | const std::string& strKind, |
| Patrick Benavoli | 9ad87f0 | 2014-09-20 21:32:54 +0200 | [diff] [blame^] | 41 | bool bValidateWithSchemas) |
| 42 | : base(strName, strKind), _bValidateSchemasOnStart(bValidateWithSchemas) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | // From IXmlSink |
| 47 | bool CXmlFileIncluderElement::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 48 | { |
| 49 | // Context |
| 50 | CXmlElementSerializingContext& elementSerializingContext = static_cast<CXmlElementSerializingContext&>(serializingContext); |
| 51 | |
| 52 | // Parse included document |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 53 | std::string strPath = xmlElement.getAttributeString("Path"); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 54 | |
| 55 | // Relative path? |
| 56 | if (strPath[0] != '/') { |
| 57 | |
| 58 | strPath = elementSerializingContext.getXmlFolder() + "/" + strPath; |
| 59 | } |
| 60 | |
| 61 | // Instantiate parser |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 62 | std::string strIncludedElementType = getIncludedElementType(); |
| Kevin Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 63 | { |
| 64 | // Open a log section titled with loading file path |
| 65 | CAutoLog autolog(this, "Loading " + strPath); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 66 | |
| Kevin Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 67 | // Use a doc source that load data from a file |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 68 | std::string strPathToXsdFile = elementSerializingContext.getXmlSchemaPathFolder() + "/" + |
| Mattijs Korpershoek | cce85f6 | 2014-04-08 14:10:03 +0200 | [diff] [blame] | 69 | strIncludedElementType + ".xsd"; |
| 70 | |
| 71 | CXmlFileDocSource fileDocSource(strPath, |
| 72 | strPathToXsdFile, |
| 73 | strIncludedElementType, |
| 74 | _bValidateSchemasOnStart); |
| Frédéric Boisnard | a409daa | 2012-10-18 18:20:03 +0200 | [diff] [blame] | 75 | |
| Kevin Rocard | 57096bd | 2012-11-30 11:24:20 +0100 | [diff] [blame] | 76 | if (!fileDocSource.isParsable(elementSerializingContext)) { |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // Get top level element |
| 82 | CXmlElement childElement; |
| 83 | |
| 84 | fileDocSource.getRootElement(childElement); |
| 85 | |
| 86 | // Create child element |
| 87 | CElement* pChild = elementSerializingContext.getElementLibrary()->createElement(childElement); |
| 88 | |
| 89 | if (pChild) { |
| 90 | |
| 91 | // Store created child! |
| 92 | getParent()->addChild(pChild); |
| 93 | } else { |
| 94 | |
| 95 | elementSerializingContext.setError("Unable to create XML element " + childElement.getPath()); |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Use a doc sink that instantiate the structure from the doc source |
| 101 | CXmlMemoryDocSink memorySink(pChild); |
| 102 | |
| 103 | if (!memorySink.process(fileDocSource, elementSerializingContext)) { |
| 104 | |
| 105 | return false; |
| 106 | } |
| Frédéric Boisnard | a409daa | 2012-10-18 18:20:03 +0200 | [diff] [blame] | 107 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 108 | // Detach from parent |
| 109 | getParent()->removeChild(this); |
| 110 | |
| 111 | // Self destroy |
| 112 | delete this; |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | // Element type |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 118 | std::string CXmlFileIncluderElement::getIncludedElementType() const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 119 | { |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 120 | std::string strKind = getKind(); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 121 | |
| 122 | int iPosToRemoveFrom = strKind.rfind("Include", -1); |
| 123 | |
| 124 | assert(iPosToRemoveFrom != -1); |
| 125 | |
| 126 | return strKind.substr(0, iPosToRemoveFrom); |
| 127 | } |