1 /*****************************************************************************
2 * libxml.c: XML parser using libxml2
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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_category( CAT_ADVANCED );
42 set_subcategory( SUBCAT_ADVANCED_XML );
43 set_description( _("XML Parser (using libxml2)") );
44 set_capability( "xml", 10 );
45 set_callbacks( Open, Close );
48 struct xml_reader_sys_t
50 /* libxml2 reader context */
51 xmlTextReaderPtr p_reader;
54 static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
55 static void ReaderDelete( xml_reader_t * );
56 static int ReaderRead( xml_reader_t * );
57 static int ReaderNodeType( xml_reader_t * );
58 static char *ReaderName( xml_reader_t * );
59 static char *ReaderValue( xml_reader_t * );
60 static int ReaderNextAttr( xml_reader_t * );
62 static int ReaderUseDTD ( xml_reader_t *, vlc_bool_t );
64 static void CatalogLoad( xml_t *, const char * );
65 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
66 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
68 /*****************************************************************************
69 * Module initialization
70 *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
73 xml_t *p_xml = (xml_t *)p_this;
75 p_xml->pf_reader_create = ReaderCreate;
76 p_xml->pf_reader_delete = ReaderDelete;
78 p_xml->pf_catalog_load = CatalogLoad;
79 p_xml->pf_catalog_add = CatalogAdd;
84 /*****************************************************************************
85 * Module deinitialization
86 *****************************************************************************/
87 static void Close( vlc_object_t *p_this )
92 /*****************************************************************************
94 *****************************************************************************/
95 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
97 if( !psz_filename ) xmlInitializeCatalog();
98 else xmlLoadCatalog( psz_filename );
101 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
102 const char *psz_arg2, const char *psz_filename )
104 xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
107 /*****************************************************************************
109 *****************************************************************************/
110 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
111 xmlParserSeverities severity,
112 xmlTextReaderLocatorPtr locator)
114 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
115 int line = xmlTextReaderLocatorLineNumber( locator );
116 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
119 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
121 xml_reader_t *p_reader;
122 xml_reader_sys_t *p_sys;
123 xmlTextReaderPtr p_libxml_reader;
124 xmlParserInputBufferPtr p_read_context;
126 p_read_context = malloc( sizeof( xmlParserInputBuffer ) );
128 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
130 if( !p_libxml_reader )
132 msg_Err( p_xml, "failed to create xml parser" );
136 p_reader = malloc( sizeof(xml_reader_t) );
137 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
138 p_reader->p_sys->p_reader = p_libxml_reader;
139 p_reader->p_xml = p_xml;
141 /* Set the error handler */
142 xmlTextReaderSetErrorHandler( p_libxml_reader,
143 ReaderErrorHandler, p_reader );
146 p_reader->pf_read = ReaderRead;
147 p_reader->pf_node_type = ReaderNodeType;
148 p_reader->pf_name = ReaderName;
149 p_reader->pf_value = ReaderValue;
150 p_reader->pf_next_attr = ReaderNextAttr;
151 p_reader->pf_use_dtd = ReaderUseDTD;
156 static void ReaderDelete( xml_reader_t *p_reader )
158 xmlFreeTextReader( p_reader->p_sys->p_reader );
159 free( p_reader->p_sys );
163 static int ReaderUseDTD ( xml_reader_t *p_reader, vlc_bool_t b_use )
165 /* Activate DTD validation */
166 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
167 XML_PARSER_DEFAULTATTRS, b_use );
168 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
169 XML_PARSER_VALIDATE, b_use );
174 static int ReaderRead( xml_reader_t *p_reader )
176 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
188 static int ReaderNodeType( xml_reader_t *p_reader )
190 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
194 case XML_READER_TYPE_ELEMENT:
195 i_ret = XML_READER_STARTELEM;
197 case XML_READER_TYPE_END_ELEMENT:
198 i_ret = XML_READER_ENDELEM;
204 i_ret = XML_READER_NONE;
211 static char *ReaderName( xml_reader_t *p_reader )
213 const xmlChar *psz_name =
214 xmlTextReaderConstName( p_reader->p_sys->p_reader );
216 if( psz_name ) return strdup( psz_name );
220 static char *ReaderValue( xml_reader_t *p_reader )
222 const xmlChar *psz_value =
223 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
225 if( psz_value ) return strdup( psz_value );
229 static int ReaderNextAttr( xml_reader_t *p_reader )
231 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
232 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
235 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
237 stream_t *s = (stream_t*)p_context;
238 return stream_Read( s, p_buffer, i_buffer );