Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

libxml.c

00001 /*****************************************************************************
00002  * libxml.c: XML parser using libxml2
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: libxml.c 12783 2005-10-08 15:35:18Z dionoea $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #include <stdlib.h>
00025 #include <vlc/vlc.h>
00026 
00027 #include "vlc_block.h"
00028 #include "vlc_stream.h"
00029 #include "vlc_xml.h"
00030 
00031 #include <libxml/xmlreader.h>
00032 #include <libxml/catalog.h>
00033 
00034 /*****************************************************************************
00035  * Module descriptor
00036  *****************************************************************************/
00037 static int  Open ( vlc_object_t * );
00038 static void Close( vlc_object_t * );
00039 
00040 vlc_module_begin();
00041     set_category( CAT_ADVANCED );
00042     set_subcategory( SUBCAT_ADVANCED_XML );
00043     set_description( _("XML Parser (using libxml2)") );
00044     set_capability( "xml", 10 );
00045     set_callbacks( Open, Close );
00046 vlc_module_end();
00047 
00048 struct xml_reader_sys_t
00049 {
00050     /* libxml2 reader context */
00051     xmlTextReaderPtr p_reader;
00052 };
00053 
00054 static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
00055 static void ReaderDelete( xml_reader_t * );
00056 static int ReaderRead( xml_reader_t * );
00057 static int ReaderNodeType( xml_reader_t * );
00058 static char *ReaderName( xml_reader_t * );
00059 static char *ReaderValue( xml_reader_t * );
00060 static int ReaderNextAttr( xml_reader_t * );
00061 
00062 static int ReaderUseDTD ( xml_reader_t *, vlc_bool_t );
00063 
00064 static void CatalogLoad( xml_t *, const char * );
00065 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
00066 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
00067 
00068 /*****************************************************************************
00069  * Module initialization
00070  *****************************************************************************/
00071 static int Open( vlc_object_t *p_this )
00072 {
00073     xml_t *p_xml = (xml_t *)p_this;
00074 
00075     p_xml->pf_reader_create = ReaderCreate;
00076     p_xml->pf_reader_delete = ReaderDelete;
00077 
00078     p_xml->pf_catalog_load = CatalogLoad;
00079     p_xml->pf_catalog_add  = CatalogAdd;
00080 
00081     return VLC_SUCCESS;
00082 }
00083 
00084 /*****************************************************************************
00085  * Module deinitialization
00086  *****************************************************************************/
00087 static void Close( vlc_object_t *p_this )
00088 {
00089     return;
00090 }
00091 
00092 /*****************************************************************************
00093  * Catalogue functions
00094  *****************************************************************************/
00095 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
00096 {
00097     if( !psz_filename ) xmlInitializeCatalog();
00098     else xmlLoadCatalog( psz_filename );
00099 }
00100 
00101 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
00102                           const char *psz_arg2, const char *psz_filename )
00103 {
00104     xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
00105 }
00106 
00107 /*****************************************************************************
00108  * Reader functions
00109  *****************************************************************************/
00110 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
00111                                 xmlParserSeverities severity,
00112                                 xmlTextReaderLocatorPtr locator)
00113 {
00114     xml_reader_t *p_reader = (xml_reader_t *)p_arg;
00115     int line = xmlTextReaderLocatorLineNumber( locator );
00116     msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
00117 }
00118 
00119 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
00120 {
00121     xml_reader_t *p_reader;
00122     xml_reader_sys_t *p_sys;
00123     xmlTextReaderPtr p_libxml_reader;
00124     xmlParserInputBufferPtr p_read_context;
00125 
00126     p_read_context = malloc( sizeof( xmlParserInputBuffer ) );
00127 
00128     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
00129                                       NULL, NULL, 0 );
00130     if( !p_libxml_reader )
00131     {
00132         msg_Err( p_xml, "failed to create xml parser" );
00133         return 0;
00134     }
00135 
00136     p_reader = malloc( sizeof(xml_reader_t) );
00137     p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
00138     p_reader->p_sys->p_reader = p_libxml_reader;
00139     p_reader->p_xml = p_xml;
00140 
00141     /* Set the error handler */
00142     xmlTextReaderSetErrorHandler( p_libxml_reader,
00143                                   ReaderErrorHandler, p_reader );
00144 
00145 
00146     p_reader->pf_read = ReaderRead;
00147     p_reader->pf_node_type = ReaderNodeType;
00148     p_reader->pf_name = ReaderName;
00149     p_reader->pf_value = ReaderValue;
00150     p_reader->pf_next_attr = ReaderNextAttr;
00151     p_reader->pf_use_dtd = ReaderUseDTD;
00152 
00153     return p_reader;
00154 }
00155 
00156 static void ReaderDelete( xml_reader_t *p_reader )
00157 {
00158     xmlFreeTextReader( p_reader->p_sys->p_reader );
00159     free( p_reader->p_sys );
00160     free( p_reader );
00161 }
00162 
00163 static int ReaderUseDTD ( xml_reader_t *p_reader, vlc_bool_t b_use )
00164 {
00165     /* Activate DTD validation */
00166     xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
00167                                 XML_PARSER_DEFAULTATTRS, b_use );
00168     xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
00169                                 XML_PARSER_VALIDATE, b_use );
00170 
00171     return VLC_SUCCESS;
00172 }
00173 
00174 static int ReaderRead( xml_reader_t *p_reader )
00175 {
00176     int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
00177 
00178 #if 0
00179     switch( i_ret )
00180     {
00181     default:
00182     }
00183 #endif
00184 
00185     return i_ret;
00186 }
00187 
00188 static int ReaderNodeType( xml_reader_t *p_reader )
00189 {
00190     int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
00191 
00192     switch( i_ret )
00193     {
00194     case XML_READER_TYPE_ELEMENT:
00195         i_ret = XML_READER_STARTELEM;
00196         break;
00197     case XML_READER_TYPE_END_ELEMENT:
00198         i_ret = XML_READER_ENDELEM;
00199         break;
00200     case XML_READER_TYPE_CDATA:
00201     case XML_READER_TYPE_TEXT:
00202         i_ret = XML_READER_TEXT;
00203         break;
00204     case -1:
00205         i_ret = -1;
00206         break;
00207     default:
00208         i_ret = XML_READER_NONE;
00209         break;
00210     }
00211 
00212     return i_ret;
00213 }
00214 
00215 static char *ReaderName( xml_reader_t *p_reader )
00216 {
00217     const xmlChar *psz_name =
00218         xmlTextReaderConstName( p_reader->p_sys->p_reader );
00219 
00220     if( psz_name ) return strdup( psz_name );
00221     else return 0;
00222 }
00223 
00224 static char *ReaderValue( xml_reader_t *p_reader )
00225 {
00226     const xmlChar *psz_value =
00227         xmlTextReaderConstValue( p_reader->p_sys->p_reader );
00228 
00229     if( psz_value ) return strdup( psz_value );
00230     else return 0;
00231 }
00232 
00233 static int ReaderNextAttr( xml_reader_t *p_reader )
00234 {
00235     return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
00236              == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
00237 }
00238 
00239 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
00240 {
00241     stream_t *s = (stream_t*)p_context;
00242     return stream_Read( s, p_buffer, i_buffer );    
00243 }

Generated on Tue Dec 20 10:14:49 2005 for vlc-0.8.4a by  doxygen 1.4.2