25
|
1 |
/* |
|
2 |
* This program is free software; you can redistribute it and/or modify |
|
3 |
* it under the terms of the GNU General Public License as published by |
|
4 |
* the Free Software Foundation; either version 2 of the License, or |
|
5 |
* (at your option) any later version. |
|
6 |
* |
|
7 |
* This program is distributed in the hope that it will be useful, |
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 |
* GNU General Public License for more details. |
|
11 |
* |
|
12 |
* You should have received a copy of the GNU General Public License |
|
13 |
* along with this program; if not, write to the Free Software |
|
14 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
15 |
* |
|
16 |
* Jabber |
|
17 |
* Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ |
|
18 |
*/ |
|
19 |
|
|
20 |
#include <libxode.h> |
|
21 |
|
|
22 |
void expat_startElement(void* userdata, const char* name, const char** atts) |
|
23 |
{ |
|
24 |
/* get the xmlnode pointed to by the userdata */ |
|
25 |
xmlnode *x = userdata; |
|
26 |
xmlnode current = *x; |
|
27 |
|
|
28 |
if (current == NULL) |
|
29 |
{ |
|
30 |
/* allocate a base node */ |
|
31 |
current = xmlnode_new_tag(name); |
|
32 |
xmlnode_put_expat_attribs(current, atts); |
|
33 |
*x = current; |
|
34 |
} |
|
35 |
else |
|
36 |
{ |
|
37 |
*x = xmlnode_insert_tag(current, name); |
|
38 |
xmlnode_put_expat_attribs(*x, atts); |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
void expat_endElement(void* userdata, const char* name) |
|
43 |
{ |
|
44 |
xmlnode *x = userdata; |
|
45 |
xmlnode current = *x; |
|
46 |
|
|
47 |
current->complete = 1; |
|
48 |
current = xmlnode_get_parent(current); |
|
49 |
|
|
50 |
/* if it's NULL we've hit the top folks, otherwise back up a level */ |
|
51 |
if(current != NULL) |
|
52 |
*x = current; |
|
53 |
} |
|
54 |
|
|
55 |
void expat_charData(void* userdata, const char* s, int len) |
|
56 |
{ |
|
57 |
xmlnode *x = userdata; |
|
58 |
xmlnode current = *x; |
|
59 |
|
|
60 |
xmlnode_insert_cdata(current, s, len); |
|
61 |
} |
|
62 |
|
|
63 |
|
|
64 |
xmlnode xmlnode_str(char *str, int len) |
|
65 |
{ |
|
66 |
XML_Parser p; |
|
67 |
xmlnode *x, node; /* pointer to an xmlnode */ |
|
68 |
|
|
69 |
if(NULL == str) |
|
70 |
return NULL; |
|
71 |
|
|
72 |
x = malloc(sizeof(void *)); |
|
73 |
|
|
74 |
*x = NULL; /* pointer to NULL */ |
|
75 |
p = XML_ParserCreate(NULL); |
|
76 |
XML_SetUserData(p, x); |
|
77 |
XML_SetElementHandler(p, expat_startElement, expat_endElement); |
|
78 |
XML_SetCharacterDataHandler(p, expat_charData); |
|
79 |
if(!XML_Parse(p, str, len, 1)) |
|
80 |
{ |
|
81 |
/* jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/ |
|
82 |
xmlnode_free(*x); |
|
83 |
*x = NULL; |
|
84 |
} |
|
85 |
node = *x; |
|
86 |
free(x); |
|
87 |
XML_ParserFree(p); |
|
88 |
return node; /* return the xmlnode x points to */ |
|
89 |
} |
|
90 |
|
|
91 |
xmlnode xmlnode_file(char *file) |
|
92 |
{ |
|
93 |
XML_Parser p; |
|
94 |
xmlnode *x, node; /* pointer to an xmlnode */ |
|
95 |
char buf[BUFSIZ]; |
|
96 |
int done, fd, len; |
|
97 |
|
|
98 |
if(NULL == file) |
|
99 |
return NULL; |
|
100 |
|
|
101 |
fd = open(file,O_RDONLY); |
|
102 |
if(fd < 0) |
|
103 |
return NULL; |
|
104 |
|
|
105 |
x = malloc(sizeof(void *)); |
|
106 |
|
|
107 |
*x = NULL; /* pointer to NULL */ |
|
108 |
p = XML_ParserCreate(NULL); |
|
109 |
XML_SetUserData(p, x); |
|
110 |
XML_SetElementHandler(p, expat_startElement, expat_endElement); |
|
111 |
XML_SetCharacterDataHandler(p, expat_charData); |
|
112 |
do{ |
|
113 |
len = read(fd, buf, BUFSIZ); |
|
114 |
done = len < BUFSIZ; |
|
115 |
if(!XML_Parse(p, buf, len, done)) |
|
116 |
{ |
|
117 |
/* jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/ |
|
118 |
xmlnode_free(*x); |
|
119 |
*x = NULL; |
|
120 |
done = 1; |
|
121 |
} |
|
122 |
}while(!done); |
|
123 |
|
|
124 |
node = *x; |
|
125 |
XML_ParserFree(p); |
|
126 |
free(x); |
|
127 |
close(fd); |
|
128 |
return node; /* return the xmlnode x points to */ |
|
129 |
} |
|
130 |
|
|
131 |
int xmlnode2file(char *file, xmlnode node) |
|
132 |
{ |
|
133 |
char *doc; |
|
134 |
int fd, i; |
|
135 |
|
|
136 |
if(file == NULL || node == NULL) |
|
137 |
return -1; |
|
138 |
|
|
139 |
fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0600); |
|
140 |
if(fd < 0) |
|
141 |
return -1; |
|
142 |
|
|
143 |
doc = xmlnode2str(node); |
|
144 |
i = write(fd,doc,strlen(doc)); |
|
145 |
if(i < 0) |
|
146 |
return -1; |
|
147 |
|
|
148 |
close(fd); |
|
149 |
return 1; |
|
150 |
} |
|
151 |
|
|
152 |
void xmlnode_put_expat_attribs(xmlnode owner, const char** atts) |
|
153 |
{ |
|
154 |
int i = 0; |
|
155 |
if (atts == NULL) return; |
|
156 |
while (atts[i] != '\0') |
|
157 |
{ |
|
158 |
xmlnode_put_attrib(owner, atts[i], atts[i+1]); |
|
159 |
i += 2; |
|
160 |
} |
|
161 |
} |