2 * UCW Library -- A simple XML parser
4 * (c) 2007--2008 Pavel Charvat <pchar@ucw.cz>
5 * (c) 2015 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include <ucw-xml/xml.h>
15 #include <ucw-xml/dtd.h>
16 #include <ucw-xml/internals.h>
17 #include <ucw/fastbuf.h>
18 #include <ucw/ff-unicode.h>
19 #include <ucw/unicode.h>
20 #include <ucw/chartype.h>
21 #include <ucw/hashfunc.h>
25 /*** Basic parsing ***/
28 xml_fatal_expected(struct xml_context *ctx, uint c)
30 if (c >= 32 && c < 127)
31 xml_fatal(ctx, "Expected '%c'", c);
33 xml_fatal(ctx, "Expected U+%04x", c);
37 xml_fatal_expected_white(struct xml_context *ctx)
39 xml_fatal(ctx, "Expected a white space");
43 xml_fatal_expected_quot(struct xml_context *ctx)
45 xml_fatal(ctx, "Expected a quotation mark");
49 xml_parse_eq(struct xml_context *ctx)
51 /* Eq ::= S? '=' S? */
52 xml_parse_white(ctx, 0);
53 xml_parse_char(ctx, '=');
54 xml_parse_white(ctx, 0);
57 /*** Names and nmtokens ***/
60 xml_parse_string(struct xml_context *ctx, struct mempool *pool, uint first_cat, uint next_cat, char *err)
62 char *p = mp_start_noalign(pool, 2);
63 *p++ = '<'; /* We always prepend a '<', so we can seek backwards in the string */
64 if (unlikely(!(xml_peek_cat(ctx) & first_cat)))
65 xml_fatal(ctx, "%s", err);
68 p = mp_spread(pool, p, 5);
69 p = utf8_32_put(p, xml_skip_char(ctx));
71 while (xml_peek_cat(ctx) & next_cat);
73 return mp_end(pool, p) + 1;
77 xml_skip_string(struct xml_context *ctx, uint first_cat, uint next_cat, char *err)
79 if (unlikely(!(xml_get_cat(ctx) & first_cat)))
80 xml_fatal(ctx, "%s", err);
81 while (xml_peek_cat(ctx) & next_cat)
86 xml_parse_name(struct xml_context *ctx, struct mempool *pool)
88 /* Name ::= NameStartChar (NameChar)* */
89 return xml_parse_string(ctx, pool, ctx->cat_sname, ctx->cat_name, "Expected a name");
93 xml_skip_name(struct xml_context *ctx)
95 xml_skip_string(ctx, ctx->cat_sname, ctx->cat_name, "Expected a name");
99 xml_parse_nmtoken(struct xml_context *ctx, struct mempool *pool)
101 /* Nmtoken ::= (NameChar)+ */
102 return xml_parse_string(ctx, pool, ctx->cat_name, ctx->cat_name, "Expected a nmtoken");
105 /*** Simple literals ***/
108 xml_parse_system_literal(struct xml_context *ctx, struct mempool *pool)
110 /* SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") */
111 char *p = mp_start_noalign(pool, 1);
112 uint q = xml_parse_quote(ctx), c;
113 while ((c = xml_get_char(ctx)) != q)
115 p = mp_spread(pool, p, 5);
116 p = utf8_32_put(p, c);
119 return mp_end(pool, p);
123 xml_parse_pubid_literal(struct xml_context *ctx, struct mempool *pool)
125 /* PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" */
126 char *p = mp_start_noalign(pool, 1);
127 uint q = xml_parse_quote(ctx), c;
128 while ((c = xml_get_char(ctx)) != q)
130 if (unlikely(!(xml_last_cat(ctx) & XML_CHAR_PUBID)))
131 xml_fatal(ctx, "Expected a pubid character");
132 p = mp_spread(pool, p, 2);
136 return mp_end(pool, p);
142 xml_push_comment(struct xml_context *ctx)
144 TRACE(ctx, "push_comment");
145 /* Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
146 * Already parsed: '<!-' */
147 xml_parse_char(ctx, '-');
148 struct xml_node *n = xml_push_dom(ctx, NULL);
149 n->type = XML_NODE_COMMENT;
150 char *p = mp_start_noalign(ctx->pool, 6);
153 if (xml_get_char(ctx) == '-')
154 if (xml_get_char(ctx) == '-')
158 p = utf8_32_put(p, xml_last_char(ctx));
159 p = mp_spread(ctx->pool, p, 6);
161 xml_parse_char(ctx, '>');
163 n->len = p - (char *)mp_ptr(ctx->pool);
164 n->text = mp_end(ctx->pool, p + 1);
165 if ((ctx->flags & XML_REPORT_COMMENTS) && ctx->h_comment)
170 xml_pop_comment(struct xml_context *ctx)
172 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_COMMENTS));
174 TRACE(ctx, "pop_comment");
178 xml_skip_comment(struct xml_context *ctx)
180 TRACE(ctx, "skip_comment");
181 xml_parse_char(ctx, '-');
182 while (xml_get_char(ctx) != '-' || xml_get_char(ctx) != '-');
183 xml_parse_char(ctx, '>');
187 /*** Processing instructions ***/
190 xml_push_pi(struct xml_context *ctx)
192 TRACE(ctx, "push_pi");
193 /* Parses a PI to ctx->value and ctx->name:
194 * PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
195 * PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
196 * Already parsed: '<?' */
197 struct xml_node *n = xml_push_dom(ctx, NULL);
198 n->type = XML_NODE_PI;
199 n->name = xml_parse_name(ctx, ctx->pool);
200 if (unlikely(!strcasecmp(n->name, "xml")))
201 xml_error(ctx, "Reserved PI target");
202 char *p = mp_start_noalign(ctx->pool, 5);
203 if (!xml_parse_white(ctx, 0))
204 xml_parse_seq(ctx, "?>");
208 if (xml_get_char(ctx) == '?')
209 if (xml_peek_char(ctx) == '>')
217 p = utf8_32_put(p, xml_last_char(ctx));
218 p = mp_spread(ctx->pool, p, 5);
221 n->len = p - (char *)mp_ptr(ctx->pool);
222 n->text = mp_end(ctx->pool, p + 1);
223 if ((ctx->flags & XML_REPORT_PIS) && ctx->h_pi)
228 xml_pop_pi(struct xml_context *ctx)
230 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_PIS));
232 TRACE(ctx, "pop_pi");
236 xml_skip_pi(struct xml_context *ctx)
238 TRACE(ctx, "skip_pi");
239 if (ctx->flags & XML_VALIDATING)
241 struct mempool_state state;
242 mp_save(ctx->stack, &state);
243 if (unlikely(!strcasecmp(xml_parse_name(ctx, ctx->stack), "xml")))
244 xml_error(ctx, "Reserved PI target");
245 mp_restore(ctx->stack, &state);
246 if (!xml_parse_white(ctx, 0))
248 xml_parse_seq(ctx, "?>");
254 if (xml_get_char(ctx) == '?')
255 if (xml_peek_char(ctx) == '>')
261 /*** Character references ***/
264 xml_parse_char_ref(struct xml_context *ctx)
266 TRACE(ctx, "parse_char_ref");
267 /* CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
268 * Already parsed: '&#' */
270 if (xml_get_char(ctx) == 'x')
272 if (!(xml_get_cat(ctx) & XML_CHAR_XDIGIT))
274 xml_error(ctx, "Expected a hexadecimal value of character reference");
279 v = (v << 4) + Cxvalue(xml_last_char(ctx));
281 while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_XDIGIT));
285 if (!(xml_last_cat(ctx) & XML_CHAR_DIGIT))
287 xml_error(ctx, "Expected a numeric value of character reference");
292 v = v * 10 + xml_last_char(ctx) - '0';
294 while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_DIGIT));
296 uint cat = xml_char_cat(v);
297 if (!(cat & ctx->cat_unrestricted))
299 xml_error(ctx, "Character reference out of range");
302 if (xml_last_char(ctx) == ';')
307 xml_error(ctx, "Expected ';'");
309 while (xml_last_char(ctx) != ';')
312 return UNI_REPLACEMENT;
315 /*** References to general entities ***/
318 xml_parse_ref(struct xml_context *ctx)
320 /* Reference ::= EntityRef | CharRef
321 * EntityRef ::= '&' Name ';'
322 * Already parsed: '&' */
323 struct fastbuf *out = &ctx->chars;
324 if (xml_peek_char(ctx) == '#')
327 bput_utf8_32(out, xml_parse_char_ref(ctx));
331 TRACE(ctx, "parse_ge_ref");
332 struct mempool_state state;
333 mp_save(ctx->stack, &state);
334 char *name = xml_parse_name(ctx, ctx->stack);
335 xml_parse_char(ctx, ';');
336 struct xml_dtd_entity *ent = xml_dtd_find_entity(ctx, name);
339 xml_error(ctx, "Unknown entity &%s;", name);
344 else if (ent->flags & XML_DTD_ENTITY_TRIVIAL)
346 TRACE(ctx, "Trivial entity &%s;", name);
347 bputs(out, ent->text);
351 TRACE(ctx, "Pushed entity &%s;", name);
352 mp_restore(ctx->stack, &state);
354 xml_push_entity(ctx, ent);
357 mp_restore(ctx->stack, &state);
362 /*** Character data ***/
365 xml_spout_chars(struct fastbuf *fb)
367 if (fb->bptr < fb->bufend)
369 struct xml_context *ctx = SKIP_BACK(struct xml_context, chars, fb);
370 struct mempool *pool = ctx->pool;
371 if (fb->bufend != fb->buffer)
373 TRACE(ctx, "growing chars");
374 uint len = fb->bufend - fb->buffer;
375 uint reported = fb->bstop - fb->buffer;
376 fb->buffer = mp_expand(pool);
377 fb->bufend = fb->buffer + mp_avail(pool);
378 fb->bptr = fb->buffer + len;
379 fb->bstop = fb->buffer + reported;
383 TRACE(ctx, "starting chars");
384 mp_save(pool, &ctx->chars_state);
385 fb->bptr = fb->buffer = fb->bstop = mp_start_noalign(pool, 2);
386 fb->bufend = fb->buffer + mp_avail(pool) - 1;
391 xml_end_chars(struct xml_context *ctx, char **out)
393 struct fastbuf *fb = &ctx->chars;
394 uint len = fb->bptr - fb->buffer;
397 TRACE(ctx, "ending chars");
399 *out = mp_end(ctx->pool, fb->bptr + 1);
400 fb->bufend = fb->bstop = fb->bptr = fb->buffer;
406 xml_report_chars(struct xml_context *ctx, char **out)
408 struct fastbuf *fb = &ctx->chars;
409 uint len = fb->bptr - fb->buffer;
414 fb->bstop = fb->bptr;
420 xml_flush_chars(struct xml_context *ctx)
423 uint len = xml_end_chars(ctx, &text), rlen;
426 if (ctx->flags & XML_NO_CHARS)
428 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_ignorable)
429 ctx->h_ignorable(ctx, text, len);
430 mp_restore(ctx->pool, &ctx->chars_state);
433 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_block && (rlen = xml_report_chars(ctx, &rtext)))
434 ctx->h_block(ctx, rtext, rlen);
435 if (!(ctx->flags & XML_ALLOC_CHARS) && !(ctx->flags & XML_REPORT_CHARS))
437 mp_restore(ctx->pool, &ctx->chars_state);
440 struct xml_node *n = xml_push_dom(ctx, &ctx->chars_state);
441 n->type = XML_NODE_CHARS;
444 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_chars)
451 xml_pop_chars(struct xml_context *ctx)
453 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_CHARS));
454 TRACE(ctx, "pop_chars");
458 xml_append_chars(struct xml_context *ctx)
460 TRACE(ctx, "append_chars");
461 struct fastbuf *out = &ctx->chars;
462 if (ctx->flags & XML_NO_CHARS)
463 while (xml_get_char(ctx) != '<')
464 if (xml_last_cat(ctx) & XML_CHAR_WHITE)
465 bput_utf8_32(out, xml_last_char(ctx));
468 xml_error(ctx, "This element must not contain character data");
469 while (xml_get_char(ctx) != '<');
473 while (xml_get_char(ctx) != '<')
474 if (xml_last_char(ctx) == '&')
480 bput_utf8_32(out, xml_last_char(ctx));
484 /*** CDATA sections ***/
487 xml_skip_cdata(struct xml_context *ctx)
489 TRACE(ctx, "skip_cdata");
490 xml_parse_seq(ctx, "CDATA[");
491 while (xml_get_char(ctx) != ']' || xml_get_char(ctx) != ']' || xml_get_char(ctx) != '>');
496 xml_append_cdata(struct xml_context *ctx)
498 /* CDSect :== '<![CDATA[' (Char* - (Char* ']]>' Char*)) ']]>'
499 * Already parsed: '<![' */
500 TRACE(ctx, "append_cdata");
501 if (ctx->flags & XML_NO_CHARS)
503 xml_error(ctx, "This element must not contain CDATA");
507 xml_parse_seq(ctx, "CDATA[");
508 struct fastbuf *out = &ctx->chars;
511 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_block && (rlen = xml_report_chars(ctx, &rtext)))
512 ctx->h_block(ctx, rtext, rlen);
515 if (xml_get_char(ctx) == ']')
517 if (xml_get_char(ctx) == ']')
518 if (xml_get_char(ctx) == '>')
524 bput_utf8_32(out, xml_last_char(ctx));
526 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_cdata && (rlen = xml_report_chars(ctx, &rtext)))
527 ctx->h_cdata(ctx, rtext, rlen);
531 /*** Attribute values ***/
534 xml_parse_attr_value(struct xml_context *ctx, struct xml_dtd_attr *attr UNUSED)
536 TRACE(ctx, "parse_attr_value");
537 /* AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'" */
538 /* FIXME: -- check value constrains / normalize leading/trailing WS and repeated WS */
539 struct mempool_state state;
540 uint quote = xml_parse_quote(ctx);
541 mp_save(ctx->stack, &state);
542 struct fastbuf *out = &ctx->chars;
543 struct xml_source *src = ctx->src;
546 uint c = xml_get_char(ctx);
552 else if (c == quote && src == ctx->src)
555 xml_error(ctx, "Attribute value must not contain '<'");
556 else if (xml_last_cat(ctx) & XML_CHAR_WHITE)
559 bput_utf8_32(out, c);
561 mp_restore(ctx->stack, &state);
563 return xml_end_chars(ctx, &text) ? text : "";
567 xml_normalize_white(struct xml_context *ctx UNUSED, char *text)
569 char *s = text, *d = text;
578 while (*++s == 0x20);
581 if (d != text && d[-1] == 0x20)
590 xml_raw_add_attr(struct xml_context *ctx, struct xml_node *e, char *name, char *value)
592 struct xml_attr *a = mp_alloc(ctx->pool, sizeof(*a));
594 a->ns = 0; /* Namespaces will be resolved later */
599 /* a->hash will be calculated later */
600 slist_add_tail(&e->attrs, &a->n);
604 xml_attr_hash(uint ns, char *name)
606 return hash_string(name) ^ hash_u32(ns);
610 xml_parse_attr(struct xml_context *ctx)
612 TRACE(ctx, "parse_attr");
613 /* Attribute ::= Name Eq AttValue */
614 struct xml_node *e = ctx->node;
615 char *n = xml_parse_name(ctx, ctx->pool);
617 char *v = xml_parse_attr_value(ctx, NULL);
618 xml_raw_add_attr(ctx, e, n, v);
622 xml_process_attr(struct xml_context *ctx, struct xml_attr *a)
624 struct xml_node *e = a->elem;
625 a->hash = xml_attr_hash(a->ns, a->name);
627 XML_ATTR_FOR_EACH(a2, e)
631 if (a2->hash == a->hash && a2->ns == a->ns && !strcmp(a2->name, a->name))
632 xml_error(ctx, "Attribute %s is not unique in element <%s>", xml_attr_qname(ctx, a), xml_node_qname(ctx, e));
637 xml_attr_find(struct xml_context *ctx, struct xml_node *node, char *name)
639 return xml_attr_find_ns(ctx, node, 0, name);
643 xml_attr_find_ns(struct xml_context *ctx UNUSED, struct xml_node *node, uint ns, char *name)
645 ASSERT(node->type == XML_NODE_ELEM);
646 uint hash = xml_attr_hash(ns, name);
647 XML_ATTR_FOR_EACH(a, node)
648 if (a->hash == hash && a->ns == ns && !strcmp(a->name, name))
654 xml_attr_value_ns(struct xml_context *ctx, struct xml_node *node, uint ns, char *name)
656 struct xml_attr *attr = xml_attr_find_ns(ctx, node, ns, name);
661 if (ns) /* So far, our DTD support is not namespace-aware */
663 struct xml_dtd_attr *dtd = xml_dtd_find_attr(ctx, node->dtd, name);
664 return dtd ? dtd->default_value : NULL;
668 xml_attr_value(struct xml_context *ctx, struct xml_node *node, char *name)
670 return xml_attr_value_ns(ctx, node, 0, name);
674 xml_attr_qname(struct xml_context *ctx UNUSED, struct xml_attr *attr)
676 char *n = attr->name;
685 xml_validate_element(struct xml_dtd_elem_node *root, struct xml_dtd_elem *elem)
688 return elem == root->elem;
690 SLIST_FOR_EACH(struct xml_dtd_elem_node *, son, root->sons)
691 if (xml_validate_element(son, elem))
697 xml_push_element(struct xml_context *ctx)
699 TRACE(ctx, "push_element");
700 /* EmptyElemTag | STag
701 * EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
702 * STag ::= '<' Name (S Attribute)* S? '>'
703 * Already parsed: '<' */
704 struct xml_node *e = xml_push_dom(ctx, NULL);
705 clist_init(&e->sons);
706 e->type = XML_NODE_ELEM;
707 e->name = xml_parse_name(ctx, ctx->pool);
708 slist_init(&e->attrs);
713 if (ctx->doctype && strcmp(e->name, ctx->doctype))
714 xml_error(ctx, "The root element <%s> does not match the document type <%s>", e->name, ctx->doctype);
719 else if (!(e->dtd = xml_dtd_find_elem(ctx, e->name)))
720 xml_error(ctx, "Undefined element <%s>", e->name);
723 struct xml_dtd_elem *dtd = e->dtd, *parent_dtd = e->parent ? e->parent->dtd : NULL;
724 if (dtd->type == XML_DTD_ELEM_MIXED)
725 ctx->flags &= ~XML_NO_CHARS;
727 ctx->flags |= XML_NO_CHARS;
729 if (parent_dtd->type == XML_DTD_ELEM_EMPTY)
730 xml_error(ctx, "Empty element must not contain children");
731 else if (parent_dtd->type != XML_DTD_ELEM_ANY)
733 // FIXME: validate regular expressions
734 if (!xml_validate_element(parent_dtd->node, dtd))
735 xml_error(ctx, "Unexpected element <%s>", e->name);
739 /* Parse attributes */
742 uint white = xml_parse_white(ctx, 0);
743 uint c = xml_get_char(ctx);
746 xml_parse_char(ctx, '>');
747 ctx->flags |= XML_EMPTY_ELEM_TAG;
753 xml_fatal_expected_white(ctx);
758 /* Resolve namespaces */
759 xml_ns_push_element(ctx);
761 /* Once we have namespaces, hash attribute names */
762 XML_ATTR_FOR_EACH(a, e)
763 xml_process_attr(ctx, a);
765 /* FIXME: DTD logic is not namespace-aware */
768 XML_ATTR_FOR_EACH(a, e)
770 if (!(a->dtd = xml_dtd_find_attr(ctx, e->dtd, a->name)))
771 xml_error(ctx, "Undefined attribute %s in element <%s>", a->name, e->name);
773 xml_validate_attr(ctx, a->dtd, a->val);
775 SLIST_FOR_EACH(struct xml_dtd_attr *, a, e->dtd->attrs)
777 if (a->default_mode == XML_ATTR_REQUIRED)
779 if (!xml_attr_find(ctx, e, a->name))
780 xml_error(ctx, "Missing required attribute %s in element <%s>", a->name, e->name);
782 else if (a->default_mode != XML_ATTR_IMPLIED && ctx->flags & XML_ALLOC_DEFAULT_ATTRS)
784 if (!xml_attr_find(ctx, e, a->name))
785 xml_raw_add_attr(ctx, e, a->name, a->default_value);
790 if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_stag)
795 xml_pop_element(struct xml_context *ctx)
797 TRACE(ctx, "pop_element");
798 if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_etag)
801 xml_ns_pop_element(ctx);
803 struct xml_node *e = ctx->node;
804 uint free = !(ctx->flags & XML_ALLOC_TAGS);
811 * With the current data structures, freeing of attributes is not necessary,
812 * but it might be if we switch to a global hash table of large elements.
814 SLIST_FOR_EACH(struct xml_attr *, a, e->attrs)
815 xml_attrs_remove(ctx->tab_attrs, a);
817 while (n = clist_head(&e->sons))
819 if (n->type == XML_NODE_ELEM)
821 SLIST_FOR_EACH(struct xml_attr *, a, n->attrs)
822 xml_attrs_remove(ctx->tab_attrs, a);
823 clist_insert_list_after(&n->sons, &n->n);
830 xml_pop_dom(ctx, free);
835 xml_parse_etag(struct xml_context *ctx)
837 /* ETag ::= '</' Name S? '>'
838 * Already parsed: '<' */
839 struct xml_node *e = ctx->node;
841 char *n = xml_node_qname(ctx, e);
845 n = utf8_32_get(n, &c);
846 if (xml_get_char(ctx) != c)
849 xml_parse_white(ctx, 0);
850 if (xml_get_char(ctx) != '>')
853 xml_error(ctx, "Invalid ETag, expected </%s>", e->name);
854 while (xml_get_char(ctx) != '>');
860 xml_node_qname(struct xml_context *ctx UNUSED, struct xml_node *node)
862 ASSERT(node->type == XML_NODE_ELEM);
863 char *n = node->name;
869 /*** Document type declaration ***/
872 xml_parse_doctype_decl(struct xml_context *ctx)
874 TRACE(ctx, "parse_doctype_decl");
875 /* doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
876 * Already parsed: '<!'
877 * Terminated before '[' or '>' */
879 xml_fatal(ctx, "Multiple document types not allowed");
880 xml_parse_seq(ctx, "DOCTYPE");
881 xml_parse_white(ctx, 1);
882 ctx->doctype = xml_parse_name(ctx, ctx->pool);
883 TRACE(ctx, "doctype=%s", ctx->doctype);
885 if (xml_parse_white(ctx, 0) && ((c = xml_peek_char(ctx)) == 'S' || c == 'P'))
889 xml_parse_seq(ctx, "SYSTEM");
890 xml_parse_white(ctx, 1);
891 ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
895 xml_parse_seq(ctx, "PUBLIC");
896 xml_parse_white(ctx, 1);
897 ctx->public_id = xml_parse_pubid_literal(ctx, ctx->pool);
898 xml_parse_white(ctx, 1);
899 ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
901 xml_parse_white(ctx, 0);
902 ctx->flags |= XML_HAS_EXTERNAL_SUBSET;
904 if (xml_peek_char(ctx) == '[')
906 ctx->flags |= XML_HAS_INTERNAL_SUBSET;
910 if (ctx->h_doctype_decl)
911 ctx->h_doctype_decl(ctx);
916 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
918 /* DTD: Internal subset */
921 xml_parse_subset(struct xml_context *ctx, uint external)
924 // -- comments/pi have no parent
925 // -- conditional sections in external subset
926 // -- check corectness of parameter entities
929 * intSubset :== (markupdecl | DeclSep)
930 * Already parsed: '['
932 * extSubsetDecl ::= ( markupdecl | conditionalSect | DeclSep)*
936 xml_parse_white(ctx, 0);
937 uint c = xml_get_char(ctx);
940 if ((c = xml_get_char(ctx)) == '!')
941 switch (c = xml_get_char(ctx))
944 xml_push_comment(ctx);
945 xml_pop_comment(ctx);
948 xml_parse_seq(ctx, "OTATION");
949 xml_parse_notation_decl(ctx);
952 if ((c = xml_get_char(ctx)) == 'N')
954 xml_parse_seq(ctx, "TITY");
955 xml_parse_entity_decl(ctx);
959 xml_parse_seq(ctx, "EMENT");
960 xml_parse_element_decl(ctx);
966 xml_parse_seq(ctx, "TTLIST");
967 xml_parse_attr_list_decl(ctx);
980 xml_parse_pe_ref(ctx);
981 else if (c == ']' && !external)
985 else if (c == '>' && external)
995 xml_fatal(ctx, "Invalid markup in the %s subset", external ? "external" : "internal");
998 /*** The State Machine ***/
1001 xml_next(struct xml_context *ctx)
1003 /* A nasty state machine */
1005 #define PULL(x) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##x; case XML_STATE_##x: ; } while (0)
1006 #define PULL_STATE(x, s) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##s, XML_STATE_##x; case XML_STATE_##s: ; } while (0)
1008 TRACE(ctx, "xml_next (state=%u)", ctx->state);
1010 ctx->throw_buf = &throw_buf;
1011 if (setjmp(throw_buf))
1014 if (ctx->err_code == XML_ERR_EOF && ctx->h_fatal)
1016 TRACE(ctx, "raised fatal error");
1017 return ctx->state = XML_STATE_EOF;
1022 case XML_STATE_START:
1023 TRACE(ctx, "entering prolog");
1024 ctx->flags |= XML_SRC_DOCUMENT | XML_SRC_EXPECTED_DECL;
1025 if (ctx->h_document_start)
1026 ctx->h_document_start(ctx);
1029 if (ctx->h_xml_decl)
1030 ctx->h_xml_decl(ctx);
1033 /* Misc* (doctypedecl Misc*)? */
1036 xml_parse_white(ctx, 0);
1037 xml_parse_char(ctx, '<');
1039 if ((c = xml_get_char(ctx)) == '?')
1040 /* Processing intruction */
1041 if (!(ctx->flags & XML_REPORT_PIS))
1046 PULL_STATE(PI, PROLOG_PI);
1051 /* Found the root tag */
1052 xml_unget_char(ctx);
1055 else if (xml_get_char(ctx) == '-')
1056 if (!(ctx->flags & XML_REPORT_COMMENTS))
1057 xml_skip_comment(ctx);
1060 xml_push_comment(ctx);
1061 PULL_STATE(COMMENT, PROLOG_COMMENT);
1062 xml_pop_comment(ctx);
1067 xml_unget_char(ctx);
1068 xml_parse_doctype_decl(ctx);
1070 if (ctx->flags & XML_HAS_DTD)
1071 if (ctx->flags & XML_PARSE_DTD)
1074 if (ctx->h_dtd_start)
1075 ctx->h_dtd_start(ctx);
1076 if (ctx->flags & XML_HAS_INTERNAL_SUBSET)
1078 xml_parse_subset(ctx, 0);
1081 if (ctx->flags & XML_HAS_EXTERNAL_SUBSET)
1083 struct xml_dtd_entity ent = {
1084 .system_id = ctx->system_id,
1085 .public_id = ctx->public_id,
1087 xml_parse_white(ctx, 0);
1088 xml_parse_char(ctx, '>');
1089 xml_unget_char(ctx);
1090 ASSERT(ctx->h_resolve_entity);
1091 ctx->h_resolve_entity(ctx, &ent);
1092 ctx->flags |= XML_SRC_EXPECTED_DECL;
1093 xml_parse_subset(ctx, 1);
1094 xml_unget_char(ctx);;
1097 ctx->h_dtd_end(ctx);
1099 else if (ctx->flags & XML_HAS_INTERNAL_SUBSET)
1100 xml_skip_internal_subset(ctx);
1101 xml_parse_white(ctx, 0);
1102 xml_parse_char(ctx, '>');
1107 case XML_STATE_CHARS:
1111 if (xml_peek_char(ctx) != '<')
1114 xml_append_chars(ctx);
1122 if ((c = xml_get_char(ctx)) == '?')
1125 if (!(ctx->flags & (XML_REPORT_PIS | XML_ALLOC_PIS)))
1129 if (xml_flush_chars(ctx))
1131 PULL_STATE(CHARS, CHARS_BEFORE_PI);
1141 if ((c = xml_get_char(ctx)) == '-')
1144 if (!(ctx->flags & (XML_REPORT_COMMENTS | XML_ALLOC_COMMENTS)))
1145 xml_skip_comment(ctx);
1148 if (xml_flush_chars(ctx))
1150 PULL_STATE(CHARS, CHARS_BEFORE_COMMENT);
1153 xml_push_comment(ctx);
1155 xml_pop_comment(ctx);
1161 xml_append_cdata(ctx);
1164 xml_fatal(ctx, "Unexpected character after '<!'");
1168 /* STag | EmptyElemTag */
1169 xml_unget_char(ctx);
1170 if (xml_flush_chars(ctx))
1172 PULL_STATE(CHARS, CHARS_BEFORE_STAG);
1176 xml_push_element(ctx);
1178 if (ctx->flags & XML_EMPTY_ELEM_TAG)
1185 if (xml_flush_chars(ctx))
1187 PULL_STATE(CHARS, CHARS_BEFORE_ETAG);
1191 xml_parse_etag(ctx);
1194 xml_pop_element(ctx);
1202 TRACE(ctx, "entering epilog");
1205 /* Epilog whitespace is the only place, where a valid document can reach EOF */
1206 if (setjmp(throw_buf))
1207 if (ctx->err_code == XML_ERR_EOF)
1209 TRACE(ctx, "reached EOF");
1210 ctx->state = XML_STATE_EOF;
1211 if (ctx->h_document_end)
1212 ctx->h_document_end(ctx);
1215 ctx->err_msg = NULL;
1216 return XML_STATE_EOF;
1220 xml_parse_white(ctx, 0);
1221 if (setjmp(throw_buf))
1225 xml_parse_char(ctx, '<');
1227 if ((c = xml_get_char(ctx)) == '?')
1228 /* Processing instruction */
1229 if (!(ctx->flags & XML_REPORT_PIS))
1234 PULL_STATE(PI, EPILOG_PI);
1239 xml_parse_char(ctx, '-');
1241 if (!(ctx->flags & XML_REPORT_COMMENTS))
1242 xml_skip_comment(ctx);
1245 xml_push_comment(ctx);
1246 PULL_STATE(COMMENT, EPILOG_COMMENT);
1247 xml_pop_comment(ctx);
1251 xml_fatal(ctx, "Syntax error in the epilog");
1259 xml_next_state(struct xml_context *ctx, uint pull)
1261 uint saved = ctx->pull;
1263 uint res = xml_next(ctx);
1269 xml_skip_element(struct xml_context *ctx)
1271 ASSERT(ctx->state == XML_STATE_STAG);
1272 struct xml_node *node = ctx->node;
1273 uint saved = ctx->pull, res;
1274 ctx->pull = XML_PULL_ETAG;
1275 while ((res = xml_next(ctx)) && ctx->node != node);
1281 xml_parse(struct xml_context *ctx)
1283 /* This cycle should run only once unless the user overrides the value of ctx->pull in a SAX handler */
1288 while (xml_next(ctx));
1289 return ctx->err_code;
1293 xml_merge_chars(struct xml_context *ctx UNUSED, struct xml_node *node, struct mempool *pool)
1295 ASSERT(node->type == XML_NODE_ELEM);
1296 char *p = mp_start_noalign(pool, 1);
1297 XML_NODE_FOR_EACH(son, node)
1298 if (son->type == XML_NODE_CHARS)
1300 p = mp_spread(pool, p, son->len + 1);
1301 memcpy(p, son->text, son->len);
1305 return mp_end(pool, p);
1309 xml_append_dom_chars(char *p, struct mempool *pool, struct xml_node *node)
1311 XML_NODE_FOR_EACH(son, node)
1312 if (son->type == XML_NODE_CHARS)
1314 p = mp_spread(pool, p, son->len + 1);
1315 memcpy(p, son->text, son->len);
1318 else if (son->type == XML_NODE_ELEM)
1319 p = xml_append_dom_chars(p, pool, son);
1324 xml_merge_dom_chars(struct xml_context *ctx UNUSED, struct xml_node *node, struct mempool *pool)
1326 ASSERT(node->type == XML_NODE_ELEM);
1327 char *p = mp_start_noalign(pool, 1);
1328 p = xml_append_dom_chars(p, pool, node);
1330 return mp_end(pool, p);