2 * Copyright (c) 2012 - 2014 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.
19 #include <libxml/parser.h>
21 #include "input_mode_configure_parser.h"
22 #include "xml_parser_utils.h"
23 #include "simple_debug.h"
25 /** Example of input mode XML file :
26 * <?xml version="1.0"?>
28 * <mode name="ENGLISH_QTY">
29 * <keyset>PORTRAIT_QTY_DEFAULT</keyset>
30 * <keyset>LANDSCAPE_QTY_DEFAULT</keyset>
32 * <mode name="PUNCTUATION_POPUP" dim_window="true">
33 * <keyset>PORTRAIT_PUNCTUATION_POPUP</keyset>
34 * <keyset>LANDSCAPE_PUNCTUATION_POPUP</keyset>
39 #define INPUT_MODE_CONFIGURE_TABLE_TAG "input_mode_table"
41 #define INPUT_MODE_CONFIGURE_MODE_TAG "mode"
42 #define INPUT_MODE_CONFIGURE_MODE_NAME_ATTRIBUTE "name"
43 #define INPUT_MODE_CONFIGURE_MODE_DIM_WINDOW_ATTRIBUTE "dim_window"
44 #define INPUT_MODE_CONFIGURE_MODE_VIRTUAL_WINDOW_ATTRIBUTE "virtual_window"
46 #define INPUT_MODE_CONFIGURE_LAYOUT_TAG "layouts"
47 #define INPUT_MODE_CONFIGURE_LAYOUT_PORTRAIT_TAG "portrait"
48 #define INPUT_MODE_CONFIGURE_LAYOUT_LANDSCAPE_TAG "landscape"
50 class InputModeConfigureParserImpl {
52 InputModeConfigureParserImpl() {
54 memset(m_input_mode_configure_table, 0x00, sizeof(SclInputModeConfigure) * MAX_SCL_INPUT_MODE);
57 ~InputModeConfigureParserImpl() {
62 for (int input_mode = 0; input_mode < MAX_SCL_INPUT_MODE; ++input_mode) {
63 SclInputModeConfigure& cur_rec = m_input_mode_configure_table[input_mode];
65 xmlFree(cur_rec.name);
68 if (cur_rec.layouts[DISPLAYMODE_PORTRAIT]) {
69 xmlFree(cur_rec.layouts[DISPLAYMODE_PORTRAIT]);
70 cur_rec.layouts[DISPLAYMODE_PORTRAIT] = NULL;
72 if (cur_rec.layouts[DISPLAYMODE_LANDSCAPE]) {
73 xmlFree(cur_rec.layouts[DISPLAYMODE_LANDSCAPE]);
74 cur_rec.layouts[DISPLAYMODE_LANDSCAPE] = NULL;
78 memset(m_input_mode_configure_table, 0x00, sizeof(SclInputModeConfigure) * MAX_SCL_INPUT_MODE);
81 int reload_input_mode_configure_table(const char* input_file) {
83 return parsing_input_mode_configure_table(input_file);
85 int parsing_input_mode_configure_table(const char* input_file) {
90 doc = xmlReadFile(input_file, NULL, 0);
92 SCLLOG(SclLog::DEBUG, "Could not load file: %s.", input_file);
96 cur_node = xmlDocGetRootElement(doc);
97 if (cur_node == NULL) {
98 SCLLOG(SclLog::DEBUG, "InputModeConfigParser: empty document.\n");
102 if (0 != xmlStrcmp(cur_node->name, (const xmlChar*)INPUT_MODE_CONFIGURE_TABLE_TAG))
104 SCLLOG(SclLog::DEBUG, "InputModeConfigParser: root name error: %s\n!", (char *)cur_node->name);
109 m_inputmode_size = 0;
110 cur_node = cur_node->xmlChildrenNode;
112 SclInputModeConfigure* cur_rec = m_input_mode_configure_table;
113 while (cur_node != NULL) {
114 if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_MODE_TAG)) {
115 parsing_mode_node(cur_node, cur_rec);
118 if (m_inputmode_size >= MAX_SCL_INPUT_MODE) {
119 SCLLOG(SclLog::ERROR, "No Space for input mode record.");
123 cur_node = cur_node->next;
130 void set_input_mode_configure_default_record(const PSclInputModeConfigure cur_rec) {
131 cur_rec->name = NULL;
132 cur_rec->layouts[DISPLAYMODE_PORTRAIT] = NULL;
133 cur_rec->layouts[DISPLAYMODE_LANDSCAPE] = NULL;
134 cur_rec->use_virtual_window = FALSE;
135 cur_rec->use_dim_window = FALSE;
138 void parsing_mode_node(const xmlNodePtr cur_node, const PSclInputModeConfigure cur_rec) {
139 assert(cur_node != NULL);
140 assert(cur_rec != NULL);
141 set_input_mode_configure_default_record(cur_rec);
143 get_prop_bool(cur_node, INPUT_MODE_CONFIGURE_MODE_VIRTUAL_WINDOW_ATTRIBUTE, &(cur_rec->use_virtual_window));
144 get_prop_bool(cur_node, INPUT_MODE_CONFIGURE_MODE_DIM_WINDOW_ATTRIBUTE, &(cur_rec->use_dim_window));
146 xmlChar* temp = xmlGetProp(cur_node, (const xmlChar*)INPUT_MODE_CONFIGURE_MODE_NAME_ATTRIBUTE);
147 cur_rec->name = (sclchar *)temp;
149 xmlNodePtr child_node = cur_node->xmlChildrenNode;
150 while (child_node != NULL) {
151 if (0 == xmlStrcmp(child_node->name, (const xmlChar *)"text")) {
152 child_node = child_node->next;
155 if (0 == xmlStrcmp(child_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_LAYOUT_TAG)) {
156 parsing_layouts(child_node, cur_rec);
158 SCLLOG(SclLog::WARNING, "input_mode_configure has no such node name: %s\n", (char *)child_node->name);
161 child_node = child_node->next;
165 void parsing_layouts(const xmlNodePtr cur_node, const PSclInputModeConfigure cur_rec) {
166 assert(cur_node != NULL);
167 assert(cur_rec != NULL);
168 assert(0 == xmlStrcmp(cur_node->name, (const xmlChar*)INPUT_MODE_CONFIGURE_LAYOUT_TAG) );
170 xmlNodePtr child_node = cur_node->xmlChildrenNode;
171 while (child_node != NULL) {
172 if ( 0 == xmlStrcmp(child_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_LAYOUT_PORTRAIT_TAG) ) {
173 xmlChar* temp = xmlNodeGetContent(child_node);
174 cur_rec->layouts[DISPLAYMODE_PORTRAIT] = (sclchar*)temp;
175 } else if ( 0 == xmlStrcmp(child_node->name, (const xmlChar *)INPUT_MODE_CONFIGURE_LAYOUT_LANDSCAPE_TAG) ) {
176 xmlChar* temp = xmlNodeGetContent(child_node);
177 cur_rec->layouts[DISPLAYMODE_LANDSCAPE] = (sclchar*)temp;
179 child_node = child_node->next;
183 int m_inputmode_size;
184 SclInputModeConfigure m_input_mode_configure_table[MAX_SCL_INPUT_MODE];
187 InputModeConfigParser::InputModeConfigParser() {
188 m_impl = new InputModeConfigureParserImpl;
191 InputModeConfigParser::~InputModeConfigParser() {
193 SCLLOG(SclLog::MESSAGE, "~InputModeConfigParser() has called");
199 InputModeConfigParser*
200 InputModeConfigParser::get_instance() {
201 static InputModeConfigParser instance;
206 InputModeConfigParser::init(const char* file) {
207 return m_impl->parsing_input_mode_configure_table(file);
211 InputModeConfigParser::reload(const char* file) {
212 return m_impl->reload_input_mode_configure_table(file);
216 InputModeConfigParser::get_inputmode_id(const char *name) {
218 SCLLOG(SclLog::DEBUG, "get_inputmode_id failed");
222 PSclInputModeConfigure config_table = get_input_mode_configure_table();
224 if (config_table == NULL) {
225 SCLLOG(SclLog::DEBUG, "get_inputmode_id failed");
229 for (int i = 0; i < get_inputmode_size(); ++i) {
230 if (config_table[i].name) {
231 if ( 0 == strcmp(config_table[i].name, name) ) {
237 SCLLOG(SclLog::DEBUG, "get_inputmode_id failed");
242 InputModeConfigParser::get_inputmode_name(int id) {
243 if (id >= 0 && id < MAX_SCL_INPUT_MODE) {
244 PSclInputModeConfigure config_table = get_input_mode_configure_table();
246 return config_table[id].name;
254 InputModeConfigParser::get_inputmode_size() {
255 return m_impl->m_inputmode_size;
258 PSclInputModeConfigure
259 InputModeConfigParser::get_input_mode_configure_table() {
260 return m_impl->m_input_mode_configure_table;