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>
27 #include <dpl/log/log.h>
29 //TODO: This file reads manifest file. This functionality is familiar with writing
30 // in wrt-installer but reading ws not necessary there.
31 // Maybe it should be changed in some way.
33 ManifestFile::ManifestFile(const std::string & file) : filename(file)
41 ManifestFile::~ManifestFile()
43 xmlXPathFreeContext(xpathCtx);
48 void ManifestFile::parse()
50 doc = xmlReadFile(filename.c_str(), NULL, 0);
53 ThrowMsg(ManifestParseError,"File Problem");
58 xpathCtx = xmlXPathNewContext(doc);
61 ThrowMsg(ManifestParseError,"Error: unable to create new XPath context\n");
63 xpathCtx->node = xmlDocGetRootElement(doc);
65 if(xmlXPathRegisterNs(xpathCtx, BAD_CAST "p", BAD_CAST "http://tizen.org/ns/packages") != 0)
67 ThrowMsg(ManifestParseError,"Error: unable to register namespace\n");
72 std::string ManifestFile::getValueByXpath(const std::string & path) const
75 xmlXPathObjectPtr xpathObject;
76 //get requested node's values
77 xpathObject = xmlXPathEvalExpression(BAD_CAST path.c_str(), xpathCtx);
78 if(xpathObject == NULL)
80 ThrowMsg(ManifestParseError,"XPath evaluation failure: " << path);
82 xmlNodeSetPtr nodes = xpathObject->nodesetval;
83 int size = (nodes) ? nodes->nodeNr : 0;
86 ThrowMsg(ManifestParseError,"Xpath does not point 1 element but " << size
87 << " for xpath: " << path);
91 if(nodes->nodeTab[0]->type == XML_ELEMENT_NODE)
93 xmlNodePtr cur = nodes->nodeTab[0];
94 xmlChar * value = xmlNodeGetContent(cur);
95 result = std::string(reinterpret_cast<char*>(value)); //this cast should be safe...
98 else if(nodes->nodeTab[0]->type == XML_ATTRIBUTE_NODE)
100 xmlNodePtr cur = nodes->nodeTab[0];
101 xmlChar * value = xmlNodeGetContent(cur);
102 result = std::string(reinterpret_cast<char*>(value));
106 //Cleanup of XPath data
107 xmlXPathFreeObject(xpathObject);