Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / scl / nine_patch_file_list_parser.cpp
1 /*
2  * Copyright (c) 2012 - 2014 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
18 #include <memory.h>
19 #include <assert.h>
20 #include <libxml/parser.h>
21
22 #include "nine_patch_file_list_parser.h"
23 #include "xml_parser_utils.h"
24 #include "simple_debug.h"
25
26 class NinePatchFileListParserImpl {
27     public:
28         NinePatchFileListParserImpl() {
29             m_size = 0;
30             memset(m_nine_patch_file_list, 0x00, sizeof(SclNinePatchInfo) * MAX_NINE_PATCH_FILE_LIST);
31         }
32         ~NinePatchFileListParserImpl() {
33             reset();
34         }
35
36         void reset() {
37             for (int i = 0; i < m_size; ++i) {
38                 if (m_nine_patch_file_list[i].image_path)
39                     xmlFree(m_nine_patch_file_list[i].image_path);
40                 m_nine_patch_file_list[i].image_path = NULL;
41             }
42             m_size = 0;
43             memset(m_nine_patch_file_list, 0x00, sizeof(SclNinePatchInfo) * MAX_NINE_PATCH_FILE_LIST);
44         }
45
46         int reload_nine_patch_file_list(const char* input_file) {
47             reset();
48             return parsing_nine_patch_file_list(input_file);
49         }
50
51         int parsing_nine_patch_file_list(const char* input_file) {
52             xmlDocPtr doc;
53             xmlNodePtr cur_node;
54
55             doc = xmlReadFile(input_file, NULL, 0);
56             if (doc == NULL) {
57                 SCLLOG(SclLog::DEBUG, "Could not load file: %s.", input_file);
58                 return -1;
59             }
60
61             cur_node = xmlDocGetRootElement(doc);
62             if (cur_node == NULL) {
63                 SCLLOG(SclLog::DEBUG, "Nine_Patch_File_Parser: empty document.\n");
64                 xmlFreeDoc(doc);
65                 return -1;
66             }
67             if (0 != xmlStrcmp(cur_node->name, (const xmlChar*)"nine_patch_file_list"))
68             {
69                 SCLLOG(SclLog::DEBUG, "Nine_Patch_File_Parser: root name error: %s\n!", (char *)cur_node->name);
70                 xmlFreeDoc(doc);
71                 return -1;
72             }
73
74             cur_node = cur_node->xmlChildrenNode;
75
76             assert(m_size == 0);
77             while (cur_node != NULL) {
78                 if (0 == xmlStrcmp(cur_node->name, (const xmlChar *)"file")) {
79                     assert(m_size >= 0 && m_size < MAX_NINE_PATCH_FILE_LIST);
80                     if (m_size >= 0 && m_size < MAX_NINE_PATCH_FILE_LIST) {
81                         m_nine_patch_file_list[m_size].image_path = (char*)xmlNodeGetContent(cur_node);
82                         get_prop_number(cur_node, "left", &(m_nine_patch_file_list[m_size].left));
83                         get_prop_number(cur_node, "right", &(m_nine_patch_file_list[m_size].right));
84                         get_prop_number(cur_node, "top", &(m_nine_patch_file_list[m_size].top));
85                         get_prop_number(cur_node, "bottom", &(m_nine_patch_file_list[m_size].bottom));
86                         if (m_nine_patch_file_list[m_size].image_path == NULL) {
87                             SCLLOG(SclLog::ERROR, "NinePatchFileParser: image_path should be not NULL");
88                         }
89                         m_size++;
90                         if (m_size >= MAX_NINE_PATCH_FILE_LIST) {
91                             SCLLOG(SclLog::ERROR, "No Space for nine patch file list record.");
92                             break;
93                         }
94                     }
95                 }
96
97                 cur_node = cur_node->next;
98             }
99             xmlFreeDoc(doc);
100
101             return 0;
102         }
103         SclNinePatchInfo m_nine_patch_file_list[MAX_NINE_PATCH_FILE_LIST];
104         int m_size;
105 };
106
107 NinePatchFileParser::NinePatchFileParser() {
108     m_impl = new NinePatchFileListParserImpl;
109 }
110
111 NinePatchFileParser::~NinePatchFileParser() {
112     if (m_impl) {
113         SCLLOG(SclLog::MESSAGE, "~NinePatchFileParser() has called.");
114         delete m_impl;
115         m_impl = NULL;
116     }
117 }
118 NinePatchFileParser*
119 NinePatchFileParser::get_instance() {
120     static NinePatchFileParser instance;
121     return &instance;
122 }
123
124 int
125 NinePatchFileParser::init(const char* file) {
126     return m_impl->parsing_nine_patch_file_list(file);
127 }
128
129 int
130 NinePatchFileParser::reload(const char* file) {
131     return m_impl->reload_nine_patch_file_list(file);
132 }
133
134 bool
135 NinePatchFileParser::get_nine_patch_info(const char* filename, SclNinePatchInfo *info) {
136     if (filename == NULL) {
137         SCLLOG(SclLog::DEBUG, "get_nine_patch_info() has failed.");
138         return false;
139     }
140
141     SclNinePatchInfo *nine_patch_list = get_nine_patch_list();
142     if (nine_patch_list == NULL) {
143         SCLLOG(SclLog::DEBUG, "get_nine_patch_info() has failed.");
144         return false;
145     }
146
147     for (int i = 0; i < MAX_NINE_PATCH_FILE_LIST && i < m_impl->m_size; ++i) {
148         if ( nine_patch_list[i].image_path != NULL &&
149             0 == strcmp(nine_patch_list[i].image_path, filename) ) {
150                 if (info) {
151                     *info = nine_patch_list[i];
152                 }
153             return true;
154         }
155     }
156
157     SCLLOG(SclLog::DEBUG, "get_nine_patch_info() has failed.");
158     return false;
159 }
160
161 SclNinePatchInfo*
162 NinePatchFileParser::get_nine_patch_list() {
163     return m_impl->m_nine_patch_file_list;
164 }