]> git.sesse.net Git - vlc/blobdiff - modules/misc/xml/libxml.c
Require libxml2 < 2.7
[vlc] / modules / misc / xml / libxml.c
index 13243c5b7d29f0cdd33c59fb6e9f8802defabf92..09c200e80e476d127dcd1b188c4a4e9d006193e5 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * libxml.c: XML parser using libxml2
  *****************************************************************************
- * Copyright (C) 2004 VideoLAN
+ * Copyright (C) 2004 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <stdlib.h>
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 
+#include "vlc_block.h"
+#include "vlc_stream.h"
 #include "vlc_xml.h"
 
 #include <libxml/xmlreader.h>
 #include <libxml/catalog.h>
 
+#if !defined (LIBXML_VERSION) || (LIBXML_VERSION > 20700)
+# error Stale config.cache detected. Erase it and re-run configure.
+#endif
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_category( CAT_ADVANCED );
-    set_subcategory( SUBCAT_ADVANCED_XML );
-    set_description( _("XML Parser (using libxml2)") );
-    set_capability( "xml", 10 );
-    set_callbacks( Open, Close );
-vlc_module_end();
+vlc_module_begin ()
+    set_description( N_("XML Parser (using libxml2)") )
+    set_capability( "xml", 10 )
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 struct xml_reader_sys_t
 {
@@ -49,7 +57,7 @@ struct xml_reader_sys_t
     xmlTextReaderPtr p_reader;
 };
 
-static xml_reader_t *ReaderCreate( xml_t *, const char * );
+static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
 static void ReaderDelete( xml_reader_t * );
 static int ReaderRead( xml_reader_t * );
 static int ReaderNodeType( xml_reader_t * );
@@ -57,8 +65,14 @@ static char *ReaderName( xml_reader_t * );
 static char *ReaderValue( xml_reader_t * );
 static int ReaderNextAttr( xml_reader_t * );
 
+static int ReaderUseDTD ( xml_reader_t *, bool );
+
 static void CatalogLoad( xml_t *, const char * );
 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
+static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
+
+static unsigned refs = 0;
+static vlc_mutex_t lock = VLC_STATIC_MUTEX;
 
 /*****************************************************************************
  * Module initialization
@@ -67,6 +81,14 @@ static int Open( vlc_object_t *p_this )
 {
     xml_t *p_xml = (xml_t *)p_this;
 
+    if( !xmlHasFeature( XML_WITH_THREAD ) )
+        return VLC_EGENERIC;
+
+    vlc_mutex_lock( &lock );
+    if( refs++ == 0 )
+        xmlInitParser();
+    vlc_mutex_unlock( &lock );
+
     p_xml->pf_reader_create = ReaderCreate;
     p_xml->pf_reader_delete = ReaderDelete;
 
@@ -81,6 +103,12 @@ static int Open( vlc_object_t *p_this )
  *****************************************************************************/
 static void Close( vlc_object_t *p_this )
 {
+    vlc_mutex_lock( &lock );
+    if( --refs == 0 )
+        xmlCleanupParser();
+    vlc_mutex_unlock( &lock );
+
+    VLC_UNUSED(p_this);
     return;
 }
 
@@ -89,6 +117,7 @@ static void Close( vlc_object_t *p_this )
  *****************************************************************************/
 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
 {
+    VLC_UNUSED(p_xml);
     if( !psz_filename ) xmlInitializeCatalog();
     else xmlLoadCatalog( psz_filename );
 }
@@ -96,7 +125,9 @@ static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
                           const char *psz_arg2, const char *psz_filename )
 {
-    xmlCatalogAdd( psz_arg1, psz_arg2, psz_filename );
+    VLC_UNUSED(p_xml);
+    xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
+        (unsigned char*)psz_filename );
 }
 
 /*****************************************************************************
@@ -106,33 +137,42 @@ static void ReaderErrorHandler( void *p_arg, const char *p_msg,
                                 xmlParserSeverities severity,
                                 xmlTextReaderLocatorPtr locator)
 {
+    VLC_UNUSED(severity);
     xml_reader_t *p_reader = (xml_reader_t *)p_arg;
     int line = xmlTextReaderLocatorLineNumber( locator );
     msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
 }
 
-static xml_reader_t *ReaderCreate( xml_t *p_xml, const char *psz_filename )
+static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
 {
     xml_reader_t *p_reader;
     xml_reader_sys_t *p_sys;
     xmlTextReaderPtr p_libxml_reader;
 
-    p_libxml_reader = xmlNewTextReaderFilename( psz_filename );
+    p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
+                                      NULL, NULL, 0 );
     if( !p_libxml_reader )
     {
-        msg_Err( p_xml, "failed to open file %s for parsing", psz_filename );
-        return 0;
+        msg_Err( p_xml, "failed to create XML parser" );
+        return NULL;
     }
 
     p_reader = malloc( sizeof(xml_reader_t) );
+    if( !p_reader )
+    {
+        xmlFreeTextReader( p_libxml_reader );
+        return NULL;
+    }
     p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
+    if( !p_sys )
+    {
+        xmlFreeTextReader( p_libxml_reader );
+        free( p_reader );
+        return NULL;
+    }
     p_reader->p_sys->p_reader = p_libxml_reader;
     p_reader->p_xml = p_xml;
 
-    /* Activate DTD validation */
-    xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_DEFAULTATTRS, 1 );
-    xmlTextReaderSetParserProp( p_libxml_reader, XML_PARSER_VALIDATE, 1 );
-
     /* Set the error handler */
     xmlTextReaderSetErrorHandler( p_libxml_reader,
                                   ReaderErrorHandler, p_reader );
@@ -143,6 +183,7 @@ static xml_reader_t *ReaderCreate( xml_t *p_xml, const char *psz_filename )
     p_reader->pf_name = ReaderName;
     p_reader->pf_value = ReaderValue;
     p_reader->pf_next_attr = ReaderNextAttr;
+    p_reader->pf_use_dtd = ReaderUseDTD;
 
     return p_reader;
 }
@@ -154,6 +195,17 @@ static void ReaderDelete( xml_reader_t *p_reader )
     free( p_reader );
 }
 
+static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
+{
+    /* Activate DTD validation */
+    xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
+                                XML_PARSER_DEFAULTATTRS, b_use );
+    xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
+                                XML_PARSER_VALIDATE, b_use );
+
+    return VLC_SUCCESS;
+}
+
 static int ReaderRead( xml_reader_t *p_reader )
 {
     int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
@@ -180,6 +232,10 @@ static int ReaderNodeType( xml_reader_t *p_reader )
     case XML_READER_TYPE_END_ELEMENT:
         i_ret = XML_READER_ENDELEM;
         break;
+    case XML_READER_TYPE_CDATA:
+    case XML_READER_TYPE_TEXT:
+        i_ret = XML_READER_TEXT;
+        break;
     case -1:
         i_ret = -1;
         break;
@@ -196,7 +252,7 @@ static char *ReaderName( xml_reader_t *p_reader )
     const xmlChar *psz_name =
         xmlTextReaderConstName( p_reader->p_sys->p_reader );
 
-    if( psz_name ) return strdup( psz_name );
+    if( psz_name ) return strdup( (const char *)psz_name );
     else return 0;
 }
 
@@ -205,7 +261,7 @@ static char *ReaderValue( xml_reader_t *p_reader )
     const xmlChar *psz_value =
         xmlTextReaderConstValue( p_reader->p_sys->p_reader );
 
-    if( psz_value ) return strdup( psz_value );
+    if( psz_value ) return strdup( (const char *)psz_value );
     else return 0;
 }
 
@@ -214,3 +270,9 @@ static int ReaderNextAttr( xml_reader_t *p_reader )
     return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
              == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
 }
+
+static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
+{
+    stream_t *s = (stream_t*)p_context;
+    return stream_Read( s, p_buffer, i_buffer );
+}