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_description( _("XML Parser (using libxml2)") );
40 set_capability( "xml", 10 );
41 set_callbacks( Open, Close );
44 struct xml_reader_sys_t
46 /* libxml2 reader context */
47 xmlTextReaderPtr p_reader;
50 static xml_reader_t *ReaderCreate( xml_t *, const char * );
51 static void ReaderDelete( xml_reader_t * );
52 static int ReaderRead( xml_reader_t * );
53 static int ReaderNodeType( xml_reader_t * );
54 static char *ReaderName( xml_reader_t * );
55 static char *ReaderValue( xml_reader_t * );
56 static int ReaderNextAttr( xml_reader_t * );
58 static void CatalogLoad( xml_t *, const char * );
59 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
61 /*****************************************************************************
62 * Module initialization
63 *****************************************************************************/
64 static int Open( vlc_object_t *p_this )
66 xml_t *p_xml = (xml_t *)p_this;
68 p_xml->pf_reader_create = ReaderCreate;
69 p_xml->pf_reader_delete = ReaderDelete;
71 p_xml->pf_catalog_load = CatalogLoad;
72 p_xml->pf_catalog_add = CatalogAdd;
77 /*****************************************************************************
78 * Module deinitialization
79 *****************************************************************************/
80 static void Close( vlc_object_t *p_this )
85 /*****************************************************************************
87 *****************************************************************************/
88 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
90 if( !psz_filename ) xmlInitializeCatalog();
91 else xmlLoadCatalog( psz_filename );
94 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
95 const char *psz_arg2, const char *psz_filename )
97 xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
100 /*****************************************************************************
102 *****************************************************************************/
103 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
104 xmlParserSeverities severity,
105 xmlTextReaderLocatorPtr locator)
107 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
108 int line = xmlTextReaderLocatorLineNumber( locator );
109 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
112 static xml_reader_t *ReaderCreate( xml_t *p_xml, const char *psz_filename )
114 xml_reader_t *p_reader;
115 xml_reader_sys_t *p_sys;
116 xmlTextReaderPtr p_libxml_reader;
118 p_libxml_reader = xmlNewTextReaderFilename( psz_filename );
119 if( !p_libxml_reader )
121 msg_Err( p_xml, "failed to open file %s for parsing", psz_filename );
125 p_reader = malloc( sizeof(xml_reader_t) );
126 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
127 p_reader->p_sys->p_reader = p_libxml_reader;
128 p_reader->p_xml = p_xml;
130 /* Activate DTD validation */
131 xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_DEFAULTATTRS, 1 );
132 xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_VALIDATE, 1 );
134 /* Set the error handler */
135 xmlTextReaderSetErrorHandler( p_libxml_reader,
136 ReaderErrorHandler, p_reader );
139 p_reader->pf_read = ReaderRead;
140 p_reader->pf_node_type = ReaderNodeType;
141 p_reader->pf_name = ReaderName;
142 p_reader->pf_value = ReaderValue;
143 p_reader->pf_next_attr = ReaderNextAttr;
148 static void ReaderDelete( xml_reader_t *p_reader )
150 xmlFreeTextReader( p_reader->p_sys->p_reader );
151 free( p_reader->p_sys );
155 static int ReaderRead( xml_reader_t *p_reader )
157 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
169 static int ReaderNodeType( xml_reader_t *p_reader )
171 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
175 case XML_READER_TYPE_ELEMENT:
176 i_ret = XML_READER_STARTELEM;
178 case XML_READER_TYPE_END_ELEMENT:
179 i_ret = XML_READER_ENDELEM;
185 i_ret = XML_READER_NONE;
192 static char *ReaderName( xml_reader_t *p_reader )
194 const xmlChar *psz_name =
195 xmlTextReaderConstName( p_reader->p_sys->p_reader );
197 if( psz_name ) return strdup( psz_name );
201 static char *ReaderValue( xml_reader_t *p_reader )
203 const xmlChar *psz_value =
204 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
206 if( psz_value ) return strdup( psz_value );
210 static int ReaderNextAttr( xml_reader_t *p_reader )
212 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
213 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;