Main Page   Modules   Compound List   File List   Compound Members   File Members   Related Pages  

wbxml_conv.c

Go to the documentation of this file.
00001 /*
00002  * WBXML Lib, the WBXML Library.
00003  * Copyright (C) 2002-2003  Aymerick Jéhanne
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License (version 2.1) as published by the Free Software Foundation.
00008  * 
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * LGPL v2.1: http://www.gnu.org/licenses/lgpl.txt
00019  *
00020  * Author Contact: libwbxml@jehanne.org
00021  * WBXML Lib home: http://libwbxml.jehanne.org
00022  */
00023  
00034 #include <string.h> /* For memcpy() and strlen() */
00035 
00036 #include "wbxml.h"
00037 
00038 
00039 /************************************
00040  * Public Functions
00041  */
00042 
00043 WBXML_DECLARE(WBXMLError) wbxml_conv_wbxml2xml(WB_UTINY *wbxml, WB_ULONG wbxml_len, WB_UTINY **xml, WBXMLConvWBXML2XMLParams *params)
00044 {
00045     WBXMLEncoder *wbxml_encoder = NULL;
00046     WBXMLTree *wbxml_tree = NULL;
00047     WBXMLError ret = WBXML_OK;
00048 
00049     /* Check Parameters */
00050     if ((wbxml == NULL) || (wbxml_len == 0) || (xml == NULL))
00051         return WBXML_ERROR_BAD_PARAMETER;
00052 
00053     *xml = NULL;
00054 
00055     /* Parse WBXML to WBXML Tree */
00056     ret = wbxml_tree_from_wbxml(wbxml, wbxml_len, params ? params->lang : WBXML_LANG_UNKNOWN, &wbxml_tree);
00057     if (ret != WBXML_OK) {
00058         WBXML_ERROR((WBXML_CONV, "wbxml2xml convertion failed - WBXML Parser Error: %s",
00059                                  wbxml_errors_string(ret)));
00060 
00061         return ret;
00062     }
00063     else {
00064         /* Create WBXML Encoder */
00065         if ((wbxml_encoder = wbxml_encoder_create()) == NULL) {
00066             wbxml_tree_destroy(wbxml_tree);
00067             return WBXML_ERROR_NOT_ENOUGH_MEMORY;
00068         }
00069 
00070         /* Set the WBXML Tree to encode */
00071         wbxml_encoder_set_tree(wbxml_encoder, wbxml_tree);
00072 
00073         /* Set encoder parameters */
00074         if (params == NULL) {
00075             /* Default Values */
00076 
00077             /* Set XML Generation Type */
00078             wbxml_encoder_set_xml_gen_type(wbxml_encoder, WBXML_ENCODER_XML_GEN_INDENT);
00079 
00080             /* Set Indent */
00081             wbxml_encoder_set_indent(wbxml_encoder, 0);
00082 
00083             /* Skip Ignorable Whitespaces */
00084             wbxml_encoder_set_ignore_empty_text(wbxml_encoder, TRUE);
00085             wbxml_encoder_set_remove_text_blanks(wbxml_encoder, TRUE);
00086         }
00087         else {
00088             /* Set XML Generation Type */
00089             wbxml_encoder_set_xml_gen_type(wbxml_encoder, params->gen_type);
00090 
00091             /* Set Indent */
00092             if (params->gen_type == WBXML_ENCODER_XML_GEN_INDENT)
00093                 wbxml_encoder_set_indent(wbxml_encoder, params->indent);
00094 
00095             /* Ignorable Whitespaces */
00096             if (params->keep_ignorable_ws) {
00097                 wbxml_encoder_set_ignore_empty_text(wbxml_encoder, FALSE);
00098                 wbxml_encoder_set_remove_text_blanks(wbxml_encoder, FALSE);
00099             }
00100             else {
00101                 wbxml_encoder_set_ignore_empty_text(wbxml_encoder, TRUE);
00102                 wbxml_encoder_set_remove_text_blanks(wbxml_encoder, TRUE);
00103             }
00104         }
00105 
00106         /* Encode WBXML Tree to XML */
00107         ret = wbxml_encoder_encode_to_xml(wbxml_encoder, xml);
00108         if (ret != WBXML_OK) {
00109             WBXML_ERROR((WBXML_CONV, "wbxml2xml convertion failed - WBXML Encoder Error: %s",
00110                                      wbxml_errors_string(ret)));
00111         }
00112 
00113         /* Clean-up */
00114         wbxml_encoder_destroy(wbxml_encoder);
00115         wbxml_tree_destroy(wbxml_tree);
00116         return ret;
00117     }
00118 }
00119 
00120 
00121 WBXML_DECLARE(WBXMLError) wbxml_conv_xml2wbxml(WB_UTINY *xml, WB_UTINY **wbxml, WB_ULONG *wbxml_len, WBXMLConvXML2WBXMLParams *params)
00122 {
00123 #if !defined( HAVE_EXPAT )
00124 
00125     return WBXML_ERROR_NO_XMLPARSER;
00126 
00127 #else /* HAVE_EXPAT */
00128 
00129     WBXMLEncoder *wbxml_encoder = NULL;    
00130     WBXMLTree *wbxml_tree = NULL;
00131     WBXMLError ret = WBXML_OK;
00132 
00133     /* Check Parameters */
00134     if ((xml == NULL) || (wbxml == NULL) || (wbxml_len == NULL))
00135         return WBXML_ERROR_BAD_PARAMETER;
00136 
00137     *wbxml = NULL;
00138     *wbxml_len = 0;
00139     
00140     /* Parse XML to WBXML Tree */
00141     ret = wbxml_tree_from_xml(xml, &wbxml_tree);
00142     if (ret != WBXML_OK) {
00143         WBXML_ERROR((WBXML_CONV, "xml2wbxml convertion failed - Error: %s",
00144                                   wbxml_errors_string(ret)));
00145 
00146         return ret;
00147     }
00148     else {
00149         /* Encode WBXML Tree to WBXML Document */
00150         if ((wbxml_encoder = wbxml_encoder_create()) == NULL) {
00151             wbxml_tree_destroy(wbxml_tree);
00152             return WBXML_ERROR_NOT_ENOUGH_MEMORY;
00153         }
00154     
00155         /* Set the WBXML Tree to encode */
00156         wbxml_encoder_set_tree(wbxml_encoder, wbxml_tree);
00157 
00158         /* Set encoder parameters */
00159         if (params == NULL) {
00160             /* Default Parameters */
00161 
00162             /* Ignores "Empty Text" Nodes */
00163             wbxml_encoder_set_ignore_empty_text(wbxml_encoder, TRUE);
00164 
00165             /* Remove leading and trailing whitespaces in "Text Nodes" */
00166             wbxml_encoder_set_remove_text_blanks(wbxml_encoder, TRUE);
00167 
00168             /* Use String Table */
00169             wbxml_encoder_set_use_strtbl(wbxml_encoder, TRUE);
00170         }
00171         else {
00172             /* WBXML Version */
00173             wbxml_encoder_set_wbxml_version(wbxml_encoder, params->wbxml_version);
00174             
00175             /* Keep Ignorable Whitespaces ? */
00176             if (!params->keep_ignorable_ws) {
00177                 /* Ignores "Empty Text" Nodes */
00178                 wbxml_encoder_set_ignore_empty_text(wbxml_encoder, TRUE);
00179 
00180                 /* Remove leading and trailing whitespaces in "Text Nodes" */
00181                 wbxml_encoder_set_remove_text_blanks(wbxml_encoder, TRUE);
00182             }
00183 
00184             /* String Table */
00185             wbxml_encoder_set_use_strtbl(wbxml_encoder, params->use_strtbl);
00186         }
00187 
00188         /* Encode WBXML */
00189         ret = wbxml_encoder_encode_to_wbxml(wbxml_encoder, wbxml, wbxml_len);
00190 
00191         /* Clean-up */
00192         wbxml_tree_destroy(wbxml_tree);
00193         wbxml_encoder_destroy(wbxml_encoder);
00194 
00195         return ret;
00196     }
00197 
00198 #endif /* HAVE_EXPAT */
00199 }

Generated on Mon Nov 24 20:09:42 2003 for WBXML Library by doxygen1.3-rc1