Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/xml/PushParser.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 29 окт. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/fmt/xml/PushParser.h>
23
24 namespace lsp
25 {
26 namespace xml
27 {
28
29 PushParser::PushParser()
30 {
31 }
32
33 PushParser::~PushParser()
34 {
35 }
36
37 void PushParser::drop_list(lltl::parray<LSPString> *list)
38 {
39 for (size_t i=0, n=list->size(); i<n; ++i)
40 {
41 LSPString *item = list->uget(i);
42 if (item != NULL)
43 delete item;
44 }
45 list->clear();
46 }
47
48 status_t PushParser::parse_file(IXMLHandler *handler, const char *path, const char *charset)
49 {
50 IXMLHandler stub;
51 status_t res = sParser.open(path, charset);
52 if (res == STATUS_OK)
53 res = parse_document((handler != NULL) ? handler : &stub);
54 if (res == STATUS_OK)
55 res = sParser.close();
56 else
57 sParser.close();
58 return res;
59 }
60
61 status_t PushParser::parse_file(IXMLHandler *handler, const LSPString *path, const char *charset)
62 {
63 IXMLHandler stub;
64 status_t res = sParser.open(path, charset);
65 if (res == STATUS_OK)
66 res = parse_document((handler != NULL) ? handler : &stub);
67 if (res == STATUS_OK)
68 res = sParser.close();
69 else
70 sParser.close();
71 return res;
72 }
73
74 status_t PushParser::parse_file(IXMLHandler *handler, const io::Path *path, const char *charset)
75 {
76 IXMLHandler stub;
77 status_t res = sParser.open(path, charset);
78 if (res == STATUS_OK)
79 res = parse_document((handler != NULL) ? handler : &stub);
80 if (res == STATUS_OK)
81 res = sParser.close();
82 else
83 sParser.close();
84 return res;
85 }
86
87 status_t PushParser::parse_data(IXMLHandler *handler, io::IInStream *is, size_t flags, const char *charset)
88 {
89 IXMLHandler stub;
90 status_t res = sParser.wrap(is, flags, charset);
91 if (res == STATUS_OK)
92 res = parse_document((handler != NULL) ? handler : &stub);
93 if (res == STATUS_OK)
94 res = sParser.close();
95 else
96 sParser.close();
97 return res;
98 }
99
100 status_t PushParser::parse_data(IXMLHandler *handler, const char *str, const char *charset)
101 {
102 IXMLHandler stub;
103 status_t res = sParser.wrap(str, charset);
104 if (res == STATUS_OK)
105 res = parse_document((handler != NULL) ? handler : &stub);
106 if (res == STATUS_OK)
107 res = sParser.close();
108 else
109 sParser.close();
110 return res;
111 }
112
113 status_t PushParser::parse_data(IXMLHandler *handler, const LSPString *str)
114 {
115 IXMLHandler stub;
116 status_t res = sParser.wrap(str);
117 if (res == STATUS_OK)
118 res = parse_document((handler != NULL) ? handler : &stub);
119 if (res == STATUS_OK)
120 res = sParser.close();
121 else
122 sParser.close();
123 return res;
124 }
125
126 status_t PushParser::parse_data(IXMLHandler *handler, io::IInSequence *seq, size_t flags)
127 {
128 IXMLHandler stub;
129 status_t res = sParser.wrap(seq, flags);
130 if (res == STATUS_OK)
131 res = parse_document((handler != NULL) ? handler : &stub);
132 if (res == STATUS_OK)
133 res = sParser.close();
134 else
135 sParser.close();
136 return res;
137 }
138
139 status_t PushParser::parse_document(IXMLHandler *handler)
140 {
141 status_t token, res = STATUS_OK, last = -1;
142 LSPString tmp, *ptmp;
143 lltl::parray<LSPString> ctag;
144
145 do
146 {
147 // Get next token
148 if ((token = sParser.read_next()) < 0)
149 {
150 res = -token;
151 break;
152 }
153
154 // Is there tag element pending?
155 if ((ctag.size() > 0) && ((token != XT_ATTRIBUTE) && (token != XT_ENTITY_RESOLVE)))
156 {
157 // Add NULL-terminating element
158 if (!ctag.add(static_cast<LSPString *>(NULL)))
159 return STATUS_NO_MEM;
160
161 // Analyze state
162 LSPString **atts = ctag.array();
163 size_t n = ctag.size();
164 if (n & 1) // Number of elements should be even
165 {
166 res = STATUS_CORRUPTED;
167 break;
168 }
169
170 // Call handler
171 res = handler->start_element(atts[0], &atts[1]);
172 drop_list(&ctag);
173 if (res != STATUS_OK)
174 break;
175 }
176
177 // Dispatch event
178 last = token;
179 switch (token)
180 {
181 case XT_START_DOCUMENT:
182 res = handler->start_document(
183 sParser.xml_version(),
184 sParser.version(),
185 sParser.encoding(),
186 sParser.is_standalone()
187 );
188 break;
189
190 case XT_END_DOCUMENT:
191 res = handler->end_document();
192 break;
193
194 case XT_CDATA:
195 res = handler->cdata(sParser.value());
196 break;
197
198 case XT_CHARACTERS:
199 res = handler->characters(sParser.value());
200 break;
201
202 case XT_COMMENT:
203 res = handler->comment(sParser.value());
204 break;
205
206 case XT_ENTITY_RESOLVE:
207 res = handler->resolve(&tmp, sParser.name());
208 if (res == STATUS_OK)
209 res = sParser.set_value(&tmp);
210 tmp.clear();
211 break;
212
213 case XT_START_ELEMENT:
214 // Create copy of tag name
215 if ((ptmp = sParser.name()->clone()) == NULL)
216 {
217 res = STATUS_NO_MEM;
218 break;
219 }
220 else if (!ctag.add(ptmp))
221 {
222 delete ptmp;
223 res = STATUS_NO_MEM;
224 break;
225 }
226 break;
227
228 case XT_ATTRIBUTE:
229 // Create copy of attribute name
230 if ((ptmp = sParser.name()->clone()) == NULL)
231 {
232 res = STATUS_NO_MEM;
233 break;
234 }
235 else if (!ctag.add(ptmp))
236 {
237 delete ptmp;
238 res = STATUS_NO_MEM;
239 break;
240 }
241
242 // Create copy of tag value
243 if ((ptmp = sParser.value()->clone()) == NULL)
244 {
245 res = STATUS_NO_MEM;
246 break;
247 }
248 else if (!ctag.add(ptmp))
249 {
250 delete ptmp;
251 res = STATUS_NO_MEM;
252 break;
253 }
254 break;
255
256 case XT_END_ELEMENT:
257 res = handler->end_element(sParser.name());
258 break;
259
260 case XT_PROCESSING_INSTRUCTION:
261 res = handler->processing(sParser.name(), sParser.value());
262 break;
263
264 case XT_DTD:
265 res = handler->doctype(sParser.doctype(), sParser.pub_literal(), sParser.sys_literal());
266 break;
267
268 default:
269 res = STATUS_CORRUPTED;
270 break;
271 }
272 } while (res == STATUS_OK);
273
274 // Drop list
275 drop_list(&ctag);
276
277 // Return result
278 return ((res == STATUS_EOF) && (last == XT_END_DOCUMENT)) ? STATUS_OK : res;
279 }
280
281 } /* namespace xml */
282 } /* namespace lsp */