Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / xml2binary / metadata_handler.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 "metadata_handler.h"
19
20 MetaData_Handler::
21 MetaData_Handler(const char* file) {
22     this->file_name = file;
23     m_current_metadata_record_id = -1;
24     parsing_metadata();
25 }
26
27 void MetaData_Handler::
28 set_current_metadata_record(const char* record_name) {
29     if (record_name == NULL) {
30         m_current_metadata_record_id = -1;
31         return;
32     }
33     int idx = find_metadata_record_index(record_name);
34
35     m_current_metadata_record_id = idx;
36 }
37
38 int MetaData_Handler::
39 encode(const char* file)const {
40     ResourceStorage storage;
41     encode_metadata(storage, metadata);
42     storage.toFile(file);
43
44     return storage.get_size();
45 }
46
47 int MetaData_Handler::
48 encode(const char* file, int& offset)const {
49     ResourceStorage storage;
50     encode_metadata(storage, metadata);
51     storage.toFile(file, offset);
52
53     return storage.get_size();
54 }
55
56 int MetaData_Handler::
57 encode(ResourceStorage& storage)const {
58     encode_metadata(storage, metadata);
59     return storage.get_size();
60 }
61
62 unsigned short MetaData_Handler::
63 get_width(const char* field_name) const {
64     if (field_name == NULL) return 0;
65
66     int idx = m_current_metadata_record_id;
67     if (idx < 0 || idx > (int)metadata.m_vec_metadata_record.size()) return 0;
68
69     const MetaData_Record&  metadata_record = metadata.m_vec_metadata_record.at(idx);
70
71     int width = get_width(field_name, metadata_record);
72
73     return width;
74 }
75
76 unsigned short MetaData_Handler::
77 get_width(const char* name, const MetaData_Record& metadata_record) const {
78     assert(name);
79     for (size_t i = 0; i < metadata_record.vField.size(); ++i) {
80         if (0 == strcmp(name, metadata_record.vField.at(i).m_name)) {
81             return metadata_record.vField.at(i).m_width;
82         }
83     }
84     return 0;
85 }
86
87 int MetaData_Handler::
88 find_metadata_record_index(const char* name)const {
89     assert(name);
90
91     for (size_t i = 0; i < metadata.m_vec_metadata_record.size(); ++i) {
92         const MetaData_Record& metadata_record = metadata.m_vec_metadata_record.at(i);
93
94         if (0 == strcmp(metadata_record.m_name, name)) {
95             return i;
96         }
97     }
98
99     return -1;
100 }
101
102 int MetaData_Handler::
103 parsing_field(const xmlNodePtr node, MetaData_Field& data, const MetaData_Width& metadataWidth) {
104     memset(data.m_name, 0, sizeof(data.m_name));
105     memset(data.m_type, 0, sizeof(data.m_type));
106     data.m_width = 0;
107
108     xmlChar* name = xmlGetProp(node, (const xmlChar*)"name");
109     if (name == NULL) return -1;
110     /*FIXME strncpy ?*/
111     strncpy(data.m_name, (const char*)name, sizeof(data.m_name));
112     data.m_name[sizeof(data.m_name)-1] = '\0';
113     xmlFree(name);
114
115     xmlChar* type = xmlGetProp(node, (const xmlChar*)"type");
116     if (type == NULL) return -1;
117
118     int ret = 0;
119     if (0 == xmlStrcmp(type, (const xmlChar*)"string_id")) {
120         /*FIXME use vaule instead string*/
121         strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
122         data.m_type[sizeof(data.m_type) - 1] = '\0';
123         data.m_width = metadataWidth.string_id_width;
124     } else if (0 == xmlStrcmp(type, (const xmlChar*)"int8")) {
125         strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
126         data.m_type[sizeof(data.m_type) - 1] = '\0';
127         data.m_width = 1;
128     } else if (0 == xmlStrcmp(type, (const xmlChar*)"int16")) {
129         strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
130         data.m_type[sizeof(data.m_type) - 1] = '\0';
131         data.m_width = 2;
132     } else if (0 == xmlStrcmp(type, (const xmlChar*)"int32")) {
133         strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
134         data.m_type[sizeof(data.m_type) - 1] = '\0';
135         data.m_width = 4;
136     } else if (0 == xmlStrcmp(type, (const xmlChar*)"float32")) {
137         strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
138         data.m_type[sizeof(data.m_type) - 1] = '\0';
139         data.m_width = 4;
140     } else if (0 == xmlStrcmp(type, (const xmlChar*)"float64")) {
141         strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
142         data.m_type[sizeof(data.m_type) - 1] = '\0';
143         data.m_width = 8;
144     } else {
145         ret = -1;
146     }
147
148     xmlFree(type);
149
150     return ret;
151 }
152
153 int MetaData_Handler::
154 parsing_record(const xmlNodePtr curNode, MetaData_Record& metadataRecord, const MetaData_Width& metadataWidth) {
155     //parsing struct name
156     xmlChar* name = xmlGetProp(curNode, (const xmlChar*)"name");
157     if (name == NULL) return -1;
158     strncpy(metadataRecord.m_name, (const char*)name, sizeof(metadataRecord.m_name));
159     metadataRecord.m_name[sizeof(metadataRecord.m_name)-1] = '\0';
160     xmlFree(name);
161
162     xmlNodePtr childNode = curNode->xmlChildrenNode;
163
164     while (childNode != NULL) {
165         if (0 == xmlStrcmp(childNode->name, (const xmlChar *)"field")) {
166             MetaData_Field data;
167             int ret = parsing_field(childNode, data, metadataWidth);
168             if (ret != -1) {
169                 metadataRecord.vField.push_back(data);
170             }
171         }
172
173         childNode = childNode->next;
174     }
175     return 0;
176 }
177 int MetaData_Handler::
178 parsing_metadata_type(const xmlNodePtr curNode, MetaData_Width& metadataWidth) {
179     assert(curNode != NULL);
180     xmlNodePtr childNode = curNode->xmlChildrenNode;
181
182     while (childNode != NULL) {
183         if (0 == xmlStrcmp(childNode->name, (const xmlChar *)"type")) {
184             xmlChar* xmlname = xmlGetProp(childNode, (const xmlChar*)"name");
185             if (xmlname == NULL) continue;
186             xmlChar* xmlwidth = xmlGetProp(childNode, (const xmlChar*)"width");
187             if (xmlwidth == NULL) {
188                 xmlFree(xmlname);
189                 continue;
190             }
191
192             /*FIXME how to assume that the atoi will get the correct num*/
193             int width = atoi((const char*)xmlwidth);
194
195             if (0 == xmlStrcmp(xmlname, (const xmlChar*)"string_id")) {
196                 metadataWidth.string_id_width = width;
197             }
198
199             xmlFree(xmlname);
200             xmlFree(xmlwidth);
201         }
202
203         childNode = childNode->next;
204     }
205     return 0;
206 }
207 void MetaData_Handler::
208 parsing_metadata() {
209     xmlDocPtr doc;
210     xmlNodePtr curNode;
211
212     doc = xmlReadFile(file_name, NULL, 0);
213     if (doc == NULL) {
214         LOGE("Could not load file.\n");
215         return;
216     }
217
218     curNode = xmlDocGetRootElement(doc);
219     if (curNode == NULL) {
220         LOGE("empty document.\n");
221         xmlFreeDoc(doc);
222         return;
223     }
224     if (0 != xmlStrcmp(curNode->name, (const xmlChar*)"metadata"))
225     {
226         LOGE("root name %s error!\n", curNode->name);
227         xmlFreeDoc(doc);
228         return;
229     }
230
231     xmlChar* version = xmlGetProp(curNode, (const xmlChar*)"version");
232     if (version) {
233         strncpy(metadata.m_version, (const char*)version, sizeof(metadata.m_version));
234         metadata.m_version[sizeof(metadata.m_version)-1] = '\0';
235         xmlFree(version);
236     } else {
237         metadata.m_version[0] = '\0';
238     }
239
240     MetaData_Width metadataWidth;
241     curNode = curNode->xmlChildrenNode;
242     while (curNode) {
243         if (0 == xmlStrcmp(curNode->name, (const xmlChar*)"metadata_type")) {
244             parsing_metadata_type(curNode, metadataWidth);
245         } else if (0 == xmlStrcmp(curNode->name, (const xmlChar*)"record")) {
246             MetaData_Record metadataRecord;
247             int ret = parsing_record(curNode, metadataRecord, metadataWidth);
248             if (ret != -1) {
249                 metadata.m_vec_metadata_record.push_back(metadataRecord);
250             }
251         }
252         curNode = curNode->next;
253     }
254     xmlFreeDoc(doc);
255 }
256
257 inline const MetaData* MetaData_Handler::
258 get_metadata()const {
259     return &metadata;
260 }