tizen 2.4 release
[framework/web/wrt-installer.git] / tests / general / 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 #include <dpl/log/secure_log.h>
27
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.
31
32 ManifestFile::ManifestFile(const std::string & file) : filename(file)
33 {
34     xmlXPathInit();
35     parse();
36 }
37
38 ManifestFile::~ManifestFile()
39 {
40     xmlXPathFreeContext(xpathCtx);
41     xmlFreeDoc(doc);
42 }
43
44 void ManifestFile::parse()
45 {
46     doc = xmlReadFile(filename.c_str(), NULL, 0);
47     if (doc == NULL)
48     {
49         ThrowMsg(ManifestParseError,"File Problem");
50     }
51     else
52     {
53         //context
54         xpathCtx = xmlXPathNewContext(doc);
55         if(xpathCtx == NULL)
56         {
57             ThrowMsg(ManifestParseError,"Error: unable to create new XPath context\n");
58         }
59         xpathCtx->node = xmlDocGetRootElement(doc);
60
61         if(xmlXPathRegisterNs(xpathCtx, BAD_CAST "p", BAD_CAST "http://tizen.org/ns/packages") != 0)
62         {
63             ThrowMsg(ManifestParseError,"Error: unable to register namespace\n");
64         }
65     }
66 }
67
68 std::string ManifestFile::getValueByXpath(const std::string & path) const
69 {
70     std::string result;
71     xmlXPathObjectPtr xpathObject;
72     //get requested node's values
73     xpathObject = xmlXPathEvalExpression(BAD_CAST path.c_str(), xpathCtx);
74     if(xpathObject == NULL)
75     {
76         ThrowMsg(ManifestParseError,"XPath evaluation failure: " << path);
77     }
78     xmlNodeSetPtr nodes = xpathObject->nodesetval;
79     int size = (nodes) ? nodes->nodeNr : 0;
80     if(size != 1)
81     {
82         ThrowMsg(ManifestParseError,"Xpath does not point 1 element but " << size
83                  << " for xpath: " << path);
84     }
85     else
86     {
87         if(nodes->nodeTab[0]->type == XML_ELEMENT_NODE)
88         {
89             xmlNodePtr cur = nodes->nodeTab[0];
90             xmlChar * value = xmlNodeGetContent(cur);
91             result = std::string(reinterpret_cast<char*>(value)); //this cast should be safe...
92             xmlFree(value);
93         }
94         else if(nodes->nodeTab[0]->type == XML_ATTRIBUTE_NODE)
95         {
96             xmlNodePtr cur = nodes->nodeTab[0];
97             xmlChar * value = xmlNodeGetContent(cur);
98             result = std::string(reinterpret_cast<char*>(value));
99             xmlFree(value);
100         }
101     }
102     //Cleanup of XPath data
103     xmlXPathFreeObject(xpathObject);
104     return result;
105 }