xref: /aosp_15_r20/external/parameter-framework/upstream/parameter/XmlFileIncluderElement.cpp (revision c33452fb792a5495ec310a9626f2638b053af5dd)
1*c33452fbSAndroid Build Coastguard Worker /*
2*c33452fbSAndroid Build Coastguard Worker  * Copyright (c) 2011-2015, Intel Corporation
3*c33452fbSAndroid Build Coastguard Worker  * All rights reserved.
4*c33452fbSAndroid Build Coastguard Worker  *
5*c33452fbSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without modification,
6*c33452fbSAndroid Build Coastguard Worker  * are permitted provided that the following conditions are met:
7*c33452fbSAndroid Build Coastguard Worker  *
8*c33452fbSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright notice, this
9*c33452fbSAndroid Build Coastguard Worker  * list of conditions and the following disclaimer.
10*c33452fbSAndroid Build Coastguard Worker  *
11*c33452fbSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c33452fbSAndroid Build Coastguard Worker  * this list of conditions and the following disclaimer in the documentation and/or
13*c33452fbSAndroid Build Coastguard Worker  * other materials provided with the distribution.
14*c33452fbSAndroid Build Coastguard Worker  *
15*c33452fbSAndroid Build Coastguard Worker  * 3. Neither the name of the copyright holder nor the names of its contributors
16*c33452fbSAndroid Build Coastguard Worker  * may be used to endorse or promote products derived from this software without
17*c33452fbSAndroid Build Coastguard Worker  * specific prior written permission.
18*c33452fbSAndroid Build Coastguard Worker  *
19*c33452fbSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20*c33452fbSAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*c33452fbSAndroid Build Coastguard Worker  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*c33452fbSAndroid Build Coastguard Worker  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23*c33452fbSAndroid Build Coastguard Worker  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c33452fbSAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26*c33452fbSAndroid Build Coastguard Worker  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*c33452fbSAndroid Build Coastguard Worker  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c33452fbSAndroid Build Coastguard Worker  */
30*c33452fbSAndroid Build Coastguard Worker #include "XmlFileIncluderElement.h"
31*c33452fbSAndroid Build Coastguard Worker #include "XmlDocSource.h"
32*c33452fbSAndroid Build Coastguard Worker #include "XmlMemoryDocSink.h"
33*c33452fbSAndroid Build Coastguard Worker #include "XmlElementSerializingContext.h"
34*c33452fbSAndroid Build Coastguard Worker #include "ElementLibrary.h"
35*c33452fbSAndroid Build Coastguard Worker #include <assert.h>
36*c33452fbSAndroid Build Coastguard Worker #include <fstream>
37*c33452fbSAndroid Build Coastguard Worker 
38*c33452fbSAndroid Build Coastguard Worker #define base CKindElement
CXmlFileIncluderElement(const std::string & strName,const std::string & strKind,bool bValidateWithSchemas,const std::string & schemaBaseUri)39*c33452fbSAndroid Build Coastguard Worker CXmlFileIncluderElement::CXmlFileIncluderElement(const std::string &strName,
40*c33452fbSAndroid Build Coastguard Worker                                                  const std::string &strKind,
41*c33452fbSAndroid Build Coastguard Worker                                                  bool bValidateWithSchemas,
42*c33452fbSAndroid Build Coastguard Worker                                                  const std::string &schemaBaseUri)
43*c33452fbSAndroid Build Coastguard Worker     : base(strName, strKind), _bValidateSchemasOnStart(bValidateWithSchemas),
44*c33452fbSAndroid Build Coastguard Worker       _schemaBaseUri(schemaBaseUri)
45*c33452fbSAndroid Build Coastguard Worker {
46*c33452fbSAndroid Build Coastguard Worker }
47*c33452fbSAndroid Build Coastguard Worker 
48*c33452fbSAndroid Build Coastguard Worker // From IXmlSink
fromXml(const CXmlElement & xmlElement,CXmlSerializingContext & serializingContext)49*c33452fbSAndroid Build Coastguard Worker bool CXmlFileIncluderElement::fromXml(const CXmlElement &xmlElement,
50*c33452fbSAndroid Build Coastguard Worker                                       CXmlSerializingContext &serializingContext)
51*c33452fbSAndroid Build Coastguard Worker {
52*c33452fbSAndroid Build Coastguard Worker     // Context
53*c33452fbSAndroid Build Coastguard Worker     CXmlElementSerializingContext &elementSerializingContext =
54*c33452fbSAndroid Build Coastguard Worker         static_cast<CXmlElementSerializingContext &>(serializingContext);
55*c33452fbSAndroid Build Coastguard Worker 
56*c33452fbSAndroid Build Coastguard Worker     // Parse included document
57*c33452fbSAndroid Build Coastguard Worker     std::string strPath;
58*c33452fbSAndroid Build Coastguard Worker     xmlElement.getAttribute("Path", strPath);
59*c33452fbSAndroid Build Coastguard Worker     strPath = CXmlDocSource::mkUri(elementSerializingContext.getXmlUri(), strPath);
60*c33452fbSAndroid Build Coastguard Worker 
61*c33452fbSAndroid Build Coastguard Worker     // Instantiate parser
62*c33452fbSAndroid Build Coastguard Worker     std::string strIncludedElementType = getIncludedElementType();
63*c33452fbSAndroid Build Coastguard Worker     {
64*c33452fbSAndroid Build Coastguard Worker         _xmlDoc *doc = CXmlDocSource::mkXmlDoc(strPath, true, true, elementSerializingContext);
65*c33452fbSAndroid Build Coastguard Worker 
66*c33452fbSAndroid Build Coastguard Worker         CXmlDocSource docSource(doc, _bValidateSchemasOnStart, strIncludedElementType);
67*c33452fbSAndroid Build Coastguard Worker 
68*c33452fbSAndroid Build Coastguard Worker         if (not _schemaBaseUri.empty()) {
69*c33452fbSAndroid Build Coastguard Worker             docSource.setSchemaBaseUri(_schemaBaseUri);
70*c33452fbSAndroid Build Coastguard Worker         }
71*c33452fbSAndroid Build Coastguard Worker 
72*c33452fbSAndroid Build Coastguard Worker         if (!docSource.isParsable()) {
73*c33452fbSAndroid Build Coastguard Worker 
74*c33452fbSAndroid Build Coastguard Worker             elementSerializingContext.setError("Could not parse document \"" + strPath + "\"");
75*c33452fbSAndroid Build Coastguard Worker 
76*c33452fbSAndroid Build Coastguard Worker             return false;
77*c33452fbSAndroid Build Coastguard Worker         }
78*c33452fbSAndroid Build Coastguard Worker 
79*c33452fbSAndroid Build Coastguard Worker         // Get top level element
80*c33452fbSAndroid Build Coastguard Worker         CXmlElement childElement;
81*c33452fbSAndroid Build Coastguard Worker 
82*c33452fbSAndroid Build Coastguard Worker         docSource.getRootElement(childElement);
83*c33452fbSAndroid Build Coastguard Worker 
84*c33452fbSAndroid Build Coastguard Worker         // Create child element
85*c33452fbSAndroid Build Coastguard Worker         CElement *pChild =
86*c33452fbSAndroid Build Coastguard Worker             elementSerializingContext.getElementLibrary()->createElement(childElement);
87*c33452fbSAndroid Build Coastguard Worker 
88*c33452fbSAndroid Build Coastguard Worker         if (pChild) {
89*c33452fbSAndroid Build Coastguard Worker 
90*c33452fbSAndroid Build Coastguard Worker             // Store created child!
91*c33452fbSAndroid Build Coastguard Worker             getParent()->addChild(pChild);
92*c33452fbSAndroid Build Coastguard Worker         } else {
93*c33452fbSAndroid Build Coastguard Worker 
94*c33452fbSAndroid Build Coastguard Worker             elementSerializingContext.setError("Unable to create XML element " +
95*c33452fbSAndroid Build Coastguard Worker                                                childElement.getPath());
96*c33452fbSAndroid Build Coastguard Worker 
97*c33452fbSAndroid Build Coastguard Worker             return false;
98*c33452fbSAndroid Build Coastguard Worker         }
99*c33452fbSAndroid Build Coastguard Worker 
100*c33452fbSAndroid Build Coastguard Worker         // Use a doc sink that instantiate the structure from the doc source
101*c33452fbSAndroid Build Coastguard Worker         CXmlMemoryDocSink memorySink(pChild);
102*c33452fbSAndroid Build Coastguard Worker 
103*c33452fbSAndroid Build Coastguard Worker         if (!memorySink.process(docSource, elementSerializingContext)) {
104*c33452fbSAndroid Build Coastguard Worker 
105*c33452fbSAndroid Build Coastguard Worker             return false;
106*c33452fbSAndroid Build Coastguard Worker         }
107*c33452fbSAndroid Build Coastguard Worker     }
108*c33452fbSAndroid Build Coastguard Worker     // Detach from parent
109*c33452fbSAndroid Build Coastguard Worker     getParent()->removeChild(this);
110*c33452fbSAndroid Build Coastguard Worker 
111*c33452fbSAndroid Build Coastguard Worker     // Self destroy
112*c33452fbSAndroid Build Coastguard Worker     delete this;
113*c33452fbSAndroid Build Coastguard Worker 
114*c33452fbSAndroid Build Coastguard Worker     return true;
115*c33452fbSAndroid Build Coastguard Worker }
116*c33452fbSAndroid Build Coastguard Worker 
117*c33452fbSAndroid Build Coastguard Worker // Element type
getIncludedElementType() const118*c33452fbSAndroid Build Coastguard Worker std::string CXmlFileIncluderElement::getIncludedElementType() const
119*c33452fbSAndroid Build Coastguard Worker {
120*c33452fbSAndroid Build Coastguard Worker     std::string strKind = getKind();
121*c33452fbSAndroid Build Coastguard Worker 
122*c33452fbSAndroid Build Coastguard Worker     std::string::size_type pos = strKind.rfind("Include", std::string::npos);
123*c33452fbSAndroid Build Coastguard Worker 
124*c33452fbSAndroid Build Coastguard Worker     assert(pos != std::string::npos);
125*c33452fbSAndroid Build Coastguard Worker 
126*c33452fbSAndroid Build Coastguard Worker     return strKind.substr(0, pos);
127*c33452fbSAndroid Build Coastguard Worker }
128