2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file ManifestFile.cpp
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
19 * @brief Manifest file reading
22 #include <ManifestFile.h>
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <dpl/log/secure_log.h>
28 //TODO: This file reads manifest file. This functionality is familiar with writing
29 // in wrt-installer but reading ws not necessary there.
30 // Maybe it should be changed in some way.
32 ManifestFile::ManifestFile(const std::string & file) : filename(file)
38 ManifestFile::~ManifestFile()
40 xmlXPathFreeContext(xpathCtx);
44 void ManifestFile::parse()
46 doc = xmlReadFile(filename.c_str(), NULL, 0);
49 ThrowMsg(ManifestParseError,"File Problem");
54 xpathCtx = xmlXPathNewContext(doc);
57 ThrowMsg(ManifestParseError,"Error: unable to create new XPath context\n");
59 xpathCtx->node = xmlDocGetRootElement(doc);
61 if(xmlXPathRegisterNs(xpathCtx, BAD_CAST "p", BAD_CAST "http://tizen.org/ns/packages") != 0)
63 ThrowMsg(ManifestParseError,"Error: unable to register namespace\n");
68 std::string ManifestFile::getValueByXpath(const std::string & path) const
71 xmlXPathObjectPtr xpathObject;
72 //get requested node's values
73 xpathObject = xmlXPathEvalExpression(BAD_CAST path.c_str(), xpathCtx);
74 if(xpathObject == NULL)
76 ThrowMsg(ManifestParseError,"XPath evaluation failure: " << path);
78 xmlNodeSetPtr nodes = xpathObject->nodesetval;
79 int size = (nodes) ? nodes->nodeNr : 0;
82 ThrowMsg(ManifestParseError,"Xpath does not point 1 element but " << size
83 << " for xpath: " << path);
87 if(nodes->nodeTab[0]->type == XML_ELEMENT_NODE)
89 xmlNodePtr cur = nodes->nodeTab[0];
90 xmlChar * value = xmlNodeGetContent(cur);
91 result = std::string(reinterpret_cast<char*>(value)); //this cast should be safe...
94 else if(nodes->nodeTab[0]->type == XML_ATTRIBUTE_NODE)
96 xmlNodePtr cur = nodes->nodeTab[0];
97 xmlChar * value = xmlNodeGetContent(cur);
98 result = std::string(reinterpret_cast<char*>(value));
102 //Cleanup of XPath data
103 xmlXPathFreeObject(xpathObject);