e88a507c4c35e8210b927476fff9d639ee59b515
[framework/web/wrt-installer.git] / tests / general / common / src / ManifestFile.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        ManifestFile.cpp
18  * @author      Tomasz Iwanek (t.iwanek@samsung.com)
19  * @brief       Manifest file reading
20  */
21
22 #include <ManifestFile.h>
23
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26
27 #include <dpl/log/log.h>
28
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.
32
33 ManifestFile::ManifestFile(const std::string & file) : filename(file)
34 {
35     xmlInitParser();
36     LIBXML_TEST_VERSION
37     xmlXPathInit();
38     parse();
39 }
40
41 ManifestFile::~ManifestFile()
42 {
43     xmlXPathFreeContext(xpathCtx);
44     xmlFreeDoc(doc);
45     xmlCleanupParser();
46 }
47
48 void ManifestFile::parse()
49 {
50     doc = xmlReadFile(filename.c_str(), NULL, 0);
51     if (doc == NULL)
52     {
53         ThrowMsg(ManifestParseError,"File Problem");
54     }
55     else
56     {
57         //context
58         xpathCtx = xmlXPathNewContext(doc);
59         if(xpathCtx == NULL)
60         {
61             ThrowMsg(ManifestParseError,"Error: unable to create new XPath context\n");
62         }
63         xpathCtx->node = xmlDocGetRootElement(doc);
64
65         if(xmlXPathRegisterNs(xpathCtx, BAD_CAST "p", BAD_CAST "http://tizen.org/ns/packages") != 0)
66         {
67             ThrowMsg(ManifestParseError,"Error: unable to register namespace\n");
68         }
69     }
70 }
71
72 std::string ManifestFile::getValueByXpath(const std::string & path) const
73 {
74     std::string result;
75     xmlXPathObjectPtr xpathObject;
76     //get requested node's values
77     xpathObject = xmlXPathEvalExpression(BAD_CAST path.c_str(), xpathCtx);
78     if(xpathObject == NULL)
79     {
80         ThrowMsg(ManifestParseError,"XPath evaluation failure: " << path);
81     }
82     xmlNodeSetPtr nodes = xpathObject->nodesetval;
83     int size = (nodes) ? nodes->nodeNr : 0;
84     if(size != 1)
85     {
86         ThrowMsg(ManifestParseError,"Xpath does not point 1 element but " << size
87                  << " for xpath: " << path);
88     }
89     else
90     {
91         if(nodes->nodeTab[0]->type == XML_ELEMENT_NODE)
92         {
93             xmlNodePtr cur = nodes->nodeTab[0];
94             xmlChar * value = xmlNodeGetContent(cur);
95             result = std::string(reinterpret_cast<char*>(value)); //this cast should be safe...
96             xmlFree(value);
97         }
98         else if(nodes->nodeTab[0]->type == XML_ATTRIBUTE_NODE)
99         {
100             xmlNodePtr cur = nodes->nodeTab[0];
101             xmlChar * value = xmlNodeGetContent(cur);
102             result = std::string(reinterpret_cast<char*>(value));
103             xmlFree(value);
104         }
105     }
106     //Cleanup of XPath data
107     xmlXPathFreeObject(xpathObject);
108     return result;
109 }