示例#1
0
nbool wbxmlDecoder_decode(NWbxmlDecoder* decoder)
{
    nbool result = N_FALSE;
    WBXMLParser *wbxml_parser = NULL;
    WBXMLContentHandler parse_handler = {
        NULL,
        NULL,
        wbxml_start_element,
        wbxml_end_element,
        wbxml_characters,
        NULL};
    
    if (decoder->srcPos <= 0)
        return N_FALSE;

    wbxml_parser = wbxml_parser_create();
    if (wbxml_parser) {
        WBXMLError ret = WBXML_OK;
        
        decoder->dstMax = decoder->srcMax * 2;
        decoder->dst = (char*)NBK_malloc(decoder->dstMax);
        decoder->dstPos = 0;
        
#if DEBUG_DECODER
        dump_char(decoder->page, "decoding...", -1);
        dump_return(decoder->page);
#endif
        wbxml_parser_set_user_data(wbxml_parser, decoder);
        wbxml_parser_set_content_handler(wbxml_parser, &parse_handler);
        ret = wbxml_parser_parse(wbxml_parser, decoder->src, decoder->srcPos);
        wbxml_parser_destroy(wbxml_parser);

#if DEBUG_DECODER
        dump_char(decoder->page, "decoded!", -1);
        dump_int(decoder->page, decoder->srcPos);
        dump_int(decoder->page, decoder->dstPos);
        dump_return(decoder->page);
#endif
        
        if (ret == WBXML_OK)
            result = N_TRUE;
    }
    
    return result;
}
示例#2
0
WBXML_DECLARE(WBXMLError) wbxml_tree_from_wbxml(WB_UTINY *wbxml, WB_ULONG wbxml_len, WBXMLLanguage lang, WBXMLTree **tree)
{
    WBXMLParser *wbxml_parser = NULL;
    WB_LONG error_index = 0;
    WBXMLTreeClbCtx wbxml_tree_clb_ctx;
    WBXMLError ret = WBXML_OK;
    WBXMLContentHandler wbxml_tree_content_handler = 
        {
            wbxml_tree_clb_wbxml_start_document,
            wbxml_tree_clb_wbxml_end_document,
            wbxml_tree_clb_wbxml_start_element,
            wbxml_tree_clb_wbxml_end_element,
            wbxml_tree_clb_wbxml_characters,
            wbxml_tree_clb_wbxml_pi
        };

    if (tree != NULL)
        *tree = NULL;

    /* Create WBXML Parser */
    if((wbxml_parser = wbxml_parser_create()) == NULL) {
        WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Parser"));
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;
    }

    /* Init context */
    wbxml_tree_clb_ctx.error = WBXML_OK;
    wbxml_tree_clb_ctx.current = NULL;
    if ((wbxml_tree_clb_ctx.tree = wbxml_tree_create(WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN)) == NULL) {
        wbxml_parser_destroy(wbxml_parser);
        WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Tree"));
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;
    }
    
    /* Set Handlers Callbacks */
    wbxml_parser_set_user_data(wbxml_parser, &wbxml_tree_clb_ctx);
    wbxml_parser_set_content_handler(wbxml_parser, &wbxml_tree_content_handler);

    /* Give the user the possibility to force Document Language */
    if (lang != WBXML_LANG_UNKNOWN)
        wbxml_parser_set_language(wbxml_parser, lang);

    /** @todo Use wbxml_parser_set_meta_charset() */

    /* Parse the WBXML document to WBXML Tree */
    ret = wbxml_parser_parse(wbxml_parser, wbxml, wbxml_len);
    if ((ret != WBXML_OK) || (wbxml_tree_clb_ctx.error != WBXML_OK)) 
    {
        error_index = wbxml_parser_get_current_byte_index(wbxml_parser);
        WBXML_ERROR((WBXML_PARSER, "WBXML Parser failed at %ld - token: %x (%s)", 
                                   error_index,
                                   wbxml[error_index],
                                   ret != WBXML_OK ? wbxml_errors_string(ret) : wbxml_errors_string(wbxml_tree_clb_ctx.error)));
        
        wbxml_tree_destroy(wbxml_tree_clb_ctx.tree);
    }
    else {
        *tree = wbxml_tree_clb_ctx.tree;
    }

    /* Clean-up */
    wbxml_parser_destroy(wbxml_parser);

    if (ret != WBXML_OK)
        return ret;
    else
        return wbxml_tree_clb_ctx.error;
}