* of the GNU Lesser General Public License.
*/
-#ifndef _SHERLOCK_XML_H
-#define _SHERLOCK_XML_H
+#ifndef _SHERLOCK_XML_XML_H
+#define _SHERLOCK_XML_XML_H
#include "lib/clists.h"
#include "lib/slists.h"
#include "lib/mempool.h"
+#include "lib/fastbuf.h"
enum xml_error {
XML_ERR_OK = 0,
cnode n; /* Node for list of parent's sons */
uns type; /* XML_NODE_x */
struct xml_node *parent; /* Parent node */
-};
-
-struct xml_elem {
- struct xml_node node;
- char *name; /* Element name */
- clist sons; /* List of subnodes */
- struct xml_dtd_elem *dtd; /* Element DTD */
- slist attrs; /* Link list of attributes */
+ char *name; /* Element name / PI target */
+ clist sons; /* Children nodes */
+ union {
+ struct {
+ char *text; /* PI text / Comment / CDATA */
+ uns len; /* Text length in bytes */
+ };
+ struct {
+ struct xml_dtd_elem *dtd; /* Element DTD */
+ slist attrs; /* Link list of element attributes */
+ };
+ };
};
struct xml_attr {
snode n;
- struct xml_elem *elem;
+ struct xml_node *elem;
char *name;
char *val;
};
struct xml_context;
-struct xml_stack {
- struct xml_stack *next; /* Link list of stack records */
- uns saved_flags; /* Saved ctx->flags */
- struct mempool_state saved_pool; /* Saved ctx->pool state */
-};
-
#define XML_BUF_SIZE 32 /* At least 16 -- hardcoded */
struct xml_source {
void (*h_fatal)(struct xml_context *ctx); /* Unrecoverable error callback */
/* Memory management */
- struct mempool *pool; /* Most data */
- struct fastbuf *chars; /* Character data */
- struct fastbuf *value; /* Attribute value / comment / processing instruction data */
- char *name; /* Attribute name, processing instruction target */
- void *tab_attrs;
-
- /* Stack */
- struct xml_stack *stack; /* See xml_push(), xml_pop() */
+ struct mempool *pool; /* DOM pool */
+ struct mempool *stack; /* Stack pool (freed as soon as possible) */
+ struct xml_stack *stack_list; /* See xml_push(), xml_pop() */
uns flags; /* XML_FLAG_x (restored on xml_pop()) */
uns depth; /* Nesting level */
+ struct fastbuf chars; /* Character data / attribute value */
+ void *tab_attrs;
/* Input */
struct xml_source *src; /* Current source */
void (*h_document_end)(struct xml_context *ctx); /* Called after leaving epilog */
void (*h_xml_decl)(struct xml_context *ctx); /* Called after the XML declaration */
void (*h_doctype_decl)(struct xml_context *ctx); /* Called in the doctype declaration just before internal subset */
- void (*h_pi)(struct xml_context *ctx); /* Called after a processing instruction */
void (*h_comment)(struct xml_context *ctx); /* Called after a comment */
+ void (*h_pi)(struct xml_context *ctx); /* Called after a processing instruction */
void (*h_element_start)(struct xml_context *ctx); /* Called after STag or EmptyElemTag */
void (*h_element_end)(struct xml_context *ctx); /* Called before ETag or after EmptyElemTag */
+ void (*h_chars)(struct xml_context *ctx); /* Called after some characters */
+ void (*h_cdata)(struct xml_context *ctx); /* Called after a CDATA section */
/* DOM */
- struct xml_elem *root; /* DOM root */
- union {
- struct xml_node *node; /* Current DOM node */
- struct xml_elem *elem; /* Current element */
- };
+ struct xml_node *root; /* DOM root */
+ struct xml_node *node; /* Current DOM node */
char *version_str;
uns standalone;
void (*start_dtd)(struct xml_context *ctx);
void (*end_dtd)(struct xml_context *ctx);
- void (*start_cdata)(struct xml_context *ctx);
- void (*end_cdata)(struct xml_context *ctx);
void (*start_entity)(struct xml_context *ctx);
void (*end_entity)(struct xml_context *ctx);
- void (*chacacters)(struct xml_context *ctx);
struct fastbuf *(*resolve_entity)(struct xml_context *ctx);
void (*notation_decl)(struct xml_context *ctx);
void (*unparsed_entity_decl)(struct xml_context *ctx);
};
-/*** Document Type Definition (DTD) ***/
-
-struct xml_dtd {
- struct mempool *pool; /* Memory pool where to allocate DTD */
- slist gents; /* Link list of general entities */
- slist pents; /* Link list of parapeter entities */
- slist notns; /* Link list of notations */
- slist elems; /* Link list of elements */
- void *tab_gents; /* Hash table of general entities */
- void *tab_pents; /* Hash table of parameter entities */
- void *tab_notns; /* Hash table of notations */
- void *tab_elems; /* Hash table of elements */
- void *tab_attrs; /* Hash table of element attributes */
- void *tab_evals; /* Hash table of enumerated attribute values */
- void *tab_enotns; /* hash table of enumerated attribute notations */
-};
-
-/* Notations */
-
-enum xml_dtd_notn_flags {
- XML_DTD_NOTN_DECLARED = 0x1, /* The notation has been declared (interbal usage) */
-};
-
-struct xml_dtd_notn {
- snode n; /* Node in xml_dtd.notns */
- uns flags; /* XML_DTD_NOTN_x */
- char *name; /* Notation name */
- struct xml_ext_id eid; /* External id */
-};
-
-/* Entities */
-
-enum xml_dtd_ent_flags {
- XML_DTD_ENT_DECLARED = 0x1, /* The entity has been declared (internal usage) */
- XML_DTD_ENT_VISITED = 0x2, /* Cycle detection (internal usage) */
- XML_DTD_ENT_PARAMETER = 0x4, /* Parameter entity, general otherwise */
- XML_DTD_ENT_EXTERNAL = 0x8, /* External entity, internal otherwise */
- XML_DTD_ENT_UNPARSED = 0x10, /* Unparsed entity, parsed otherwise */
- XML_DTD_ENT_TRIVIAL = 0x20, /* Replacement text is a sequence of characters and character references */
-};
-
-struct xml_dtd_ent {
- snode n; /* Node in xml_dtd.[gp]ents */
- uns flags; /* XML_DTD_ENT_x */
- char *name; /* Entity name */
- char *text; /* Replacement text / expanded replacement text (XML_DTD_ENT_TRIVIAL) */
- uns len; /* Text length */
- struct xml_ext_id eid; /* External ID */
- struct xml_dtd_notn *notn; /* Notation (XML_DTD_ENT_UNPARSED only) */
-};
-
-/* Elements */
-
-enum xml_dtd_elem_flags {
- XML_DTD_ELEM_DECLARED = 0x1, /* The element has been declared (internal usage) */
-};
-
-struct xml_dtd_elem {
- snode n;
- uns flags;
- char *name;
- struct xml_dtd_elem_node *node;
-};
-
-struct xml_dtd_elem_node {
- snode n;
- struct xml_dtd_elem_node *parent;
- slist sons;
- uns type;
- uns occur;
-};
-
-enum xml_dtd_elem_node_type {
- XML_DTD_ELEM_PCDATA,
- XML_DTD_ELEM_SEQ,
- XML_DTD_ELEM_OR,
-};
-
-enum xml_dtd_elem_node_occur {
- XML_DTD_ELEM_OCCUR_ONCE,
- XML_DTD_ELEM_OCCUR_OPT,
- XML_DTD_ELEM_OCCUR_MULT,
- XML_DTD_ELEM_OCCUR_PLUS,
-};
-
-/* Attributes */
-
-
-enum xml_dtd_attribute_default {
- XML_ATTR_NONE,
- XML_ATTR_REQUIRED,
- XML_ATTR_IMPLIED,
- XML_ATTR_FIXED,
-};
-
-enum xml_dtd_attribute_type {
- XML_ATTR_CDATA,
- XML_ATTR_ID,
- XML_ATTR_IDREF,
- XML_ATTR_IDREFS,
- XML_ATTR_ENTITY,
- XML_ATTR_ENTITIES,
- XML_ATTR_NMTOKEN,
- XML_ATTR_NMTOKENS,
- XML_ATTR_ENUM,
- XML_ATTR_NOTATION,
-};
-
-struct xml_dtd_attr {
- char *name;
- struct xml_dtd_elem *elem;
- enum xml_dtd_attribute_type type;
- enum xml_dtd_attribute_default default_mode;
- char *default_value;
-};
-
-struct xml_dtd_eval {
- struct xml_dtd_attr *attr;
- char *val;
-};
-
-struct xml_dtd_enotn {
- struct xml_dtd_attr *attr;
- struct xml_dtd_notn *notn;
-};
-
void xml_init(struct xml_context *ctx);
void xml_cleanup(struct xml_context *ctx);
void xml_set_source(struct xml_context *ctx, struct fastbuf *fb);
int xml_next(struct xml_context *ctx);
+uns xml_row(struct xml_context *ctx);
#endif