* See the file LICENSE for details.
* Sebastian Hammer, Adam Dickmeiss
*
- * $Id: d1_read.c,v 1.45 2002-07-05 16:04:28 adam Exp $
+ * $Id: d1_read.c,v 1.51 2002-08-28 07:54:11 adam Exp $
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
-#include <errno.h>
-
-#if HAVE_ICONV_H
-#include <iconv.h>
-#endif
-
#include <yaz/xmalloc.h>
+#include <yaz/yaz-util.h>
#include <yaz/log.h>
#include <yaz/data1.h>
{
if (!n)
return 0;
- n = n->child;
- while (n && n->which != DATA1N_tag)
- n = n->next;
+ if (data1_is_xmlmode(dh))
+ {
+ n = n->child;
+ while (n && n->which != DATA1N_tag)
+ n = n->next;
+ }
return n;
}
*/
data1_node *get_parent_tag (data1_handle dh, data1_node *n)
{
- for (; n && n->which != DATA1N_root; n = n->parent)
- if (n->which == DATA1N_tag)
- return n;
+ if (data1_is_xmlmode(dh))
+ {
+ for (; n && n->which != DATA1N_root; n = n->parent)
+ if (n->which == DATA1N_tag && n->parent &&
+ n->parent->which != DATA1N_root)
+ return n;
+ }
+ else
+ {
+ for (; n && n->which != DATA1N_root; n = n->parent)
+ if (n->which == DATA1N_tag)
+ return n;
+ }
return 0;
}
{
data1_node *partag = get_parent_tag(dh, at);
data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_tag, at);
- data1_element *e = NULL;
data1_xattr **p;
+ data1_element *e = 0;
res->u.tag.tag = data1_insert_string_n (dh, res, nmem, tag, len);
- if (partag)
- e = partag->u.tag.element;
- res->u.tag.element =
- data1_getelementbytagname (dh, at->root->u.root.absyn,
- e, res->u.tag.tag);
+ if (!partag) /* top tag? */
+ e = data1_getelementbytagname (dh, at->root->u.root.absyn,
+ 0 /* index as local */,
+ res->u.tag.tag);
+ else
+ {
+ /* only set element for known tags */
+ e = partag->u.tag.element;
+ if (e)
+ e = data1_getelementbytagname (dh, at->root->u.root.absyn,
+ e, res->u.tag.tag);
+ }
+ res->u.tag.element = e;
p = &res->u.tag.attributes;
while (attr && *attr)
{
data1_node *datn = 0;
data1_node *tagn = 0;
- if (partag)
+ if (!partag)
+ e = data1_getelementbytagname (dh, root->u.root.absyn, 0, tagname);
+ else
+ {
e = partag->u.tag.element;
- e = data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
+ if (e)
+ e = data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
+ }
if (local_allowed || e)
{
tagn = data1_mk_node2 (dh, m, DATA1N_tag, at);
}
}
+static int ampr (int (*get_byte)(void *fh), void *fh, int *amp)
+{
+ int c = (*get_byte)(fh);
+ *amp = 0;
+ if (c == '&')
+ {
+ char ent[20];
+ int i = 0;
+
+ while (1)
+ {
+ c = (*get_byte)(fh);
+ if (c == ';')
+ {
+ ent[i] = 0;
+
+ c = ' ';
+ if (!strcmp (ent, "quot"))
+ c = '"';
+ if (!strcmp (ent, "apos"))
+ c = '\'';
+ if (!strcmp (ent, "gt"))
+ c = '>';
+ if (!strcmp (ent, "lt"))
+ c = '<';
+ if (!strcmp (ent, "amp"))
+ c = '&';
+ *amp = 1;
+ break;
+ }
+ else if (c == 0 || d1_isspace(c))
+ break;
+ if (i < 19)
+ ent[i++] = c;
+ }
+ }
+ return c;
+}
data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
int (*get_byte)(void *fh), void *fh,
- WRBUF wrbuf, int *ch)
+ WRBUF wrbuf, int *ch, int *amp)
{
data1_xattr *p_first = 0;
data1_xattr **pp = &p_first;
{
data1_xattr *p;
int len;
- while (c && d1_isspace(c))
- c = (*get_byte)(fh);
- if (!c || c == '>' || c == '/')
+ while (*amp || (c && d1_isspace(c)))
+ c = ampr (get_byte, fh, amp);
+ if (*amp == 0 && (c == 0 || c == '>' || c == '/'))
break;
*pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
p->next = 0;
while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
}
wrbuf_putc (wrbuf, '\0');
len = wrbuf_len(wrbuf);
strcpy (p->name, wrbuf_buf(wrbuf));
if (c == '=')
{
- c = (*get_byte)(fh);
- if (c == '"')
+ c = ampr (get_byte, fh, amp);
+ if (*amp == 0 && c == '"')
{
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
wrbuf_rewind(wrbuf);
- while (c && c != '"')
+ while (*amp || (c && c != '"'))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
}
if (c)
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
+ }
+ else if (*amp == 0 && c == '\'')
+ {
+ c = ampr (get_byte, fh, amp);
+ wrbuf_rewind(wrbuf);
+ while (*amp || (c && c != '\''))
+ {
+ wrbuf_putc (wrbuf, c);
+ c = ampr (get_byte, fh, amp);
+ }
+ if (c)
+ c = ampr (get_byte, fh, amp);
}
else
{
wrbuf_rewind(wrbuf);
- while (c && c != '>' && c != '/')
+ while (*amp || (c && c != '>' && c != '/'))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
}
}
wrbuf_putc (wrbuf, '\0');
{
data1_node *d1_stack[256];
data1_node *res;
- int c;
+ int c, amp;
int level = 0;
int line = 1;
d1_stack[level] = 0;
- c = (*get_byte)(fh);
- while (1)
+ c = ampr (get_byte, fh, &);
+ while (c != '\0')
{
data1_node *parent = level ? d1_stack[level-1] : 0;
- while (c != '\0' && d1_isspace(c))
- {
- if (c == '\n')
- line++;
- c = (*get_byte)(fh);
- }
- if (c == '\0')
- break;
-
- if (c == '<') /* beginning of tag */
+
+ if (amp == 0 && c == '<') /* beginning of tag */
{
data1_xattr *xattr;
int end_tag = 0;
size_t i = 0;
- c = (*get_byte)(fh);
- if (c == '/')
+ c = ampr (get_byte, fh, &);
+ if (amp == 0 && c == '/')
{
end_tag = 1;
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
- else if (c == '!') /* tags/comments that we don't deal with yet */
+ else if (amp == 0 && c == '!')
+ /* tags/comments that we don't deal with yet */
{
- while (c && c != '>')
- c = (*get_byte)(fh);
+ while (amp || (c && c != '>'))
+ c = ampr (get_byte, fh, &);
if (c)
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
continue;
}
- while (c && c != '>' && c != '/' && !d1_isspace(c))
+ while (amp || (c && c != '>' && c != '/' && !d1_isspace(c)))
{
if (i < (sizeof(tag)-1))
tag[i++] = c;
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
tag[i] = '\0';
- xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c);
+ xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c, &);
args[0] = '\0';
- if (c == '/')
+ if (amp == 0 && c == '/')
{ /* <tag attrs/> or <tag/> */
null_tag = 1;
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
- if (c != '>')
+ if (amp || c != '>')
{
yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
return 0;
}
else
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
/* End tag? */
if (end_tag)
break;
}
}
- if (level <= 1)
- return d1_stack[0];
+ if (data1_is_xmlmode(dh))
+ {
+ if (level <= 1)
+ return d1_stack[0];
+ }
+ else
+ {
+ if (level <= 0)
+ return d1_stack[0];
+ }
continue;
}
else if (!strcmp(tag, "var"))
if (level == 0)
{
parent = data1_mk_root (dh, m, tag);
- d1_stack[level++] = parent;
+ res = d1_stack[level] = parent;
+
+ if (data1_is_xmlmode(dh))
+ {
+ level++;
+ res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
+ res->u.tag.attributes = xattr;
+ }
+ }
+ else
+ {
+ res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
+ res->u.tag.attributes = xattr;
}
- res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
- res->u.tag.attributes = xattr;
}
d1_stack[level] = res;
d1_stack[level+1] = 0;
}
else /* != '<'... this is a body of text */
{
- const char *src;
- char *dst;
- int len, prev_char = 0;
+ int len;
if (level == 0)
{
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
continue;
}
res = data1_mk_node2 (dh, m, DATA1N_data, parent);
wrbuf_rewind(wrbuf);
- while (c && c != '<')
+ while (amp || (c && c != '<'))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
len = wrbuf_len(wrbuf);
res->u.data.data = (char*) nmem_malloc (m, len);
else
res->u.data.data = res->lbuf;
-
- /* read "data" and transfer while removing white space */
- dst = res->u.data.data;
- for (src = wrbuf_buf(wrbuf); --len >= 0; src++)
- {
- if (*src == '\n')
- line++;
- if (d1_isspace (*src))
- prev_char = ' ';
- else
- {
- if (prev_char)
- {
- *dst++ = prev_char;
- prev_char = 0;
- }
- *dst++ = *src;
- }
- }
- res->u.data.len = dst - res->u.data.data;
+
+ if (len)
+ memcpy (res->u.data.data, wrbuf_buf(wrbuf), len);
+ else
+ res->u.data.data = 0;
+ res->u.data.len = len;
}
}
return 0;
}
-#if HAVE_ICONV_H
-
-static int conv_item (NMEM m, iconv_t t,
+static int conv_item (NMEM m, yaz_iconv_t t,
WRBUF wrbuf, char *inbuf, size_t inlen)
{
wrbuf_rewind (wrbuf);
{
char *outbuf = wrbuf->buf + wrbuf->pos;
size_t outlen = wrbuf->size - wrbuf->pos;
- if (iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
- (size_t)(-1) && errno != E2BIG)
+ if (yaz_iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
+ (size_t)(-1) && yaz_iconv_error(t) != YAZ_ICONV_E2BIG)
{
/* bad data. stop and skip conversion entirely */
return -1;
}
static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
- iconv_t t, WRBUF wrbuf)
+ yaz_iconv_t t, WRBUF wrbuf, const char *tocode)
{
for (; n; n = n->next)
{
data1_xattr *p;
for (p = n->u.tag.attributes; p; p = p->next)
{
- if (conv_item(m, t, wrbuf, p->value, strlen(p->value))
+ if (p->value &&
+ conv_item(m, t, wrbuf, p->value, strlen(p->value))
== 0)
{
wrbuf_puts (wrbuf, "");
}
}
break;
+ case DATA1N_preprocess:
+ if (strcmp(n->u.preprocess.target, "xml") == 0)
+ {
+ data1_xattr *p = n->u.preprocess.attributes;
+ for (; p; p = p->next)
+ if (strcmp (p->name, "encoding") == 0)
+ p->value = nmem_strdup (m, tocode);
+ }
+ break;
}
- data1_iconv_s (dh, m, n->child, t, wrbuf);
+ data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
}
}
-#endif
+
+const char *data1_get_encoding (data1_handle dh, data1_node *n)
+{
+ /* see if we have an xml header that specifies encoding */
+ if (n && n->child && n->child->which == DATA1N_preprocess &&
+ strcmp (n->child->u.preprocess.target, "xml") == 0)
+ {
+ data1_xattr *xp = n->child->u.preprocess.attributes;
+ for (; xp; xp = xp->next)
+ if (!strcmp (xp->name, "encoding") == 0)
+ return xp->value;
+ }
+ /* no encoding in header, so see if "encoding" was specified for abs */
+ if (n && n->which == DATA1N_root &&
+ n->u.root.absyn && n->u.root.absyn->encoding)
+ return n->u.root.absyn->encoding;
+ /* none of above, return a hard coded default */
+ return "ISO-8859-1";
+}
int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
const char *tocode,
const char *fromcode)
{
-#if HAVE_ICONV_H
- WRBUF wrbuf = wrbuf_alloc();
- iconv_t t = iconv_open (tocode, fromcode);
- if (t == (iconv_t) (-1))
- return -1;
- data1_iconv_s (dh, m, n, t, wrbuf);
- iconv_close (t);
- wrbuf_free (wrbuf, 1);
+ if (strcmp (tocode, fromcode))
+ {
+ WRBUF wrbuf = wrbuf_alloc();
+ yaz_iconv_t t = yaz_iconv_open (tocode, fromcode);
+ if (!t)
+ return -1;
+ data1_iconv_s (dh, m, n, t, wrbuf, tocode);
+ yaz_iconv_close (t);
+ wrbuf_free (wrbuf, 1);
+ }
return 0;
-#else
- return -2;
-#endif
}