[vlc-devel] [RFC] vlc_xml: add copy node function

Nicolas Bertrand nicolas at indecp.org
Thu Feb 23 11:39:21 CET 2017


Add a copy node function to the XML parser.
Will be used to send in, XML format, parts of subs XML files from demux to codec.

The goal is to limit the use of libxml2 function in module/misc/xml/libxml.c


---
 include/vlc_xml.h         |  7 +++++++
 modules/misc/xml/libxml.c | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/include/vlc_xml.h b/include/vlc_xml.h
index 661e76d..fd14ab6 100644
--- a/include/vlc_xml.h
+++ b/include/vlc_xml.h
@@ -75,6 +75,7 @@ struct xml_reader_t
 
     int (*pf_use_dtd) ( xml_reader_t * );
     int (*pf_is_empty) ( xml_reader_t * );
+    int (*pf_copy_node) ( xml_reader_t *, const char **);
 };
 
 VLC_API xml_reader_t * xml_ReaderCreate(vlc_object_t *, stream_t *) VLC_USED;
@@ -106,6 +107,12 @@ static inline int xml_ReaderIsEmptyElement( xml_reader_t *reader )
     return reader->pf_is_empty( reader );
 }
 
+static inline int xml_ReaderCopyNode( xml_reader_t *reader,
+        const char ** psz_text)
+{
+    return reader->pf_copy_node( reader, psz_text);
+}
+
 enum {
     XML_READER_ERROR=-1,
     XML_READER_NONE=0,
diff --git a/modules/misc/xml/libxml.c b/modules/misc/xml/libxml.c
index 059c269..5487ab4 100644
--- a/modules/misc/xml/libxml.c
+++ b/modules/misc/xml/libxml.c
@@ -34,6 +34,7 @@
 
 #include <libxml/xmlreader.h>
 #include <libxml/catalog.h>
+#include <libxml/parser.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -65,6 +66,7 @@ static const char *ReaderNextAttr( xml_reader_t *, const char ** );
 static int ReaderIsEmptyElement( xml_reader_t *);
 
 static int ReaderUseDTD ( xml_reader_t * );
+static int ReaderCopyNode( xml_reader_t *, const char ** );
 
 static void CatalogLoad( xml_t *, const char * );
 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
@@ -178,6 +180,7 @@ static int ReaderOpen( vlc_object_t *p_this )
     p_reader->pf_next_attr = ReaderNextAttr;
     p_reader->pf_is_empty = ReaderIsEmptyElement;
     p_reader->pf_use_dtd = ReaderUseDTD;
+    p_reader->pf_copy_node = ReaderCopyNode;
 
     return VLC_SUCCESS;
 }
@@ -294,3 +297,40 @@ static int ReaderIsEmptyElement( xml_reader_t *p_reader )
 {
     return xmlTextReaderIsEmptyElement( p_reader->p_sys->xml );
 }
+
+static int ReaderCopyNode(xml_reader_t *p_reader, const char **psz_text)
+{
+    xmlDocPtr doc;
+    xmlNode *p_cur_node = NULL;
+    xmlNode *p_copy_node = NULL;
+    xmlNode *p_root_node = NULL;
+    xmlChar *xmlbuff;
+    int xmlbuffsize;
+    const char *p_text;
+
+    xmlTextReaderPtr xml = p_reader->p_sys->xml;
+
+    p_cur_node = xmlTextReaderExpand(xml);
+    if (!p_cur_node)
+        return VLC_EGENERIC;
+
+    p_copy_node = xmlCopyNode(p_cur_node,1);
+    if (!p_copy_node)
+        return VLC_EGENERIC;
+
+    /* create new XML doc */
+    doc = xmlNewDoc(BAD_CAST "1.0");
+    p_root_node = xmlDocSetRootElement(doc, p_copy_node);
+    if (p_root_node)
+        /* No previous root node */
+        return VLC_EGENERIC;
+
+    xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &xmlbuffsize, "UTF-8", 0);
+
+    if( psz_text != NULL )
+        *psz_text =  strdup( (char *) xmlbuff);
+
+    xmlFree(xmlbuff);
+    xmlFreeDoc(doc);
+    return VLC_SUCCESS;
+}
-- 
2.1.4



More information about the vlc-devel mailing list