📄 wbxml_encoder.c
字号:
ret = wbxml_encode_end(encoder); WBXML_DEBUG((WBXML_ENCODER, "End Element")); } } return ret;}/** * @brief Parse an XML Attribute * @param encoder The WBXML Encoder * @param attribute The XML Attribute to parse * @return WBXML_OK if parsing is OK, an error code otherwise */static WBXMLError parse_attribute(WBXMLEncoder *encoder, WBXMLAttribute *attribute){ if (encoder->lang == NULL) return WBXML_ERROR_LANG_TABLE_UNDEFINED; if (encoder->lang->attrTable == NULL) return WBXML_OK; /* Check that this attribute has a name */ if (attribute->name == NULL) return WBXML_ERROR_XML_NULL_ATTR_NAME; WBXML_DEBUG((WBXML_ENCODER, "Attribute: %s = %s", wbxml_attribute_get_xml_name(attribute), wbxml_attribute_get_xml_value(attribute))); /* Encode: Attribute */ switch (encoder->output_type) { case WBXML_ENCODER_OUTPUT_WBXML: return wbxml_encode_attr(encoder, attribute); case WBXML_ENCODER_OUTPUT_XML: return xml_encode_attr(encoder, attribute); default: return WBXML_ERROR_INTERNAL; }}/** * @brief Parse an XML Text * @param encoder The WBXML Encoder * @param node The text to parse * @return WBXML_OK if parsing is OK, an error code otherwise */static WBXMLError parse_text(WBXMLEncoder *encoder, WBXMLTreeNode *node){ WBXMLError ret = WBXML_OK; /* Do not modify text inside a CDATA section */ if (!encoder->in_cdata) { /* If Canonical Form: "Ignorable white space is considered significant and is treated equivalently to data" */ if ((encoder->output_type != WBXML_ENCODER_OUTPUT_XML) || (encoder->xml_gen_type != WBXML_GEN_XML_CANONICAL)) { /* Ignore blank nodes */ if ((encoder->ignore_empty_text) && (wbxml_buffer_contains_only_whitespaces(node->content))) return WBXML_OK; /* Strip Blanks */ if (encoder->remove_text_blanks) wbxml_buffer_strip_blanks(node->content); } } WBXML_DEBUG((WBXML_ENCODER, "Text: <%s>", wbxml_buffer_get_cstr(node->content))); /* Encode Text */ switch (encoder->output_type) { case WBXML_ENCODER_OUTPUT_WBXML: if (encoder->in_cdata) { if (encoder->cdata == NULL) { /* Must never happen */ return WBXML_ERROR_INTERNAL; }#if defined( WBXML_SUPPORT_SYNCML ) if ((encoder->lang->langID == WBXML_LANG_SYNCML_SYNCML10) || (encoder->lang->langID == WBXML_LANG_SYNCML_SYNCML11) || (encoder->lang->langID == WBXML_LANG_SYNCML_SYNCML12)) { /** @todo We suppose that Opaque Data in SyncML messages can only be vCard or vCal documents. CHANGE THAT ! */ if (node->content != NULL) { if (wbxml_buffer_get_cstr(node->content)[0] == 0x0a && wbxml_buffer_len(node->content) == 1) { wbxml_buffer_insert_cstr(node->content, (WB_UTINY*) "\r", 0); } } }#endif /* WBXML_SUPPORT_SYNCML */ /* Add text into CDATA Buffer */ if (!wbxml_buffer_append(encoder->cdata, node->content)) return WBXML_ERROR_ENCODER_APPEND_DATA; return WBXML_OK; } else { /* Encode text */ encoder->current_text_parent = node->parent; ret = wbxml_encode_value_element_buffer(encoder, wbxml_buffer_get_cstr(node->content), WBXML_VALUE_ELEMENT_CTX_CONTENT); encoder->current_text_parent = NULL; return ret; } case WBXML_ENCODER_OUTPUT_XML: return xml_encode_text(encoder, node); default: return WBXML_ERROR_INTERNAL; }}/** * @brief Parse an XML CDATA * @param encoder The WBXML Encoder * @param node The CDATA to parse * @return WBXML_OK if parsing is OK, an error code otherwise */static WBXMLError parse_cdata(WBXMLEncoder *encoder, WBXMLTreeNode *node){ WBXML_DEBUG((WBXML_ENCODER, "CDATA Begin")); /* Keep in mind that we are in a CDATA section */ encoder->in_cdata = TRUE; /* Encode CDATA */ switch (encoder->output_type) { case WBXML_ENCODER_OUTPUT_WBXML: if (encoder->cdata != NULL) { /* Must never happend */ return WBXML_ERROR_INTERNAL; } /* Create a new CDATA Buffer */ if ((encoder->cdata = wbxml_buffer_create("", 0, 0)) == NULL) { return WBXML_ERROR_NOT_ENOUGH_MEMORY; } return WBXML_OK; case WBXML_ENCODER_OUTPUT_XML: return xml_encode_cdata(encoder); default: return WBXML_ERROR_INTERNAL; }}/** * @brief Parse an XML PI * @param encoder The WBXML Encoder * @param node The PI to parse * @return WBXML_OK if parsing is OK, an error code otherwise */static WBXMLError parse_pi(WBXMLEncoder *encoder, WBXMLTreeNode *node){ /** @todo parse_pi() */ return WBXML_ERROR_NOT_IMPLEMENTED;}/** * @brief Parse a WBXML Tree * @param encoder The WBXML Encoder * @param node The WBXML Tree to parse * @return WBXML_OK if parsing is OK, an error code otherwise */static WBXMLError parse_tree(WBXMLEncoder *encoder, WBXMLTreeNode *node){ switch (encoder->output_type) { case WBXML_ENCODER_OUTPUT_WBXML: return wbxml_encode_tree(encoder, node->tree); case WBXML_ENCODER_OUTPUT_XML: return xml_encode_tree(encoder, node->tree); default: return WBXML_ERROR_INTERNAL; }}/***************************************** * WBXML Output Functions *//**************************** * Build WBXML Result *//** * @brief Build WBXML Result * @param encoder [in] The WBXML Encoder * @param wbxml [out] Resulting WBXML document * @param wbxml_len [out] The resulting WBXML document length * @return WBXML_OK if built is OK, an error code otherwise * @note WBXML Header = version publicid charset length */static WBXMLError wbxml_build_result(WBXMLEncoder *encoder, WB_UTINY **wbxml, WB_ULONG *wbxml_len){ WBXMLBuffer *header = NULL; WBXMLError ret = WBXML_OK; if (encoder->flow_mode == TRUE) { /* Header already built */ header = encoder->output_header; } else { /* Create WBXML Header buffer */ if ((header = wbxml_buffer_create("", 0, WBXML_ENCODER_WBXML_HEADER_MALLOC_BLOCK)) == NULL) return WBXML_ERROR_NOT_ENOUGH_MEMORY; /* Fill Header Buffer */ if ((ret = wbxml_fill_header(encoder, header)) != WBXML_OK) { wbxml_buffer_destroy(header); return ret; } } /* Result Buffer Length */ *wbxml_len = wbxml_buffer_len(header) + wbxml_buffer_len(encoder->output); /* Create Result Buffer */ *wbxml = (WB_UTINY *) wbxml_malloc(*wbxml_len * sizeof(WB_UTINY)); if (*wbxml == NULL) { if (encoder->flow_mode == FALSE) wbxml_buffer_destroy(header); *wbxml_len = 0; return WBXML_ERROR_NOT_ENOUGH_MEMORY; } /* Copy WBXML Header */ memcpy(*wbxml, wbxml_buffer_get_cstr(header), wbxml_buffer_len(header)); /* Copy WBXML Buffer */ memcpy(*wbxml + wbxml_buffer_len(header), wbxml_buffer_get_cstr(encoder->output), wbxml_buffer_len(encoder->output)); if (encoder->flow_mode == FALSE) wbxml_buffer_destroy(header); return WBXML_OK;}static WBXMLError wbxml_fill_header(WBXMLEncoder *encoder, WBXMLBuffer *header){ WBXMLBuffer *pid = NULL; WB_ULONG public_id = 0, public_id_index = 0, strstbl_len = 0; WB_BOOL pi_in_strtbl = FALSE; WBXMLError ret = WBXML_OK;#if defined( WBXML_ENCODER_USE_STRTBL ) WBXMLStringTableElement *elt = NULL; WB_BOOL added = FALSE; strstbl_len = encoder->strstbl_len;#endif /* WBXML_ENCODER_USE_STRTBL */ if ((encoder == NULL) || (encoder->lang == NULL) || (encoder->lang->publicID == NULL) || (header == NULL)) return WBXML_ERROR_BAD_PARAMETER; /* WBXML Public ID */ public_id = encoder->lang->publicID->wbxmlPublicID; /* Encode WBXML Version */ if (!wbxml_buffer_append_char(header, (WB_UTINY) encoder->wbxml_version)) return WBXML_ERROR_ENCODER_APPEND_DATA; /* Encode Public ID */ /* If WBXML Public Id is '0x01' (unknown), or we forced it, add the XML Public ID in the String Table */ if (encoder->textual_publicid || (public_id == WBXML_PUBLIC_ID_UNKNOWN)) { if (encoder->lang->publicID->xmlPublicID != NULL) { if ((pid = wbxml_buffer_create(encoder->lang->publicID->xmlPublicID, WBXML_STRLEN(encoder->lang->publicID->xmlPublicID), WBXML_STRLEN(encoder->lang->publicID->xmlPublicID))) == NULL) { return WBXML_ERROR_NOT_ENOUGH_MEMORY; }#if defined( WBXML_ENCODER_USE_STRTBL ) if (encoder->use_strtbl) { if ((elt = wbxml_strtbl_element_create(pid, FALSE)) == NULL) { wbxml_buffer_destroy(pid); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } if (!wbxml_strtbl_add_element(encoder, elt, &public_id_index, &added)) { wbxml_strtbl_element_destroy(elt); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } if (!added) wbxml_strtbl_element_destroy(elt); strstbl_len = encoder->strstbl_len; } else {#endif /* WBXML_ENCODER_USE_STRTBL */ /* Length of String Table is length of XML Public ID */ strstbl_len = wbxml_buffer_len(pid); /* There is only the XML Public ID in String Table */ public_id_index = 0;#if defined( WBXML_ENCODER_USE_STRTBL ) }#endif /* WBXML_ENCODER_USE_STRTBL */ pi_in_strtbl = TRUE; } } /* publicid = mb_u_int32 | ( zero index ) */ if (pi_in_strtbl) { /* Encode XML Public ID String Table index */ if (!wbxml_buffer_append_char(header, 0x00) || !wbxml_buffer_append_mb_uint_32(header, public_id_index)) { return WBXML_ERROR_NOT_ENOUGH_MEMORY; } } else { /* Encode WBXML Public ID */ if (!wbxml_buffer_append_mb_uint_32(header, public_id)) return WBXML_ERROR_ENCODER_APPEND_DATA; } /* Encode Charset (default: UTF-8) and String Table Length */ /** @todo Handle correctly the charset */ if (!wbxml_buffer_append_mb_uint_32(header, WBXML_ENCODER_DEFAULT_CHARSET) || !wbxml_buffer_append_mb_uint_32(header, strstbl_len)) { return WBXML_ERROR_ENCODER_APPEND_DATA; } /* Copy WBXML String Table */#if defined( WBXML_ENCODER_USE_STRTBL ) if (encoder->use_strtbl) { if ((ret = wbxml_strtbl_construct(header, encoder->strstbl)) != WBXML_OK) return ret; } else {#endif /* WBXML_ENCODER_USE_STRTBL */ if (pid != NULL) { /* The append includes terminating NULL char */ if (!wbxml_buffer_append(header, pid)) return WBXML_ERROR_ENCODER_APPEND_DATA; /* Clean up */ wbxml_buffer_destroy(pid); } #if defined( WBXML_ENCODER_USE_STRTBL ) }#endif /* WBXML_ENCODER_USE_STRTBL */ return WBXML_OK;}/**************************** * WBXML Encoding Functions *//** * @brief Encode a WBXML End Token * @param encoder The WBXML Encoder * @return WBXML_OK if encoding is OK, an error code otherwise */static WBXMLError wbxml_encode_end(WBXMLEncoder *encoder){ /* Append END */ if (!wbxml_buffer_append_char(encoder->output, WBXML_END)) return WBXML_ERROR_ENCODER_APPEND_DATA; return WBXML_OK;}/** * @brief Encode a WBXML Tag * @param encoder The WBXML Encoder * @param node The element to encode * @param has_content Does the element has content ? * @return WBXML_OK if encoding is OK, an error code otherwise
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -