1 /*****************************************************************************
2 * libxml.c: XML parser using libxml2
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #include "vlc_block.h"
28 #include "vlc_stream.h"
31 #include <libxml/xmlreader.h>
32 #include <libxml/catalog.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
41 set_description( _("XML Parser (using libxml2)") );
42 set_capability( "xml", 10 );
43 set_callbacks( Open, Close );
46 struct xml_reader_sys_t
48 /* libxml2 reader context */
49 xmlTextReaderPtr p_reader;
52 static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
53 static void ReaderDelete( xml_reader_t * );
54 static int ReaderRead( xml_reader_t * );
55 static int ReaderNodeType( xml_reader_t * );
56 static char *ReaderName( xml_reader_t * );
57 static char *ReaderValue( xml_reader_t * );
58 static int ReaderNextAttr( xml_reader_t * );
60 static int ReaderUseDTD ( xml_reader_t *, vlc_bool_t );
62 static void CatalogLoad( xml_t *, const char * );
63 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
64 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
66 /*****************************************************************************
67 * Module initialization
68 *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
71 xml_t *p_xml = (xml_t *)p_this;
73 p_xml->pf_reader_create = ReaderCreate;
74 p_xml->pf_reader_delete = ReaderDelete;
76 p_xml->pf_catalog_load = CatalogLoad;
77 p_xml->pf_catalog_add = CatalogAdd;
82 /*****************************************************************************
83 * Module deinitialization
84 *****************************************************************************/
85 static void Close( vlc_object_t *p_this )
90 /*****************************************************************************
92 *****************************************************************************/
93 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
95 if( !psz_filename ) xmlInitializeCatalog();
96 else xmlLoadCatalog( psz_filename );
99 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
100 const char *psz_arg2, const char *psz_filename )
102 xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
105 /*****************************************************************************
107 *****************************************************************************/
108 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
109 xmlParserSeverities severity,
110 xmlTextReaderLocatorPtr locator)
112 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
113 int line = xmlTextReaderLocatorLineNumber( locator );
114 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
117 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
119 xml_reader_t *p_reader;
120 xml_reader_sys_t *p_sys;
121 xmlTextReaderPtr p_libxml_reader;
122 xmlParserInputBufferPtr p_read_context;
124 p_read_context = malloc( sizeof( xmlParserInputBuffer ) );
126 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
128 if( !p_libxml_reader )
130 msg_Err( p_xml, "failed to create xml parser" );
134 p_reader = malloc( sizeof(xml_reader_t) );
135 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
136 p_reader->p_sys->p_reader = p_libxml_reader;
137 p_reader->p_xml = p_xml;
139 /* Set the error handler */
140 xmlTextReaderSetErrorHandler( p_libxml_reader,
141 ReaderErrorHandler, p_reader );
144 p_reader->pf_read = ReaderRead;
145 p_reader->pf_node_type = ReaderNodeType;
146 p_reader->pf_name = ReaderName;
147 p_reader->pf_value = ReaderValue;
148 p_reader->pf_next_attr = ReaderNextAttr;
149 p_reader->pf_use_dtd = ReaderUseDTD;
154 static void ReaderDelete( xml_reader_t *p_reader )
156 xmlFreeTextReader( p_reader->p_sys->p_reader );
157 free( p_reader->p_sys );
161 static int ReaderUseDTD ( xml_reader_t *p_reader, vlc_bool_t b_use )
163 /* Activate DTD validation */
164 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
165 XML_PARSER_DEFAULTATTRS, b_use );
166 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
167 XML_PARSER_VALIDATE, b_use );
172 static int ReaderRead( xml_reader_t *p_reader )
174 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
186 static int ReaderNodeType( xml_reader_t *p_reader )
188 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
192 case XML_READER_TYPE_ELEMENT:
193 i_ret = XML_READER_STARTELEM;
195 case XML_READER_TYPE_END_ELEMENT:
196 i_ret = XML_READER_ENDELEM;
198 case XML_READER_TYPE_CDATA:
199 case XML_READER_TYPE_TEXT:
200 i_ret = XML_READER_TEXT;
206 i_ret = XML_READER_NONE;
213 static char *ReaderName( xml_reader_t *p_reader )
215 const xmlChar *psz_name =
216 xmlTextReaderConstName( p_reader->p_sys->p_reader );
218 if( psz_name ) return strdup( psz_name );
222 static char *ReaderValue( xml_reader_t *p_reader )
224 const xmlChar *psz_value =
225 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
227 if( psz_value ) return strdup( psz_value );
231 static int ReaderNextAttr( xml_reader_t *p_reader )
233 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
234 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
237 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
239 stream_t *s = (stream_t*)p_context;
240 return stream_Read( s, p_buffer, i_buffer );