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 *, const char * );
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 * );
63 /*****************************************************************************
64 * Module initialization
65 *****************************************************************************/
66 static int Open( vlc_object_t *p_this )
68 xml_t *p_xml = (xml_t *)p_this;
70 p_xml->pf_reader_create = ReaderCreate;
71 p_xml->pf_reader_delete = ReaderDelete;
73 p_xml->pf_catalog_load = CatalogLoad;
74 p_xml->pf_catalog_add = CatalogAdd;
79 /*****************************************************************************
80 * Module deinitialization
81 *****************************************************************************/
82 static void Close( vlc_object_t *p_this )
87 /*****************************************************************************
89 *****************************************************************************/
90 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
92 if( !psz_filename ) xmlInitializeCatalog();
93 else xmlLoadCatalog( psz_filename );
96 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
97 const char *psz_arg2, const char *psz_filename )
99 xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
102 /*****************************************************************************
104 *****************************************************************************/
105 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
106 xmlParserSeverities severity,
107 xmlTextReaderLocatorPtr locator)
109 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
110 int line = xmlTextReaderLocatorLineNumber( locator );
111 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
114 static xml_reader_t *ReaderCreate( xml_t *p_xml, const char *psz_filename )
116 xml_reader_t *p_reader;
117 xml_reader_sys_t *p_sys;
118 xmlTextReaderPtr p_libxml_reader;
120 p_libxml_reader = xmlNewTextReaderFilename( psz_filename );
121 if( !p_libxml_reader )
123 msg_Err( p_xml, "failed to open file %s for parsing", psz_filename );
127 p_reader = malloc( sizeof(xml_reader_t) );
128 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
129 p_reader->p_sys->p_reader = p_libxml_reader;
130 p_reader->p_xml = p_xml;
132 /* Activate DTD validation */
133 xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_DEFAULTATTRS, 1 );
134 xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_VALIDATE, 1 );
136 /* Set the error handler */
137 xmlTextReaderSetErrorHandler( p_libxml_reader,
138 ReaderErrorHandler, p_reader );
141 p_reader->pf_read = ReaderRead;
142 p_reader->pf_node_type = ReaderNodeType;
143 p_reader->pf_name = ReaderName;
144 p_reader->pf_value = ReaderValue;
145 p_reader->pf_next_attr = ReaderNextAttr;
150 static void ReaderDelete( xml_reader_t *p_reader )
152 xmlFreeTextReader( p_reader->p_sys->p_reader );
153 free( p_reader->p_sys );
157 static int ReaderRead( xml_reader_t *p_reader )
159 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
171 static int ReaderNodeType( xml_reader_t *p_reader )
173 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
177 case XML_READER_TYPE_ELEMENT:
178 i_ret = XML_READER_STARTELEM;
180 case XML_READER_TYPE_END_ELEMENT:
181 i_ret = XML_READER_ENDELEM;
187 i_ret = XML_READER_NONE;
194 static char *ReaderName( xml_reader_t *p_reader )
196 const xmlChar *psz_name =
197 xmlTextReaderConstName( p_reader->p_sys->p_reader );
199 if( psz_name ) return strdup( psz_name );
203 static char *ReaderValue( xml_reader_t *p_reader )
205 const xmlChar *psz_value =
206 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
208 if( psz_value ) return strdup( psz_value );
212 static int ReaderNextAttr( xml_reader_t *p_reader )
214 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
215 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;