1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "wgt/extension_config_parser.h"
6 #include <manifest_parser/utils/string_util.h>
7 #include <manifest_parser/manifest_handler.h>
15 const xmlChar kExtensionNodeKey[] = "extension";
16 const xmlChar kNamePrivilegeKey[] = "privilege";
17 const xmlChar kPrivigeNameAttributeKey[] = "name";
18 const char kAttributePrefix[] = "@";
19 const xmlChar kDirAttributeKey[] = "dir";
20 const char kXmlTextKey[] = "#text";
21 const char kNamespaceKey[] = "@namespace";
22 const char kExtensionPath[] = "extension.privilege";
23 const char kExtensionNameKey[] = "@name";
26 ExtensionConfigParser::ExtensionConfigParser(std::string config_xml) {
27 config_xml_ = config_xml;
30 std::unique_ptr<parser::DictionaryValue>
31 ExtensionConfigParser::LoadExtensionConfig(const std::string& config_xml) {
32 xmlDoc *doc = nullptr;
33 xmlNode* root_node = nullptr;
34 doc = xmlReadFile(config_xml.c_str(), nullptr, XML_PARSE_NOENT);
36 LOG(ERROR) << "Failed to read xml document model from" << config_xml;
39 root_node = xmlDocGetRootElement(doc);
40 std::unique_ptr<parser::DictionaryValue> dv = LoadXMLNode(root_node);
41 std::unique_ptr<parser::DictionaryValue> result(new parser::DictionaryValue);
43 result->Set(reinterpret_cast<const char*>(root_node->name), dv.release());
47 std::string ExtensionConfigParser::GetNodeDir(
48 xmlNode* node, const std::string& inherit_dir) {
49 std::string dir(inherit_dir);
50 for (xmlAttr* prop = node->properties; prop; prop = prop->next) {
51 if (xmlStrEqual(prop->name, kDirAttributeKey)) {
52 char* prop_value = reinterpret_cast<char*>(xmlNodeListGetString(
53 node->doc, prop->children, 1));
61 std::string ExtensionConfigParser::GetNodeText(
62 xmlNode* root, const std::string& inherit_dir) {
63 if (root->type != XML_ELEMENT_NODE)
65 std::string current_dir(GetNodeDir(root, inherit_dir));
67 if (!current_dir.empty())
68 text += parser::utils::GetDirUTF8Start(current_dir);
69 for (xmlNode* node = root->children; node; node = node->next) {
70 if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)
71 text = text + std::string(reinterpret_cast<char*>(node->content));
73 text += GetNodeText(node, current_dir);
75 if (!current_dir.empty())
76 text += parser::utils::GetDirUTF8End();
79 bool ExtensionConfigParser::IsPropSupportDir(xmlNode* root, xmlAttr* prop) {
80 if (xmlStrEqual(root->name, kNamePrivilegeKey)
81 && xmlStrEqual(prop->name, kPrivigeNameAttributeKey)) {
86 bool ExtensionConfigParser::IsTrimRequiredForElement(xmlNode* root) {
87 if (xmlStrEqual(root->name, kNamePrivilegeKey)) {
92 bool ExtensionConfigParser::IsTrimRequiredForProp(
93 xmlNode* root, xmlAttr* prop) {
94 if (xmlStrEqual(root->name, kNamePrivilegeKey) &&
95 xmlStrEqual(prop->name, kPrivigeNameAttributeKey)) {
100 std::unique_ptr<parser::DictionaryValue>
101 ExtensionConfigParser::LoadXMLNode(
102 xmlNode* root, const std::string& inherit_dir) {
103 std::unique_ptr<parser::DictionaryValue> value(new parser::DictionaryValue());
104 if (root->type != XML_ELEMENT_NODE)
107 std::string current_dir(GetNodeDir(root, inherit_dir));
109 xmlAttr* prop = nullptr;
110 for (prop = root->properties; prop; prop = prop->next) {
111 xmlChar* value_ptr = xmlNodeListGetString(root->doc, prop->children, 1);
112 std::string prop_value(reinterpret_cast<char*>(value_ptr));
114 if (IsPropSupportDir(root, prop))
115 prop_value = parser::utils::GetDirTextUTF8(prop_value, current_dir);
117 if (IsTrimRequiredForProp(root, prop))
118 prop_value = parser::utils::CollapseWhitespaceUTF8(prop_value);
121 std::string(kAttributePrefix)
122 + reinterpret_cast<const char*>(prop->name),
127 value->SetString(kNamespaceKey,
128 reinterpret_cast<const char*>(root->ns->href));
130 for (xmlNode* node = root->children; node; node = node->next) {
131 std::string sub_node_name(reinterpret_cast<const char*>(node->name));
132 std::unique_ptr<parser::DictionaryValue> sub_value =
133 LoadXMLNode(node, current_dir);
138 if (!value->HasKey(sub_node_name)) {
139 value->Set(sub_node_name, sub_value.release());
144 value->Get(sub_node_name, &temp);
146 if (temp->IsType(parser::Value::TYPE_LIST)) {
147 parser::ListValue* list;
148 temp->GetAsList(&list);
149 list->Append(sub_value.release());
151 assert(temp->IsType(parser::Value::TYPE_DICTIONARY));
152 parser::DictionaryValue* dict;
153 temp->GetAsDictionary(&dict);
154 parser::DictionaryValue* prev_value = dict->DeepCopy();
156 parser::ListValue* list = new parser::ListValue();
157 list->Append(prev_value);
158 list->Append(sub_value.release());
159 value->Set(sub_node_name, list);
164 xmlChar* text_ptr = xmlNodeListGetString(root->doc, root->children, 1);
166 text = reinterpret_cast<char*>(text_ptr);
169 if (IsTrimRequiredForElement(root))
170 text = parser::utils::CollapseWhitespaceUTF8(text);
172 value->SetString(kXmlTextKey, text);
176 std::vector<std::string> ExtensionConfigParser::GetExtensionPrivilegeList() {
177 std::unique_ptr<parser::DictionaryValue> dic
178 = LoadExtensionConfig(config_xml_);
179 std::vector<std::string> privilege_list;
181 for (auto& item : parser::GetOneOrMany(dic.get(), kExtensionPath, "")) {
182 std::string privilege;
183 if (item->GetString(kExtensionNameKey, &privilege)) {
184 LOG(DEBUG) << "User defined extra privilege: " << privilege;
185 privilege_list.push_back(privilege);
188 return privilege_list;