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 "XmlFileIncluderElement.h"
31 #include "XmlDocSource.h"
32 #include "XmlMemoryDocSink.h"
33 #include "XmlElementSerializingContext.h"
34 #include "ElementLibrary.h"
35 #include "AutoLog.h"
36 #include <assert.h>
37 #include <fstream>
38 
39 #define base CKindElement
CXmlFileIncluderElement(const std::string & strName,const std::string & strKind,bool bValidateWithSchemas)40 CXmlFileIncluderElement::CXmlFileIncluderElement(const std::string& strName,
41                                                  const std::string& strKind,
42                                                  bool bValidateWithSchemas)
43     : base(strName, strKind), _bValidateSchemasOnStart(bValidateWithSchemas)
44 {
45 }
46 
47 // From IXmlSink
fromXml(const CXmlElement & xmlElement,CXmlSerializingContext & serializingContext)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     std::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     std::string strIncludedElementType = getIncludedElementType();
64     {
65         // Open a log section titled with loading file path
66         CAutoLog autolog(this, "Loading " + strPath);
67 
68         // Use a doc source that load data from a file
69         std::string strPathToXsdFile = elementSerializingContext.getXmlSchemaPathFolder() + "/" +
70                                strIncludedElementType + ".xsd";
71 
72         std::string xmlErrorMsg;
73         _xmlDoc *doc = CXmlDocSource::mkXmlDoc(strPath, true, true, xmlErrorMsg);
74         if (doc == NULL) {
75             elementSerializingContext.setError(xmlErrorMsg);
76             return false;
77         }
78 
79         CXmlDocSource docSource(doc, _bValidateSchemasOnStart,
80                                 strPathToXsdFile,
81                                 strIncludedElementType);
82 
83         if (!docSource.isParsable()) {
84 
85             elementSerializingContext.setError("Could not parse document \"" + strPath + "\"");
86 
87             return false;
88         }
89 
90         // Get top level element
91         CXmlElement childElement;
92 
93         docSource.getRootElement(childElement);
94 
95         // Create child element
96         CElement* pChild = elementSerializingContext.getElementLibrary()->createElement(childElement);
97 
98         if (pChild) {
99 
100             // Store created child!
101             getParent()->addChild(pChild);
102         } else {
103 
104             elementSerializingContext.setError("Unable to create XML element " + childElement.getPath());
105 
106             return false;
107         }
108 
109         // Use a doc sink that instantiate the structure from the doc source
110         CXmlMemoryDocSink memorySink(pChild);
111 
112         if (!memorySink.process(docSource, elementSerializingContext)) {
113 
114             return false;
115         }
116     }
117     // Detach from parent
118     getParent()->removeChild(this);
119 
120     // Self destroy
121     delete this;
122 
123     return true;
124 }
125 
126 // Element type
getIncludedElementType() const127 std::string CXmlFileIncluderElement::getIncludedElementType() const
128 {
129     std::string strKind = getKind();
130 
131     std::string::size_type pos = strKind.rfind("Include", std::string::npos);
132 
133     assert(pos != std::string::npos);
134 
135     return strKind.substr(0, pos);
136 }
137