Tizen 2.0 Release
[profile/ivi/libscl-ui.git] / xmlresource / main_entry_parser.cpp
1 /*
2  * Copyright 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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
18 #include "main_entry_parser.h"
19 #include <assert.h>
20 #include "xmlresource.h"
21 #include "xml_parser_utils.h"
22
23 using namespace std;
24 using namespace xmlresource;
25
26 static int get_keyset_id(const xmlNodePtr cur_node);
27 static int get_layout_id(const xmlNodePtr cur_node);
28
29 Main_Entry_Parser* Main_Entry_Parser::m_instance = NULL;
30
31 Main_Entry_Parser::Main_Entry_Parser() {
32 }
33
34 Main_Entry_Parser::~Main_Entry_Parser() {
35 }
36 Main_Entry_Parser* Main_Entry_Parser::get_instance() {
37     if (m_instance == NULL) {
38         m_instance = new Main_Entry_Parser();
39     }
40     return m_instance;
41 }
42 void
43 Main_Entry_Parser::Main_Entry_Parser::init(const char *entry_filepath) {
44     char input_file[_POSIX_PATH_MAX] = {0};
45     XMLResource *xml_resource = XMLResource::get_instance();
46     snprintf(input_file, _POSIX_PATH_MAX, "%s/%s",
47         xml_resource->get_resource_directory(), entry_filepath);
48     parsing_main_entry(input_file);
49 }
50
51 XMLFiles& Main_Entry_Parser::get_xml_files() {
52     return m_xml_files;
53 }
54
55 void Main_Entry_Parser::parsing_main_entry(const char *entry_filepath) {
56     xmlDocPtr doc;
57     xmlNodePtr cur_node;
58
59     doc = xmlReadFile(entry_filepath, NULL, 0);
60     if (doc == NULL) {
61         printf("Could not load file.\n");
62         exit(1);
63     }
64
65     cur_node = xmlDocGetRootElement(doc);
66     if (cur_node == NULL) {
67         printf("empty document.\n");
68         xmlFreeDoc(doc);
69         exit(1);
70     }
71     if (0 != xmlStrcmp(cur_node->name, (const xmlChar*)"main-entry"))
72     {
73        printf("root name %s error!\n", cur_node->name);
74        xmlFreeDoc(doc);
75        exit(1);
76     }
77
78     make_xml_files(cur_node);
79
80     xmlFreeDoc(doc);
81 }
82
83 void
84 Main_Entry_Parser::make_xml_files(const xmlNodePtr p_node) {
85     assert(p_node != NULL);
86     xmlNodePtr node = p_node->xmlChildrenNode;
87     while (node != NULL) {
88         if (0 == xmlStrcmp(node->name, (const xmlChar *)"files")) {
89             parsing_files_node(node);
90         }
91         node = node->next;
92     }
93 }
94
95 void
96 Main_Entry_Parser::parsing_files_node(const xmlNodePtr p_node) {
97     assert(p_node != NULL);
98     xmlNodePtr node = p_node->xmlChildrenNode;
99
100     while (node != NULL) {
101         if (0 == xmlStrcmp(node->name, (const xmlChar *)"inputmode-name-list")) {
102             m_xml_files.inputmode_name_list = (char*)xmlNodeGetContent(node);
103         }
104         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"inputmode-popup-name-list")) {
105             m_xml_files.inputmode_popup_name_list = (char*)xmlNodeGetContent(node);
106         }
107         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"layout-name-list")) {
108             m_xml_files.layout_name_list = (char*)xmlNodeGetContent(node);
109         }
110         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"keyset-name-list")) {
111             m_xml_files.keyset_name_list = (char*)xmlNodeGetContent(node);
112         }
113         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"input-mode-configure")) {
114             m_xml_files.input_mode_configure = (char*)xmlNodeGetContent(node);
115         }
116         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"input-mode-popup-configure")) {
117             m_xml_files.input_mode_popup_configure = (char*)xmlNodeGetContent(node);
118         }
119         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"layout")) {
120             m_xml_files.layout = (char*)xmlNodeGetContent(node);
121         }
122         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"layout-keyset")) {
123             m_xml_files.layout_keyset = (char*)xmlNodeGetContent(node);
124         }
125         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"configure-files")) {
126             parsing_configure_files(node);
127         }
128         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"coordinate-files")) {
129             parsing_coordinate_files(node);
130         }
131         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"property-files")) {
132             parsing_property_files(node);
133         }
134         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"key-label-property")) {
135             m_xml_files.key_label_property = (char*)xmlNodeGetContent(node);
136         }
137         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"modifier-decoration")) {
138             m_xml_files.modifier_decoration = (char*)xmlNodeGetContent(node);
139         }
140         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"default-configure")) {
141             m_xml_files.default_configure = (char*)xmlNodeGetContent(node);
142         }
143         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"autopopup-configure")) {
144             m_xml_files.autopopup_configure = (char*)xmlNodeGetContent(node);
145         }
146         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"magnifier-configure")) {
147             m_xml_files.magnifier_configure = (char*)xmlNodeGetContent(node);
148         }
149         else if (0 == xmlStrcmp(node->name, (const xmlChar *)"nine-patch-file-list")) {
150             m_xml_files.nine_patch_file_list = (char*)xmlNodeGetContent(node);
151         }
152         node = node->next;
153     }
154 }
155
156 void
157 Main_Entry_Parser::parsing_configure_files(const xmlNodePtr p_node) {
158     assert(p_node != NULL);
159     xmlNodePtr node = p_node->xmlChildrenNode;
160
161     while (node != NULL) {
162         if (0 == xmlStrcmp(node->name, (const xmlChar *)"file")) {
163             xmlChar* keyset = xmlGetProp(node, (const xmlChar*)"keyset");
164             assert(keyset != NULL);
165             xmlChar* file_name = xmlNodeGetContent(node);
166             assert(file_name != NULL);
167             m_xml_files.layout_key_configure.insert(Iter_data::value_type((const char*)keyset, (char*)file_name));
168         }
169         node = node->next;
170     }
171 }
172 void
173 Main_Entry_Parser::parsing_property_files(const xmlNodePtr p_node) {
174     assert(p_node != NULL);
175     xmlNodePtr node = p_node->xmlChildrenNode;
176
177     while (node != NULL) {
178         if (0 == xmlStrcmp(node->name, (const xmlChar *)"file")) {
179             xmlChar* keyset = xmlGetProp(node, (const xmlChar*)"keyset");
180             xmlChar* file_name = xmlNodeGetContent(node);
181             assert(keyset != NULL);
182             assert(file_name != NULL);
183
184             m_xml_files.layout_key_property.insert(Iter_data::value_type((const char*)keyset, (char*)file_name));
185         }
186         node = node->next;
187     }
188 }
189
190 void
191 Main_Entry_Parser::parsing_coordinate_files(const xmlNodePtr p_node) {
192     assert(p_node != NULL);
193     xmlNodePtr node = p_node->xmlChildrenNode;
194
195     while (node != NULL) {
196         if (0 == xmlStrcmp(node->name, (const xmlChar *)"file")) {
197             xmlChar* layout = xmlGetProp(node, (const xmlChar*)"layout");
198             xmlChar* file_name = xmlNodeGetContent(node);
199             assert(layout != NULL);
200             assert(file_name != NULL);
201
202             m_xml_files.layout_key_coordinate.insert(Iter_data::value_type((const char*)layout, (char*)file_name));
203         }
204         node = node->next;
205     }
206 }
207
208 void
209 Main_Entry_Parser::get_file_full_path(char* dest_path, const char* file_type) {
210     assert(file_type != NULL);
211     assert(dest_path != NULL);
212
213     Main_Entry_Parser* main_entry_parser = Main_Entry_Parser::get_instance();
214     XMLFiles& xml_files = main_entry_parser->get_xml_files();
215
216     const char* input_file = NULL;
217     if (0 == strcmp(file_type, "input_mode_configure")) {
218         input_file = xml_files.input_mode_configure;
219     }
220     else if (0 == strcmp(file_type, "layout")) {
221         input_file = xml_files.layout;
222     }
223     else if (0 == strcmp(file_type, "label_property")) {
224         input_file = xml_files.key_label_property;
225     }
226     else if (0 == strcmp(file_type, "modifier_decoration")) {
227         input_file = xml_files.modifier_decoration;
228     }
229     else if (0 == strcmp(file_type, "default_configure")) {
230         input_file = xml_files.default_configure;
231     }
232     else if (0 == strcmp(file_type, "autopopup_configure")) {
233         input_file = xml_files.autopopup_configure;
234     }
235     else if (0 == strcmp(file_type, "magnifier_configure")) {
236         input_file = xml_files.magnifier_configure;
237     }
238     else if (0 == strcmp(file_type, "nine_patch_file_list")) {
239         input_file = xml_files.nine_patch_file_list;
240     } else {
241         printf("file_type : %s is not exist.\n", file_type);
242         assert(0);
243     }
244
245     if (input_file && dest_path) {
246         XMLResource *xml_resource = XMLResource::get_instance();
247         snprintf(dest_path, _POSIX_PATH_MAX, "%s/%s",
248             xml_resource->get_resource_directory(), input_file);
249     }
250 }