From 72a2e8a1824ba8d41b8bb6f561bc56f703423915 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Tue, 9 Oct 2018 15:07:39 +0100 Subject: Add as_app_to_xml() This allows us to load in a file and export it as AppData XML. --- libappstream-glib/as-app.c | 41 ++++++++++++++++++++++++++++------------- libappstream-glib/as-app.h | 2 ++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/libappstream-glib/as-app.c b/libappstream-glib/as-app.c index f84011c..01f6d5b 100644 --- a/libappstream-glib/as-app.c +++ b/libappstream-glib/as-app.c @@ -6413,6 +6413,31 @@ as_app_parse_file (AsApp *app, const gchar *filename, guint32 flags, GError **er return TRUE; } +/** + * as_app_to_xml: + * @app: a #AsApp instance. + * @error: A #GError or %NULL + * + * Exports a DOM tree to an XML string. + * + * Returns: (transfer full): an XML string, or %NULL + * + * Since: 0.7.14 + **/ +GString * +as_app_to_xml (AsApp *app, GError **error) +{ + g_autoptr(AsNodeContext) ctx = as_node_context_new (); + g_autoptr(AsNode) root = as_node_new (); + as_node_context_set_version (ctx, 1.0); + as_node_context_set_output (ctx, AS_FORMAT_KIND_APPDATA); + as_app_node_insert (app, root, ctx); + return as_node_to_xml (root, + AS_NODE_TO_XML_FLAG_ADD_HEADER | + AS_NODE_TO_XML_FLAG_FORMAT_INDENT | + AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); +} + /** * as_app_to_file: * @app: a #AsApp instance. @@ -6432,19 +6457,9 @@ as_app_to_file (AsApp *app, GCancellable *cancellable, GError **error) { - g_autoptr(AsNodeContext) ctx = NULL; - g_autoptr(AsNode) root = NULL; - g_autoptr(GString) xml = NULL; - - root = as_node_new (); - ctx = as_node_context_new (); - as_node_context_set_version (ctx, 1.0); - as_node_context_set_output (ctx, AS_FORMAT_KIND_APPDATA); - as_app_node_insert (app, root, ctx); - xml = as_node_to_xml (root, - AS_NODE_TO_XML_FLAG_ADD_HEADER | - AS_NODE_TO_XML_FLAG_FORMAT_INDENT | - AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); + g_autoptr(GString) xml = as_app_to_xml (app, error); + if (xml == NULL) + return FALSE; return g_file_replace_contents (file, xml->str, xml->len, diff --git a/libappstream-glib/as-app.h b/libappstream-glib/as-app.h index 425b5c0..3e62b7d 100644 --- a/libappstream-glib/as-app.h +++ b/libappstream-glib/as-app.h @@ -715,6 +715,8 @@ gboolean as_app_to_file (AsApp *app, GFile *file, GCancellable *cancellable, GError **error); +GString *as_app_to_xml (AsApp *app, + GError **error); AsContentRating *as_app_get_content_rating (AsApp *app, const gchar *kind); AsAgreement *as_app_get_agreement_by_kind (AsApp *app, -- cgit v1.2.1