2 * Sherlock Library -- A simple XML parser
4 * (c) 2007--2008 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include "sherlock/sherlock.h"
13 #include "sherlock/xml/xml.h"
14 #include "sherlock/xml/dtd.h"
15 #include "sherlock/xml/internals.h"
16 #include "lib/fastbuf.h"
17 #include "lib/ff-unicode.h"
18 #include "lib/unicode.h"
22 #define HASH_PREFIX(x) xml_dtd_notns_##x
23 #define HASH_NODE struct xml_dtd_notn
24 #define HASH_KEY_STRING name
25 #define HASH_ZERO_FILL
26 #define HASH_TABLE_DYNAMIC
27 #define HASH_WANT_LOOKUP
28 #define HASH_WANT_FIND
29 #define HASH_GIVE_ALLOC
30 #define HASH_TABLE_ALLOC
32 #include "lib/hashtable.h"
35 xml_dtd_find_notn(struct xml_context *ctx, char *name)
37 struct xml_dtd *dtd = ctx->dtd;
38 struct xml_dtd_notn *notn = xml_dtd_notns_find(dtd->tab_notns, name);
39 return !notn ? NULL : (notn->flags & XML_DTD_NOTN_DECLARED) ? notn : NULL;
42 /* General entities */
44 #define HASH_PREFIX(x) xml_dtd_ents_##x
45 #define HASH_NODE struct xml_dtd_entity
46 #define HASH_KEY_STRING name
47 #define HASH_ZERO_FILL
48 #define HASH_TABLE_DYNAMIC
49 #define HASH_WANT_FIND
50 #define HASH_WANT_LOOKUP
51 #define HASH_GIVE_ALLOC
52 #define HASH_TABLE_ALLOC
54 #include "lib/hashtable.h"
56 static struct xml_dtd_entity *
57 xml_dtd_declare_trivial_entity(struct xml_context *ctx, char *name, char *text)
59 struct xml_dtd *dtd = ctx->dtd;
60 struct xml_dtd_entity *ent = xml_dtd_ents_lookup(dtd->tab_ents, name);
61 if (ent->flags & XML_DTD_ENTITY_DECLARED)
63 xml_warn(ctx, "Entity &%s; already declared", name);
66 slist_add_tail(&dtd->ents, &ent->n);
67 ent->flags = XML_DTD_ENTITY_DECLARED | XML_DTD_ENTITY_TRIVIAL;
73 xml_dtd_declare_default_entities(struct xml_context *ctx)
75 xml_dtd_declare_trivial_entity(ctx, "lt", "<");
76 xml_dtd_declare_trivial_entity(ctx, "gt", ">");
77 xml_dtd_declare_trivial_entity(ctx, "amp", "&");
78 xml_dtd_declare_trivial_entity(ctx, "apos", "'");
79 xml_dtd_declare_trivial_entity(ctx, "quot", "\"");
82 struct xml_dtd_entity *
83 xml_def_find_entity(struct xml_context *ctx UNUSED, char *name)
85 #define ENT(n, t) ent_##n = { .name = #n, .text = t, .flags = XML_DTD_ENTITY_DECLARED | XML_DTD_ENTITY_TRIVIAL }
86 static struct xml_dtd_entity ENT(lt, "<"), ENT(gt, ">"), ENT(amp, "&"), ENT(apos, "'"), ENT(quot, "\"");
91 if (!strcmp(name, "lt"))
95 if (!strcmp(name, "gt"))
99 if (!strcmp(name, "amp"))
101 if (!strcmp(name, "apos"))
105 if (!strcmp(name, "quot"))
112 struct xml_dtd_entity *
113 xml_dtd_find_entity(struct xml_context *ctx, char *name)
115 struct xml_dtd *dtd = ctx->dtd;
116 if (ctx->h_find_entity)
117 return ctx->h_find_entity(ctx, name);
120 struct xml_dtd_entity *ent = xml_dtd_ents_find(dtd->tab_ents, name);
121 return !ent ? NULL : (ent->flags & XML_DTD_ENTITY_DECLARED) ? ent : NULL;
124 return xml_def_find_entity(ctx, name);
127 /* Parameter entities */
129 static struct xml_dtd_entity *
130 xml_dtd_find_pentity(struct xml_context *ctx, char *name)
132 struct xml_dtd *dtd = ctx->dtd;
133 struct xml_dtd_entity *ent = xml_dtd_ents_find(dtd->tab_pents, name);
134 return !ent ? NULL : (ent->flags & XML_DTD_ENTITY_DECLARED) ? ent : NULL;
139 #define HASH_PREFIX(x) xml_dtd_elems_##x
140 #define HASH_NODE struct xml_dtd_elem
141 #define HASH_KEY_STRING name
142 #define HASH_TABLE_DYNAMIC
143 #define HASH_ZERO_FILL
144 #define HASH_WANT_FIND
145 #define HASH_WANT_LOOKUP
146 #define HASH_GIVE_ALLOC
147 #define HASH_TABLE_ALLOC
149 #include "lib/hashtable.h"
151 struct xml_dtd_elem *
152 xml_dtd_find_elem(struct xml_context *ctx, char *name)
154 return ctx->dtd ? xml_dtd_elems_find(ctx->dtd->tab_elems, name) : NULL;
159 struct xml_dtd_enodes_table;
162 xml_dtd_enodes_hash(struct xml_dtd_enodes_table *tab UNUSED, struct xml_dtd_elem_node *parent, struct xml_dtd_elem *elem)
164 return hash_pointer(parent) ^ hash_pointer(elem);
168 xml_dtd_enodes_eq(struct xml_dtd_enodes_table *tab UNUSED, struct xml_dtd_elem_node *parent1, struct xml_dtd_elem *elem1, struct xml_dtd_elem_node *parent2, struct xml_dtd_elem *elem2)
170 return (parent1 == parent2) && (elem1 == elem2);
174 xml_dtd_enodes_init_key(struct xml_dtd_enodes_table *tab UNUSED, struct xml_dtd_elem_node *node, struct xml_dtd_elem_node *parent, struct xml_dtd_elem *elem)
176 node->parent = parent;
180 #define HASH_PREFIX(x) xml_dtd_enodes_##x
181 #define HASH_NODE struct xml_dtd_elem_node
182 #define HASH_KEY_COMPLEX(x) x parent, x elem
183 #define HASH_KEY_DECL struct xml_dtd_elem_node *parent, struct xml_dtd_elem *elem
184 #define HASH_GIVE_HASHFN
186 #define HASH_GIVE_INIT_KEY
187 #define HASH_TABLE_DYNAMIC
188 #define HASH_ZERO_FILL
189 #define HASH_WANT_FIND
190 #define HASH_WANT_NEW
191 #define HASH_GIVE_ALLOC
192 #define HASH_TABLE_ALLOC
194 #include "lib/hashtable.h"
196 /* Element attributes */
198 struct xml_dtd_attrs_table;
201 xml_dtd_attrs_hash(struct xml_dtd_attrs_table *tab UNUSED, struct xml_dtd_elem *elem, char *name)
203 return hash_pointer(elem) ^ hash_string(name);
207 xml_dtd_attrs_eq(struct xml_dtd_attrs_table *tab UNUSED, struct xml_dtd_elem *elem1, char *name1, struct xml_dtd_elem *elem2, char *name2)
209 return (elem1 == elem2) && !strcmp(name1, name2);
213 xml_dtd_attrs_init_key(struct xml_dtd_attrs_table *tab UNUSED, struct xml_dtd_attr *attr, struct xml_dtd_elem *elem, char *name)
219 #define HASH_PREFIX(x) xml_dtd_attrs_##x
220 #define HASH_NODE struct xml_dtd_attr
221 #define HASH_ZERO_FILL
222 #define HASH_TABLE_DYNAMIC
223 #define HASH_KEY_COMPLEX(x) x elem, x name
224 #define HASH_KEY_DECL struct xml_dtd_elem *elem, char *name
225 #define HASH_GIVE_HASHFN
227 #define HASH_GIVE_INIT_KEY
228 #define HASH_WANT_FIND
229 #define HASH_WANT_NEW
230 #define HASH_GIVE_ALLOC
231 #define HASH_TABLE_ALLOC
233 #include "lib/hashtable.h"
235 struct xml_dtd_attr *
236 xml_dtd_find_attr(struct xml_context *ctx, struct xml_dtd_elem *elem, char *name)
238 return ctx->dtd ? xml_dtd_attrs_find(ctx->dtd->tab_attrs, elem, name) : NULL;
241 /* Enumerated attribute values */
243 struct xml_dtd_evals_table;
246 xml_dtd_evals_hash(struct xml_dtd_evals_table *tab UNUSED, struct xml_dtd_attr *attr, char *val)
248 return hash_pointer(attr) ^ hash_string(val);
252 xml_dtd_evals_eq(struct xml_dtd_evals_table *tab UNUSED, struct xml_dtd_attr *attr1, char *val1, struct xml_dtd_attr *attr2, char *val2)
254 return (attr1 == attr2) && !strcmp(val1, val2);
258 xml_dtd_evals_init_key(struct xml_dtd_evals_table *tab UNUSED, struct xml_dtd_eval *eval, struct xml_dtd_attr *attr, char *val)
264 #define HASH_PREFIX(x) xml_dtd_evals_##x
265 #define HASH_NODE struct xml_dtd_eval
266 #define HASH_TABLE_DYNAMIC
267 #define HASH_KEY_COMPLEX(x) x attr, x val
268 #define HASH_KEY_DECL struct xml_dtd_attr *attr, char *val
269 #define HASH_GIVE_HASHFN
271 #define HASH_GIVE_INIT_KEY
272 #define HASH_WANT_FIND
273 #define HASH_WANT_NEW
274 #define HASH_GIVE_ALLOC
275 #define HASH_TABLE_ALLOC
277 #include "lib/hashtable.h"
279 /* Enumerated attribute notations */
281 struct xml_dtd_enotns_table;
284 xml_dtd_enotns_hash(struct xml_dtd_enotns_table *tab UNUSED, struct xml_dtd_attr *attr, struct xml_dtd_notn *notn)
286 return hash_pointer(attr) ^ hash_pointer(notn);
290 xml_dtd_enotns_eq(struct xml_dtd_enotns_table *tab UNUSED, struct xml_dtd_attr *attr1, struct xml_dtd_notn *notn1, struct xml_dtd_attr *attr2, struct xml_dtd_notn *notn2)
292 return (attr1 == attr2) && (notn1 == notn2);
296 xml_dtd_enotns_init_key(struct xml_dtd_enotns_table *tab UNUSED, struct xml_dtd_enotn *enotn, struct xml_dtd_attr *attr, struct xml_dtd_notn *notn)
302 #define HASH_PREFIX(x) xml_dtd_enotns_##x
303 #define HASH_NODE struct xml_dtd_enotn
304 #define HASH_TABLE_DYNAMIC
305 #define HASH_KEY_COMPLEX(x) x attr, x notn
306 #define HASH_KEY_DECL struct xml_dtd_attr *attr, struct xml_dtd_notn *notn
307 #define HASH_GIVE_HASHFN
309 #define HASH_GIVE_INIT_KEY
310 #define HASH_WANT_FIND
311 #define HASH_WANT_NEW
312 #define HASH_GIVE_ALLOC
313 #define HASH_TABLE_ALLOC
315 #include "lib/hashtable.h"
317 /* DTD initialization/cleanup */
320 xml_dtd_init(struct xml_context *ctx)
324 struct mempool *pool = mp_new(4096);
325 struct xml_dtd *dtd = ctx->dtd = mp_alloc_zero(pool, sizeof(*ctx->dtd));
327 xml_dtd_ents_init(dtd->tab_ents = xml_hash_new(pool, sizeof(struct xml_dtd_ents_table)));
328 xml_dtd_ents_init(dtd->tab_pents = xml_hash_new(pool, sizeof(struct xml_dtd_ents_table)));
329 xml_dtd_notns_init(dtd->tab_notns = xml_hash_new(pool, sizeof(struct xml_dtd_notns_table)));
330 xml_dtd_elems_init(dtd->tab_elems = xml_hash_new(pool, sizeof(struct xml_dtd_elems_table)));
331 xml_dtd_enodes_init(dtd->tab_enodes = xml_hash_new(pool, sizeof(struct xml_dtd_enodes_table)));
332 xml_dtd_attrs_init(dtd->tab_attrs = xml_hash_new(pool, sizeof(struct xml_dtd_attrs_table)));
333 xml_dtd_evals_init(dtd->tab_evals = xml_hash_new(pool, sizeof(struct xml_dtd_evals_table)));
334 xml_dtd_enotns_init(dtd->tab_enotns = xml_hash_new(pool, sizeof(struct xml_dtd_enotns_table)));
335 xml_dtd_declare_default_entities(ctx);
339 xml_dtd_cleanup(struct xml_context *ctx)
343 mp_delete(ctx->dtd->pool);
348 xml_dtd_finish(struct xml_context *ctx)
352 // FIXME: validity checks
355 /*** Parsing functions ***/
357 /* References to parameter entities */
360 xml_parse_pe_ref(struct xml_context *ctx)
362 /* PEReference ::= '%' Name ';'
363 * Already parsed: '%' */
364 struct mempool_state state;
365 mp_save(ctx->stack, &state);
366 char *name = xml_parse_name(ctx, ctx->stack);
367 xml_parse_char(ctx, ';');
368 struct xml_dtd_entity *ent = xml_dtd_find_pentity(ctx, name);
370 xml_error(ctx, "Unknown entity %%%s;", name);
373 TRACE(ctx, "Pushed entity %%%s;", name);
374 mp_restore(ctx->stack, &state);
376 xml_push_entity(ctx, ent);
379 mp_restore(ctx->stack, &state);
384 xml_parse_dtd_pe(struct xml_context *ctx, uns entity_decl)
386 /* Already parsed: '%' */
390 if (!~entity_decl && (xml_peek_cat(ctx) & XML_CHAR_WHITE))
395 xml_parse_pe_ref(ctx);
396 while (xml_peek_cat(ctx) & XML_CHAR_WHITE)
399 while (xml_get_char(ctx) == '%');
405 xml_parse_dtd_white(struct xml_context *ctx, uns mandatory)
407 /* Whitespace or parameter entity,
408 * mandatory==~0U has a special maening of the whitespace before the '%' character in an parameter entity declaration */
410 while (xml_peek_cat(ctx) & XML_CHAR_WHITE)
415 if (xml_peek_char(ctx) == '%')
418 return xml_parse_dtd_pe(ctx, mandatory);
420 else if (unlikely(mandatory && !cnt))
421 xml_fatal_expected_white(ctx);
426 xml_dtd_parse_external_id(struct xml_context *ctx, char **system_id, char **public_id, uns allow_public)
428 struct xml_dtd *dtd = ctx->dtd;
429 uns c = xml_peek_char(ctx);
432 xml_parse_seq(ctx, "SYSTEM");
433 xml_parse_dtd_white(ctx, 1);
435 *system_id = xml_parse_system_literal(ctx, dtd->pool);
439 xml_parse_seq(ctx, "PUBLIC");
440 xml_parse_dtd_white(ctx, 1);
442 *public_id = xml_parse_pubid_literal(ctx, dtd->pool);
443 if (xml_parse_dtd_white(ctx, !allow_public))
444 if ((c = xml_peek_char(ctx)) == '\'' || c == '"' || !allow_public)
445 *system_id = xml_parse_system_literal(ctx, dtd->pool);
448 xml_fatal(ctx, "Expected an external ID");
451 /* DTD: <!NOTATION ...> */
454 xml_parse_notation_decl(struct xml_context *ctx)
456 /* NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
457 * Already parsed: '<!NOTATION' */
458 TRACE(ctx, "parse_notation_decl");
459 struct xml_dtd *dtd = ctx->dtd;
460 xml_parse_dtd_white(ctx, 1);
462 struct xml_dtd_notn *notn = xml_dtd_notns_lookup(dtd->tab_notns, xml_parse_name(ctx, dtd->pool));
463 xml_parse_dtd_white(ctx, 1);
464 char *system_id, *public_id;
465 xml_dtd_parse_external_id(ctx, &system_id, &public_id, 1);
466 xml_parse_dtd_white(ctx, 0);
467 xml_parse_char(ctx, '>');
469 if (notn->flags & XML_DTD_NOTN_DECLARED)
470 xml_warn(ctx, "Notation %s already declared", notn->name);
473 notn->flags = XML_DTD_NOTN_DECLARED;
474 notn->system_id = system_id;
475 notn->public_id = public_id;
476 slist_add_tail(&dtd->notns, ¬n->n);
481 /* DTD: <!ENTITY ...> */
484 xml_parse_entity_decl(struct xml_context *ctx)
486 /* Already parsed: '<!ENTITY' */
487 TRACE(ctx, "parse_entity_decl");
488 struct xml_dtd *dtd = ctx->dtd;
489 uns flags = ~xml_parse_dtd_white(ctx, ~0U) ? 0 : XML_DTD_ENTITY_PARAMETER;
491 xml_parse_dtd_white(ctx, 1);
492 struct xml_dtd_entity *ent = xml_dtd_ents_lookup(flags ? dtd->tab_pents : dtd->tab_ents, xml_parse_name(ctx, dtd->pool));
493 xml_parse_dtd_white(ctx, 1);
494 slist *list = flags ? &dtd->pents : &dtd->ents;
495 if (ent->flags & XML_DTD_ENTITY_DECLARED)
497 xml_fatal(ctx, "Entity &%s; already declared, skipping not implemented", ent->name);
498 // FIXME: should be only warning
500 uns c, sep = xml_get_char(ctx);
501 if (sep == '\'' || sep == '"')
504 * EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | "'" ([^%&'] | PEReference | Reference)* "'" */
505 char *p = mp_start_noalign(dtd->pool, 1);
508 if ((c = xml_get_char(ctx)) == sep)
514 //xml_parse_parameter_ref(ctx);
520 if (xml_peek_char(ctx) != '#')
522 /* Bypass references to general entities */
523 struct mempool_state state;
524 mp_save(ctx->stack, &state);
525 char *n = xml_parse_name(ctx, ctx->stack);
526 xml_parse_char(ctx, ';');
529 p = mp_spread(dtd->pool, p, 3 + l);
534 mp_restore(ctx->stack, &state);
540 c = xml_parse_char_ref(ctx);
543 p = mp_spread(dtd->pool, p, 5);
544 p = utf8_32_put(p, c);
547 ent->len = p - (char *)mp_ptr(dtd->pool);
548 ent->text = mp_end(dtd->pool, p + 1);
549 slist_add_tail(list, &ent->n);
550 ent->flags = flags | XML_DTD_ENTITY_DECLARED;
554 /* External entity */
555 struct xml_dtd_notn *notn = NULL;
556 char *system_id, *public_id;
558 xml_dtd_parse_external_id(ctx, &system_id, &public_id, 0);
559 if (xml_parse_dtd_white(ctx, 0) && flags && xml_peek_char(ctx) != '>')
561 /* General external unparsed entity */
562 flags |= XML_DTD_ENTITY_UNPARSED;
563 xml_parse_seq(ctx, "NDATA");
564 xml_parse_dtd_white(ctx, 1);
565 notn = xml_dtd_notns_lookup(dtd->tab_notns, xml_parse_name(ctx, dtd->pool));
567 slist_add_tail(list, &ent->n);
568 ent->flags = flags | XML_DTD_ENTITY_DECLARED | XML_DTD_ENTITY_EXTERNAL;
569 ent->system_id = system_id;
570 ent->public_id = public_id;
573 xml_parse_dtd_white(ctx, 0);
574 xml_parse_char(ctx, '>');
578 /* DTD: <!ELEMENT ...> */
581 xml_parse_element_decl(struct xml_context *ctx)
583 /* Elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
584 * Already parsed: '<!ELEMENT' */
585 struct xml_dtd *dtd = ctx->dtd;
586 xml_parse_dtd_white(ctx, 1);
587 char *name = xml_parse_name(ctx, dtd->pool);
588 xml_parse_dtd_white(ctx, 1);
589 struct xml_dtd_elem *elem = xml_dtd_elems_lookup(dtd->tab_elems, name);
590 if (elem->flags & XML_DTD_ELEM_DECLARED)
591 xml_fatal(ctx, "Element <%s> already declared", name);
593 /* contentspec ::= 'EMPTY' | 'ANY' | Mixed | children */
594 uns c = xml_peek_char(ctx);
597 xml_parse_seq(ctx, "EMPTY");
598 elem->type = XML_DTD_ELEM_EMPTY;
602 xml_parse_seq(ctx, "ANY");
603 elem->type = XML_DTD_ELEM_ANY;
609 xml_parse_dtd_white(ctx, 0);
610 struct xml_dtd_elem_node *parent = elem->node = mp_alloc_zero(dtd->pool, sizeof(*parent));
611 if (xml_peek_char(ctx) == '#')
613 /* Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | '(' S? '#PCDATA' S? ')' */
615 xml_parse_seq(ctx, "PCDATA");
616 elem->type = XML_DTD_ELEM_MIXED;
617 parent->type = XML_DTD_ELEM_PCDATA;
620 xml_parse_dtd_white(ctx, 0);
621 if ((c = xml_get_char(ctx)) == ')')
624 xml_fatal_expected(ctx, ')');
625 xml_parse_dtd_white(ctx, 0);
626 struct xml_dtd_elem *son_elem = xml_dtd_elems_lookup(dtd->tab_elems, xml_parse_name(ctx, dtd->pool));
627 if (xml_dtd_enodes_find(dtd->tab_enodes, parent, son_elem))
628 xml_error(ctx, "Duplicate content '%s'", son_elem->name);
631 struct xml_dtd_elem_node *son = xml_dtd_enodes_new(dtd->tab_enodes, parent, son_elem);
632 slist_add_tail(&parent->sons, &son->n);
636 if (xml_peek_char(ctx) == '*')
639 parent->occur = XML_DTD_ELEM_OCCUR_MULT;
641 else if (!slist_head(&parent->sons))
642 parent->occur = XML_DTD_ELEM_OCCUR_ONCE;
644 xml_fatal_expected(ctx, '*');
648 /* children ::= (choice | seq) ('?' | '*' | '+')?
649 * cp ::= (Name | choice | seq) ('?' | '*' | '+')?
650 * choice ::= '(' S? cp ( S? '|' S? cp )+ S? ')'
651 * seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' */
653 elem->type = XML_DTD_ELEM_CHILDREN;
654 parent->type = XML_DTD_ELEM_PCDATA;
661 xml_parse_dtd_white(ctx, 0);
662 if ((c = xml_get_char(ctx)) == ')')
665 if (parent->type == XML_DTD_ELEM_PCDATA)
666 parent->type = XML_DTD_ELEM_SEQ;
667 if ((c = xml_get_char(ctx)) == '?')
668 parent->occur = XML_DTD_ELEM_OCCUR_OPT;
670 parent->occur = XML_DTD_ELEM_OCCUR_MULT;
672 parent->occur = XML_DTD_ELEM_OCCUR_PLUS;
676 parent->occur = XML_DTD_ELEM_OCCUR_ONCE;
680 parent = parent->parent;
685 if (parent->type == XML_DTD_ELEM_PCDATA)
686 parent->type = XML_DTD_ELEM_OR;
687 else if (parent->type != XML_DTD_ELEM_OR)
688 xml_fatal(ctx, "Mixed operators in the list of element children");
692 if (parent->type == XML_DTD_ELEM_PCDATA)
693 parent->type = XML_DTD_ELEM_SEQ;
694 else if (parent->type != XML_DTD_ELEM_SEQ)
695 xml_fatal(ctx, "Mixed operators in the list of element children");
700 struct xml_dtd_elem_node *son = mp_alloc_zero(dtd->pool, sizeof(*son));
701 son->parent = parent;
702 slist_add_tail(&parent->sons, &son->n);
703 parent = son->parent;
704 son->type = XML_DTD_ELEM_MIXED;
710 xml_parse_dtd_white(ctx, 0);
712 struct xml_dtd_elem *son_elem = xml_dtd_elems_lookup(dtd->tab_elems, xml_parse_name(ctx, dtd->pool));
713 // FIXME: duplicates, occurance
714 //struct xml_dtd_elem_node *son = xml_dtd_enodes_new(dtd->tab_enodes, parent, son_elem);
715 struct xml_dtd_elem_node *son = mp_alloc_zero(dtd->pool, sizeof(*son));
716 son->parent = parent;
717 son->elem = son_elem;
718 slist_add_tail(&parent->sons, &son->n);
723 xml_fatal(ctx, "Expected element content specification");
725 xml_parse_dtd_white(ctx, 0);
726 xml_parse_char(ctx, '>');
731 xml_parse_attr_list_decl(struct xml_context *ctx)
733 /* AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
734 * AttDef ::= S Name S AttType S DefaultDecl
735 * Already parsed: '<!ATTLIST' */
736 struct xml_dtd *dtd = ctx->dtd;
737 xml_parse_dtd_white(ctx, 1);
738 struct xml_dtd_elem *elem = xml_dtd_elems_lookup(ctx->dtd->tab_elems, xml_parse_name(ctx, dtd->pool));
740 while (xml_parse_dtd_white(ctx, 0) && xml_peek_char(ctx) != '>')
742 char *name = xml_parse_name(ctx, dtd->pool);
743 struct xml_dtd_attr *attr = xml_dtd_attrs_find(dtd->tab_attrs, elem, name);
747 xml_warn(ctx, "Duplicate attribute definition");
751 attr = xml_dtd_attrs_new(ctx->dtd->tab_attrs, elem, name);
752 xml_parse_dtd_white(ctx, 1);
753 if (xml_peek_char(ctx) == '(')
755 xml_skip_char(ctx); // FIXME: xml_inc/dec ?
757 attr->type = XML_ATTR_ENUM;
760 xml_parse_dtd_white(ctx, 0);
761 char *value = xml_parse_nmtoken(ctx, dtd->pool);
763 if (xml_dtd_evals_find(ctx->dtd->tab_evals, attr, value))
764 xml_error(ctx, "Duplicate enumeration value");
766 xml_dtd_evals_new(ctx->dtd->tab_evals, attr, value);
767 xml_parse_dtd_white(ctx, 0);
769 while (xml_get_char(ctx) == '|');
771 xml_parse_char(ctx, ')');
775 char *type = xml_parse_name(ctx, dtd->pool);
776 enum xml_dtd_attr_type t = XML_ATTR_CDATA;
777 if (!strcmp(type, "CDATA"))
779 else if (!strcmp(type, "ID"))
781 else if (!strcmp(type, "IDREF"))
783 else if (!strcmp(type, "IDREFS"))
785 else if (!strcmp(type, "ENTITY"))
787 else if (!strcmp(type, "ENTITIES"))
788 t = XML_ATTR_ENTITIES;
789 else if (!strcmp(type, "NMTOKEN"))
790 t = XML_ATTR_NMTOKEN;
791 else if (!strcmp(type, "NMTOKENS"))
792 t = XML_ATTR_NMTOKENS;
793 else if (!strcmp(type, "NOTATION"))
795 if (elem->type == XML_DTD_ELEM_EMPTY)
796 xml_fatal(ctx, "Empty element must not have notation attribute");
797 // FIXME: An element type MUST NOT have more than one NOTATION attribute specified.
798 t = XML_ATTR_NOTATION;
799 xml_parse_dtd_white(ctx, 1);
800 xml_parse_char(ctx, '(');
803 xml_parse_dtd_white(ctx, 0);
804 struct xml_dtd_notn *n = xml_dtd_notns_lookup(ctx->dtd->tab_notns, xml_parse_name(ctx, dtd->pool));
806 if (xml_dtd_enotns_find(ctx->dtd->tab_enotns, attr, n))
807 xml_error(ctx, "Duplicate enumerated notation");
809 xml_dtd_enotns_new(ctx->dtd->tab_enotns, attr, n);
810 xml_parse_dtd_white(ctx, 0);
812 while (xml_get_char(ctx) == '|');
814 xml_parse_char(ctx, ')');
817 xml_fatal(ctx, "Unknown attribute type");
821 xml_parse_dtd_white(ctx, 1);
822 enum xml_dtd_attr_default def = XML_ATTR_NONE;
823 if (xml_get_char(ctx) == '#')
824 switch (xml_peek_char(ctx))
827 xml_parse_seq(ctx, "REQUIRED");
828 def = XML_ATTR_REQUIRED;
831 xml_parse_seq(ctx, "IMPLIED");
832 def = XML_ATTR_IMPLIED;
835 xml_parse_seq(ctx, "FIXED");
836 def = XML_ATTR_FIXED;
837 xml_parse_dtd_white(ctx, 1);
840 xml_fatal(ctx, "Expected a modifier for default attribute value");
844 if (def != XML_ATTR_REQUIRED && def != XML_ATTR_IMPLIED)
846 char *v = xml_parse_attr_value(ctx, attr);
848 attr->default_value = v;
851 attr->default_mode = def;
858 xml_skip_internal_subset(struct xml_context *ctx)
860 TRACE(ctx, "skip_internal_subset");
861 /* AlreadyParsed: '[' */
863 while ((c = xml_get_char(ctx)) != ']')
867 if ((c = xml_get_char(ctx)) == '?')
874 else if (xml_get_char(ctx) == '-')
877 xml_skip_comment(ctx);
880 while ((c = xml_get_char(ctx)) != '>')
881 if (c == '\'' || c == '"')
882 while (xml_get_char(ctx) != c);
887 /*** Validation of attribute values ***/
890 xml_check_tokens(char *value, uns first_cat, uns next_cat, uns seq)
896 p = utf8_32_get(p, &u);
897 if (!(xml_char_cat(u) & first_cat))
901 p = utf8_32_get(p, &u);
902 if (!(xml_char_cat(u) & next_cat))
914 xml_is_name(struct xml_context *ctx, char *value)
916 /* Name ::= NameStartChar (NameChar)* */
917 return xml_check_tokens(value, ctx->cat_sname, ctx->cat_name, 0);
921 xml_is_names(struct xml_context *ctx, char *value)
923 /* Names ::= Name (#x20 Name)* */
924 return xml_check_tokens(value, ctx->cat_sname, ctx->cat_name, 1);
928 xml_is_nmtoken(struct xml_context *ctx, char *value)
930 /* Nmtoken ::= (NameChar)+ */
931 return xml_check_tokens(value, ctx->cat_name, ctx->cat_name, 0);
935 xml_is_nmtokens(struct xml_context *ctx, char *value)
937 /* Nmtokens ::= Nmtoken (#x20 Nmtoken)* */
938 return xml_check_tokens(value, ctx->cat_name, ctx->cat_name, 1);
942 xml_err_attr_format(struct xml_context *ctx, struct xml_dtd_attr *dtd, char *type)
944 xml_error(ctx, "Attribute %s in <%s> does not match the production of %s", dtd->name, dtd->elem->name, type);
948 xml_validate_attr(struct xml_context *ctx, struct xml_dtd_attr *dtd, char *value)
950 if (dtd->type == XML_ATTR_CDATA)
952 xml_normalize_white(ctx, value);
956 if (!xml_is_name(ctx, value))
957 xml_err_attr_format(ctx, dtd, "NAME");
958 //FIXME: add to a hash table
961 if (!xml_is_name(ctx, value))
962 xml_err_attr_format(ctx, dtd, "NAME");
963 // FIXME: find in hash table (beware forward references)
965 case XML_ATTR_IDREFS:
966 if (!xml_is_names(ctx, value))
967 xml_err_attr_format(ctx, dtd, "NAMES");
970 case XML_ATTR_ENTITY:
973 case XML_ATTR_ENTITIES:
976 case XML_ATTR_NMTOKEN:
977 if (!xml_is_nmtoken(ctx, value))
978 xml_err_attr_format(ctx, dtd, "NMTOKEN");
980 case XML_ATTR_NMTOKENS:
981 if (!xml_is_nmtokens(ctx, value))
982 xml_err_attr_format(ctx, dtd, "NMTOKENS");
985 if (!xml_dtd_evals_find(ctx->dtd->tab_evals, dtd, value))
986 xml_error(ctx, "Attribute %s in <%s> contains an undefined enumeration value", dtd->name, dtd->elem->name);
988 case XML_ATTR_NOTATION:
989 if (!xml_dtd_find_notn(ctx, value))
990 xml_error(ctx, "Attribute %s in <%s> contains an undefined notation", dtd->name, dtd->elem->name);