#include "sx.h"
void _sx_element_start(void *arg, const char *name, const char **atts) {
sx_t s = (sx_t) arg;
char buf[1024];
char *uri, *elem, *prefix;
const char **attr;
int ns;
if(s->fail) return;
if(s->nad == NULL)
s->nad = nad_new(s->nad_cache);
strncpy(buf, name, 1024);
buf[1023] = '\0';
uri = buf;
elem = strchr(uri, '|');
if(elem != NULL) {
*elem = '\0';
elem++;
prefix = strchr(elem, '|');
if(prefix != NULL) {
*prefix = '\0';
prefix++;
}
ns = nad_add_namespace(s->nad, uri, prefix);
} else {
uri = NULL;
elem = buf;
prefix = NULL;
ns = -1;
}
nad_append_elem(s->nad, ns, elem, s->depth - 1);
attr = atts;
while(attr[0] != NULL) {
strncpy(buf, attr[0], 1024);
buf[1023] = '\0';
uri = buf;
elem = strchr(uri, '|');
if(elem != NULL) {
*elem = '\0';
elem++;
prefix = strchr(elem, '|');
if(prefix != NULL) {
*prefix = '\0';
prefix++;
}
ns = nad_add_namespace(s->nad, uri, prefix);
} else {
uri = NULL;
elem = buf;
prefix = NULL;
ns = -1;
}
nad_append_attr(s->nad, ns, elem, (char *) attr[1]);
attr += 2;
}
s->depth++;
}
void _sx_element_end(void *arg, const char *name) {
sx_t s = (sx_t) arg;
if(s->fail) return;
s->depth--;
if(s->depth == 1) {
jqueue_push(s->rnadq, s->nad, 0);
s->nad = NULL;
}
else if(s->depth == 0)
s->depth = -1;
}
void _sx_cdata(void *arg, const char *str, int len) {
sx_t s = (sx_t) arg;
if(s->fail) return;
if(s->nad == NULL)
return;
nad_append_cdata(s->nad, (char *) str, len, s->depth - 1);
}
void _sx_namespace_start(void *arg, const char *prefix, const char *uri) {
sx_t s = (sx_t) arg;
int ns;
if(s->fail) return;
if(uri == NULL) return;
if(s->nad == NULL)
s->nad = nad_new(s->nad_cache);
ns = nad_add_namespace(s->nad, (char *) uri, (char *) prefix);
s->nad->scope = ns;
}