1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) Dragos Dena 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301 USA
22 #include "snippets-xml-parser.h"
24 #include <libxml/parser.h>
25 #include <libxml/xmlwriter.h>
28 #define NATIVE_XML_HEADER "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
30 #define NATIVE_XML_ROOT "anjuta-snippets-packet"
31 #define NATIVE_XML_GROUP_TAG "anjuta-snippets-group"
32 #define NATIVE_XML_GROUP_NAME_TAG "name"
33 #define NATIVE_XML_SNIPPETS_TAG "anjuta-snippets"
34 #define NATIVE_XML_SNIPPET_TAG "anjuta-snippet"
35 #define NATIVE_XML_LANGUAGES_TAG "languages"
36 #define NATIVE_XML_VARIABLES_TAG "variables"
37 #define NATIVE_XML_VARIABLE_TAG "variable"
38 #define NATIVE_XML_CONTENT_TAG "snippet-content"
39 #define NATIVE_XML_KEYWORDS_TAG "keywords"
40 #define NATIVE_XML_NAME_PROP "name"
41 #define NATIVE_XML_GLOBAL_PROP "is_global"
42 #define NATIVE_XML_DEFAULT_PROP "default"
43 #define NATIVE_XML_TRIGGER_PROP "trigger"
44 #define NATIVE_XML_TRUE "true"
45 #define NATIVE_XML_FALSE "false"
47 #define GLOBAL_VARS_XML_ROOT "anjuta-global-variables"
48 #define GLOBAL_VARS_XML_VAR_TAG "global-variable"
49 #define GLOBAL_VARS_XML_NAME_PROP "name"
50 #define GLOBAL_VARS_XML_COMMAND_PROP "is_command"
51 #define GLOBAL_VARS_XML_TRUE "true"
52 #define GLOBAL_VARS_XML_FALSE "false"
54 #define CDATA_START "<![CDATA["
55 #define CDATA_END "]]>"
56 #define CDATA_MID "]]><![CDATA["
57 #define IS_CDATA_END(text, i) (text[i - 1] == ']' && text[i] == ']' && text[i + 1] == '>')
59 #define QUOTE_STR """
63 write_simple_start_tag (GOutputStream
*os
,
66 gchar
*tag
= g_strconcat ("<", name
, ">\n", NULL
);
67 g_output_stream_write (os
, tag
, strlen (tag
), NULL
, NULL
);
72 write_simple_end_tag (GOutputStream
*os
,
75 gchar
*tag
= g_strconcat ("</", name
, ">\n", NULL
);
76 g_output_stream_write (os
, tag
, strlen (tag
), NULL
, NULL
);
81 escape_text_cdata (const gchar
*content
)
83 GString
*formated_content
= g_string_new (CDATA_START
);
84 gint i
= 0, content_len
= 0;
86 content_len
= strlen (content
);
87 for (i
= 0; i
< content_len
; i
++)
89 /* If we found the "]]>" string in the content we should escape it. */
90 if (i
> 0 && IS_CDATA_END (content
, i
))
91 g_string_append (formated_content
, CDATA_MID
);
93 g_string_append_c (formated_content
, content
[i
]);
95 g_string_append (formated_content
, CDATA_END
);
97 return g_string_free (formated_content
, FALSE
);
102 escape_quotes (const gchar
*text
)
104 GString
*escaped_text
= g_string_new ("");
108 for (i
= 0; i
< len
; i
+= 1)
112 escaped_text
= g_string_append (escaped_text
, QUOTE_STR
);
116 escaped_text
= g_string_append_c (escaped_text
, text
[i
]);
119 return g_string_free (escaped_text
, FALSE
);
123 write_start_end_tag_with_content (GOutputStream
*os
,
124 const gchar
*tag_name
,
125 const gchar
*content
)
127 gchar
*tag_with_content
= NULL
, *escaped_content
= NULL
;
129 escaped_content
= escape_text_cdata (content
);
130 tag_with_content
= g_strconcat ("<", tag_name
, ">", escaped_content
, "</", tag_name
, ">\n", NULL
);
132 g_output_stream_write (os
, tag_with_content
, strlen (tag_with_content
), NULL
, NULL
);
133 g_free (tag_with_content
);
134 g_free (escaped_content
);
138 write_start_end_tag_with_content_as_list (GOutputStream
*os
,
139 const gchar
*tag_name
,
143 GString
*content
= g_string_new ("");
144 gchar
*cur_word
= NULL
;
146 for (iter
= g_list_first (content_list
); iter
!= NULL
; iter
= g_list_next (iter
))
148 cur_word
= (gchar
*)iter
->data
;
149 g_string_append (content
, cur_word
);
150 g_string_append (content
, " ");
153 write_start_end_tag_with_content (os
, tag_name
, content
->str
);
154 g_string_free (content
, TRUE
);
159 write_anjuta_snippet_tag (GOutputStream
*os
,
160 const gchar
*trigger
,
163 gchar
*tag
= NULL
, *escaped_name
= NULL
;
165 escaped_name
= escape_quotes (name
);
166 tag
= g_strconcat ("<anjuta-snippet trigger=\"", trigger
, "\" name=\"", escaped_name
, "\">\n", NULL
);
167 g_output_stream_write (os
, tag
, strlen (tag
), NULL
, NULL
);
169 g_free (escaped_name
);
173 write_variable_tag (GOutputStream
*os
,
175 const gchar
*default_val
,
178 const gchar
*global_val
= (is_global
) ? NATIVE_XML_TRUE
: NATIVE_XML_FALSE
;
179 gchar
*tag
= NULL
, *escaped_name
= NULL
, *escaped_default_val
= NULL
;
181 /* Escape the quotes */
182 escaped_name
= escape_quotes (name
);
183 escaped_default_val
= escape_quotes (default_val
);
186 tag
= g_strconcat ("<variable name=\"", escaped_name
,
187 "\" default=\"", escaped_default_val
,
188 "\" is_global=\"", global_val
, "\" />\n", NULL
);
189 g_output_stream_write (os
, tag
, strlen (tag
), NULL
, NULL
);
193 g_free (escaped_name
);
194 g_free (escaped_default_val
);
198 write_snippet (GOutputStream
*os
,
199 AnjutaSnippet
*snippet
)
201 GList
*iter
= NULL
, *iter2
= NULL
, *iter3
= NULL
, *keywords
= NULL
,
202 *vars_names
= NULL
, *vars_defaults
= NULL
, *vars_globals
= NULL
;
203 const gchar
*content
= NULL
;
206 g_return_val_if_fail (G_IS_OUTPUT_STREAM (os
), FALSE
);
207 g_return_val_if_fail (ANJUTA_IS_SNIPPET (snippet
), FALSE
);
209 write_anjuta_snippet_tag (os
,
210 snippet_get_trigger_key (snippet
),
211 snippet_get_name (snippet
));
213 /* Write the languages */
214 write_start_end_tag_with_content_as_list (os
,
215 NATIVE_XML_LANGUAGES_TAG
,
216 (GList
*)snippet_get_languages (snippet
));
218 /* Write the variables */
219 write_simple_start_tag (os
, NATIVE_XML_VARIABLES_TAG
);
220 /* Write each variable */
221 vars_names
= snippet_get_variable_names_list (snippet
);
222 vars_defaults
= snippet_get_variable_defaults_list (snippet
);
223 vars_globals
= snippet_get_variable_globals_list (snippet
);
224 iter
= g_list_first (vars_names
);
225 iter2
= g_list_first (vars_defaults
);
226 iter3
= g_list_first (vars_globals
);
227 while (iter
!= NULL
&& iter2
!= NULL
&& iter3
!= NULL
)
229 write_variable_tag (os
,
231 (gchar
*)iter2
->data
,
232 GPOINTER_TO_INT (iter3
->data
));
234 iter
= g_list_next (iter
);
235 iter2
= g_list_next (iter2
);
236 iter3
= g_list_next (iter3
);
238 g_list_free (vars_names
);
239 g_list_free (vars_defaults
);
240 g_list_free (vars_globals
);
241 write_simple_end_tag (os
, NATIVE_XML_VARIABLES_TAG
);
243 /* Write the content */
244 content
= snippet_get_content (snippet
);
245 write_start_end_tag_with_content (os
, NATIVE_XML_CONTENT_TAG
, content
);
247 /* Write the keywords */
248 keywords
= snippet_get_keywords_list (snippet
);
249 write_start_end_tag_with_content_as_list (os
, NATIVE_XML_KEYWORDS_TAG
, keywords
);
250 g_list_free (keywords
);
251 write_simple_end_tag (os
, NATIVE_XML_SNIPPET_TAG
);
257 write_snippets_group (GOutputStream
*os
,
258 AnjutaSnippetsGroup
*snippets_group
)
260 GList
*iter
= NULL
, *snippets
= NULL
;
263 g_return_val_if_fail (G_IS_OUTPUT_STREAM (os
), FALSE
);
264 g_return_val_if_fail (ANJUTA_IS_SNIPPETS_GROUP (snippets_group
), FALSE
);
266 write_simple_start_tag (os
, NATIVE_XML_GROUP_TAG
);
268 /* Write the name tag */
269 write_start_end_tag_with_content (os
,
270 NATIVE_XML_GROUP_NAME_TAG
,
271 snippets_group_get_name (snippets_group
));
273 write_simple_start_tag (os
, NATIVE_XML_SNIPPETS_TAG
);
275 /* Write the snippets */
276 snippets
= snippets_group_get_snippets_list (snippets_group
);
277 for (iter
= g_list_first (snippets
); iter
!= NULL
; iter
= g_list_next (iter
))
279 if (!ANJUTA_IS_SNIPPET (iter
->data
))
282 write_snippet (os
, ANJUTA_SNIPPET (iter
->data
));
284 write_simple_end_tag (os
, NATIVE_XML_SNIPPETS_TAG
);
286 write_simple_end_tag (os
, NATIVE_XML_GROUP_TAG
);
292 snippets_manager_save_native_xml_file (GList
* snippets_groups
,
293 const gchar
*file_path
)
296 GOutputStream
*os
= NULL
;
300 g_return_val_if_fail (file_path
!= NULL
, FALSE
);
303 file
= g_file_new_for_path (file_path
);
304 os
= G_OUTPUT_STREAM (g_file_replace (file
, NULL
, FALSE
, G_FILE_CREATE_NONE
, NULL
, NULL
));
305 if (!G_IS_OUTPUT_STREAM (os
))
307 g_object_unref (file
);
311 if (g_output_stream_write (os
, NATIVE_XML_HEADER
, strlen (NATIVE_XML_HEADER
), NULL
, NULL
) < 0)
313 g_output_stream_close (os
, NULL
, NULL
);
315 g_object_unref (file
);
319 write_simple_start_tag (os
, NATIVE_XML_ROOT
);
321 for (iter
= g_list_first (snippets_groups
); iter
!= NULL
; iter
= g_list_next (iter
))
323 if (!ANJUTA_IS_SNIPPETS_GROUP (iter
->data
))
326 write_snippets_group (os
, ANJUTA_SNIPPETS_GROUP (iter
->data
));
329 write_simple_end_tag (os
, NATIVE_XML_ROOT
);
332 g_output_stream_close (os
, NULL
, NULL
);
334 g_object_unref (file
);
340 snippets_manager_save_gedit_xml_file (GList
* snippets_group
,
341 const gchar
*file_path
)
347 static AnjutaSnippet
*
348 parse_snippet_node (xmlNodePtr snippet_node
)
350 AnjutaSnippet
* snippet
= NULL
;
351 gchar
*trigger_key
= NULL
, *snippet_name
= NULL
, *snippet_content
= NULL
, *cur_var_name
= NULL
,
352 *cur_var_default
= NULL
, *cur_var_global
= NULL
, *keywords_temp
= NULL
,
353 **keywords_temp_array
= NULL
, *keyword_temp
= NULL
, *languages_temp
= NULL
,
354 **languages_temp_array
= NULL
, *language_temp
= NULL
;
355 GList
*variable_names
= NULL
, *variable_default_values
= NULL
,
356 *variable_globals
= NULL
, *keywords
= NULL
, *iter
= NULL
,
357 *snippet_languages
= NULL
;
358 xmlNodePtr cur_field_node
= NULL
, cur_variable_node
= NULL
;
359 gboolean cur_var_is_global
= FALSE
;
362 /* Assert that the snippet_node is indeed a anjuta-snippet tag */
363 g_return_val_if_fail (!g_strcmp0 ((gchar
*)snippet_node
->name
, NATIVE_XML_SNIPPET_TAG
), NULL
);
365 /* Get the snippet name, trigger-key and language properties */
366 trigger_key
= (gchar
*)xmlGetProp (snippet_node
, (const xmlChar
*)NATIVE_XML_TRIGGER_PROP
);
367 snippet_name
= (gchar
*)xmlGetProp (snippet_node
, (const xmlChar
*)NATIVE_XML_NAME_PROP
);
369 /* Make sure we got all the above properties */
370 if (trigger_key
== NULL
|| snippet_name
== NULL
)
372 g_free (trigger_key
);
373 g_free (snippet_name
);
377 /* Get the snippet fields (variables, content and keywords) in the following loop */
378 cur_field_node
= snippet_node
->xmlChildrenNode
;
379 while (cur_field_node
!= NULL
)
381 /* We will look in all the variables and save them */
382 if (!g_strcmp0 ((gchar
*)cur_field_node
->name
, NATIVE_XML_VARIABLES_TAG
))
384 cur_variable_node
= cur_field_node
->xmlChildrenNode
;
385 while (cur_variable_node
!= NULL
)
387 /* Check if it's a variable tag */
388 if (g_strcmp0 ((gchar
*)cur_variable_node
->name
, NATIVE_XML_VARIABLE_TAG
))
390 cur_variable_node
= cur_variable_node
->next
;
394 /* Get the variable properties */
395 cur_var_name
= (gchar
*)xmlGetProp (cur_variable_node
,\
396 (const xmlChar
*)NATIVE_XML_NAME_PROP
);
397 cur_var_default
= (gchar
*)xmlGetProp (cur_variable_node
,\
398 (const xmlChar
*)NATIVE_XML_DEFAULT_PROP
);
399 cur_var_global
= (gchar
*)xmlGetProp (cur_variable_node
,\
400 (const xmlChar
*)NATIVE_XML_GLOBAL_PROP
);
401 if (!g_strcmp0 (cur_var_global
, NATIVE_XML_TRUE
))
402 cur_var_is_global
= TRUE
;
404 cur_var_is_global
= FALSE
;
405 g_free (cur_var_global
);
407 /* Add the properties to the lists */
408 variable_names
= g_list_append (variable_names
, cur_var_name
);
409 variable_default_values
= g_list_append (variable_default_values
, cur_var_default
);
410 variable_globals
= g_list_append (variable_globals
, GINT_TO_POINTER (cur_var_is_global
));
412 cur_variable_node
= cur_variable_node
->next
;
416 /* Get the actual snippet content */
417 if (!g_strcmp0 ((gchar
*)cur_field_node
->name
, NATIVE_XML_CONTENT_TAG
))
419 snippet_content
= (gchar
*)xmlNodeGetContent (cur_field_node
);
422 /* Parse the keywords of the snippet */
423 if (!g_strcmp0 ((gchar
*)cur_field_node
->name
, NATIVE_XML_KEYWORDS_TAG
))
425 keywords_temp
= (gchar
*)xmlNodeGetContent (cur_field_node
);
426 keywords_temp_array
= g_strsplit (keywords_temp
, " ", -1);
429 while (keywords_temp_array
[i
])
431 if (g_strcmp0 (keywords_temp_array
[i
], ""))
433 keyword_temp
= g_strdup (keywords_temp_array
[i
]);
434 keywords
= g_list_append (keywords
, keyword_temp
);
439 g_free (keywords_temp
);
440 g_strfreev (keywords_temp_array
);
443 /* Parse the languages of the snippet */
444 if (!g_strcmp0 ((gchar
*)cur_field_node
->name
, NATIVE_XML_LANGUAGES_TAG
))
446 languages_temp
= (gchar
*)xmlNodeGetContent (cur_field_node
);
447 languages_temp_array
= g_strsplit (languages_temp
, " ", -1);
450 while (languages_temp_array
[i
])
452 if (g_strcmp0 (languages_temp_array
[i
], ""))
454 language_temp
= g_strdup (languages_temp_array
[i
]);
455 snippet_languages
= g_list_append (snippet_languages
, language_temp
);
460 g_free (languages_temp
);
461 g_strfreev (languages_temp_array
);
464 cur_field_node
= cur_field_node
->next
;
467 /* Make a new AnjutaSnippet object */
468 snippet
= snippet_new (trigger_key
,
473 variable_default_values
,
477 /* Free the memory (the data is copied on the snippet constructor) */
478 g_free (trigger_key
);
479 g_free (snippet_name
);
480 g_free (snippet_content
);
481 for (iter
= g_list_first (variable_names
); iter
!= NULL
; iter
= g_list_next (iter
))
485 for (iter
= g_list_first (variable_default_values
); iter
!= NULL
; iter
= g_list_next (iter
))
489 g_list_free (variable_names
);
490 g_list_free (variable_default_values
);
491 g_list_free (variable_globals
);
492 for (iter
= g_list_first (snippet_languages
); iter
!= NULL
; iter
= g_list_next (iter
))
496 g_list_free (snippet_languages
);
497 for (iter
= g_list_first (keywords
); iter
!= NULL
; iter
= g_list_next (iter
))
501 g_list_free (keywords
);
506 static AnjutaSnippetsGroup
*
507 parse_snippets_group_node (xmlNodePtr snippets_group_node
)
509 AnjutaSnippetsGroup
*snippets_group
= NULL
;
510 AnjutaSnippet
*cur_snippet
= NULL
;
511 xmlNodePtr cur_snippet_node
= NULL
, cur_node
= NULL
;
512 gchar
*group_name
= NULL
;
514 /* Get the group name */
515 cur_node
= snippets_group_node
->xmlChildrenNode
;
516 while (cur_node
!= NULL
)
518 if (!g_strcmp0 ((gchar
*)cur_node
->name
, NATIVE_XML_GROUP_NAME_TAG
))
520 group_name
= g_strdup ((gchar
*)xmlNodeGetContent (cur_node
));
524 cur_node
= cur_node
->next
;
526 if (group_name
== NULL
)
529 /* Initialize the snippets group */
530 snippets_group
= snippets_group_new (group_name
);
532 /* Get the snippets */
533 cur_node
= snippets_group_node
->xmlChildrenNode
;
536 if (!g_strcmp0 ((gchar
*)cur_node
->name
, NATIVE_XML_SNIPPETS_TAG
))
538 cur_snippet_node
= cur_node
->xmlChildrenNode
;
540 /* Look trough all the snippets */
541 while (cur_snippet_node
)
543 /* Make sure it's a snippet node */
544 if (g_strcmp0 ((gchar
*)cur_snippet_node
->name
, NATIVE_XML_SNIPPET_TAG
))
546 cur_snippet_node
= cur_snippet_node
->next
;
550 /* Get a new AnjutaSnippet object from the current node and add it to the group*/
551 cur_snippet
= parse_snippet_node (cur_snippet_node
);
552 if (ANJUTA_IS_SNIPPET (cur_snippet
))
553 snippets_group_add_snippet (snippets_group
, cur_snippet
);
555 cur_snippet_node
= cur_snippet_node
->next
;
561 cur_node
= cur_node
->next
;
564 return snippets_group
;
568 snippets_manager_parse_native_xml_file (const gchar
*snippet_packet_path
)
570 AnjutaSnippetsGroup
* snippets_group
= NULL
;
571 GList
* snippets_groups
= NULL
;
572 xmlDocPtr snippet_packet_doc
= NULL
;
573 xmlNodePtr cur_node
= NULL
;
575 /* Parse the XML file and load it into a xmlDoc */
576 snippet_packet_doc
= xmlParseFile (snippet_packet_path
);
577 if (snippet_packet_doc
== NULL
)
580 /* Get the root and assert it */
581 cur_node
= xmlDocGetRootElement (snippet_packet_doc
);
582 if (cur_node
== NULL
|| g_strcmp0 ((gchar
*)cur_node
->name
, NATIVE_XML_ROOT
))
584 xmlFreeDoc (snippet_packet_doc
);
588 /* Get the snippets groups */
589 cur_node
= cur_node
->xmlChildrenNode
;
590 while (cur_node
!= NULL
)
592 /* Get the current snippets group */
593 if (!g_strcmp0 ((gchar
*)cur_node
->name
, NATIVE_XML_GROUP_TAG
))
595 snippets_group
= parse_snippets_group_node (cur_node
);
597 if (ANJUTA_IS_SNIPPETS_GROUP (snippets_group
))
598 snippets_groups
= g_list_prepend (snippets_groups
, snippets_group
);
601 cur_node
= cur_node
->next
;
604 xmlFreeDoc (snippet_packet_doc
);
606 return snippets_groups
;
610 snippets_manager_parse_gedit_xml_file (const gchar
* snippet_packet_path
)
618 * snippets_manager_parse_xml_file:
619 * @snippet_packet_path: The path of the XML file describing the Snippet Group
620 * @format_Type: The type of the XML file (see snippets-db.h for the supported types)
622 * Parses the given XML file.
624 * Returns: A list of #AnjutaSnippetGroup objects.
627 snippets_manager_parse_snippets_xml_file (const gchar
* snippet_packet_path
,
628 FormatType format_type
)
633 return snippets_manager_parse_native_xml_file (snippet_packet_path
);
636 return snippets_manager_parse_gedit_xml_file (snippet_packet_path
);
644 * snippets_manager_parse_xml_file:
645 * @format_type: The type of the XML file (see snippets-db.h for the supported types)
646 * @snippets_group: A list #AnjutaSnippetsGroup objects.
648 * Saves the given snippets to a snippet-packet XML file.
650 * Returns: TRUE on success.
653 snippets_manager_save_snippets_xml_file (FormatType format_type
,
654 GList
* snippets_groups
,
655 const gchar
*file_path
)
660 return snippets_manager_save_native_xml_file (snippets_groups
, file_path
);
663 return snippets_manager_save_gedit_xml_file (snippets_groups
, file_path
);
671 * snippets_manager_parse_variables_xml_file:
672 * @global_vars_path: A path with a XML file describing snippets global variables.
673 * @snippets_db: A #SnippetsDB object where the global variables should be loaded.
675 * Loads the global variables from the given XML file in the given #SnippetsDB object.
677 * Returns: TRUE on success.
680 snippets_manager_parse_variables_xml_file (const gchar
* global_vars_path
,
681 SnippetsDB
* snippets_db
)
683 xmlDocPtr global_vars_doc
= NULL
;
684 xmlNodePtr cur_var_node
= NULL
;
685 gchar
*cur_var_name
= NULL
, *cur_var_is_command
= NULL
, *cur_var_content
= NULL
;
686 gboolean cur_var_is_command_bool
= FALSE
;
689 g_return_val_if_fail (global_vars_path
!= NULL
, FALSE
);
690 g_return_val_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db
), FALSE
);
692 /* Parse the XML file */
693 global_vars_doc
= xmlParseFile (global_vars_path
);
694 g_return_val_if_fail (global_vars_doc
!= NULL
, FALSE
);
696 /* Get the root and assert it */
697 cur_var_node
= xmlDocGetRootElement (global_vars_doc
);
698 if (cur_var_node
== NULL
||\
699 g_strcmp0 ((gchar
*)cur_var_node
->name
, GLOBAL_VARS_XML_ROOT
))
701 xmlFreeDoc (global_vars_doc
);
705 /* Get the name and description fields */
706 cur_var_node
= cur_var_node
->xmlChildrenNode
;
707 while (cur_var_node
!= NULL
)
709 /* Get the current Snippet Global Variable */
710 if (!g_strcmp0 ((gchar
*)cur_var_node
->name
, GLOBAL_VARS_XML_VAR_TAG
))
712 /* Get the name, is_command properties and the content */
713 cur_var_name
= (gchar
*)xmlGetProp (cur_var_node
,\
714 (const xmlChar
*)GLOBAL_VARS_XML_NAME_PROP
);
715 cur_var_is_command
= (gchar
*)xmlGetProp (cur_var_node
,\
716 (const xmlChar
*)GLOBAL_VARS_XML_COMMAND_PROP
);
717 cur_var_content
= g_strdup ((gchar
*)xmlNodeGetContent (cur_var_node
));
718 if (!g_strcmp0 (cur_var_is_command
, GLOBAL_VARS_XML_TRUE
))
719 cur_var_is_command_bool
= TRUE
;
721 cur_var_is_command_bool
= FALSE
;
723 /* Add the Global Variable to the Snippet Database */
724 snippets_db_add_global_variable (snippets_db
,
727 cur_var_is_command_bool
,
730 g_free (cur_var_content
);
731 g_free (cur_var_name
);
732 g_free (cur_var_is_command
);
735 cur_var_node
= cur_var_node
->next
;
742 write_global_var_tags (GOutputStream
*os
,
747 gchar
*command_string
= NULL
, *escaped_content
= NULL
, *line
= NULL
,
748 *escaped_name
= NULL
;
751 g_return_if_fail (G_IS_OUTPUT_STREAM (os
));
753 /* Escape the text */
754 command_string
= (is_command
) ? GLOBAL_VARS_XML_TRUE
: GLOBAL_VARS_XML_FALSE
;
755 escaped_content
= escape_text_cdata (value
);
756 escaped_name
= escape_quotes (name
);
759 line
= g_strconcat ("<global-variable name=\"", escaped_name
,
760 "\" is_command=\"", command_string
, "\">",
762 "</global-variable>\n",
764 g_output_stream_write (os
, line
, strlen (line
), NULL
, NULL
);
766 /* Free the memory */
768 g_free (escaped_content
);
769 g_free (escaped_name
);
773 * snippets_manager_save_variables_xml_file:
774 * @global_vars_path: A path with a XML file describing snippets global variables.
775 * @global_vars_name_list: A #GList with the name of the variables.
776 * @global_vars_value_list: A #GList with the values of the variables.
777 * @global_vars_is_command_list: A #Glist with #gboolean values showing if the value
778 * of the given variable is a command.
780 * Saves the given snippets global variables in a XML file at the given path.
782 * Returns: TRUE on success.
785 snippets_manager_save_variables_xml_file (const gchar
* global_variables_path
,
786 GList
* global_vars_name_list
,
787 GList
* global_vars_value_list
,
788 GList
* global_vars_is_command_list
)
790 GList
*iter
= NULL
, *iter2
= NULL
, *iter3
= NULL
;
792 GOutputStream
*os
= NULL
;
795 g_return_val_if_fail (global_variables_path
!= NULL
, FALSE
);
798 file
= g_file_new_for_path (global_variables_path
);
799 os
= G_OUTPUT_STREAM (g_file_replace (file
, NULL
, FALSE
, G_FILE_CREATE_NONE
, NULL
, NULL
));
800 if (!G_IS_OUTPUT_STREAM (os
))
802 g_object_unref (file
);
806 if (g_output_stream_write (os
, NATIVE_XML_HEADER
, strlen (NATIVE_XML_HEADER
), NULL
, NULL
) < 0)
808 g_output_stream_close (os
, NULL
, NULL
);
810 g_object_unref (file
);
814 write_simple_start_tag (os
, GLOBAL_VARS_XML_ROOT
);
816 /* Write each of the global variable */
817 iter
= g_list_first (global_vars_name_list
);
818 iter2
= g_list_first (global_vars_value_list
);
819 iter3
= g_list_first (global_vars_is_command_list
);
820 while (iter
!= NULL
&& iter2
!= NULL
&& iter3
!= NULL
)
822 write_global_var_tags (os
,
824 (gchar
*)iter2
->data
,
825 GPOINTER_TO_INT (iter3
->data
));
827 iter
= g_list_next (iter
);
828 iter2
= g_list_next (iter2
);
829 iter3
= g_list_next (iter3
);
832 write_simple_end_tag (os
, GLOBAL_VARS_XML_ROOT
);
835 g_output_stream_close (os
, NULL
, NULL
);
837 g_object_unref (file
);