/** * as_node_to_file: (skip) * @root: A populated #AsNode tree * @file: a #GFile * @flags: #AsNodeToXmlFlags, e.g. %AS_NODE_TO_XML_FLAG_NONE * @cancellable: A #GCancellable, or %NULL * @error: A #GError or %NULL * * Exports a DOM tree to an XML file. * * Returns: %TRUE for success * * Since: 0.2.0 **/ gboolean as_node_to_file (const AsNode *root, GFile *file, AsNodeToXmlFlags flags, GCancellable *cancellable, GError **error) { g_autoptr(GString) xml = NULL; xml = as_node_to_xml (root, flags); return g_file_replace_contents (file, xml->str, xml->len, NULL, FALSE, G_FILE_CREATE_NONE, NULL, cancellable, error); }
/** * as_node_get_localized_unwrap: * @node: a #AsNode. * @error: A #GError or %NULL. * * Denormalize AppData data like this: * * |[ * <description> * <p>Hi</p> * <p xml:lang="pl">Czesc</p> * <ul> * <li>First</li> * <li xml:lang="pl">Pierwszy</li> * </ul> * </description> * ]| * * into a hash that contains: * * |[ * "C" -> "<p>Hi</p><ul><li>First</li></ul>" * "pl" -> "<p>Czesc</p><ul><li>Pierwszy</li></ul>" * ]| * * Returns: (transfer full): a hash table of data * * Since: 0.1.0 **/ GHashTable * as_node_get_localized_unwrap (const AsNode *node, GError **error) { AsNodeData *data; GHashTable *results; GList *l; AsNode *tmp; GString *str; const gchar *xml_lang; gboolean is_li_translated = TRUE; g_autoptr(GHashTable) hash = NULL; g_autoptr(GList) keys = NULL; g_return_val_if_fail (node != NULL, NULL); /* work out what kind of normalization this is */ xml_lang = as_node_get_attribute (node, "xml:lang"); if (xml_lang != NULL && node->children != NULL) { str = as_node_to_xml (node->children, AS_NODE_TO_XML_FLAG_NONE); results = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); g_hash_table_insert (results, g_strdup (xml_lang), g_strdup (str->str)); g_string_free (str, TRUE); return results; } for (tmp = node->children; tmp != NULL; tmp = tmp->next) { data = tmp->data; if (g_strcmp0 (data->name, "ul") == 0 || g_strcmp0 (data->name, "ol") == 0) { if (as_node_attr_lookup (data, "xml:lang") != NULL) { is_li_translated = FALSE; break; } } } /* unwrap it to a hash */ hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) as_node_string_free); if (is_li_translated) { if (!as_node_get_localized_unwrap_type_li (node, hash, error)) return NULL; } else { if (!as_node_get_localized_unwrap_type_ul (node, hash, error)) return NULL; } /* copy into a hash table of the correct size */ results = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); keys = g_hash_table_get_keys (hash); for (l = keys; l != NULL; l = l->next) { gchar *locale_fixed; xml_lang = l->data; locale_fixed = as_node_fix_locale (xml_lang); if (locale_fixed == NULL) continue; str = g_hash_table_lookup (hash, xml_lang); g_hash_table_insert (results, locale_fixed, g_strdup (str->str)); } return results; }
/** * as_review_node_parse: * @review: a #AsReview instance. * @node: a #GNode. * @ctx: a #AsNodeContext. * @error: A #GError or %NULL. * * Populates the object from a DOM node. * * Returns: %TRUE for success * * Since: 0.6.1 **/ gboolean as_review_node_parse (AsReview *review, GNode *node, AsNodeContext *ctx, GError **error) { AsReviewPrivate *priv = GET_PRIVATE (review); AsNode *c; const gchar *tmp; gint itmp; itmp = as_node_get_attribute_as_int (node, "rating"); if (itmp != G_MAXINT) as_review_set_rating (review, itmp); tmp = as_node_get_attribute (node, "date"); if (tmp != NULL) { g_autoptr(GDateTime) dt = as_utils_iso8601_to_datetime (tmp); if (dt != NULL) as_review_set_date (review, dt); } tmp = as_node_get_attribute (node, "id"); if (tmp != NULL) as_review_set_id (review, tmp); for (c = node->children; c != NULL; c = c->next) { if (as_node_get_tag (c) == AS_TAG_SUMMARY) { as_review_set_summary (review, as_node_get_data (c)); continue; } if (as_node_get_tag (c) == AS_TAG_PRIORITY) { gint64 prio = g_ascii_strtoll (as_node_get_data (c), NULL, 10); as_review_set_priority (review, (gint) prio); continue; } if (as_node_get_tag (c) == AS_TAG_DESCRIPTION) { g_autoptr(GString) xml = NULL; xml = as_node_to_xml (c->children, AS_NODE_TO_XML_FLAG_INCLUDE_SIBLINGS); as_review_set_description (review, xml->str); continue; } if (as_node_get_tag (c) == AS_TAG_VERSION) { as_review_set_version (review, as_node_get_data (c)); continue; } if (as_node_get_tag (c) == AS_TAG_REVIEWER_ID) { as_review_set_reviewer_id (review, as_node_get_data (c)); continue; } if (as_node_get_tag (c) == AS_TAG_REVIEWER_NAME) { as_review_set_reviewer_name (review, as_node_get_data (c)); continue; } if (as_node_get_tag (c) == AS_TAG_LANG) { as_review_set_locale (review, as_node_get_data (c)); continue; } if (as_node_get_tag (c) == AS_TAG_METADATA) { AsNode *c2; for (c2 = c->children; c2 != NULL; c2 = c2->next) { AsRefString *key; AsRefString *value; if (as_node_get_tag (c2) != AS_TAG_VALUE) continue; key = as_node_get_attribute (c2, "key"); value = as_node_get_data (c2); if (value == NULL) { g_hash_table_insert (priv->metadata, as_ref_string_ref (key), as_ref_string_new_static ("")); } else { g_hash_table_insert (priv->metadata, as_ref_string_ref (key), as_ref_string_ref (value)); } } continue; } } return TRUE; }