Beispiel #1
0
static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
{
    yaz_attributes_to_wrbuf(b, zapt->attributes);
    switch (zapt->term->which)
    {
    case Z_Term_general:
        yaz_encode_pqf_term(b, (const char *)zapt->term->u.general->buf,
                            zapt->term->u.general->len);
        break;
    case Z_Term_characterString:
        wrbuf_puts(b, "@term string ");
        yaz_encode_pqf_term(b, zapt->term->u.characterString,
                            strlen(zapt->term->u.characterString));
        break;
    case Z_Term_numeric:
        wrbuf_printf(b, "@term numeric " ODR_INT_PRINTF " ",
                     *zapt->term->u.numeric);
        break;
    case Z_Term_null:
        wrbuf_puts(b, "@term null x");
        break;
    default:
        wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
    }
}
Beispiel #2
0
void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
{
    assert(q);
    assert(b);
    switch (q->which)
    {
    case Z_Query_type_1:
    case Z_Query_type_101:
        wrbuf_puts(b,"RPN ");
        yaz_rpnquery_to_wrbuf(b, q->u.type_1);
        break;
    case Z_Query_type_2:
        wrbuf_puts(b, "CCL ");
        wrbuf_write(b, (const char *) q->u.type_2->buf, q->u.type_2->len);
        break;
    case Z_Query_type_100:
        wrbuf_puts(b, "Z39.58 ");
        wrbuf_write(b, (const char *) q->u.type_100->buf, q->u.type_100->len);
        break;
    case Z_Query_type_104:
        if (q->u.type_104->which == Z_External_CQL)
        {
            wrbuf_puts(b, "CQL ");
            wrbuf_puts(b, q->u.type_104->u.cql);
        }
        else
            wrbuf_printf(b,"UNKNOWN type 104 query %d", q->u.type_104->which);
    }
}
Beispiel #3
0
static void tst2(void)
{
    WRBUF w = wrbuf_alloc();
    cql_transform_t ct = 0;
    const char *srcdir = getenv("srcdir");
    if (srcdir)
    {
        wrbuf_puts(w, srcdir);
        wrbuf_puts(w, "/");
    }
    wrbuf_puts(w, "../etc/pqf.properties");
    
    ct = cql_transform_open_fname(wrbuf_cstr(w));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title=/exact abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc",
                      "dc.title=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
    YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date<1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date=1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date>1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date<=1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date>=1980"));

    YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "cql.allRecords=1"));
    YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
    cql_transform_close(ct);
    wrbuf_destroy(w);
}
Beispiel #4
0
static int rpn2solr_attr(solr_transform_t ct,
                         Z_AttributeList *attributes, WRBUF w)
{
    const char *index = solr_lookup_reverse(ct, "index.", attributes);
    const char *structure = solr_lookup_reverse(ct, "structure.", attributes);

    /* if no real match, try string attribute */
    if (!index)
        index = lookup_index_from_string_attr(attributes);
    if (!index)
        return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
    /* for serverChoice we omit index+relation+structure */
    if (strcmp(index, "cql.serverChoice"))
    {
        wrbuf_puts(w, index);
        wrbuf_puts(w, ":");
        if (structure)
        {
            if (strcmp(structure, "*"))
            {
                wrbuf_puts(w, "/");
                wrbuf_puts(w, structure);
                wrbuf_puts(w, " ");
            }
        }
    }
    return 0;
}
Beispiel #5
0
void http_set_proxyaddr(const char *host, struct conf_server *server)
{
    const char *p;
    short port;
    struct hostent *he;
    WRBUF w = wrbuf_alloc();

    yaz_log(YLOG_LOG, "HTTP backend  %s", host);

    p = strchr(host, ':');
    if (p)
    {
        port = atoi(p + 1);
        wrbuf_write(w, host, p - host);
        wrbuf_puts(w, "");
    }
    else
    {
        port = 80;
        wrbuf_puts(w, host);
    }
    if (!(he = gethostbyname(wrbuf_cstr(w))))
    {
        fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
        exit(1);
    }
    wrbuf_destroy(w);

    server->http_server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
    server->http_server->proxy_addr->sin_family = he->h_addrtype;
    memcpy(&server->http_server->proxy_addr->sin_addr.s_addr,
           he->h_addr_list[0], he->h_length);
    server->http_server->proxy_addr->sin_port = htons(port);
}
Beispiel #6
0
int relevance_snippet(struct relevance *r,
                      const char *words, const char *name,
                      WRBUF w_snippet)
{
    int no = 0;
    const char *norm_str;
    int highlight = 0;

    pp2_charset_token_first(r->prt, words, 0);
    while ((norm_str = pp2_charset_token_next(r->prt)))
    {
        size_t org_start, org_len;
        struct word_entry *entries = r->entries;
        int i;

        pp2_get_org(r->prt, &org_start, &org_len);
        for (; entries; entries = entries->next, i++)
        {
            if (*norm_str && !strcmp(norm_str, entries->norm_str))
            {
                break;
                if (!highlight)
                {
                    highlight = 1;
                    wrbuf_puts(w_snippet, "<match>");
                }
                break;
            }
        }
        if (entries)
        {
            if (!highlight)
            {
                highlight = 1;
                wrbuf_puts(w_snippet, "<match>");
                no++;
            }
        }
        else
        {
            if (highlight)
            {
                highlight = 0;
                wrbuf_puts(w_snippet, "</match>");
            }
        }
        wrbuf_xmlputs_n(w_snippet, words + org_start, org_len);
    }
    if (highlight)
        wrbuf_puts(w_snippet, "</match>");
    if (no)
    {
        yaz_log(YLOG_DEBUG, "SNIPPET match: %s", wrbuf_cstr(w_snippet));
    }
    return no;
}
Beispiel #7
0
static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
{
    if (zs->which == Z_RPNStructure_complex)
    {
        Z_Operator *op = zs->u.complex->roperator;
        wrbuf_printf(b, "@%s ", complex_op_name(op) );
        if (op->which== Z_Operator_prox)
        {
            if (!op->u.prox->exclusion)
                wrbuf_putc(b, 'n');
            else if (*op->u.prox->exclusion)
                wrbuf_putc(b, '1');
            else
                wrbuf_putc(b, '0');

            wrbuf_printf(b, " " ODR_INT_PRINTF " %d "
                         ODR_INT_PRINTF " ", *op->u.prox->distance,
                         *op->u.prox->ordered,
                         *op->u.prox->relationType);

            switch(op->u.prox->which)
            {
            case Z_ProximityOperator_known:
                wrbuf_putc(b, 'k');
                break;
            case Z_ProximityOperator_private:
                wrbuf_putc(b, 'p');
                break;
            default:
                wrbuf_printf(b, "%d", op->u.prox->which);
            }
            if (op->u.prox->u.known)
                wrbuf_printf(b, " " ODR_INT_PRINTF " ", *op->u.prox->u.known);
            else
                wrbuf_printf(b, " 0 ");
        }
        yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
        yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
    }
    else if (zs->which == Z_RPNStructure_simple)
    {
        if (zs->u.simple->which == Z_Operand_APT)
            yaz_apt_to_wrbuf(b, zs->u.simple->u.attributesPlusTerm);
        else if (zs->u.simple->which == Z_Operand_resultSetId)
        {
            wrbuf_printf(b, "@set ");
            yaz_encode_pqf_term(b, zs->u.simple->u.resultSetId,
                                strlen(zs->u.simple->u.resultSetId));
        }
        else
            wrbuf_puts(b, "(unknown simple structure)");
    }
    else
        wrbuf_puts(b, "(unknown structure)");
}
Beispiel #8
0
// Serialize a HTTP request
static struct http_buf *http_serialize_request(struct http_request *r)
{
    struct http_channel *c = r->channel;
    struct http_header *h;

    wrbuf_rewind(c->wrbuf);
    wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
                 *r->search ? "?" : "", r->search);

    wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);

    for (h = r->headers; h; h = h->next)
        wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);

    wrbuf_puts(c->wrbuf, "\r\n");

    if (r->content_buf)
        wrbuf_write(c->wrbuf, r->content_buf, r->content_len);

#if 0
    yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
            wrbuf_cstr(c->wrbuf));
#endif
    return http_buf_bywrbuf(c->http_server, c->wrbuf);
}
Beispiel #9
0
void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
                       const Odr_oid *attrbute_set)
{
    /* should print attr set here */
    wrbuf_puts(b, "RPN ");
    yaz_apt_to_wrbuf(b, zapt);
}
Beispiel #10
0
static int yaz_marc_line_gets(int (*getbyte)(void *client_data),
                              void (*ungetbyte)(int b, void *client_data),
                              void *client_data,
                              WRBUF w)
{
    int more;

    wrbuf_rewind(w);
    more = yaz_gets(getbyte, ungetbyte, client_data, w);
    if (!more)
        return 0;

    while (more)
    {
        int i;
        for (i = 0; i<4; i++)
        {
            int ch = getbyte(client_data);
            if (ch != ' ')
            {
                if (ch)
                    ungetbyte(ch, client_data);
                return 1;
            }
        }
        if (wrbuf_len(w) > 60 && wrbuf_buf(w)[wrbuf_len(w)-1] == '=')
            wrbuf_cut_right(w, 1);
        else
            wrbuf_puts(w, " ");
        more = yaz_gets(getbyte, ungetbyte, client_data, w);
    }
    return 1;
}
Beispiel #11
0
int yaz_marc_write_check(yaz_marc_t mt, WRBUF wr)
{
    struct yaz_marc_node *n;
    int identifier_length;
    const char *leader = 0;

    for (n = mt->nodes; n; n = n->next)
        if (n->which == YAZ_MARC_LEADER)
        {
            leader = n->u.leader;
            break;
        }

    if (!leader)
        return -1;
    if (!atoi_n_check(leader+11, 1, &identifier_length))
        return -1;

    for (n = mt->nodes; n; n = n->next)
    {
        switch(n->which)
        {
        case YAZ_MARC_COMMENT:
            wrbuf_iconv_write(wr, mt->iconv_cd,
                              n->u.comment, strlen(n->u.comment));
            wrbuf_puts(wr, "\n");
            break;
        default:
            break;
        }
    }
    return 0;
}
Beispiel #12
0
static void write_metadata(WRBUF w, struct conf_service *service,
                           struct record_metadata **ml, int full, int indent)
{
    int imeta;

    for (imeta = 0; imeta < service->num_metadata; imeta++)
    {
        struct conf_metadata *cmd = &service->metadata[imeta];
        struct record_metadata *md;
        if (!cmd->brief && !full)
            continue;
        for (md = ml[imeta]; md; md = md->next)
        {
            struct record_metadata_attr *attr = md->attributes;
            int i;
            for (i = 0; i < indent; i++)
                wrbuf_putc(w, ' ');
            wrbuf_printf(w, "<md-%s", cmd->name);

            for (; attr; attr = attr->next)
            {
                wrbuf_printf(w, " %s=\"", attr->name);
                wrbuf_xmlputs(w, attr->value);
                wrbuf_puts(w, "\"");
            }
            wrbuf_puts(w, ">");
            switch (cmd->type)
            {
            case Metadata_type_generic:
                wrbuf_xmlputs(w, md->data.text.disp);
                break;
            case Metadata_type_year:
                wrbuf_printf(w, "%d", md->data.number.min);
                if (md->data.number.min != md->data.number.max)
                    wrbuf_printf(w, "-%d", md->data.number.max);
                break;
            default:
                wrbuf_puts(w, "[can't represent]");
                break;
            }
            wrbuf_printf(w, "</md-%s>\n", cmd->name);
        }
    }
}
Beispiel #13
0
void add_marc_datafield_turbo_xml(yaz_marc_t mt, struct yaz_marc_node *n,
                                  xmlNode *record_ptr,
                                  xmlNsPtr ns_record, WRBUF wr_cdata,
                                  int identifier_length)
{
    xmlNode *ptr;
    struct yaz_marc_subfield *s;
    WRBUF subfield_name = wrbuf_alloc();

    /* TODO consider if safe */
    char field[10];
    field[0] = 'd';
    strncpy(field + 1, n->u.datafield.tag, 3);
    field[4] = '\0';
    ptr = xmlNewChild(record_ptr, ns_record, BAD_CAST field, 0);

    if (n->u.datafield.indicator)
    {
        int i;
        for (i = 0; n->u.datafield.indicator[i]; i++)
        {
            char ind_str[6];
            char ind_val[2];

            ind_val[0] = n->u.datafield.indicator[i];
            ind_val[1] = '\0';
            sprintf(ind_str, "%s%d", indicator_name[1], i+1);
            xmlNewProp(ptr, BAD_CAST ind_str, BAD_CAST ind_val);
        }
    }
    for (s = n->u.datafield.subfields; s; s = s->next)
    {
        int not_written;
        xmlNode *ptr_subfield;
        size_t using_code_len = get_subfield_len(mt, s->code_data,
                                                 identifier_length);
        wrbuf_rewind(wr_cdata);
        wrbuf_iconv_puts(wr_cdata, mt->iconv_cd, s->code_data + using_code_len);
        marc_iconv_reset(mt, wr_cdata);

        wrbuf_rewind(subfield_name);
        wrbuf_puts(subfield_name, "s");
        not_written = element_name_append_attribute_value(mt, subfield_name, 0, s->code_data, using_code_len) != 0;
        ptr_subfield = xmlNewTextChild(ptr, ns_record,
                                       BAD_CAST wrbuf_cstr(subfield_name),
                                       BAD_CAST wrbuf_cstr(wr_cdata));
        if (not_written)
        {
            /* Generate code attribute value and add */
            wrbuf_rewind(wr_cdata);
            wrbuf_iconv_write(wr_cdata, mt->iconv_cd,s->code_data, using_code_len);
            xmlNewProp(ptr_subfield, BAD_CAST "code",  BAD_CAST wrbuf_cstr(wr_cdata));
        }
    }
    wrbuf_destroy(subfield_name);
}
Beispiel #14
0
void yaz_facet_list_to_wrbuf(WRBUF w, const Z_FacetList *fl)
{
    int i;
    for (i = 0; i < fl->num; i++)
    {
        Z_FacetField *ff = fl->elements[i];

        if (i)
            wrbuf_puts(w, ", ");
        yaz_attributes_to_wrbuf(w, ff->attributes);
    }
}
Beispiel #15
0
static struct http_buf *http_serialize_response(struct http_channel *c,
        struct http_response *r)
{
    struct http_header *h;

    wrbuf_rewind(c->wrbuf);
    wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
    for (h = r->headers; h; h = h->next)
        wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
    if (r->payload)
    {
        wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
                (int) strlen(r->payload) : 0);
        wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
        if (!strcmp(r->content_type, "text/xml"))
        {
            xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
            if (doc)
            {
                xmlFreeDoc(doc);
            }
            else
            {
                yaz_log(YLOG_WARN, "Sending non-wellformed "
                        "response (bug #1162");
                yaz_log(YLOG_WARN, "payload: %s", r->payload);
            }
        }
    }
    wrbuf_puts(c->wrbuf, "\r\n");

    if (r->payload)
        wrbuf_puts(c->wrbuf, r->payload);

    return http_buf_bywrbuf(c->http_server, c->wrbuf);
}
Beispiel #16
0
static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
{
    if (prt->last_cp == 0)
        return 0;
    else
    {
        char *tmp = xstrdup(prt->last_cp);
        char *result = 0;
        result = normalize7bit_mergekey(tmp);
        
        wrbuf_rewind(prt->sort_str);
        wrbuf_puts(prt->sort_str, result);
        xfree(tmp);
        return wrbuf_cstr(prt->sort_str);
    }
}
Beispiel #17
0
yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
{
    xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
    if (!doc)
    {
        wrbuf_printf(w, "xmlParseMemory");
        return 0;
    }
    else
    {
        xmlNodePtr ptr = xmlDocGetRootElement(doc);
        yaz_record_conv_t p = yaz_record_conv_create();

        if (p)
        {
            const char *srcdir = getenv("srcdir");
            if (srcdir)
                yaz_record_conv_set_path(p, srcdir);
        }
        if (!ptr)
        {
            wrbuf_printf(w, "xmlDocGetRootElement");
            yaz_record_conv_destroy(p);
            p = 0;
        }
        else if (!p)
        {
            wrbuf_printf(w, "yaz_record_conv_create");
        }
        else
        {


            int r = yaz_record_conv_configure(p, ptr);
            
            if (r)
            {
                wrbuf_puts(w, yaz_record_conv_get_error(p));
                yaz_record_conv_destroy(p);
                p = 0;
            }
        }
        xmlFreeDoc(doc);
        return p;
    }    
}
Beispiel #18
0
static int convert_select(void *vinfo, WRBUF record, WRBUF wr_error)
{
    int ret = 0;
    struct select_info *info = vinfo;

    xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
                                   wrbuf_len(record));
    if (!doc)
    {
        wrbuf_printf(wr_error, "xmlParseMemory failed");
        ret = -1;
    }
    else
    {
        xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
        if (xpathCtx && info->xpath_expr)
        {
            xmlXPathObjectPtr xpathObj =
                xmlXPathEvalExpression((const xmlChar *) info->xpath_expr,
                                       xpathCtx);
            if (xpathObj)
            {
                xmlNodeSetPtr nodes = xpathObj->nodesetval;
                if (nodes)
                {
                    int i;
                    if (nodes->nodeNr > 0)
                        wrbuf_rewind(record);
                    for (i = 0; i < nodes->nodeNr; i++)
                    {
                        xmlNode *ptr = nodes->nodeTab[i];
                        if (ptr->type == XML_ELEMENT_NODE)
                            ptr = ptr->children;
                        for (; ptr; ptr = ptr->next)
                            if (ptr->type == XML_TEXT_NODE)
                                wrbuf_puts(record, (const char *) ptr->content);
                    }
                }
                xmlXPathFreeObject(xpathObj);
            }
            xmlXPathFreeContext(xpathCtx);
        }
        xmlFreeDoc(doc);
    }
    return ret;
}
Beispiel #19
0
Datei: rpn2cql.c Projekt: nla/yaz
static int rpn2cql_attr(cql_transform_t ct,
                        Z_AttributeList *attributes, WRBUF w)
{
    const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
    const char *index = cql_lookup_reverse(ct, "index.", attributes);
    const char *structure = cql_lookup_reverse(ct, "structure.", attributes);

    /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
    if (!index)
        index = lookup_index_from_string_attr(attributes);

    /* Attempt to fix bug #2978: Look for a relation attribute */
    if (!relation)
        relation = lookup_relation_index_from_attr(attributes);

    if (!index)
    {
        cql_transform_set_error(ct,
                                YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
        return -1;
    }
    /* for serverChoice we omit index+relation+structure */
    if (strcmp(index, "cql.serverChoice"))
    {
        wrbuf_puts(w, index);
        if (relation)
        {
            if (!strcmp(relation, "exact"))
                relation = "==";
            else if (!strcmp(relation, "eq"))
                relation = "=";
            else if (!strcmp(relation, "le"))
                relation = "<=";
            else if (!strcmp(relation, "ge"))
                relation = ">=";
            /* Missing mapping of not equal, phonetic, stem and relevance */
            wrbuf_puts(w, relation);
        }
        else
            wrbuf_puts(w, "=");

        if (structure)
        {
            if (strcmp(structure, "*"))
            {
                wrbuf_puts(w, "/");
                wrbuf_puts(w, structure);
                wrbuf_puts(w, " ");
            }
        }
    }
    return 0;
}
Beispiel #20
0
int yaz_record_conv_opac_record(yaz_record_conv_t p,
                                Z_OPACRecord *input_record,
                                WRBUF output_record)
{
    int ret = 0;
    struct yaz_record_conv_rule *r = p->rules;
    if (!r || r->type->construct != construct_marc)
    {
        wrbuf_puts(p->wr_error, "Expecting MARC rule as first rule for OPAC");
        ret = -1; /* no marc rule so we can't do OPAC */
    }
    else
    {
        struct marc_info *mi = r->info;
        const char *input_charset = mi->input_charset;
        yaz_iconv_t cd;

        WRBUF res = wrbuf_alloc();
        yaz_marc_t mt = yaz_marc_create();

        if (yaz_opac_check_marc21_coding(input_charset, input_record))
            input_charset = "utf-8";
        cd = yaz_iconv_open(mi->output_charset, input_charset);

        wrbuf_rewind(p->wr_error);
        yaz_marc_xml(mt, mi->output_format_mode);

        yaz_marc_iconv(mt, cd);

        yaz_opac_decode_wrbuf(mt, input_record, res);
        if (ret != -1)
        {
            ret = yaz_record_conv_record_rule(p,
                                              r->next,
                                              wrbuf_buf(res), wrbuf_len(res),
                                              output_record);
        }
        yaz_marc_destroy(mt);
        if (cd)
            yaz_iconv_close(cd);
        wrbuf_destroy(res);
    }
    return ret;
}
Beispiel #21
0
static void tstwrbuf(void)
{
    int step;
    WRBUF wr = wrbuf_alloc();

    YAZ_CHECK(wr);

    wrbuf_destroy(wr);

    wr = wrbuf_alloc();

    YAZ_CHECK(wr);

    for (step = 1; step < 65; step++)
    {
        int i, j, k;
        int len;
        char buf[64];
        char *cp;
        for (j = 1; j<step; j++)
        {
            for (i = 0; i<j; i++)
                buf[i] = i+1;
            buf[i] = '\0';
            wrbuf_puts(wr, buf);
        }
        
        cp = wrbuf_buf(wr);
        len = wrbuf_len(wr);
        YAZ_CHECK(len == step * (step-1) / 2);
        k = 0;
        for (j = 1; j<step; j++)
            for (i = 0; i<j; i++)
            {
                YAZ_CHECK(cp[k] == i+1);
                k++;
            }
        wrbuf_rewind(wr);
    }
    wrbuf_destroy(wr);
}
Beispiel #22
0
static void yaz_attribute_element_to_wrbuf(WRBUF b,
                                           const Z_AttributeElement *element)
{
    int i;

    wrbuf_puts(b, "@attr ");
    if (element->attributeSet)
    {
        char oid_name_str[OID_STR_MAX];
        const char *setname = yaz_oid_to_string_buf(element->attributeSet,
                                                    0, oid_name_str);
        if (setname)
        {
            wrbuf_puts(b, setname);
            wrbuf_puts(b, " ");
        }
    }
    wrbuf_printf(b, ODR_INT_PRINTF "=", *element->attributeType);
    switch (element->which)
    {
    case Z_AttributeValue_numeric:
        wrbuf_printf(b, ODR_INT_PRINTF, *element->value.numeric);
        break;
    case Z_AttributeValue_complex:
        for (i = 0; i < element->value.complex->num_list; i++)
        {
            if (i)
                wrbuf_puts(b, ",");
            if (element->value.complex->list[i]->which ==
                Z_StringOrNumeric_string)
                wrbuf_puts(b, element->value.complex->list[i]->u.string);
            else if (element->value.complex->list[i]->which ==
                     Z_StringOrNumeric_numeric)
                wrbuf_printf(b, ODR_INT_PRINTF,
                             *element->value.complex->list[i]->u.numeric);
        }
        break;
    default:
        wrbuf_puts(b, "@attr 1=unknown");
    }
    wrbuf_puts(b, " ");
}
Beispiel #23
0
static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
                                     const char *ns,
                                     const char *format,
                                     const char *type,
                                     int turbo)
{
    if (mt->write_using_libxml2)
    {
#if YAZ_HAVE_XML2
        int ret;
        xmlNode *root_ptr;

        if (!turbo)
            ret = yaz_marc_write_xml(mt, &root_ptr, ns, format, type);
        else
            ret = yaz_marc_write_xml_turbo_xml(mt, &root_ptr, ns, format, type);
        if (ret == 0)
        {
            xmlChar *buf_out;
            xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
            int len_out;

            xmlDocSetRootElement(doc, root_ptr);
            xmlDocDumpMemory(doc, &buf_out, &len_out);

            wrbuf_write(wr, (const char *) buf_out, len_out);
            wrbuf_puts(wr, "");
            xmlFree(buf_out);
            xmlFreeDoc(doc);
        }
        return ret;
#else
        return -1;
#endif
    }
    else
        return yaz_marc_write_marcxml_wrbuf(mt, wr, ns, format, type, turbo);
}
Beispiel #24
0
static void write_subrecord(struct record *r, WRBUF w,
                            struct conf_service *service, int show_details)
{
    const char *name = session_setting_oneval(
                           client_get_database(r->client), PZ_NAME);

    wrbuf_puts(w, " <location id=\"");
    wrbuf_xmlputs(w, client_get_id(r->client));
    wrbuf_puts(w, "\"\n");

    wrbuf_puts(w, "    name=\"");
    wrbuf_xmlputs(w,  *name ? name : "Unknown");
    wrbuf_puts(w, "\" ");

    wrbuf_puts(w, "checksum=\"");
    wrbuf_printf(w,  "%u", r->checksum);
    wrbuf_puts(w, "\">\n");

    write_metadata(w, service, r->metadata, show_details, 2);
    wrbuf_puts(w, " </location>\n");
}
Beispiel #25
0
static void error(struct http_response *rs,
                  enum pazpar2_error_code code,
                  const char *addinfo)
{
    struct http_channel *c = rs->channel;
    WRBUF text = wrbuf_alloc();
    const char *http_status = "417";
    const char *msg = get_msg(code);

    rs->msg = nmem_strdup(c->nmem, msg);
    strcpy(rs->code, http_status);

    wrbuf_printf(text, HTTP_COMMAND_RESPONSE_PREFIX "<error code=\"%d\" msg=\"%s\">", (int) code,
                 msg);
    if (addinfo)
        wrbuf_xmlputs(text, addinfo);
    wrbuf_puts(text, "</error>");

    yaz_log(YLOG_WARN, "HTTP %s %s%s%s", http_status,
            msg, addinfo ? ": " : "" , addinfo ? addinfo : "");
    rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(text));
    wrbuf_destroy(text);
    http_send_response(c);
}
Beispiel #26
0
void wrbuf_diags(WRBUF b, int num_diagnostics, Z_DiagRec **diags)
{
    /* we only dump the first diag - that keeps the log cleaner. */
    wrbuf_puts(b," ERROR ");
    if (diags[0]->which != Z_DiagRec_defaultFormat)
        wrbuf_puts(b,"(diag not in default format?)");
    else
    {
        Z_DefaultDiagFormat *e = diags[0]->u.defaultFormat;
        if (e->condition)
            wrbuf_printf(b, ODR_INT_PRINTF " ",*e->condition);
        else
            wrbuf_puts(b, "?? ");
        if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
            wrbuf_puts(b, e->u.v2Addinfo);
        else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
            wrbuf_puts(b, e->u.v3Addinfo);
        wrbuf_puts(b, " ");
    }
}
Beispiel #27
0
static void tst2(void)
{
    WRBUF w = wrbuf_alloc();
    cql_transform_t ct = 0;
    const char *srcdir = getenv("srcdir");
    if (srcdir)
    {
        wrbuf_puts(w, srcdir);
        wrbuf_puts(w, "/");
    }
    wrbuf_puts(w, "../etc/pqf.properties");

    ct = cql_transform_open_fname(wrbuf_cstr(w));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title=/exact abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
    /* Date tests */
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=1 1980", "dc.date<1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date<=1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date=1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date>=1980"));
    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date>1980"));
    YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=4 234 @attr 1=30 @attr 2=2 1990", "dc.date>=234 and dc.date<=1990"));

    /* Truncation */
    YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
    YAZ_CHECK(compare(ct, "@attr 5=2 water", "*water"));
    YAZ_CHECK(compare(ct, "@attr 5=3 water", "*water*"));
    YAZ_CHECK(compare(ct, "@attr 5=100 water", "water"));
    YAZ_CHECK(compare(ct, "@attr 5=102 water", "water"));
    YAZ_CHECK(compare(ct, "@attr 5=104 water", "water"));

    YAZ_CHECK(compare(ct, "@attr 5=102 wat.*er", "wat*er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat?er", "wat*er"));

    YAZ_CHECK(compare(ct, "@attr 5=102 wat.er", "wat?er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat#er", "wat?er"));
    YAZ_CHECK(compare(ct, "@attr 5=102 wat?er", "wat\\?er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat*er", "wat\\*er"));
    YAZ_CHECK(compare(ct, "@attr 5=102 wat#er", "wat#er"));

    /* \. is 'eaten' by PQF parser */
    YAZ_CHECK(compare(ct, "@attr 5=102 wat\\.er", "wat?er"));

    /* Escape sequences */
    /* note: escape sequences that survive after PQF parse below */
    YAZ_CHECK(compare(ct, "@attr 5=102 wat\\\\?er", "wat\\?er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat\\\\?er", "wat\\?er"));

    YAZ_CHECK(compare(ct, "@attr 5=102 wat\\\\*er", "wat\\*er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat\\\\*er", "wat\\*er"));

    YAZ_CHECK(compare(ct, "wat\\\\#er", "wat#er"));
    YAZ_CHECK(compare(ct, "@attr 5=100 wat\\\\#er", "wat#er"));
    YAZ_CHECK(compare(ct, "@attr 5=102 wat\\\\#er", "wat#er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat\\\\#er", "wat#er"));
    YAZ_CHECK(compare(ct, "@attr 5=102 wat\\\\.er", "wat.er"));
    YAZ_CHECK(compare(ct, "@attr 5=104 wat\\\\.er", "wat.er"));

    /* Quoting */
    YAZ_CHECK(compare(ct, "@attr 5=100 \"\"", "\"\""));
    YAZ_CHECK(compare(ct, "@attr 5=1 \"\"", "\"*\""));
    YAZ_CHECK(compare(ct, "@attr 5=2 \"\"", "\"*\""));
    YAZ_CHECK(compare(ct, "@attr 5=3 \"\"", "\"**\""));
    YAZ_CHECK(compare(ct, "@attr 5=102 \"\"", "\"\""));
    YAZ_CHECK(compare(ct, "@attr 5=104 \"\"", "\"\""));

    YAZ_CHECK(compare(ct, "@attr 5=1 \"water basket\"", "\"water basket*\""));
    YAZ_CHECK(compare(ct, "@attr 5=2 \"water basket\"", "\"*water basket\""));
    YAZ_CHECK(compare(ct, "@attr 5=3 \"water basket\"", "\"*water basket*\""));

    /* Other */
    YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "cql.allRecords=1"));
    YAZ_CHECK(compare2(ct, "@attr 1=500 abc", 0, 114));
    YAZ_CHECK(compare2(ct, "@attr 5=99 x", "99", 120));
    cql_transform_close(ct);
    wrbuf_destroy(w);
}
Beispiel #28
0
/** \brief common MARC XML/Xchange/turbomarc writer
    \param mt handle
    \param wr WRBUF output
    \param ns XMLNS for the elements
    \param format record format (e.g. "MARC21")
    \param type record type (e.g. "Bibliographic")
    \param turbo =1 for turbomarc
    \retval 0 OK
    \retval -1 failure
*/
static int yaz_marc_write_marcxml_wrbuf(yaz_marc_t mt, WRBUF wr,
                                        const char *ns,
                                        const char *format,
                                        const char *type,
                                        int turbo)
{
    struct yaz_marc_node *n;
    int identifier_length;
    const char *leader = 0;

    for (n = mt->nodes; n; n = n->next)
        if (n->which == YAZ_MARC_LEADER)
        {
            leader = n->u.leader;
            break;
        }

    if (!leader)
        return -1;
    if (!atoi_n_check(leader+11, 1, &identifier_length))
        return -1;

    if (mt->enable_collection != no_collection)
    {
        if (mt->enable_collection == collection_first)
        {
            wrbuf_printf(wr, "<collection xmlns=\"%s\">\n", ns);
            mt->enable_collection = collection_second;
        }
        wrbuf_printf(wr, "<%s", record_name[turbo]);
    }
    else
    {
        wrbuf_printf(wr, "<%s xmlns=\"%s\"", record_name[turbo], ns);
    }
    if (format)
        wrbuf_printf(wr, " format=\"%.80s\"", format);
    if (type)
        wrbuf_printf(wr, " type=\"%.80s\"", type);
    wrbuf_printf(wr, ">\n");
    for (n = mt->nodes; n; n = n->next)
    {
        struct yaz_marc_subfield *s;

        switch(n->which)
        {
        case YAZ_MARC_DATAFIELD:

            wrbuf_printf(wr, "  <%s", datafield_name[turbo]);
            if (!turbo)
            	wrbuf_printf(wr, " tag=\"");
            wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.datafield.tag,
                                    strlen(n->u.datafield.tag));
            if (!turbo)
                wrbuf_printf(wr, "\"");
    	    if (n->u.datafield.indicator)
    	    {
    	    	int i;
    	    	for (i = 0; n->u.datafield.indicator[i]; i++)
    	    	{
                    wrbuf_printf(wr, " %s%d=\"", indicator_name[turbo], i+1);
                    wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                            n->u.datafield.indicator+i, 1);
                    wrbuf_iconv_puts(wr, mt->iconv_cd, "\"");
                }
            }
            wrbuf_printf(wr, ">\n");
            for (s = n->u.datafield.subfields; s; s = s->next)
            {
                size_t using_code_len = get_subfield_len(mt, s->code_data,
                                                         identifier_length);
                wrbuf_printf(wr, "    <%s", subfield_name[turbo]);
                if (!turbo)
                {
                    wrbuf_printf(wr, " code=\"");
                    wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                            s->code_data, using_code_len);
                    wrbuf_iconv_puts(wr, mt->iconv_cd, "\">");
                }
                else
                {
                    element_name_append_attribute_value(mt, wr, "code", s->code_data, using_code_len);
                    wrbuf_puts(wr, ">");
                }
                wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                        s->code_data + using_code_len,
                                        strlen(s->code_data + using_code_len));
                marc_iconv_reset(mt, wr);
                wrbuf_printf(wr, "</%s", subfield_name[turbo]);
                if (turbo)
                    element_name_append_attribute_value(mt, wr, 0, s->code_data, using_code_len);
                wrbuf_puts(wr, ">\n");
            }
            wrbuf_printf(wr, "  </%s", datafield_name[turbo]);
            /* TODO Not CDATA */
            if (turbo)
            	wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.datafield.tag,
                                        strlen(n->u.datafield.tag));
            wrbuf_printf(wr, ">\n");
            break;
        case YAZ_MARC_CONTROLFIELD:
            wrbuf_printf(wr, "  <%s", controlfield_name[turbo]);
            if (!turbo)
            {
            	wrbuf_printf(wr, " tag=\"");
                wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.controlfield.tag,
        				strlen(n->u.controlfield.tag));
                wrbuf_iconv_puts(wr, mt->iconv_cd, "\">");
            }
            else
            {
                /* TODO convert special */
                wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.controlfield.tag,
        				strlen(n->u.controlfield.tag));
                wrbuf_iconv_puts(wr, mt->iconv_cd, ">");
            }
            wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                    n->u.controlfield.data,
                                    strlen(n->u.controlfield.data));
            marc_iconv_reset(mt, wr);
            wrbuf_printf(wr, "</%s", controlfield_name[turbo]);
            /* TODO convert special */
            if (turbo)
                wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.controlfield.tag,
    					strlen(n->u.controlfield.tag));
            wrbuf_puts(wr, ">\n");
            break;
        case YAZ_MARC_COMMENT:
            wrbuf_printf(wr, "<!-- ");
            wrbuf_puts(wr, n->u.comment);
            wrbuf_printf(wr, " -->\n");
            break;
        case YAZ_MARC_LEADER:
            wrbuf_printf(wr, "  <%s>", leader_name[turbo]);
            wrbuf_iconv_write_cdata(wr,
                                    0 , /* no charset conversion for leader */
                                    n->u.leader, strlen(n->u.leader));
            wrbuf_printf(wr, "</%s>\n", leader_name[turbo]);
        }
    }
    wrbuf_printf(wr, "</%s>\n", record_name[turbo]);
    return 0;
}
Beispiel #29
0
int yaz_marc_write_line(yaz_marc_t mt, WRBUF wr)
{
    struct yaz_marc_node *n;
    int identifier_length;
    const char *leader = 0;

    for (n = mt->nodes; n; n = n->next)
        if (n->which == YAZ_MARC_LEADER)
        {
            leader = n->u.leader;
            break;
        }

    if (!leader)
        return -1;
    if (!atoi_n_check(leader+11, 1, &identifier_length))
        return -1;

    for (n = mt->nodes; n; n = n->next)
    {
        struct yaz_marc_subfield *s;
        switch(n->which)
        {
        case YAZ_MARC_DATAFIELD:
            wrbuf_printf(wr, "%s %s", n->u.datafield.tag,
                         n->u.datafield.indicator);
            for (s = n->u.datafield.subfields; s; s = s->next)
            {
                size_t using_code_len = get_subfield_len(mt, s->code_data,
                                                         identifier_length);

                wrbuf_puts (wr, mt->subfield_str);
                wrbuf_iconv_write(wr, mt->iconv_cd, s->code_data,
                                  using_code_len);
                wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
                wrbuf_iconv_puts(wr, mt->iconv_cd,
                                 s->code_data + using_code_len);
                marc_iconv_reset(mt, wr);
            }
            wrbuf_puts (wr, mt->endline_str);
            break;
        case YAZ_MARC_CONTROLFIELD:
            wrbuf_printf(wr, "%s", n->u.controlfield.tag);
            wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
            wrbuf_iconv_puts(wr, mt->iconv_cd, n->u.controlfield.data);
            marc_iconv_reset(mt, wr);
            wrbuf_puts (wr, mt->endline_str);
            break;
        case YAZ_MARC_COMMENT:
            wrbuf_puts(wr, "(");
            wrbuf_iconv_write(wr, mt->iconv_cd,
                              n->u.comment, strlen(n->u.comment));
            marc_iconv_reset(mt, wr);
            wrbuf_puts(wr, ")\n");
            break;
        case YAZ_MARC_LEADER:
            wrbuf_printf(wr, "%s\n", n->u.leader);
        }
    }
    wrbuf_puts(wr, "\n");
    return 0;
}
Beispiel #30
0
int yaz_marc_write_json(yaz_marc_t mt, WRBUF w)
{
    int identifier_length;
    struct yaz_marc_node *n;
    const char *leader = 0;
    int first = 1;

    wrbuf_puts(w, "{\n");
    for (n = mt->nodes; n; n = n->next)
        if (n->which == YAZ_MARC_LEADER)
            leader = n->u.leader;

    if (!leader)
        return -1;

    if (!atoi_n_check(leader+11, 1, &identifier_length))
        return -1;

    wrbuf_puts(w, "\t\"leader\":\"");
    wrbuf_json_puts(w, leader);
    wrbuf_puts(w, "\",\n");
    wrbuf_puts(w, "\t\"fields\":\n\t[\n");

    for (n = mt->nodes; n; n = n->next)
    {
        struct yaz_marc_subfield *s;
        const char *sep = "";
        switch (n->which)
        {
        case YAZ_MARC_LEADER:
        case YAZ_MARC_COMMENT:
            break;
        case YAZ_MARC_CONTROLFIELD:
            if (first)
                first = 0;
            else
                wrbuf_puts(w, ",\n");
            wrbuf_puts(w, "\t\t{\n\t\t\t\"");
            wrbuf_iconv_json_puts(w, mt->iconv_cd, n->u.controlfield.tag);
            wrbuf_puts(w, "\":\"");
            wrbuf_iconv_json_puts(w, mt->iconv_cd, n->u.controlfield.data);
            wrbuf_puts(w, "\"\n\t\t}");
            break;
        case YAZ_MARC_DATAFIELD:
            if (first)
                first = 0;
            else
                wrbuf_puts(w, ",\n");

            wrbuf_puts(w, "\t\t{\n\t\t\t\"");
            wrbuf_json_puts(w, n->u.datafield.tag);
            wrbuf_puts(w, "\":\n\t\t\t{\n\t\t\t\t\"subfields\":\n\t\t\t\t[\n");
            for (s = n->u.datafield.subfields; s; s = s->next)
            {
                size_t using_code_len = get_subfield_len(mt, s->code_data,
                                                         identifier_length);
                wrbuf_puts(w, sep);
                sep = ",\n";
                wrbuf_puts(w, "\t\t\t\t\t{\n\t\t\t\t\t\t\"");
                wrbuf_iconv_json_write(w, mt->iconv_cd,
                                       s->code_data, using_code_len);
                wrbuf_puts(w, "\":\"");
                wrbuf_iconv_json_puts(w, mt->iconv_cd,
                                      s->code_data + using_code_len);
                wrbuf_puts(w, "\"\n\t\t\t\t\t}");
            }
            wrbuf_puts(w, "\n\t\t\t\t]");
            if (n->u.datafield.indicator[0])
            {
                int i;
                for (i = 0; n->u.datafield.indicator[i]; i++)
                {
                    wrbuf_printf(w, ",\n\t\t\t\t\"ind%d\":\"%c\"", i + 1,
                                 n->u.datafield.indicator[i]);
                }
            }
            wrbuf_puts(w, "\n\t\t\t}\n");
            wrbuf_puts(w, "\n\t\t}");
            break;
        }
    }
    wrbuf_puts(w, "\n\t]\n");
    wrbuf_puts(w, "}\n");
    return 0;
}