]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
all: ported xml parsers to stream_t
[vlc] / modules / misc / xml / libxml.c
1 /*****************************************************************************
2  * libxml.c: XML parser using libxml2
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26
27 #include "vlc_xml.h"
28
29 #include <libxml/xmlreader.h>
30 #include <libxml/catalog.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
37
38 vlc_module_begin();
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 );
44 vlc_module_end();
45
46 struct xml_reader_sys_t
47 {
48     /* libxml2 reader context */
49     xmlTextReaderPtr p_reader;
50 };
51
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 * );
59
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 );
63
64 /*****************************************************************************
65  * Module initialization
66  *****************************************************************************/
67 static int Open( vlc_object_t *p_this )
68 {
69     xml_t *p_xml = (xml_t *)p_this;
70
71     p_xml->pf_reader_create = ReaderCreate;
72     p_xml->pf_reader_delete = ReaderDelete;
73
74     p_xml->pf_catalog_load = CatalogLoad;
75     p_xml->pf_catalog_add  = CatalogAdd;
76
77     return VLC_SUCCESS;
78 }
79
80 /*****************************************************************************
81  * Module deinitialization
82  *****************************************************************************/
83 static void Close( vlc_object_t *p_this )
84 {
85     return;
86 }
87
88 /*****************************************************************************
89  * Catalogue functions
90  *****************************************************************************/
91 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
92 {
93     if( !psz_filename ) xmlInitializeCatalog();
94     else xmlLoadCatalog( psz_filename );
95 }
96
97 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
98                           const char *psz_arg2, const char *psz_filename )
99 {
100     xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
101 }
102
103 /*****************************************************************************
104  * Reader functions
105  *****************************************************************************/
106 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
107                                 xmlParserSeverities severity,
108                                 xmlTextReaderLocatorPtr locator)
109 {
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 );
113 }
114
115 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
116 {
117     xml_reader_t *p_reader;
118     xml_reader_sys_t *p_sys;
119     xmlTextReaderPtr p_libxml_reader;
120     xmlParserInputBufferPtr p_read_context;
121
122     p_read_context = malloc( sizeof( xmlParserInputBuffer ) );
123
124     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
125                                       NULL, NULL, 0 );
126     if( !p_libxml_reader )
127     {
128         msg_Err( p_xml, "failed to create xml parser" );
129         return 0;
130     }
131
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;
136
137     /* Activate DTD validation */
138     xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_DEFAULTATTRS, 1 );
139     xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_VALIDATE, 1 );
140
141     /* Set the error handler */
142     xmlTextReaderSetErrorHandler( p_libxml_reader,
143                                   ReaderErrorHandler, p_reader );
144
145
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
152     return p_reader;
153 }
154
155 static void ReaderDelete( xml_reader_t *p_reader )
156 {
157     xmlFreeTextReader( p_reader->p_sys->p_reader );
158     free( p_reader->p_sys );
159     free( p_reader );
160 }
161
162 static int ReaderRead( xml_reader_t *p_reader )
163 {
164     int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
165
166 #if 0
167     switch( i_ret )
168     {
169     default:
170     }
171 #endif
172
173     return i_ret;
174 }
175
176 static int ReaderNodeType( xml_reader_t *p_reader )
177 {
178     int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
179
180     switch( i_ret )
181     {
182     case XML_READER_TYPE_ELEMENT:
183         i_ret = XML_READER_STARTELEM;
184         break;
185     case XML_READER_TYPE_END_ELEMENT:
186         i_ret = XML_READER_ENDELEM;
187         break;
188     case -1:
189         i_ret = -1;
190         break;
191     default:
192         i_ret = XML_READER_NONE;
193         break;
194     }
195
196     return i_ret;
197 }
198
199 static char *ReaderName( xml_reader_t *p_reader )
200 {
201     const xmlChar *psz_name =
202         xmlTextReaderConstName( p_reader->p_sys->p_reader );
203
204     if( psz_name ) return strdup( psz_name );
205     else return 0;
206 }
207
208 static char *ReaderValue( xml_reader_t *p_reader )
209 {
210     const xmlChar *psz_value =
211         xmlTextReaderConstValue( p_reader->p_sys->p_reader );
212
213     if( psz_value ) return strdup( psz_value );
214     else return 0;
215 }
216
217 static int ReaderNextAttr( xml_reader_t *p_reader )
218 {
219     return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
220              == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
221 }
222
223 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
224 {
225     stream_t *s = (stream_t*)p_context;
226     return stream_Read( s, p_buffer, i_buffer );    
227 }