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.
20 #include <libxml/parser.h>
22 #include "nine_patch_file_list_parser.h"
23 #include "xml_parser_utils.h"
24 #include "simple_debug.h"
26 class NinePatchFileListParserImpl {
28 NinePatchFileListParserImpl() {
30 memset(m_nine_patch_file_list, 0x00, sizeof(SclNinePatchInfo) * MAX_NINE_PATCH_FILE_LIST);
32 ~NinePatchFileListParserImpl() {
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;
43 memset(m_nine_patch_file_list, 0x00, sizeof(SclNinePatchInfo) * MAX_NINE_PATCH_FILE_LIST);
46 int reload_nine_patch_file_list(const char* input_file) {
48 return parsing_nine_patch_file_list(input_file);
51 int parsing_nine_patch_file_list(const char* input_file) {
55 doc = xmlReadFile(input_file, NULL, 0);
57 SCLLOG(SclLog::DEBUG, "Could not load file: %s.", input_file);
61 cur_node = xmlDocGetRootElement(doc);
62 if (cur_node == NULL) {
63 SCLLOG(SclLog::DEBUG, "Nine_Patch_File_Parser: empty document.\n");
67 if (0 != xmlStrcmp(cur_node->name, (const xmlChar*)"nine_patch_file_list"))
69 SCLLOG(SclLog::DEBUG, "Nine_Patch_File_Parser: root name error: %s\n!", (char *)cur_node->name);
74 cur_node = cur_node->xmlChildrenNode;
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");
90 if (m_size >= MAX_NINE_PATCH_FILE_LIST) {
91 SCLLOG(SclLog::ERROR, "No Space for nine patch file list record.");
97 cur_node = cur_node->next;
103 SclNinePatchInfo m_nine_patch_file_list[MAX_NINE_PATCH_FILE_LIST];
107 NinePatchFileParser::NinePatchFileParser() {
108 m_impl = new NinePatchFileListParserImpl;
111 NinePatchFileParser::~NinePatchFileParser() {
113 SCLLOG(SclLog::MESSAGE, "~NinePatchFileParser() has called.");
119 NinePatchFileParser::get_instance() {
120 static NinePatchFileParser instance;
125 NinePatchFileParser::init(const char* file) {
126 return m_impl->parsing_nine_patch_file_list(file);
130 NinePatchFileParser::reload(const char* file) {
131 return m_impl->reload_nine_patch_file_list(file);
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.");
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.");
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) ) {
151 *info = nine_patch_list[i];
157 SCLLOG(SclLog::DEBUG, "get_nine_patch_info() has failed.");
162 NinePatchFileParser::get_nine_patch_list() {
163 return m_impl->m_nine_patch_file_list;