1 | /* copyright (C) 2001 Sun Microsystems, Inc.*/ |
---|
2 | |
---|
3 | /* |
---|
4 | * This library is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU Lesser General Public |
---|
6 | * License as published by the Free Software Foundation; either |
---|
7 | * version 2.1 of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This library is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * Lesser General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU Lesser General Public |
---|
15 | * License along with this library; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | #include <libxml/tree.h> |
---|
21 | #include <libxml/parser.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | #include <unistd.h> |
---|
25 | #include <sys/stat.h> |
---|
26 | #include <libintl.h> |
---|
27 | #include <scrollkeeper.h> |
---|
28 | |
---|
29 | #define _(String) gettext (String) |
---|
30 | |
---|
31 | int update_doc_url_in_omf_file(char *omf_name, char *url, char *omf_new_name) |
---|
32 | { |
---|
33 | xmlNodePtr node; |
---|
34 | xmlDocPtr omf_doc; |
---|
35 | |
---|
36 | |
---|
37 | /* Parse file and make sure it is well-formed */ |
---|
38 | omf_doc = xmlParseFile(omf_name); |
---|
39 | if (omf_doc == NULL || omf_doc->children == NULL) { |
---|
40 | printf(_("OMF file was not well-formed.\n")); |
---|
41 | return 0; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /* Look for root element and make sure it is <omf> */ |
---|
46 | node = xmlDocGetRootElement(omf_doc); |
---|
47 | if (node == NULL) { |
---|
48 | printf(_("Could not find root element of OMF file.\n")); |
---|
49 | return 0; |
---|
50 | } |
---|
51 | if (xmlStrcmp(node->name, (xmlChar *)"omf")) { |
---|
52 | printf(_("Root element of OMF file is not <omf>.\n")); |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | /* Look for <resource> */ |
---|
58 | for(node = node->children; node != NULL; node = node->next) { |
---|
59 | if (!xmlStrcmp(node->name, (xmlChar *)"resource")) { |
---|
60 | break; |
---|
61 | } |
---|
62 | } |
---|
63 | if (node == NULL) { |
---|
64 | printf(_("OMF file does not have <resource> element.\n")); |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /* Modify <url> element */ |
---|
70 | for(node = node->children; node != NULL; node = node->next) |
---|
71 | { |
---|
72 | if (node->type == XML_ELEMENT_NODE && |
---|
73 | !xmlStrcmp(node->name, (xmlChar *)"identifier")) |
---|
74 | { |
---|
75 | xmlSetProp(node, (xmlChar *)"url", (xmlChar *)url); |
---|
76 | break; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /* Save the modified XML tree to file */ |
---|
81 | xmlSaveFile(omf_new_name, omf_doc); |
---|
82 | xmlFreeDoc(omf_doc); |
---|
83 | |
---|
84 | if (node != NULL) { |
---|
85 | return 1; |
---|
86 | } |
---|
87 | |
---|
88 | return 0; |
---|
89 | } |
---|