3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <libxml/parser.h>
21 #include "protocol-binder/util_internal.h"
22 #include "protocol-binder/internal.h"
23 #include "utility/sync_util.h"
24 #include "utility/fw_assert.h"
27 #define EXPORT_API __attribute__ ((visibility("default")))
30 #ifndef SYNC_AGENT_LOG
32 #define LOG_TAG "AF_PB_UTIL"
35 /* internal function */
36 unsigned int pb_remove_xml_header(char *xml, unsigned int xml_len)
40 /* note that xml generated by wbxml library always have <!DOCTYPE */
41 char *doctype_part_start = strstr(xml, "<!DOCTYPE");
42 SYNC_AGENT_UTIL_ASSERT_CONDITION(doctype_part_start != NULL, "<!DOCTYPE part missing in XML\n");
44 unsigned int xml_len_before_doctype_part_start = doctype_part_start - xml;
45 _DEBUG_INFO("doctype_part_start = %s\n", doctype_part_start);
47 char *doctype_part_end = strchr(doctype_part_start, '>');
48 SYNC_AGENT_UTIL_ASSERT_CONDITION(doctype_part_end != NULL, "<!DOCTYPE part doesn't finished\n");
50 char *after_doctype_part = strchr(doctype_part_end, '<');
51 SYNC_AGENT_UTIL_ASSERT_CONDITION(doctype_part_end != NULL, "No xml tag after xml header part\n");
53 unsigned int xml_len_doctype_part = after_doctype_part - doctype_part_start;
54 _DEBUG_INFO("after_doctype_part = %s\n", after_doctype_part);
56 unsigned int xml_len_after_doctype_part = xml_len - xml_len_before_doctype_part_start - xml_len_doctype_part;
57 _DEBUG_INFO("ret_xml_len = %d, xml_len_before_doctype_part_start = %d, xml_len_doctype_part = %d\n", xml_len, xml_len_before_doctype_part_start, xml_len_doctype_part);
60 unsigned int xml_len_after_doctype_part_expected = strlen(after_doctype_part);
61 _DEBUG_INFO("real = %d, expected = %d\n", xml_len_after_doctype_part, xml_len_after_doctype_part_expected);
64 memmove(xml, after_doctype_part, xml_len_after_doctype_part + 1);
65 _DEBUG_INFO("xml without header = %s\n", xml);
69 return xml_len_after_doctype_part;
72 /* external functions */
73 EXPORT_API sync_agent_pb_error_e sync_agent_switch_protocol(sync_agent_pb_protocol_binder_info_s * binder, sync_agent_pb_protocol_e protocol)
77 retvm_if(binder == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "binder is NULL. FAIL !!!");
79 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
81 WBXMLLanguage lang = pb_get_wbxml_language(protocol);
82 if (lang == WBXML_LANG_UNKNOWN) {
83 err = SYNC_AGENT_PB_RETURN_INVALID_PROTOCOL;
87 if (binder->prev_lang_table != NULL) {
88 err = SYNC_AGENT_PB_RETURN_ALREADY_SWITCH_PROTOCOL;
92 binder->prev_lang_table = binder->lang_table;
93 binder->lang_table = (WBXMLLangEntry *) wbxml_tables_get_table(lang);
100 EXPORT_API sync_agent_pb_error_e sync_agent_undo_switch_protocol(sync_agent_pb_protocol_binder_info_s * binder)
104 retvm_if(binder == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "binder is NULL. FAIL !!!");
106 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
108 if (binder->prev_lang_table == NULL) {
109 err = SYNC_AGENT_PB_RETURN_HAS_NO_UNDO_SWITCH_PROTOCOL;
113 binder->lang_table = binder->prev_lang_table;
114 binder->prev_lang_table = NULL;
121 EXPORT_API WBXMLTree *sync_agent_create_wbxml_tree(const sync_agent_pb_protocol_binder_info_s * binder)
125 retvm_if(binder == NULL, NULL, "binder is NULL. FAIL !!!");
127 WBXMLCharsetMIBEnum orig_charset = WBXML_PARSER_DEFAULT_CHARSET;
128 WBXMLLanguage lang = binder->lang_table->langID;
132 return wbxml_tree_create(lang, orig_charset);
135 EXPORT_API sync_agent_pb_error_e sync_agent_add_wbxml_tree_to_wbxml_node(WBXMLTreeNode * parent_node, WBXMLTree * tree)
139 retvm_if(parent_node == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "parent_node is NULL. FAIL !!!");
140 retvm_if(tree == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "tree is NULL. FAIL !!!");
142 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
144 WBXMLTreeNode *tree_node = wbxml_tree_node_create(WBXML_TREE_TREE_NODE);
145 if (tree_node == NULL) {
146 err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY;
150 WB_BOOL success = wbxml_tree_node_add_child(parent_node, tree_node);
152 err = SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER;
153 wbxml_tree_node_destroy(tree_node);
157 tree_node->tree = tree;
164 EXPORT_API sync_agent_pb_error_e sync_agent_set_wbxml_tree_root(WBXMLTree * tree, WBXMLTreeNode * node)
168 retvm_if(node == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "node is NULL. FAIL !!!");
169 retvm_if(tree == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "tree is NULL. FAIL !!!");
171 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
173 WB_BOOL success = wbxml_tree_add_node(tree, NULL, node);
175 err = SYNC_AGENT_PB_RETURN_TREE_ALREADY_HAS_ROOT;
184 EXPORT_API WBXMLTreeNode *sync_agent_create_node(const sync_agent_pb_protocol_binder_info_s * binder, const char *xml_name)
188 retvm_if(binder == NULL, NULL, "binder is NULL. FAIL !!!");
192 return wbxml_tree_node_create_xml_elt(binder->lang_table, (unsigned char *)xml_name);
195 EXPORT_API WBXMLTreeNode *sync_agent_create_node_with_namespace(const sync_agent_pb_protocol_binder_info_s * binder, const char *xml_name, const char *name_space)
199 retvm_if(binder == NULL, NULL, "binder is NULL. FAIL !!!");
200 retvm_if(name_space == NULL, NULL, "name_space is NULL. FAIL !!!");
201 retvm_if(xml_name == NULL, NULL, "xml_name is NULL. FAIL !!!");
203 WBXMLTreeNode *node = NULL;
205 const WBXMLTagEntry *tag_entry = NULL;
206 WBXMLTag *tag = NULL;
207 WB_UTINY code_page = 0;
209 code_page = wbxml_tables_get_code_page(binder->lang_table->nsTable, name_space);
210 if ((tag_entry = wbxml_tables_get_tag_from_xml(binder->lang_table, code_page, (unsigned char *)xml_name)) != NULL) {
212 tag = wbxml_tag_create_token(tag_entry);
214 tag = wbxml_tag_create_literal((unsigned char *)xml_name);
220 /* Create a new Node */
221 if ((node = wbxml_tree_node_create(WBXML_TREE_ELEMENT_NODE)) == NULL) {
222 wbxml_tag_destroy(tag);
234 EXPORT_API WBXMLTreeNode *sync_agent_create_text_node(const sync_agent_pb_protocol_binder_info_s * binder, const char *xml_name, const char *text, unsigned int text_len)
238 retvm_if(binder == NULL, NULL, "binder is NULL. FAIL !!!");
242 return wbxml_tree_node_create_xml_elt_with_text(binder->lang_table, (unsigned char *)xml_name, (unsigned char *)text, text_len);
245 EXPORT_API WBXMLTreeNode *sync_agent_create_text_node_with_namespace(const sync_agent_pb_protocol_binder_info_s * binder, const char *xml_name, const char *name_space, const char *text, unsigned int text_len)
249 retvm_if(binder == NULL, NULL, "binder is NULL. FAIL !!!");
250 retvm_if(name_space == NULL, NULL, "name_space is NULL. FAIL !!!");
251 retvm_if(xml_name == NULL, NULL, "xml_name is NULL. FAIL !!!");
252 retvm_if(text == NULL, NULL, "text is NULL. FAIL !!!");
254 WBXMLTreeNode *node = NULL;
255 WBXMLTreeNode *text_node = NULL;
257 node = sync_agent_create_node_with_namespace(binder, xml_name, name_space);
262 /* Create text node */
263 if ((text_node = wbxml_tree_node_create_text((unsigned char *)text, text_len)) == NULL) {
264 wbxml_tree_node_destroy(node);
268 /* Add text node to element node */
269 if (!wbxml_tree_node_add_child(node, text_node)) {
270 wbxml_tree_node_destroy(node);
271 wbxml_tree_node_destroy(text_node);
280 EXPORT_API WBXMLTreeNode *sync_agent_create_cdata_node(const sync_agent_pb_protocol_binder_info_s * binder, const char *xml_name, const char *text, unsigned int text_len)
284 retvm_if(binder == NULL, NULL, "binder is NULL. FAIL !!!");
285 retvm_if(xml_name == NULL, NULL, "xml_name is NULL. FAIL !!!");
286 retvm_if(text == NULL, NULL, "text is NULL. FAIL !!!");
288 bool success = false;
289 WBXMLTreeNode *wbxml_tree_node = wbxml_tree_node_create_xml_elt(binder->lang_table, (unsigned char *)xml_name);
290 if (wbxml_tree_node == NULL) {
295 WBXMLTreeNode *text_node = wbxml_tree_node_create_cdata((unsigned char *)text, text_len);
296 if (text_node == NULL) {
301 success = sync_agent_wbxml_node_add_child_node(wbxml_tree_node, text_node);
303 wbxml_tree_node_destroy(text_node);
310 if (wbxml_tree_node != NULL) {
311 wbxml_tree_node_destroy(wbxml_tree_node);
312 wbxml_tree_node = NULL;
318 return wbxml_tree_node;
321 EXPORT_API WBXMLTreeNode *sync_agent_add_child_node(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name)
325 WBXMLTreeNode *new_node = sync_agent_create_node(binder, xml_name);
326 if (new_node == NULL) {
330 bool success = sync_agent_wbxml_node_add_child_node(parent_node, new_node);
335 wbxml_tree_node_destroy(new_node);
340 EXPORT_API WBXMLTreeNode *sync_agent_add_child_node_with_namespace(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const char *name_space)
344 WBXMLTreeNode *new_node = sync_agent_create_node_with_namespace(binder,
347 if (new_node == NULL) {
351 bool success = sync_agent_wbxml_node_add_child_node(parent_node, new_node);
356 wbxml_tree_node_destroy(new_node);
361 EXPORT_API WBXMLTreeNode *sync_agent_add_child_text_node(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const char *text, unsigned int text_len)
365 WBXMLTreeNode *new_node = sync_agent_create_text_node(binder, xml_name, text, text_len);
366 if (new_node == NULL) {
370 bool success = sync_agent_wbxml_node_add_child_node(parent_node, new_node);
375 wbxml_tree_node_destroy(new_node);
380 EXPORT_API WBXMLTreeNode *sync_agent_add_child_text_node_with_namespace(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const char *name_space, const char *text, unsigned int text_len)
384 WBXMLTreeNode *new_node = sync_agent_create_text_node_with_namespace(binder,
390 bool success = sync_agent_wbxml_node_add_child_node(parent_node, new_node);
395 wbxml_tree_node_destroy(new_node);
400 EXPORT_API WBXMLTreeNode *sync_agent_add_child_cdata_node(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const char *text, unsigned int text_len)
404 WBXMLTreeNode *new_node = sync_agent_create_cdata_node(binder, xml_name, text, text_len);
405 if (new_node == NULL) {
409 bool success = sync_agent_wbxml_node_add_child_node(parent_node, new_node);
414 wbxml_tree_node_destroy(new_node);
419 EXPORT_API WBXMLTreeNode *sync_agent_add_child_int_node(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const int num)
423 WBXMLTreeNode *node = NULL;
424 char *num_string = g_strdup_printf("%d", num);
425 if (num_string == NULL) {
429 node = sync_agent_add_child_text_node(binder, parent_node, xml_name, num_string, strlen(num_string));
437 EXPORT_API WBXMLTreeNode *sync_agent_add_child_int_node_with_namespace(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const char *name_space, const int num)
441 WBXMLTreeNode *node = NULL;
442 char *num_string = g_strdup_printf("%d", num);
443 if (num_string == NULL) {
447 node = sync_agent_add_child_text_node_with_namespace(binder, parent_node, xml_name, name_space, num_string, strlen(num_string));
455 EXPORT_API WBXMLTreeNode *sync_agent_add_child_wbxml_node_using_xml_name_with_unsigned_int(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const unsigned int num)
459 WBXMLTreeNode *node = NULL;
460 char *num_string = g_strdup_printf("%u", num);
461 if (num_string == NULL) {
465 node = sync_agent_add_child_text_node(binder, parent_node, xml_name, num_string, strlen(num_string));
473 EXPORT_API WBXMLTreeNode *sync_agent_add_child_wbxml_node_using_xml_name_and_namespace_with_unsigned_int(const sync_agent_pb_protocol_binder_info_s * binder, WBXMLTreeNode * parent_node, const char *xml_name, const char *name_space, const unsigned int num)
477 WBXMLTreeNode *node = NULL;
478 char *num_string = g_strdup_printf("%u", num);
479 if (num_string == NULL) {
483 node = sync_agent_add_child_text_node_with_namespace(binder, parent_node, xml_name, name_space, num_string, strlen(num_string));
491 EXPORT_API bool sync_agent_wbxml_node_add_child_node(WBXMLTreeNode * parent, WBXMLTreeNode * child)
497 return wbxml_tree_node_add_child(parent, child);
500 EXPORT_API sync_agent_pb_error_e sync_agent_get_name_from_element_node(const WBXMLTreeNode * element_node, char **element_xml_name)
504 retvm_if(element_node == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "element_node is NULL. FAIL !!!");
506 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
507 char *out_element_xml_name = NULL;
509 if (!pb_has_wbxml_node_same_type((WBXMLTreeNode *) element_node, WBXML_TREE_ELEMENT_NODE)) {
510 err = SYNC_AGENT_PB_RETURN_TREE_NODE_IS_NOT_ELEMENT_NODE;
514 if (element_node->name != NULL) {
515 out_element_xml_name = (char *)wbxml_tag_get_xml_name(element_node->name);
519 if (err == SYNC_AGENT_PB_RETURN_OK) {
520 *element_xml_name = out_element_xml_name;
528 EXPORT_API sync_agent_pb_error_e sync_agent_get_namespace_from_element_node(const sync_agent_pb_protocol_binder_reverse_info_s * binder, const WBXMLTreeNode * element_node, char **name_space)
532 retvm_if(binder == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "binder is NULL. FAIL !!!");
533 retvm_if(element_node == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "element_node is NULL. FAIL !!!");
535 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
536 char *out_namespace = NULL;
538 if (!pb_has_wbxml_node_same_type((WBXMLTreeNode *) element_node, WBXML_TREE_ELEMENT_NODE)) {
539 err = SYNC_AGENT_PB_RETURN_TREE_NODE_IS_NOT_ELEMENT_NODE;
543 if (element_node->name != NULL && element_node->name->type == WBXML_VALUE_TOKEN) {
544 const WBXMLLangEntry *pLang = pb_get_current_subtree_lang((sync_agent_pb_protocol_binder_reverse_info_s *) binder);
545 assert(pLang != NULL);
547 out_namespace = (char *)wbxml_tables_get_xmlns(pLang->nsTable, element_node->name->u.token->wbxmlCodePage);
551 if (err == SYNC_AGENT_PB_RETURN_OK) {
552 *name_space = out_namespace;
560 EXPORT_API sync_agent_pb_error_e sync_agent_get_text_from_node(WBXMLTreeNode * parent, char **text_pointer)
564 retvm_if(parent == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "parent is NULL. FAIL !!!");
566 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
567 if (parent->children != NULL && parent->children->type == WBXML_TREE_TEXT_NODE && parent->children->content != NULL) {
568 *text_pointer = (char *)wbxml_buffer_get_cstr(parent->children->content);
570 *text_pointer = NULL;
578 EXPORT_API sync_agent_pb_error_e sync_agent_get_binary_from_node(WBXMLTreeNode * parent, char **binary_pointer, unsigned int *binary_size)
582 retvm_if(parent == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "parent is NULL. FAIL !!!");
584 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
585 if (parent->children != NULL && parent->children->type == WBXML_TREE_TEXT_NODE && parent->children->content != NULL) {
586 *binary_pointer = (char *)wbxml_buffer_get_cstr(parent->children->content);
587 *binary_size = wbxml_buffer_len(parent->children->content);
589 err = SYNC_AGENT_PB_RETURN_UTIL_NO_BINARY_VALUE;
597 EXPORT_API sync_agent_pb_error_e sync_agent_get_int_from_node(WBXMLTreeNode * parent, int *value)
601 retvm_if(parent == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "parent is NULL. FAIL !!!");
603 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
604 char *text_pointer = NULL;
605 err = sync_agent_get_text_from_node(parent, &text_pointer);
606 /*if (err != SYNC_AGENT_PB_RETURN_OK) {
607 assert(err == SYNC_AGENT_PB_RETURN_UTIL_NO_TEXT_VALUE);
608 err = SYNC_AGENT_PB_RETURN_UTIL_NO_INT_VALUE;
612 if (text_pointer != NULL) {
613 *value = atoi(text_pointer);
615 err = SYNC_AGENT_PB_RETURN_UTIL_NO_INT_VALUE;
623 EXPORT_API sync_agent_pb_error_e sync_agent_get_text_from_cdata_node(WBXMLTreeNode * parent, char **text_pointer)
627 retvm_if(parent == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "parent is NULL. FAIL !!!");
629 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
630 WBXMLTreeNode *cdata_node = parent->children;
632 if (cdata_node != NULL && cdata_node->type == WBXML_TREE_CDATA_NODE) {
633 err = sync_agent_get_text_from_node(cdata_node, text_pointer);
635 *text_pointer = NULL;
643 EXPORT_API sync_agent_pb_error_e sync_agent_get_binary_pointer_from_wbxml_cdata_node(WBXMLTreeNode * parent, char **binary_pointer, unsigned int *binary_size)
647 retvm_if(parent == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "parent is NULL. FAIL !!!");
649 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
650 WBXMLTreeNode *cdata_node = parent->children;
652 if (cdata_node != NULL && cdata_node->type == WBXML_TREE_CDATA_NODE) {
653 err = sync_agent_get_binary_from_node(cdata_node, binary_pointer, binary_size);
655 err = SYNC_AGENT_PB_RETURN_UTIL_NO_BINARY_VALUE;
663 EXPORT_API sync_agent_pb_error_e sync_agent_destroy_wbxml_node(WBXMLTreeNode * node)
667 retvm_if(node == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "node is NULL. FAIL !!!");
669 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
670 wbxml_tree_node_destroy_all(node);
678 sync_agent_pb_error_e pb_get_wbxml_tree_from_wbxml(char *byte_stream, int byte_stream_len, WBXMLLanguage wbxml_lang, WBXMLTree ** wbxml_tree)
682 retvm_if(byte_stream == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "byte_stream is NULL. FAIL !!!");
684 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
685 WBXMLTree *ret_wbxml_tree = NULL;
687 _DEBUG_INFO("start with byte_stream = %x, byte_stream_len = %d\n", byte_stream, byte_stream_len);
688 WBXMLError wbxml_err = wbxml_tree_from_wbxml((unsigned char *)byte_stream, byte_stream_len, wbxml_lang, WBXML_CHARSET_UNKNOWN, &ret_wbxml_tree);
690 if (wbxml_err != WBXML_OK) {
691 _DEBUG_ERROR("wbxml_tree_from_wbxml error occurred. wbxml error(%d) = %s", wbxml_err, wbxml_errors_string(wbxml_err));
692 err = SYNC_AGENT_PB_RETURN_WBXML_PARSE_FAIL;
697 if (err == SYNC_AGENT_PB_RETURN_OK) {
698 *wbxml_tree = ret_wbxml_tree;
700 _DEBUG_INFO("ended with err = %d\n", err);
707 sync_agent_pb_error_e pb_get_wbxml_tree_from_xml(char *byte_stream, int byte_stream_len, WBXMLLanguage wbxml_lang, WBXMLTree ** wbxml_tree)
711 retvm_if(byte_stream == NULL, SYNC_AGENT_PB_RETURN_INVALID_INPUT_PARAMETER, "byte_stream is NULL. FAIL !!!");
713 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
714 WBXMLTree *ret_wbxml_tree = NULL;
716 _DEBUG_INFO("start with byte_stream = %x, byte_stream_len = %d\n", byte_stream, byte_stream_len);
717 WBXMLError wbxml_err = wbxml_tree_from_xml_with_lang((unsigned char *)byte_stream, byte_stream_len, wbxml_lang, &ret_wbxml_tree);
719 if (wbxml_err != WBXML_OK) {
720 _DEBUG_ERROR("wbxml_tree_from_xml error occurred. wbxml error(%d) = %s", wbxml_err, wbxml_errors_string(wbxml_err));
721 err = SYNC_AGENT_PB_RETURN_XML_PARSE_FAIL;
726 if (err == SYNC_AGENT_PB_RETURN_OK) {
727 *wbxml_tree = ret_wbxml_tree;
729 _DEBUG_INFO("ended with err = %d\n", err);
736 bool pb_validate_protocol(sync_agent_pb_protocol_e protocol)
740 if (SYNC_AGENT_PB_PROTOCOL_UNKNOWN <= protocol && protocol < SYNC_AGENT_PB_PROTOCOL_MAX_COUNT) {
748 WBXMLLanguage pb_get_wbxml_language(sync_agent_pb_protocol_e protocol)
752 WBXMLLanguage lang = WBXML_LANG_UNKNOWN;
755 case SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML10:
756 lang = WBXML_LANG_SYNCML_SYNCML10;
758 case SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML11:
759 lang = WBXML_LANG_SYNCML_SYNCML11;
761 case SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML12:
762 lang = WBXML_LANG_SYNCML_SYNCML12;
764 case SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF10:
765 lang = WBXML_LANG_SYNCML_DEVINF10;
767 case SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF11:
768 lang = WBXML_LANG_SYNCML_DEVINF11;
770 case SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF12:
771 lang = WBXML_LANG_SYNCML_DEVINF12;
773 case SYNC_AGENT_PB_PROTOCOL_SYNCML_METINF10:
774 lang = WBXML_LANG_SYNCML_METINF10;
776 case SYNC_AGENT_PB_PROTOCOL_SYNCML_METINF11:
777 lang = WBXML_LANG_SYNCML_METINF11;
779 case SYNC_AGENT_PB_PROTOCOL_SYNCML_METINF12:
780 lang = WBXML_LANG_SYNCML_METINF12;
782 case SYNC_AGENT_PB_PROTOCOL_SYNCML_DMDDF12:
783 lang = WBXML_LANG_SYNCML_DMDDF12;
785 case SYNC_AGENT_PB_PROTOCOL_ACTIVESYNC:
786 lang = WBXML_LANG_ACTIVESYNC;
788 case SYNC_AGENT_PB_PROTOCOL_PROV10:
789 lang = WBXML_LANG_PROV10;
800 sync_agent_pb_protocol_e pb_get_protocol_from_wbxml_lang(WBXMLLanguage wbxml_lang)
804 sync_agent_pb_protocol_e protocol = SYNC_AGENT_PB_PROTOCOL_UNKNOWN;
806 switch (wbxml_lang) {
807 case WBXML_LANG_SYNCML_SYNCML10:
808 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML10;
810 case WBXML_LANG_SYNCML_SYNCML11:
811 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML11;
813 case WBXML_LANG_SYNCML_SYNCML12:
814 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_SYNCML12;
816 case WBXML_LANG_SYNCML_DEVINF10:
817 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF10;
819 case WBXML_LANG_SYNCML_DEVINF11:
820 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF11;
822 case WBXML_LANG_SYNCML_DEVINF12:
823 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_DEVINF12;
825 case WBXML_LANG_SYNCML_METINF10:
826 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_METINF10;
828 case WBXML_LANG_SYNCML_METINF11:
829 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_METINF11;
831 case WBXML_LANG_SYNCML_METINF12:
832 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_METINF12;
834 case WBXML_LANG_SYNCML_DMDDF12:
835 protocol = SYNC_AGENT_PB_PROTOCOL_SYNCML_DMDDF12;
837 case WBXML_LANG_ACTIVESYNC:
838 protocol = SYNC_AGENT_PB_PROTOCOL_ACTIVESYNC;
840 case WBXML_LANG_PROV10:
841 protocol = SYNC_AGENT_PB_PROTOCOL_PROV10;
852 WBXMLVersion pb_get_wbxml_version(sync_agent_pb_encoding_e enc)
856 WBXMLVersion version = WBXML_VERSION_UNKNOWN;
858 case SYNC_AGENT_PB_ENCODING_WBXML_10:
859 version = WBXML_VERSION_10;
861 case SYNC_AGENT_PB_ENCODING_WBXML_11:
862 version = WBXML_VERSION_11;
864 case SYNC_AGENT_PB_ENCODING_WBXML_12:
865 version = WBXML_VERSION_12;
867 case SYNC_AGENT_PB_ENCODING_WBXML_13:
868 version = WBXML_VERSION_13;
879 WBXMLTreeNode *pb_get_next_node_of_tree_node(WBXMLTreeNode * node, bool * first_visit)
883 retvm_if(node == NULL, NULL, "node is NULL. FAIL !!!");
885 WBXMLTreeNode *next_node = NULL;
888 /* *first_visit = true; */
891 next_node = node->tree->root;
893 /* if child node does not exist, then mark second visit and set current_node as next_node */
894 if (next_node == NULL) {
895 *first_visit = false;
899 next_node = node->next;
901 /* if next node exist */
902 if (next_node != NULL) {
905 /* if next node does not exist, then set next node as parent node */
907 /* *first_visit = false; */
908 next_node = node->parent;
917 WBXMLTreeNode *pb_get_next_node_of_element_node(WBXMLTreeNode * node, bool * first_visit)
921 retvm_if(node == NULL, NULL, "node is NULL. FAIL !!!");
923 WBXMLTreeNode *next_node = NULL;
926 /* *first_visit = true; */
929 next_node = node->children;
931 /* if child node does not exist, then mark second visit and set current_node as next_node */
932 if (next_node == NULL) {
933 *first_visit = false;
937 next_node = node->next;
939 /* if next node exist */
940 if (next_node != NULL) {
943 /* if next node does not exist, then set next node as parent node */
945 /* *first_visit = false; */
946 next_node = node->parent;
955 const WBXMLLangEntry *pb_get_current_subtree_lang(sync_agent_pb_protocol_binder_reverse_info_s * reverse_binder)
959 retvm_if(reverse_binder == NULL, NULL, "reverse_binder is NULL. FAIL !!!");
961 const WBXMLLangEntry *lang = NULL;
963 if (reverse_binder->tree_stack != NULL) {
964 /* current node belongs to subtree of root tree */
965 WBXMLTreeNode *tree_node = (WBXMLTreeNode *) (reverse_binder->tree_stack->data);
966 assert(tree_node != NULL);
967 assert(pb_has_wbxml_node_same_type(tree_node, WBXML_TREE_TREE_NODE));
969 lang = tree_node->tree->lang;
971 lang = reverse_binder->tree->lang;
979 bool pb_has_wbxml_node_same_type(WBXMLTreeNode * node, WBXMLTreeNodeType type)
983 retvm_if(node == NULL, NULL, "node is NULL. FAIL !!!");
986 if (node->type == type) {
995 void pb_print_wbxml_dom_in_xml(const WBXMLTree * tree)
999 retm_if(tree == NULL, "tree is NULL. FAIL !!!");
1002 unsigned char *xml = NULL;
1003 unsigned int xml_len = 0;
1004 WBXMLGenXMLParams params = { WBXML_GEN_XML_INDENT, WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN, 4, FALSE };
1005 WBXMLError wbxml_err = wbxml_tree_to_xml((WBXMLTree *) tree, &xml, &xml_len, ¶ms);
1007 if (wbxml_err != WBXML_OK) {
1008 /* TODO : _DEBUG_ERROR using wbxml_errors_string(wbxml_err) */
1012 _DEBUG_INFO("xml printing : \n%s\n", xml);