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 *****************************************************************************/
29 #include <libxml/xmlreader.h>
30 #include <libxml/catalog.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
39 set_category( CAT_ADVANCED );
40 set_subcategory( SUBCAT_ADVANCED_XML );
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 void CatalogLoad( xml_t *, const char * );
61 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
62 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
64 /*****************************************************************************
65 * Module initialization
66 *****************************************************************************/
67 static int Open( vlc_object_t *p_this )
69 xml_t *p_xml = (xml_t *)p_this;
71 p_xml->pf_reader_create = ReaderCreate;
72 p_xml->pf_reader_delete = ReaderDelete;
74 p_xml->pf_catalog_load = CatalogLoad;
75 p_xml->pf_catalog_add = CatalogAdd;
80 /*****************************************************************************
81 * Module deinitialization
82 *****************************************************************************/
83 static void Close( vlc_object_t *p_this )
88 /*****************************************************************************
90 *****************************************************************************/
91 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
93 if( !psz_filename ) xmlInitializeCatalog();
94 else xmlLoadCatalog( psz_filename );
97 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
98 const char *psz_arg2, const char *psz_filename )
100 xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
103 /*****************************************************************************
105 *****************************************************************************/
106 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
107 xmlParserSeverities severity,
108 xmlTextReaderLocatorPtr locator)
110 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
111 int line = xmlTextReaderLocatorLineNumber( locator );
112 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
115 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
117 xml_reader_t *p_reader;
118 xml_reader_sys_t *p_sys;
119 xmlTextReaderPtr p_libxml_reader;
120 xmlParserInputBufferPtr p_read_context;
122 p_read_context = malloc( sizeof( xmlParserInputBuffer ) );
124 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
126 if( !p_libxml_reader )
128 msg_Err( p_xml, "failed to create xml parser" );
132 p_reader = malloc( sizeof(xml_reader_t) );
133 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
134 p_reader->p_sys->p_reader = p_libxml_reader;
135 p_reader->p_xml = p_xml;
137 /* Activate DTD validation */
138 xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_DEFAULTATTRS, 1 );
139 xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_VALIDATE, 1 );
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;
155 static void ReaderDelete( xml_reader_t *p_reader )
157 xmlFreeTextReader( p_reader->p_sys->p_reader );
158 free( p_reader->p_sys );
162 static int ReaderRead( xml_reader_t *p_reader )
164 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
176 static int ReaderNodeType( xml_reader_t *p_reader )
178 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
182 case XML_READER_TYPE_ELEMENT:
183 i_ret = XML_READER_STARTELEM;
185 case XML_READER_TYPE_END_ELEMENT:
186 i_ret = XML_READER_ENDELEM;
192 i_ret = XML_READER_NONE;
199 static char *ReaderName( xml_reader_t *p_reader )
201 const xmlChar *psz_name =
202 xmlTextReaderConstName( p_reader->p_sys->p_reader );
204 if( psz_name ) return strdup( psz_name );
208 static char *ReaderValue( xml_reader_t *p_reader )
210 const xmlChar *psz_value =
211 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
213 if( psz_value ) return strdup( psz_value );
217 static int ReaderNextAttr( xml_reader_t *p_reader )
219 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
220 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
223 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
225 stream_t *s = (stream_t*)p_context;
226 return stream_Read( s, p_buffer, i_buffer );