libgsf r1032 - in trunk: . gsf



Author: jody
Date: Wed Jan  7 02:59:36 2009
New Revision: 1032
URL: http://svn.gnome.org/viewvc/libgsf?rev=1032&view=rev

Log:
* Add libxml2 wrapper to simplify probing.


Modified:
   trunk/NEWS
   trunk/gsf/gsf-libxml.c
   trunk/gsf/gsf-libxml.h

Modified: trunk/NEWS
==============================================================================
--- trunk/NEWS	(original)
+++ trunk/NEWS	Wed Jan  7 02:59:36 2009
@@ -3,6 +3,7 @@
 Jody:
 	* win32 build fix for glib-2.18 deprecation.
 	* Extend MS Office Open Pkg handling to accept POI files.
+	* Add libxml2 wrapper to simplify probing.
 
 Morten:
 	* Solaris compilation issue.  [#558253]

Modified: trunk/gsf/gsf-libxml.c
==============================================================================
--- trunk/gsf/gsf-libxml.c	(original)
+++ trunk/gsf/gsf-libxml.c	Wed Jan  7 02:59:36 2009
@@ -419,6 +419,101 @@
 	return xmlSaveFormatFileTo (buf, cur, encoding, format);
 }
 
+/**************************************************************************/
+/* We parse and do some limited validation of the XML file, if this passes,
+ * then we return TRUE
+ */
+typedef struct {
+	GsfXMLProbeFunc	func;
+	gboolean success;
+} GsfXMLProbeState;
+
+static void
+gsf_xml_probe_error (GsfXMLProbeState *state, char const *msg, ...)
+{
+	state->func = NULL;
+	state->success = FALSE;
+}
+static void
+gsf_xml_probe_element (GsfXMLProbeState *state,
+		       const xmlChar *name,
+		       const xmlChar *prefix,
+		       const xmlChar *URI,
+		       int nb_namespaces,
+		       const xmlChar **namespaces,
+		       int nb_attributes,
+		       int nb_defaulted,
+		       const xmlChar **attributes)
+{
+	state->success = (state->func) (name, prefix, URI, nb_namespaces, namespaces,
+					nb_attributes, nb_defaulted, attributes);
+	state->func = NULL;
+}
+
+gboolean
+gsf_xml_probe (GsfInput *input, GsfXMLProbeFunc func)
+{
+	static xmlSAXHandler gsf_xml_prober = {
+		NULL, /* internalSubset */
+		NULL, /* isStandalone */
+		NULL, /* hasInternalSubset */
+		NULL, /* hasExternalSubset */
+		NULL, /* resolveEntity */
+		NULL, /* getEntity */
+		NULL, /* entityDecl */
+		NULL, /* notationDecl */
+		NULL, /* attributeDecl */
+		NULL, /* elementDecl */
+		NULL, /* unparsedEntityDecl */
+		NULL, /* setDocumentLocator */
+		NULL, /* startDocument */
+		NULL, /* endDocument */
+		NULL, /* startElement */
+		NULL, /* endElement */
+		NULL, /* reference */
+		NULL, /* characters */
+		NULL, /* ignorableWhitespace */
+		NULL, /* processingInstruction */
+		NULL, /* comment */
+		NULL, /* xmlParserWarning */
+		(errorSAXFunc) &gsf_xml_probe_error, /* error */
+		(errorSAXFunc) &gsf_xml_probe_error, /* fatalError */
+		NULL, /* getParameterEntity */
+		NULL, /* cdataBlock; */
+		NULL, /* externalSubset; */
+		XML_SAX2_MAGIC,
+		NULL,
+		(startElementNsSAX2Func) &gsf_xml_probe_element, /* startElementNs */
+		NULL, /* endElementNs */
+		NULL  /* xmlStructuredErrorFunc */
+	};
+	GsfXMLProbeState probe_state = { func, FALSE };
+	xmlParserCtxt *parse_context;
+	char const *buf;
+
+	if (gsf_input_seek (input, 0, G_SEEK_SET))
+		return FALSE;
+
+	g_object_ref (input);
+	input = gsf_input_uncompress (input);
+	gsf_input_seek (input, 0, G_SEEK_SET);
+
+	buf = gsf_input_read (input, 4, NULL);
+	if (NULL != buf ) {
+		parse_context = xmlCreatePushParserCtxt (&gsf_xml_prober, &probe_state,
+			(char *)buf, 4, gsf_input_name (input));
+		if (NULL != parse_context) {
+			while (NULL != probe_state.func &&
+			       NULL != (buf = gsf_input_read (input, 1, NULL)))
+				xmlParseChunk (parse_context, (char *)buf, 1, 0);
+		}
+		xmlFreeParserCtxt (parse_context);
+	}
+	g_object_unref (input);
+
+	return probe_state.success;
+}
+
 /***************************************************************************/
 
 typedef struct {

Modified: trunk/gsf/gsf-libxml.h
==============================================================================
--- trunk/gsf/gsf-libxml.h	(original)
+++ trunk/gsf/gsf-libxml.h	Wed Jan  7 02:59:36 2009
@@ -40,6 +40,17 @@
 				       char const *encoding,
 				       gboolean    format);
 
+typedef gboolean (*GsfXMLProbeFunc) (const xmlChar *name,
+				     const xmlChar *prefix,
+				     const xmlChar *URI,
+				     int nb_namespaces,
+				     const xmlChar **namespaces,
+				     int nb_attributes,
+				     int nb_defaulted,
+				     const xmlChar **attributes);
+gboolean gsf_xml_probe (GsfInput *input,
+			GsfXMLProbeFunc startElement);
+
 /****************************************************************************/
 /* Simplified wrapper to SAX based xml import */
 typedef struct _GsfXMLIn		GsfXMLIn;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]