]> mj.ucw.cz Git - libucw.git/blob - sherlock/xml/xml.h
XML: The great reorganization... several improvements in the iface,
[libucw.git] / sherlock / xml / xml.h
1 /*
2  *      Sherlock Library -- A simple XML parser
3  *
4  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_XML_XML_H
11 #define _SHERLOCK_XML_XML_H
12
13 #include "lib/clists.h"
14 #include "lib/slists.h"
15 #include "lib/mempool.h"
16 #include "lib/fastbuf.h"
17
18 enum xml_error {
19   // FIXME
20   XML_ERR_OK = 0,
21   XML_ERR_WARN = 1000,                                  /* Warning */
22   XML_ERR_ERROR = 2000,                                 /* Recoverable error */
23   XML_ERR_FATAL = 3000,                                 /* Unrecoverable error */
24   XML_ERR_EOF,
25 };
26
27 enum xml_state {
28   XML_STATE_EOF,                                        /* EOF or a fatal error */
29   XML_STATE_START,                                      /* Initial state */
30   XML_STATE_XML_DECL,                                   /* XML_PULL_XML_DECL */
31   XML_STATE_DOCTYPE_DECL,                               /* XML_PULL_DOCTYPE_DECL */
32   XML_STATE_CHARS,                                      /* XML_PULL_CHARS */
33   XML_STATE_CDATA,                                      /* XML_PULL_CDATA */
34   XML_STATE_STAG,                                       /* XML_PULL_STAG */
35   XML_STATE_ETAG,                                       /* XML_PULL_ETAG */
36   XML_STATE_COMMENT,                                    /* XML_PULL_COMMENT */
37   XML_STATE_PI,                                         /* XML_PULL_PI */
38
39   /* Internal states */
40   XML_STATE_CHARS_BEFORE_STAG,
41   XML_STATE_CHARS_BEFORE_ETAG,
42   XML_STATE_CHARS_BEFORE_CDATA,
43   XML_STATE_CHARS_BEFORE_COMMENT,
44   XML_STATE_CHARS_BEFORE_PI,
45   XML_STATE_PROLOG_COMMENT,
46   XML_STATE_PROLOG_PI,
47   XML_STATE_EPILOG_COMMENT,
48   XML_STATE_EPILOG_PI,
49 };
50
51 enum xml_pull {
52   XML_PULL_XML_DECL =                   0x00000001,     /* Stop after the XML declaration */
53   XML_PULL_DOCTYPE_DECL =               0x00000002,     /* Stop in the doctype declaration (before optional internal subset) */
54   XML_PULL_CHARS =                      0x00000004,
55   XML_PULL_CDATA =                      0x00000008,
56   XML_PULL_STAG =                       0x00000010,
57   XML_PULL_ETAG =                       0x00000020,
58   XML_PULL_COMMENT =                    0x00000040,
59   XML_PULL_PI =                         0x00000080,
60   XML_PULL_ALL =                        0xffffffff,
61 };
62
63 enum xml_flags {
64   /* Enable reporting of various events via SAX and/or PUSH interface */
65   XML_REPORT_COMMENTS =                 0x00000001,     /* Report comments */
66   XML_REPORT_PIS =                      0x00000002,     /* Report processing instructions */
67   XML_REPORT_CHARS =                    0x00000004,     /* Report characters */
68   XML_REPORT_TAGS =                     0x00000008,     /* Report element starts/ends */
69   XML_REPORT_MISC = XML_REPORT_COMMENTS | XML_REPORT_PIS,
70   XML_REPORT_ALL = XML_REPORT_MISC | XML_REPORT_CHARS | XML_REPORT_TAGS,
71
72   /* Enable construction of DOM for these types */
73   XML_ALLOC_COMMENTS =                  0x00000010,     /* Create comment nodes */
74   XML_ALLOC_PIS =                       0x00000020,     /* Create processing instruction nodes */
75   XML_ALLOC_CHARS =                     0x00000040,     /* Create character nodes */
76   XML_ALLOC_TAGS =                      0x00000080,     /* Create element nodes */
77   XML_ALLOC_MISC = XML_ALLOC_COMMENTS | XML_ALLOC_PIS,
78   XML_ALLOC_ALL = XML_ALLOC_MISC | XML_ALLOC_CHARS | XML_ALLOC_TAGS,
79
80   /* Other parameters */
81   XML_UNFOLD_CDATA =                    0x00000100,     /* Unfold CDATA sections */
82   XML_VALIDATING =                      0x00000200,     /* Validate everything (not fully implemented!) */
83
84   /* Internals, do not change! */
85   XML_EMPTY_ELEM_TAG =                  0x00010000,     /* The current element match EmptyElemTag */
86   XML_VERSION_1_1 =                     0x00020000,     /* XML version is 1.1, otherwise 1.0 */
87   XML_HAS_EXTERNAL_SUBSET =             0x00040000,     /* The document contains a reference to external DTD subset */
88   XML_HAS_INTERNAL_SUBSET =             0x00080000,     /* The document contains an internal subset */
89   XML_SRC_EOF =                         0x00100000,     /* EOF reached */
90   XML_SRC_EXPECTED_DECL =               0x00200000,     /* Just before optional or required XMLDecl/TextDecl */
91   XML_SRC_NEW_LINE =                    0x00400000,     /* The last read character is 0xD */
92   XML_SRC_SURROUND =                    0x00800000,     /* Surround the text with 0x20 (references to parameter entities) */
93   XML_SRC_DOCUMENT =                    0x01000000,     /* The document entity */
94   XML_SRC_EXTERNAL =                    0x02000000,     /* An external entity */
95 };
96
97 enum xml_node_type {
98   XML_NODE_ELEM,
99   XML_NODE_COMMENT,
100   XML_NODE_CHARS,
101   XML_NODE_PI,
102 };
103
104 #define XML_NODE_FOR_EACH(var, node) CLIST_FOR_EACH(struct xml_node *, var, (node)->sons)
105 #define XML_ATTR_FOR_EACH(var, node) SLIST_FOR_EACH(struct xml_attr *, var, (node)->attrs)
106
107 struct xml_node {
108   cnode n;                                              /* Node for list of parent's sons */
109   uns type;                                             /* XML_NODE_x */
110   struct xml_node *parent;                              /* Parent node */
111   char *name;                                           /* Element name / PI target */
112   clist sons;                                           /* Children nodes */
113   union {
114     struct {
115       char *text;                                       /* PI text / Comment / CDATA */
116       uns len;                                          /* Text length in bytes */
117     };
118     struct {
119       struct xml_dtd_elem *dtd;                         /* Element DTD */
120       slist attrs;                                      /* Link list of element attributes */
121     };
122   };
123 };
124
125 struct xml_attr {
126   snode n;                                              /* Node for elem->attrs */
127   struct xml_node *elem;                                /* Parent element */
128   char *name;                                           /* Attribute name */
129   char *val;                                            /* Attribute value */
130 };
131
132 struct xml_context {
133   /* Error handling */
134   char *err_msg;                                        /* Last error message */
135   enum xml_error err_code;                              /* Last error code */
136   void *throw_buf;                                      /* Where to jump on error */
137   void (*h_warn)(struct xml_context *ctx);              /* Warning callback */
138   void (*h_error)(struct xml_context *ctx);             /* Recoverable error callback */
139   void (*h_fatal)(struct xml_context *ctx);             /* Unrecoverable error callback */
140
141   /* Memory management */
142   struct mempool *pool;                                 /* DOM pool */
143   struct mempool *stack;                                /* Stack pool (free as soon as possible) */
144   struct xml_stack *stack_list;                         /* See xml_push(), xml_pop() */
145   uns flags;                                            /* XML_FLAG_x (restored on xml_pop()) */
146   uns depth;                                            /* Nesting level */
147   struct fastbuf chars;                                 /* Character data / attribute value */
148   void *tab_attrs;                                      /* Hash table of element attributes */
149
150   /* Input */
151   struct xml_source *src;                               /* Current source */
152   u32 *bptr, *bstop;                                    /* Buffer with preprocessed characters (validated UCS-4 + category flags) */
153   uns cat_chars;                                        /* Unicode range of supported characters (cdata, attribute values, ...) */
154   uns cat_unrestricted;                                 /* Unrestricted characters (may appear in document/external entities) */
155   uns cat_new_line;                                     /* New line characters */
156   uns cat_name;                                         /* Characters that may appear in names */
157   uns cat_sname;                                        /* Characters that may begin a name */
158
159   /* SAX-like interface */
160   void (*h_document_start)(struct xml_context *ctx);    /* Called before entering prolog */
161   void (*h_document_end)(struct xml_context *ctx);      /* Called after leaving epilog */
162   void (*h_xml_decl)(struct xml_context *ctx);          /* Called after the XML declaration */
163   void (*h_doctype_decl)(struct xml_context *ctx);      /* Called in the doctype declaration (before optional internal subset) */
164   void (*h_comment)(struct xml_context *ctx);           /* Called after a comment (only with XML_REPORT_COMMENTS) */
165   void (*h_pi)(struct xml_context *ctx);                /* Called after a processing instruction (only with XML_REPORT_PIS) */
166   void (*h_stag)(struct xml_context *ctx);              /* Called after STag or EmptyElemTag (only with XML_REPORT_TAGS) */
167   void (*h_etag)(struct xml_context *ctx);              /* Called before ETag or after EmptyElemTag (only with XML_REPORT_TAGS) */
168   void (*h_chars)(struct xml_context *ctx);             /* Called after some characters (only with XML_REPORT_CHARS) */
169   void (*h_cdata)(struct xml_context *ctx);             /* Called after a CDATA section (only with XML_REPORT_CHARS and XML_UNFOLD_CDATA) */
170   void (*h_dtd_start)(struct xml_context *ctx);         /* Called just after the DTD structure is initialized */
171   void (*h_dtd_end)(struct xml_context *ctx);           /* Called after DTD subsets subsets */
172
173   /* DOM */
174   struct xml_node *root;                                /* DOM root */
175   struct xml_node *node;                                /* Current DOM node */
176
177   char *version_str;
178   uns standalone;
179   char *doctype;                                        /* The document type (or NULL if unknown) */
180   char *system_id;                                      /* DTD external id */
181   char *public_id;                                      /* DTD public id */
182   struct xml_dtd *dtd;                                  /* The DTD structure (or NULL) */
183   uns state;                                            /* Current state for the PULL interface (XML_STATE_x) */
184   uns pull;                                             /* Parameters for the PULL interface (XML_PULL_x) */
185
186   void (*start_entity)(struct xml_context *ctx);
187   void (*end_entity)(struct xml_context *ctx);
188   struct fastbuf *(*resolve_entity)(struct xml_context *ctx);
189   void (*notation_decl)(struct xml_context *ctx);
190   void (*unparsed_entity_decl)(struct xml_context *ctx);
191 };
192
193 /* Initialize XML context */
194 void xml_init(struct xml_context *ctx);
195
196 /* Clean up all internal structures */
197 void xml_cleanup(struct xml_context *ctx);
198
199 /* Reuse XML context */
200 void xml_reset(struct xml_context *ctx);
201
202 /* Setup XML source (fastbuf will be automatically closed) */
203 void xml_set_source(struct xml_context *ctx, struct fastbuf *fb);
204
205 /* Parse without the PUSH interface, return XML_ERR_x code (zero on success) */
206 uns xml_parse(struct xml_context *ctx);
207
208 /* Parse with the PUSH interface, return XML_STATE_x (zero on EOF or fatal error) */
209 uns xml_next(struct xml_context *ctx);
210
211 uns xml_row(struct xml_context *ctx);
212 struct xml_attr *xml_attr_find(struct xml_context *ctx, struct xml_node *node, char *name);
213
214 #endif