From a774858896a9b56c966edbfb70d8eae1c91c4ad1 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 3 Jul 2002 10:04:04 +0000 Subject: [PATCH] Extra root tag node for data1 --- include/yaz/data1.h | 26 ++++++++++++- retrieval/d1_expat.c | 69 ++++++++++++++++++++++++--------- retrieval/d1_read.c | 105 +++++++++++++++++++++++++++++++++++++++++++++----- retrieval/d1_write.c | 35 +++++++++++++---- 4 files changed, 197 insertions(+), 38 deletions(-) diff --git a/include/yaz/data1.h b/include/yaz/data1.h index 282d301..a1ce5ee 100644 --- a/include/yaz/data1.h +++ b/include/yaz/data1.h @@ -23,7 +23,7 @@ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. * - * $Id: data1.h,v 1.11 2002-05-21 07:43:16 adam Exp $ + * $Id: data1.h,v 1.12 2002-07-03 10:04:04 adam Exp $ */ #ifndef DATA1_H @@ -227,9 +227,11 @@ typedef struct data1_node #define DATA1N_data 3 /* variant specification (a triple, actually) */ #define DATA1N_variant 4 - int which; /* comment (same as data) */ #define DATA1N_comment 5 + /* preprocessing instruction */ +#define DATA1N_preprocess 6 + int which; union { struct @@ -272,6 +274,12 @@ typedef struct data1_node data1_vartype *type; char *value; } variant; + + struct + { + char *target; + data1_xattr *attributes; + } preprocess; } u; void (*destroy)(struct data1_node *n); @@ -343,10 +351,15 @@ YAZ_EXPORT data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem, const char *tag, size_t len, const char **attr, data1_node *at); +YAZ_EXPORT void data1_tag_add_attr (data1_handle dh, NMEM nmem, + data1_node *res, const char **attr); YAZ_EXPORT data1_node *data1_mk_text_n (data1_handle dh, NMEM mem, const char *buf, size_t len, data1_node *parent); +YAZ_EXPORT data1_node *data1_mk_text_nf (data1_handle dh, NMEM mem, + const char *buf, size_t len, + data1_node *parent); YAZ_EXPORT data1_node *data1_mk_text (data1_handle dh, NMEM mem, const char *buf, data1_node *parent); @@ -357,8 +370,15 @@ YAZ_EXPORT data1_node *data1_mk_comment_n (data1_handle dh, NMEM mem, YAZ_EXPORT data1_node *data1_mk_comment (data1_handle dh, NMEM mem, const char *buf, data1_node *parent); +YAZ_EXPORT data1_node *data1_mk_preprocess (data1_handle dh, NMEM nmem, + const char *target, + const char **attr, + data1_node *at); + YAZ_EXPORT data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name); +YAZ_EXPORT void data1_set_root(data1_handle dh, data1_node *res, + NMEM nmem, const char *name); YAZ_EXPORT data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at, const char *tag, int num, @@ -447,6 +467,8 @@ YAZ_EXPORT data1_node *data1_add_taggeddata (data1_handle dh, data1_node *root, const char *tagname, NMEM m); +YAZ_EXPORT data1_node *data1_get_root_tag (data1_handle dh, data1_node *n); + YAZ_END_CDECL #endif diff --git a/retrieval/d1_expat.c b/retrieval/d1_expat.c index d308328..dc1ed78 100644 --- a/retrieval/d1_expat.c +++ b/retrieval/d1_expat.c @@ -2,7 +2,7 @@ * Copyright (c) 2002, Index Data. * See the file LICENSE for details. * - * $Id: d1_expat.c,v 1.2 2002-05-21 07:43:16 adam Exp $ + * $Id: d1_expat.c,v 1.3 2002-07-03 10:04:04 adam Exp $ */ #if HAVE_EXPAT_H @@ -27,10 +27,12 @@ struct user_info { static void cb_start (void *user, const char *el, const char **attr) { struct user_info *ui = (struct user_info*) user; + if (ui->level == 1) + data1_set_root (ui->dh, ui->d1_stack[0], ui->nmem, el); ui->d1_stack[ui->level] = data1_mk_tag (ui->dh, ui->nmem, el, attr, ui->d1_stack[ui->level-1]); ui->level++; - printf ("cb_start %s\n", el); + yaz_log (LOG_DEBUG, "cb_start %s", el); } static void cb_end (void *user, const char *el) @@ -38,7 +40,7 @@ static void cb_end (void *user, const char *el) struct user_info *ui = (struct user_info*) user; ui->level--; - printf ("cb_end %s\n", el); + yaz_log (LOG_DEBUG, "cb_end %s", el); } static void cb_chardata (void *user, const char *s, int len) @@ -59,33 +61,61 @@ static void cb_chardata (void *user, const char *s, int len) static void cb_decl (void *user, const char *version, const char*encoding, int standalone) { - printf ("decl version=%s encoding=%s\n", version ? version : "null", - encoding ? encoding : "null"); -} + struct user_info *ui = (struct user_info*) user; + const char *attr_list[7]; + + attr_list[0] = "version"; + attr_list[1] = version; + + attr_list[2] = "encoding"; + attr_list[3] = encoding; -static void cb_processing (void *userData, const char *target, + attr_list[4] = "standalone"; + attr_list[5] = standalone ? "yes" : "no"; + + attr_list[6] = 0; + + data1_mk_preprocess (ui->dh, ui->nmem, "xml", attr_list, + ui->d1_stack[ui->level-1]); + yaz_log (LOG_DEBUG, "decl version=%s encoding=%s", + version ? version : "null", + encoding ? encoding : "null"); +} + +static void cb_processing (void *user, const char *target, const char *data) { - printf ("decl processing target=%s data=%s\n", target ? target : "null", - data ? data : "null"); + struct user_info *ui = (struct user_info*) user; + data1_node *res = + data1_mk_preprocess (ui->dh, ui->nmem, target, 0, + ui->d1_stack[ui->level-1]); + data1_mk_text_nf (ui->dh, ui->nmem, data, strlen(data), res); + + yaz_log (LOG_DEBUG, "decl processing target=%s data=%s", + target ? target : "null", + data ? data : "null"); + + } -static void cb_comment (void *userData, const char *data) +static void cb_comment (void *user, const char *data) { - printf ("decl comment data=%s\n", data ? data : "null"); + struct user_info *ui = (struct user_info*) user; + yaz_log (LOG_DEBUG, "decl comment data=%s", data ? data : "null"); + data1_mk_comment (ui->dh, ui->nmem, data, ui->d1_stack[ui->level-1]); } static void cb_doctype_start (void *userData, const char *doctypeName, const char *sysid, const char *pubid, int has_internal_subset) { - printf ("doctype start doctype=%s sysid=%s pubid=%s\n", - doctypeName, sysid, pubid); + yaz_log (LOG_DEBUG, "doctype start doctype=%s sysid=%s pubid=%s", + doctypeName, sysid, pubid); } static void cb_doctype_end (void *userData) { - printf ("doctype end\n"); + yaz_log (LOG_DEBUG, "doctype end"); } @@ -95,10 +125,11 @@ static void cb_entity_decl (void *userData, const char *entityName, const char *base, const char *systemId, const char *publicId, const char *notationName) { - printf ("entity %s is_para_entry=%d value=%.*s base=%s systemId=%s\n" - " publicId=%s notationName=%s\n", - entityName, is_parameter_entity, value_length, value, - base, systemId, publicId, notationName); + yaz_log (LOG_DEBUG, + "entity %s is_para_entry=%d value=%.*s base=%s systemId=%s" + " publicId=%s notationName=%s", + entityName, is_parameter_entity, value_length, value, + base, systemId, publicId, notationName); } @@ -115,7 +146,7 @@ data1_node *data1_read_xml (data1_handle dh, uinfo.level = 1; uinfo.dh = dh; uinfo.nmem = m; - uinfo.d1_stack[0] = data1_mk_root (dh, m, "root"); + uinfo.d1_stack[0] = data1_mk_node2 (dh, m, DATA1N_root, 0); uinfo.d1_stack[1] = 0; /* indicate no children (see end of routine) */ parser = XML_ParserCreate (0 /* encoding */); diff --git a/retrieval/d1_read.c b/retrieval/d1_read.c index d2c8863..66baf41 100644 --- a/retrieval/d1_read.c +++ b/retrieval/d1_read.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: d1_read.c,v 1.43 2002-05-21 07:43:16 adam Exp $ + * $Id: d1_read.c,v 1.44 2002-07-03 10:04:04 adam Exp $ */ #include @@ -14,6 +14,16 @@ #include #include +data1_node *data1_get_root_tag (data1_handle dh, data1_node *n) +{ + if (!n) + return 0; + n = n->child; + while (n && n->which != DATA1N_tag) + n = n->next; + return n; +} + /* * get the tag which is the immediate parent of this node (this may mean * traversing intermediate things like variants and stuff. @@ -81,10 +91,20 @@ data1_node *data1_mk_node2 (data1_handle dh, NMEM m, int type, r->u.data.what = 0; r->u.data.formatted_text = 0; break; + case DATA1N_comment: + r->u.data.data = 0; + r->u.data.len = 0; + r->u.data.what = 0; + r->u.data.formatted_text = 1; + break; case DATA1N_variant: r->u.variant.type = 0; r->u.variant.value = 0; break; + case DATA1N_preprocess: + r->u.preprocess.target = 0; + r->u.preprocess.attributes = 0; + break; default: logf (LOG_WARN, "data_mk_node_type. bad type = %d\n", type); } @@ -105,7 +125,7 @@ void data1_free_tree (data1_handle dh, data1_node *t) (*t->destroy)(t); } -data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name) +data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name) { data1_absyn *absyn = data1_get_absyn (dh, name); data1_node *res; @@ -121,6 +141,35 @@ data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name) return res; } +void data1_set_root(data1_handle dh, data1_node *res, + NMEM nmem, const char *name) +{ + data1_absyn *absyn = data1_get_absyn (dh, name); + + res->u.root.type = data1_insert_string (dh, res, nmem, name); + res->u.root.absyn = absyn; +} + +data1_node *data1_mk_preprocess (data1_handle dh, NMEM nmem, + const char *target, const char **attr, + data1_node *at) +{ + data1_xattr **p; + data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_preprocess, at); + res->u.preprocess.target = data1_insert_string (dh, res, nmem, target); + + p = &res->u.preprocess.attributes; + while (attr && *attr) + { + *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p)); + (*p)->name = nmem_strdup (nmem, *attr++); + (*p)->value = nmem_strdup (nmem, *attr++); + p = &(*p)->next; + } + *p = 0; + return res; +} + data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem, const char *tag, size_t len, const char **attr, data1_node *at) @@ -149,6 +198,28 @@ data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem, return res; } +void data1_tag_add_attr (data1_handle dh, NMEM nmem, + data1_node *res, const char **attr) +{ + data1_xattr **p; + + if (res->which != DATA1N_tag) + return; + + p = &res->u.tag.attributes; + while (*p) + p = &(*p)->next; + + while (attr && *attr) + { + *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p)); + (*p)->name = nmem_strdup (nmem, *attr++); + (*p)->value = nmem_strdup (nmem, *attr++); + p = &(*p)->next; + } + *p = 0; +} + data1_node *data1_mk_tag (data1_handle dh, NMEM nmem, const char *tag, const char **attr, data1_node *at) { @@ -158,6 +229,13 @@ data1_node *data1_mk_tag (data1_handle dh, NMEM nmem, data1_node *data1_search_tag (data1_handle dh, data1_node *n, const char *tag) { + if (*tag == '/') + { + n = data1_get_root_tag (dh, n); + if (n) + n = n->child; + tag++; + } for (; n; n = n->next) if (n->which == DATA1N_tag && n->u.tag.tag && !yaz_matchstr (tag, n->u.tag.tag)) @@ -178,7 +256,6 @@ data1_node *data1_mk_tag_uni (data1_handle dh, NMEM nmem, return node; } - data1_node *data1_mk_text_n (data1_handle dh, NMEM mem, const char *buf, size_t len, data1_node *parent) { @@ -190,6 +267,14 @@ data1_node *data1_mk_text_n (data1_handle dh, NMEM mem, return res; } +data1_node *data1_mk_text_nf (data1_handle dh, NMEM mem, + const char *buf, size_t len, data1_node *parent) +{ + data1_node *res = data1_mk_text_n (dh, mem, buf, len, parent); + res->u.data.formatted_text = 1; + return res; +} + data1_node *data1_mk_text (data1_handle dh, NMEM mem, const char *buf, data1_node *parent) { @@ -531,14 +616,10 @@ data1_node *data1_read_nodex (data1_handle dh, NMEM m, break; } } - if (level == 0) + if (level <= 1) return d1_stack[0]; continue; } - if (level == 0) /* root ? */ - { - res = data1_mk_root (dh, m, tag); - } else if (!strcmp(tag, "var")) { char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL]; @@ -584,7 +665,13 @@ data1_node *data1_read_nodex (data1_handle dh, NMEM m, } else { - /* tag.. acquire our element in the abstract syntax */ + + /* tag .. acquire our element in the abstract syntax */ + if (level == 0) + { + parent = data1_mk_root (dh, m, tag); + d1_stack[level++] = parent; + } res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent); res->u.tag.attributes = xattr; } diff --git a/retrieval/d1_write.c b/retrieval/d1_write.c index b93a1e7..8789270 100644 --- a/retrieval/d1_write.c +++ b/retrieval/d1_write.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Id: d1_write.c,v 1.12 2002-05-21 07:43:16 adam Exp $ + * $Id: d1_write.c,v 1.13 2002-07-03 10:04:04 adam Exp $ */ #include @@ -31,7 +31,29 @@ static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col) { char *tag; - if (c->which == DATA1N_tag) + if (c->which == DATA1N_preprocess) + { + data1_xattr *p; + + sprintf (line, "%*su.preprocess.target); + for (p = c->u.preprocess.attributes; p; p = p->next) + { + wrbuf_putc (b, ' '); + wrbuf_puts (b, p->name); + wrbuf_putc (b, '='); + wrbuf_putc (b, '"'); + wrbuf_puts (b, p->value); + wrbuf_putc (b, '"'); + } + if (c->child) + wrbuf_puts(b, " "); + if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0) + return -1; + wrbuf_puts (b, "?>\n"); + } + else if (c->which == DATA1N_tag) { if (select && c->u.tag.node_selected) continue; @@ -125,13 +147,15 @@ static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col) break; case DATA1I_num: wrbuf_write(b, c->u.data.data, c->u.data.len); + wrbuf_write(b, "\n", 1); break; case DATA1I_oid: wrbuf_write(b, c->u.data.data, c->u.data.len); + wrbuf_write(b, "\n", 1); } if (c->which == DATA1N_comment) { - wrbuf_write (b, "-->", 3); + wrbuf_write (b, "-->\n", 4); } } } @@ -141,16 +165,11 @@ static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col) char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len) { WRBUF b = data1_get_wrbuf (dh); - char line[1024]; wrbuf_rewind(b); - sprintf(line, "<%s>\n", n->u.root.type); - wrbuf_write(b, line, strlen(line)); if (nodetoidsgml(n, select, b, 0)) return 0; - sprintf(line, "\n", n->u.root.type); - wrbuf_write(b, line, strlen(line)); *len = wrbuf_len(b); return wrbuf_buf(b); } -- 1.7.10.4