1 | /** |
---|
2 | * |
---|
3 | * @file xhandler.c |
---|
4 | * @author Aleix Conchillo Flaque <aleix@member.fsf.org> |
---|
5 | * @date Mon Nov 25, 2002 00:21 |
---|
6 | * @brief SCEW Expat handlers |
---|
7 | * |
---|
8 | * $Id: xhandler.c,v 1.2 2004/05/25 20:23:05 aleix Exp $ |
---|
9 | * |
---|
10 | * @if copyright |
---|
11 | * |
---|
12 | * Copyright (C) 2002, 2003, 2004 Aleix Conchillo Flaque |
---|
13 | * |
---|
14 | * SCEW is free software; you can redistribute it and/or |
---|
15 | * modify it under the terms of the GNU Lesser General Public |
---|
16 | * License as published by the Free Software Foundation; either |
---|
17 | * version 2.1 of the License, or (at your option) any later version. |
---|
18 | * |
---|
19 | * SCEW is distributed in the hope that it will be useful, |
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | * Lesser General Public License for more details. |
---|
23 | * |
---|
24 | * You should have received a copy of the GNU Lesser General Public |
---|
25 | * License along with this library; if not, write to the Free Software |
---|
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
27 | * |
---|
28 | * @endif |
---|
29 | */ |
---|
30 | |
---|
31 | #include "xhandler.h" |
---|
32 | |
---|
33 | #include "xparser.h" |
---|
34 | |
---|
35 | #include "str.h" |
---|
36 | |
---|
37 | #include <stdio.h> |
---|
38 | |
---|
39 | void |
---|
40 | xmldecl_handler(void* data, XML_Char const* version, XML_Char const* encoding, |
---|
41 | int standalone) |
---|
42 | { |
---|
43 | scew_parser* parser = (scew_parser*) data; |
---|
44 | |
---|
45 | /* Avoid warning: standalone is unused */ |
---|
46 | (void) standalone; |
---|
47 | |
---|
48 | if (parser == NULL) |
---|
49 | { |
---|
50 | return; |
---|
51 | } |
---|
52 | |
---|
53 | if (parser->tree == NULL) |
---|
54 | { |
---|
55 | parser->tree = scew_tree_create(); |
---|
56 | } |
---|
57 | |
---|
58 | if (parser->tree == NULL) |
---|
59 | { |
---|
60 | return; |
---|
61 | } |
---|
62 | |
---|
63 | if (version != NULL) |
---|
64 | { |
---|
65 | parser->tree->version = scew_strdup(version); |
---|
66 | } |
---|
67 | if (encoding != NULL) |
---|
68 | { |
---|
69 | parser->tree->encoding = scew_strdup(encoding); |
---|
70 | } |
---|
71 | |
---|
72 | /* by now, we ignore standalone attribute */ |
---|
73 | } |
---|
74 | |
---|
75 | void |
---|
76 | start_handler(void* data, XML_Char const* elem, XML_Char const** attr) |
---|
77 | { |
---|
78 | int i = 0; |
---|
79 | scew_parser* parser = (scew_parser*) data; |
---|
80 | |
---|
81 | if (parser == NULL) |
---|
82 | { |
---|
83 | return; |
---|
84 | } |
---|
85 | |
---|
86 | if ((parser->tree == NULL) || (scew_tree_root(parser->tree) == NULL)) |
---|
87 | { |
---|
88 | if (parser->tree == NULL) |
---|
89 | { |
---|
90 | parser->tree = scew_tree_create(); |
---|
91 | } |
---|
92 | parser->current = scew_tree_add_root(parser->tree, elem); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | stack_push(&parser->stack, parser->current); |
---|
97 | parser->current = scew_element_add(parser->current, elem); |
---|
98 | } |
---|
99 | |
---|
100 | for (i = 0; attr[i]; i += 2) |
---|
101 | { |
---|
102 | scew_element_add_attr_pair(parser->current, attr[i], attr[i + 1]); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | void |
---|
107 | end_handler(void* data, XML_Char const* elem) |
---|
108 | { |
---|
109 | XML_Char* contents = NULL; |
---|
110 | scew_element* current = NULL; |
---|
111 | scew_parser* parser = (scew_parser*) data; |
---|
112 | |
---|
113 | /* Avoid warning: elem is unused */ |
---|
114 | (void) elem; |
---|
115 | |
---|
116 | if (parser == NULL) |
---|
117 | { |
---|
118 | return; |
---|
119 | } |
---|
120 | |
---|
121 | current = parser->current; |
---|
122 | if ((current != NULL) && (current->contents != NULL)) |
---|
123 | { |
---|
124 | if (parser->ignore_whitespaces) |
---|
125 | { |
---|
126 | scew_strtrim(current->contents); |
---|
127 | if (scew_strlen(current->contents) == 0) |
---|
128 | { |
---|
129 | free(current->contents); |
---|
130 | current->contents = NULL; |
---|
131 | } |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | contents = scew_strdup(current->contents); |
---|
136 | scew_strtrim(contents); |
---|
137 | if (scew_strlen(contents) == 0) |
---|
138 | { |
---|
139 | free(current->contents); |
---|
140 | current->contents = NULL; |
---|
141 | } |
---|
142 | free(contents); |
---|
143 | } |
---|
144 | } |
---|
145 | parser->current = stack_pop(&parser->stack); |
---|
146 | } |
---|
147 | |
---|
148 | void |
---|
149 | char_handler(void* data, XML_Char const* s, int len) |
---|
150 | { |
---|
151 | int total = 0; |
---|
152 | int total_old = 0; |
---|
153 | scew_element* current = NULL; |
---|
154 | scew_parser* parser = (scew_parser*) data; |
---|
155 | |
---|
156 | if (parser == NULL) |
---|
157 | { |
---|
158 | return; |
---|
159 | } |
---|
160 | |
---|
161 | current = parser->current; |
---|
162 | |
---|
163 | if (current == NULL) |
---|
164 | { |
---|
165 | return; |
---|
166 | } |
---|
167 | |
---|
168 | if (current->contents != NULL) |
---|
169 | { |
---|
170 | total_old = scew_strlen(current->contents); |
---|
171 | } |
---|
172 | total = (total_old + len + 1) * sizeof(XML_Char); |
---|
173 | current->contents = (XML_Char*) realloc(current->contents, total); |
---|
174 | |
---|
175 | if (total_old == 0) |
---|
176 | { |
---|
177 | current->contents[0] = '\0'; |
---|
178 | } |
---|
179 | |
---|
180 | scew_strncat(current->contents, s, len); |
---|
181 | } |
---|