Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / agent / dm-engine / dl-manager / dd_parser.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*lib*/
19 #include <stdio.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
22
23 /*sync-agent*/
24 #include <sync_agent.h>
25
26 /*dm-agent*/
27 #include "common/dm_common.h"
28 #include "dm-engine/dl-manager/dd_parser.h"
29
30 #ifndef OMADM_AGENT_LOG
31 #undef LOG_TAG
32 #define LOG_TAG "OMA_DM_DL"
33 #endif
34
35 typedef enum {
36         DD_NODE_NO_TYPE = 0,
37         DD_NODE_OBJECT_TYPE,
38         DD_NODE_DESCRIPTION,
39         DD_NODE_OBJECT_URI,
40         DD_NODE_SIZE,
41         DD_NODE_INSTALL_NOTIFY_URI
42 } DD_NODE_TYPE;
43
44 static DD_NODE_TYPE ___get_node_type(unsigned char *node_name);
45 static DM_ERROR __set_value_to_dd_object(xmlNode * a_node, Download_Descriptor ** dd_object);
46 static DM_ERROR _convert_doc_to_dd_object(xmlDocPtr doc, Download_Descriptor ** dd_object);
47
48 DM_ERROR convert_xml_to_DD_object(const char *dd_xml, unsigned int dd_xml_length, Download_Descriptor ** dd_object)
49 {
50         _EXTERN_FUNC_ENTER;
51
52         DM_ERROR ret = DM_OK;
53         /* parse the in memory document */
54         xmlDocPtr doc = NULL;
55
56         retvm_if((dd_xml) == NULL, COMMON_ERR_IS_NULL, "dd_xml is NULL!!");
57
58         if (dd_xml == NULL || dd_xml_length == 0 || *dd_object == NULL) {
59                 _DEBUG_INFO("dd_xml == NULL || dd_xml_length == 0 || *dd_object == NULL");
60                 ret = COMMON_ERR_IS_NULL;
61                 goto error;
62         }
63         _DEBUG_INFO("xml: %s, size: %d", dd_xml, dd_xml_length);
64         doc = xmlReadMemory(dd_xml, dd_xml_length, 0, 0, 0);
65         if (doc == NULL) {
66                 _DEBUG_INFO("xmlReadMemory() Fail\n");
67                 ret = COMMON_ERR_IS_NULL;
68                 goto error;
69         }
70         /* convert doc to dd_object */
71         ret = _convert_doc_to_dd_object(doc, dd_object);
72         _DEBUG_INFO("convert doc to dd object : %d", ret);
73
74  error:
75
76         if (doc != NULL)
77                 xmlFreeDoc(doc);
78
79         _EXTERN_FUNC_EXIT;
80         return ret;
81 }
82
83 static DD_NODE_TYPE ___get_node_type(unsigned char *node_name)
84 {
85         _INNER_FUNC_ENTER;
86
87         _DEBUG_VERBOSE("node_name: %s\n", node_name);
88
89         if (strcmp((const char *)node_name, "type") == 0) {
90                 _INNER_FUNC_EXIT;
91                 return DD_NODE_OBJECT_TYPE;
92         } else if (strcmp((const char *)node_name, "description") == 0) {
93                 _INNER_FUNC_EXIT;
94                 return DD_NODE_DESCRIPTION;
95         } else if (strcmp((const char *)node_name, "objectURI") == 0) {
96                 _INNER_FUNC_EXIT;
97                 return DD_NODE_OBJECT_URI;
98         } else if (strcmp((const char *)node_name, "size") == 0) {
99                 _INNER_FUNC_EXIT;
100                 return DD_NODE_SIZE;
101         } else if (strcmp((const char *)node_name, "installNotifyURI") == 0) {
102                 _INNER_FUNC_EXIT;
103                 return DD_NODE_INSTALL_NOTIFY_URI;
104         }
105
106         _INNER_FUNC_EXIT;
107         return DD_NODE_NO_TYPE;
108 }
109
110 static DM_ERROR __set_value_to_dd_object(xmlNode * a_node, Download_Descriptor ** dd_object)
111 {
112         _INNER_FUNC_ENTER;
113         DM_ERROR ret = DM_OK;
114         xmlNode *cur_node = 0;
115         DD_NODE_TYPE node_type = DD_NODE_NO_TYPE;
116
117         retvm_if((a_node) == NULL, COMMON_ERR_IS_NULL, "a_node is NULL!!");
118
119         unsigned char *content = NULL;
120         for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
121                 if (cur_node->type == XML_ELEMENT_NODE) {
122                         content = xmlNodeGetContent(cur_node);
123
124                         _DEBUG_VERBOSE("node type: Element, name: %s\n", cur_node->name);
125                         _DEBUG_VERBOSE("node type: Element, content: %s\n", content);
126
127                         node_type = ___get_node_type((unsigned char *)(cur_node->name));
128                         switch (node_type) {
129                         case DD_NODE_OBJECT_TYPE:
130                                 {
131                                         if (content != NULL) {
132                                                 (*dd_object)->object_type = strdup((const char *)content);
133                                         }
134                                 }
135                                 break;
136                         case DD_NODE_DESCRIPTION:
137                                 {
138                                         if (content != NULL) {
139                                                 (*dd_object)->object_description = strdup((const char *)content);
140                                         }
141                                 }
142                                 break;
143                         case DD_NODE_OBJECT_URI:
144                                 {
145 #ifdef _DM_BUNDANG_TEST
146                                         if (content != NULL) {
147                                                 (*dd_object)->object_uri = get_new_uri((char *)content);
148                                         }
149 #else
150                                         if (content != NULL) {
151                                                 (*dd_object)->object_uri = strdup((const char *)content);
152                                         }
153 #endif
154                                         if ((*dd_object)->object_uri != NULL) {
155                                                 _DEBUG_VERBOSE("object_uri = %s\n", (*dd_object)->object_uri);
156                                         } else {
157                                                 _DEBUG_VERBOSE("object uri null");
158                                         }
159                                 }
160                                 break;
161                         case DD_NODE_SIZE:
162                                 {
163                                         if (content != NULL) {
164                                                 (*dd_object)->object_size = atoi((const char *)content);
165                                         }
166                                 }
167                                 break;
168                         case DD_NODE_INSTALL_NOTIFY_URI:
169                                 {
170 #ifdef _DM_BUNDANG_TEST
171                                         if (content != NULL) {
172                                                 (*dd_object)->install_notify_uri = get_new_uri((char *)content);
173                                         }
174 #else
175                                         if (content != NULL) {
176                                                 (*dd_object)->install_notify_uri = strdup((const char *)content);
177                                         }
178 #endif
179                                         if ((*dd_object)->install_notify_uri != NULL) {
180                                                 _DEBUG_VERBOSE("install_notify_uri = %s\n", (*dd_object)->install_notify_uri);
181                                         } else {
182                                                 _DEBUG_VERBOSE("install notify uri null");
183                                         }
184                                 }
185                                 break;
186                         default:
187                                 break;
188                         }
189                 }
190
191                 ret = __set_value_to_dd_object(cur_node->children, dd_object);
192                 _DEBUG_VERBOSE("set value to dd object : %d", ret);
193         }
194
195         _INNER_FUNC_EXIT;
196         return DM_OK;
197 }
198
199 static DM_ERROR _convert_doc_to_dd_object(xmlDocPtr doc, Download_Descriptor ** dd_object)
200 {
201         _INNER_FUNC_ENTER;
202
203         DM_ERROR ret = DM_OK;
204
205         xmlNode *root_element = 0;
206
207         /* get the root element node */
208         root_element = xmlDocGetRootElement(doc);
209         if (root_element == NULL) {
210                 _INNER_FUNC_EXIT;
211                 return COMMON_ERR_IS_NULL;
212         }
213
214         /*DD_NODE_TYPE node_type = DD_NODE_NO_TYPE; */
215         ret = __set_value_to_dd_object(root_element, dd_object);
216         _DEBUG_TRACE("set value dd object : %d", ret);
217
218         _INNER_FUNC_EXIT;
219         return ret;
220 }