4 * XML parser abstraction
6 * Copyright (c) 2017, Novell Inc.
8 * This program is licensed under the BSD license, read LICENSE.BSD
9 * for further information
12 #include <sys/types.h>
18 #include <libxml/parser.h>
25 #include "solv_xmlparser.h"
28 add_contentspace(struct solv_xmlparser *xmlp, int l)
30 l += xmlp->lcontent + 1; /* plus room for trailing zero */
31 if (l > xmlp->acontent)
33 xmlp->acontent = l + 256;
34 xmlp->content = solv_realloc(xmlp->content, xmlp->acontent);
41 character_data(void *userData, const xmlChar *s, int len)
44 character_data(void *userData, const XML_Char *s, int len)
47 struct solv_xmlparser *xmlp = userData;
49 if (!xmlp->docontent || !len)
51 add_contentspace(xmlp, len);
52 memcpy(xmlp->content + xmlp->lcontent, s, len);
53 xmlp->lcontent += len;
58 start_element(void *userData, const xmlChar *name, const xmlChar **atts)
61 start_element(void *userData, const char *name, const char **atts)
64 struct solv_xmlparser *xmlp = userData;
65 struct solv_xmlparser_element *elements;
67 struct solv_xmlparser_element *el;
75 elementhelper = xmlp->elementhelper;
76 elements = xmlp->elements;
77 oldstate = xmlp->state;
78 for (i = elementhelper[xmlp->nelements + oldstate]; i; i = elementhelper[i - 1])
79 if (!strcmp(elements[i - 1].element, (char *)name))
84 fprintf(stderr, "into unknown: %s\n", name);
89 el = xmlp->elements + i - 1;
90 queue_push(&xmlp->elementq, xmlp->state);
91 xmlp->state = el->tostate;
92 xmlp->docontent = el->docontent;
97 static const char *nullattr;
98 atts = (const xmlChar **)&nullattr;
101 if (xmlp->state != oldstate)
102 xmlp->startelement(xmlp, xmlp->state, el->element, (const char **)atts);
107 end_element(void *userData, const xmlChar *name)
110 end_element(void *userData, const char *name)
113 struct solv_xmlparser *xmlp = userData;
115 if (xmlp->unknowncnt)
122 xmlp->content[xmlp->lcontent] = 0;
123 if (xmlp->elementq.count && xmlp->state != xmlp->elementq.elements[xmlp->elementq.count - 1])
124 xmlp->endelement(xmlp, xmlp->state, xmlp->content);
125 xmlp->state = queue_pop(&xmlp->elementq);
131 solv_xmlparser_init(struct solv_xmlparser *xmlp,
132 struct solv_xmlparser_element *elements,
134 void (*startelement)(struct solv_xmlparser *, int state, const char *name, const char **atts),
135 void (*endelement)(struct solv_xmlparser *, int state, char *content))
137 int i, nstates, nelements;
138 struct solv_xmlparser_element *el;
141 memset(xmlp, 0, sizeof(*xmlp));
144 for (el = elements; el->element; el++)
147 if (el->fromstate > nstates)
148 nstates = el->fromstate;
149 if (el->tostate > nstates)
150 nstates = el->tostate;
154 xmlp->elements = elements;
155 xmlp->nelements = nelements;
156 elementhelper = solv_calloc(nelements + nstates, sizeof(Id));
157 for (i = nelements - 1; i >= 0; i--)
159 int fromstate = elements[i].fromstate;
160 elementhelper[i] = elementhelper[nelements + fromstate];
161 elementhelper[nelements + fromstate] = i + 1;
163 xmlp->elementhelper = elementhelper;
164 queue_init(&xmlp->elementq);
165 xmlp->acontent = 256;
166 xmlp->content = solv_malloc(xmlp->acontent);
168 xmlp->userdata = userdata;
169 xmlp->startelement = startelement;
170 xmlp->endelement = endelement;
174 solv_xmlparser_free(struct solv_xmlparser *xmlp)
176 xmlp->elementhelper = solv_free(xmlp->elementhelper);
177 queue_free(&xmlp->elementq);
178 xmlp->content = solv_free(xmlp->content);
179 xmlp->errstr = solv_free(xmlp->errstr);
183 set_error(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column)
185 solv_free(xmlp->errstr);
186 xmlp->errstr = solv_strdup(errstr);
188 xmlp->column = column;
194 create_parser(struct solv_xmlparser *xmlp)
196 /* delayed to parse_block so that we have the first bytes */
201 free_parser(struct solv_xmlparser *xmlp)
204 xmlFreeParserCtxt(xmlp->parser);
208 static xmlParserCtxtPtr
209 create_parser_ctx(struct solv_xmlparser *xmlp, char *buf, int l)
212 memset(&sax, 0, sizeof(sax));
213 sax.startElement = start_element;
214 sax.endElement = end_element;
215 sax.characters = character_data;
216 return xmlCreatePushParserCtxt(&sax, xmlp, buf, l, NULL);
220 parse_block(struct solv_xmlparser *xmlp, char *buf, int l)
224 int l2 = l > 4 ? 4 : 0;
225 xmlp->parser = create_parser_ctx(xmlp, buf, l2);
228 set_error(xmlp, "could not create parser", 0, 0);
236 if (xmlParseChunk(xmlp->parser, buf, l, l == 0 ? 1 : 0))
238 xmlErrorPtr err = xmlCtxtGetLastError(xmlp->parser);
239 set_error(xmlp, err->message, err->line, err->int2);
246 solv_xmlparser_lineno(struct solv_xmlparser *xmlp)
248 return (unsigned int)xmlSAX2GetLineNumber(xmlp->parser);
254 create_parser(struct solv_xmlparser *xmlp)
256 xmlp->parser = XML_ParserCreate(NULL);
259 XML_SetUserData(xmlp->parser, xmlp);
260 XML_SetElementHandler(xmlp->parser, start_element, end_element);
261 XML_SetCharacterDataHandler(xmlp->parser, character_data);
266 free_parser(struct solv_xmlparser *xmlp)
268 XML_ParserFree(xmlp->parser);
273 parse_block(struct solv_xmlparser *xmlp, char *buf, int l)
275 if (XML_Parse(xmlp->parser, buf, l, l == 0) == XML_STATUS_ERROR)
277 set_error(xmlp, XML_ErrorString(XML_GetErrorCode(xmlp->parser)), XML_GetCurrentLineNumber(xmlp->parser), XML_GetCurrentColumnNumber(xmlp->parser));
284 solv_xmlparser_lineno(struct solv_xmlparser *xmlp)
286 return (unsigned int)XML_GetCurrentLineNumber(xmlp->parser);
292 solv_xmlparser_parse(struct solv_xmlparser *xmlp, FILE *fp)
295 int l, ret = SOLV_XMLPARSER_OK;
298 xmlp->unknowncnt = 0;
301 queue_empty(&xmlp->elementq);
303 if (!create_parser(xmlp))
305 set_error(xmlp, "could not create parser", 0, 0);
306 return SOLV_XMLPARSER_ERROR;
310 l = fread(buf, 1, sizeof(buf), fp);
311 if (!parse_block(xmlp, buf, l))
313 ret = SOLV_XMLPARSER_ERROR;
324 solv_xmlparser_contentspace(struct solv_xmlparser *xmlp, int l)
327 if (l > xmlp->acontent)
329 xmlp->acontent = l + 256;
330 xmlp->content = solv_realloc(xmlp->content, xmlp->acontent);
332 return xmlp->content;