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 *****************************************************************************/
30 #include "vlc_block.h"
31 #include "vlc_stream.h"
34 #include <libxml/xmlreader.h>
35 #include <libxml/catalog.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_description( _("XML Parser (using libxml2)") );
45 set_capability( "xml", 10 );
46 set_callbacks( Open, Close );
49 struct xml_reader_sys_t
51 /* libxml2 reader context */
52 xmlTextReaderPtr p_reader;
55 static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
56 static void ReaderDelete( xml_reader_t * );
57 static int ReaderRead( xml_reader_t * );
58 static int ReaderNodeType( xml_reader_t * );
59 static char *ReaderName( xml_reader_t * );
60 static char *ReaderValue( xml_reader_t * );
61 static int ReaderNextAttr( xml_reader_t * );
63 static int ReaderUseDTD ( xml_reader_t *, vlc_bool_t );
65 static void CatalogLoad( xml_t *, const char * );
66 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
67 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
69 /*****************************************************************************
70 * Module initialization
71 *****************************************************************************/
72 static int Open( vlc_object_t *p_this )
74 xml_t *p_xml = (xml_t *)p_this;
76 p_xml->pf_reader_create = ReaderCreate;
77 p_xml->pf_reader_delete = ReaderDelete;
79 p_xml->pf_catalog_load = CatalogLoad;
80 p_xml->pf_catalog_add = CatalogAdd;
85 /*****************************************************************************
86 * Module deinitialization
87 *****************************************************************************/
88 static void Close( vlc_object_t *p_this )
94 /*****************************************************************************
96 *****************************************************************************/
97 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
100 if( !psz_filename ) xmlInitializeCatalog();
101 else xmlLoadCatalog( psz_filename );
104 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
105 const char *psz_arg2, const char *psz_filename )
108 xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
109 (unsigned char*)psz_filename );
112 /*****************************************************************************
114 *****************************************************************************/
115 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
116 xmlParserSeverities severity,
117 xmlTextReaderLocatorPtr locator)
119 VLC_UNUSED(severity);
120 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
121 int line = xmlTextReaderLocatorLineNumber( locator );
122 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
125 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
127 xml_reader_t *p_reader;
128 xml_reader_sys_t *p_sys;
129 xmlTextReaderPtr p_libxml_reader;
131 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
133 if( !p_libxml_reader )
135 msg_Err( p_xml, "failed to create XML parser" );
139 p_reader = malloc( sizeof(xml_reader_t) );
140 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
141 p_reader->p_sys->p_reader = p_libxml_reader;
142 p_reader->p_xml = p_xml;
144 /* Set the error handler */
145 xmlTextReaderSetErrorHandler( p_libxml_reader,
146 ReaderErrorHandler, p_reader );
149 p_reader->pf_read = ReaderRead;
150 p_reader->pf_node_type = ReaderNodeType;
151 p_reader->pf_name = ReaderName;
152 p_reader->pf_value = ReaderValue;
153 p_reader->pf_next_attr = ReaderNextAttr;
154 p_reader->pf_use_dtd = ReaderUseDTD;
159 static void ReaderDelete( xml_reader_t *p_reader )
161 xmlFreeTextReader( p_reader->p_sys->p_reader );
162 free( p_reader->p_sys );
166 static int ReaderUseDTD ( xml_reader_t *p_reader, vlc_bool_t b_use )
168 /* Activate DTD validation */
169 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
170 XML_PARSER_DEFAULTATTRS, b_use );
171 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
172 XML_PARSER_VALIDATE, b_use );
177 static int ReaderRead( xml_reader_t *p_reader )
179 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
191 static int ReaderNodeType( xml_reader_t *p_reader )
193 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
197 case XML_READER_TYPE_ELEMENT:
198 i_ret = XML_READER_STARTELEM;
200 case XML_READER_TYPE_END_ELEMENT:
201 i_ret = XML_READER_ENDELEM;
203 case XML_READER_TYPE_CDATA:
204 case XML_READER_TYPE_TEXT:
205 i_ret = XML_READER_TEXT;
211 i_ret = XML_READER_NONE;
218 static char *ReaderName( xml_reader_t *p_reader )
220 const xmlChar *psz_name =
221 xmlTextReaderConstName( p_reader->p_sys->p_reader );
223 if( psz_name ) return strdup( (const char *)psz_name );
227 static char *ReaderValue( xml_reader_t *p_reader )
229 const xmlChar *psz_value =
230 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
232 if( psz_value ) return strdup( (const char *)psz_value );
236 static int ReaderNextAttr( xml_reader_t *p_reader )
238 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
239 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
242 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
244 stream_t *s = (stream_t*)p_context;
245 return stream_Read( s, p_buffer, i_buffer );