예제 #1
0
파일: http.c 프로젝트: saiful76/pazpar2
static struct http_channel *http_channel_create(http_server_t hs,
                                                const char *addr,
                                                struct conf_server *server)
{
    struct http_channel *r;

    r = xmalloc(sizeof(struct http_channel));
    r->nmem = nmem_create();
    r->wrbuf = wrbuf_alloc();

    http_server_incref(hs);
    r->http_server = hs;
    r->http_sessions = hs->http_sessions;
    assert(r->http_sessions);
    r->server = server;
    r->proxy = 0;
    r->iochan = 0;
    r->iqueue = r->oqueue = 0;
    r->state = Http_Idle;
    r->keep_alive = 0;
    r->request = 0;
    r->response = 0;
    if (!addr)
    {
        yaz_log(YLOG_WARN, "Invalid HTTP forward address");
        exit(1);
    }
    strcpy(r->addr, addr);
    r->observers = 0;
    return r;
}
예제 #2
0
struct http_session *http_session_create(struct conf_service *service,
        http_sessions_t http_sessions,
        unsigned int sesid)
{
    NMEM nmem = nmem_create();
    struct http_session *r = nmem_malloc(nmem, sizeof(*r));
    char tmp_str[50];

    sprintf(tmp_str, "session#%u", sesid);
    r->psession = new_session(nmem, service, sesid);
    r->session_id = sesid;
    r->timestamp = 0;
    r->nmem = nmem;
    r->destroy_counter = r->activity_counter = 0;
    r->http_sessions = http_sessions;

    yaz_mutex_enter(http_sessions->mutex);
    r->next = http_sessions->session_list;
    http_sessions->session_list = r;
    yaz_mutex_leave(http_sessions->mutex);

    r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout");
    iochan_setdata(r->timeout_iochan, r);
    yaz_log(http_sessions->log_level, "Session %u created. timeout chan=%p timeout=%d", sesid, r->timeout_iochan, service->session_timeout);
    iochan_settimeout(r->timeout_iochan, service->session_timeout);

    iochan_add(service->server->iochan_man, r->timeout_iochan);
    http_session_use(1);
    return r;
}
예제 #3
0
static void *construct_select(const xmlNode *ptr,
                              const char *path, WRBUF wr_error)
{
    if (strcmp((const char *) ptr->name, "select"))
        return 0;
    else
    {
        NMEM nmem = nmem_create();
        struct select_info *info = nmem_malloc(nmem, sizeof(*info));
        const char *attr_str;
        const char *xpath = 0;

        info->nmem = nmem;
        info->xpath_expr = 0;
        attr_str = yaz_xml_get_prop(ptr, "path%s", &xpath);
        if (attr_str)
        {
            wrbuf_printf(wr_error, "Bad attribute '%s'"
                         "Expected xpath.", attr_str);
            nmem_destroy(nmem);
                return 0;
        }
        if (xpath)
            info->xpath_expr = nmem_strdup(nmem, xpath);
        return info;
    }
}
예제 #4
0
파일: odr_mem.c 프로젝트: nla/yaz
/*
 * Extract the memory control block from o.
 */
NMEM odr_extract_mem(ODR o)
{
    NMEM r = o->mem;

    o->mem = nmem_create();
    return r;
}
예제 #5
0
struct relevance *relevance_create_ccl(pp2_charset_fact_t pft,
                                       struct ccl_rpn_node *query,
                                       int rank_cluster,
                                       double follow_factor, double lead_decay,
                                       int length_divide)
{
    NMEM nmem = nmem_create();
    struct relevance *res = nmem_malloc(nmem, sizeof(*res));
    int i;

    res->nmem = nmem;
    res->entries = 0;
    res->vec_len = 1;
    res->rank_cluster = rank_cluster;
    res->follow_factor = follow_factor;
    res->lead_decay = lead_decay;
    res->length_divide = length_divide;
    res->prt = pp2_charset_token_create(pft, "relevance");

    pull_terms(res, query);

    res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int));
    for (i = 0; i < res->vec_len; i++)
        res->doc_frequency_vec[i] = 0;

    // worker array
    res->term_frequency_vec_tmp =
        nmem_malloc(res->nmem,
                    res->vec_len * sizeof(*res->term_frequency_vec_tmp));

    res->term_pos =
        nmem_malloc(res->nmem, res->vec_len * sizeof(*res->term_pos));

    return res;
}
예제 #6
0
yaz_record_conv_t yaz_record_conv_create()
{
    yaz_record_conv_t p = (yaz_record_conv_t) xmalloc(sizeof(*p));
    p->nmem = nmem_create();
    p->wr_error = wrbuf_alloc();
    p->rules = 0;
    p->path = 0;
    return p;
}
예제 #7
0
normalize_cache_t normalize_cache_create(void)
{
    NMEM nmem = nmem_create();
    normalize_cache_t nc = nmem_malloc(nmem, sizeof(*nc));
    nc->nmem = nmem;
    nc->items = 0;
    nc->mutex = 0;
    pazpar2_mutex_create(&nc->mutex, "normalize_cache");
    return nc;
}
예제 #8
0
파일: cqltransform.c 프로젝트: nla/yaz
cql_transform_t cql_transform_create(void)
{
    cql_transform_t ct = (cql_transform_t) xmalloc(sizeof(*ct));
    ct->tok_cfg = yaz_tok_cfg_create();
    ct->w = wrbuf_alloc();
    ct->error = 0;
    ct->addinfo = 0;
    ct->entry = 0;
    ct->nmem = nmem_create();
    return ct;
}
예제 #9
0
sel_thread_t sel_thread_create(void (*work_handler)(void *work_data),
                               void (*work_destroy)(void *work_data),
                               int *read_fd, int no_of_threads)
{
    int i;
    NMEM nmem = nmem_create();
    sel_thread_t p = nmem_malloc(nmem, sizeof(*p));

    assert(work_handler);
    /* work_destroy may be NULL */
    assert(read_fd);
    assert(no_of_threads >= 1);

    p->nmem = nmem;

#ifdef WIN32
    /* use port 12119 temporarily on Windos and hope for the best */
    p->spipe = yaz_spipe_create(12119, 0);
#else
    p->spipe = yaz_spipe_create(0, 0);
#endif
    if (!p->spipe)
    {
        nmem_destroy(nmem);
        return 0;
    }    

    *read_fd = p->read_fd = yaz_spipe_get_read_fd(p->spipe);
    p->write_fd = yaz_spipe_get_write_fd(p->spipe);

    p->input_queue = 0;
    p->output_queue = 0;
    p->free_queue = 0;
    p->work_handler = work_handler;
    p->work_destroy = work_destroy;
    p->no_threads = 0; /* we if need to destroy */
    p->stop_flag = 0;
    p->mutex = 0;
    yaz_mutex_create(&p->mutex);
    yaz_cond_create(&p->input_data);
    if (p->input_data == 0) /* condition variable could not be created? */
    {
        sel_thread_destroy(p);
        return 0;
    }

    p->no_threads = no_of_threads;
    p->thread_id = nmem_malloc(nmem, sizeof(*p->thread_id) * p->no_threads);
    for (i = 0; i < p->no_threads; i++)
        p->thread_id[i] = yaz_thread_create(sel_thread_handler, p);
    return p;
}
예제 #10
0
yaz_marc_t yaz_marc_create(void)
{
    yaz_marc_t mt = (yaz_marc_t) xmalloc(sizeof(*mt));
    mt->output_format = YAZ_MARC_LINE;
    mt->debug = 0;
    mt->write_using_libxml2 = 0;
    mt->enable_collection = no_collection;
    mt->m_wr = wrbuf_alloc();
    mt->iconv_cd = 0;
    mt->leader_spec = 0;
    strcpy(mt->subfield_str, " $");
    strcpy(mt->endline_str, "\n");

    mt->nmem = nmem_create();
    yaz_marc_reset(mt);
    return mt;
}
예제 #11
0
static void xml_config_open(void)
{
    if (!getcwd(gfs_root_dir, FILENAME_MAX))
    {
        yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
        gfs_root_dir[0] = '\0';
    }
#ifdef WIN32
    init_control_tls = 1;
    current_control_tls = TlsAlloc();
#elif YAZ_POSIX_THREADS
    init_control_tls = 1;
    pthread_key_create(&current_control_tls, 0);
#endif
    
    gfs_nmem = nmem_create();
#if YAZ_HAVE_XML2
    if (control_block.xml_config[0] == '\0')
        return;

    if (!xml_config_doc)
    {
        xml_config_doc = xmlParseFile(control_block.xml_config);
        if (!xml_config_doc)
        {
            yaz_log(YLOG_FATAL, "Could not parse %s", control_block.xml_config);
            exit(1);
        }
        else
        {
            int noSubstitutions = xmlXIncludeProcess(xml_config_doc);
            if (noSubstitutions == -1)
            {
                yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
                        control_block.xml_config);
                exit(1);
            }
        }
    }
    xml_config_read();
#endif
}
예제 #12
0
파일: cqltransform.c 프로젝트: nla/yaz
int cql_transform(cql_transform_t ct, struct cql_node *cn,
                  void (*pr)(const char *buf, void *client_data),
                  void *client_data)
{
    struct cql_prop_entry *e;
    NMEM nmem = nmem_create();

    ct->error = 0;
    xfree(ct->addinfo);
    ct->addinfo = 0;

    for (e = ct->entry; e ; e = e->next)
    {
        if (!cql_strncmp(e->pattern, "set.", 4))
            cql_apply_prefix(nmem, cn, e->pattern+4, e->value);
        else if (!cql_strcmp(e->pattern, "set"))
            cql_apply_prefix(nmem, cn, 0, e->value);
    }
    cql_transform_r(ct, cn, pr, client_data);
    nmem_destroy(nmem);
    return ct->error;
}
예제 #13
0
static void tst_convert4(void)
{
    NMEM nmem = nmem_create();
    int ret;

    const char *opacxml_rec =
        "<opacRecord>\n"
        "  <bibliographicRecord>\n"
        "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
        "  <leader>00077nam a22000498a 4500</leader>\n"
        "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
        "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
        "    <subfield code=\"a\">k" "\xc3" "\xb8" /* oslash in UTF_8 */
        "benhavn</subfield>\n"
        "  </datafield>\n"
        "</record>\n"
        "  </bibliographicRecord>\n"
        "  <holdings>\n"
        "   <holding>\n"
        "    <shelvingLocation>Sprague Library hidden basement</shelvingLocation>\n"
        "    <callNumber>E98.L7L44 1976 </callNumber>\n"
        "    <volumes/>\n"
        "   </holding>\n"
        "  </holdings>\n"
        " </opacRecord>\n"
        ;

    Z_OPACRecord *opac = 0;
    yaz_marc_t mt =  yaz_marc_create();
    ret = yaz_xml_to_opac(mt, opacxml_rec, strlen(opacxml_rec),
                          &opac, 0 /* iconv */, nmem, 0);
    YAZ_CHECK(ret);
    YAZ_CHECK(opac);
    yaz_marc_destroy(mt);
    nmem_destroy(nmem);
}
예제 #14
0
static void *construct_marc(const xmlNode *ptr,
                            const char *path, WRBUF wr_error)
{
    NMEM nmem = nmem_create();
    struct marc_info *info = nmem_malloc(nmem, sizeof(*info));
    struct _xmlAttr *attr;
    const char *input_format = 0;
    const char *output_format = 0;

    if (strcmp((const char *) ptr->name, "marc"))
    {
        nmem_destroy(nmem);
        return 0;
    }
    info->nmem = nmem;
    info->input_charset = 0;
    info->output_charset = 0;
    info->input_format_mode = 0;
    info->output_format_mode = 0;
    info->leader_spec = 0;

    for (attr = ptr->properties; attr; attr = attr->next)
    {
        if (!xmlStrcmp(attr->name, BAD_CAST "inputcharset") &&
            attr->children && attr->children->type == XML_TEXT_NODE)
            info->input_charset = (const char *) attr->children->content;
        else if (!xmlStrcmp(attr->name, BAD_CAST "outputcharset") &&
            attr->children && attr->children->type == XML_TEXT_NODE)
            info->output_charset = (const char *) attr->children->content;
        else if (!xmlStrcmp(attr->name, BAD_CAST "inputformat") &&
            attr->children && attr->children->type == XML_TEXT_NODE)
            input_format = (const char *) attr->children->content;
        else if (!xmlStrcmp(attr->name, BAD_CAST "outputformat") &&
            attr->children && attr->children->type == XML_TEXT_NODE)
            output_format = (const char *) attr->children->content;
        else if (!xmlStrcmp(attr->name, BAD_CAST "leaderspec") &&
                 attr->children && attr->children->type == XML_TEXT_NODE)
            info->leader_spec =
                nmem_strdup(info->nmem,(const char *) attr->children->content);
        else
        {
            wrbuf_printf(wr_error, "Element <marc>: expected attributes"
                         "'inputformat', 'inputcharset', 'outputformat' or"
                         " 'outputcharset', got attribute '%s'",
                         attr->name);
            nmem_destroy(info->nmem);
            return 0;
        }
    }
    if (!input_format)
    {
        wrbuf_printf(wr_error, "Element <marc>: "
                     "attribute 'inputformat' required");
        nmem_destroy(info->nmem);
        return 0;
    }
    else if (!strcmp(input_format, "marc"))
    {
        info->input_format_mode = YAZ_MARC_ISO2709;
    }
    else if (!strcmp(input_format, "xml"))
    {
        info->input_format_mode = YAZ_MARC_MARCXML;
        /** Libxml2 generates UTF-8 encoding by default .
            So we convert from UTF-8 to outputcharset (if defined)
        */
        if (!info->input_charset && info->output_charset)
            info->input_charset = "utf-8";
    }
    else if (!strcmp(input_format, "json"))
    {
        info->input_format_mode = YAZ_MARC_JSON;
    }
    else
    {
        wrbuf_printf(wr_error, "Element <marc inputformat='%s'>: "
                     " Unsupported input format"
                     " defined by attribute value",
                     input_format);
        nmem_destroy(info->nmem);
        return 0;
    }

    if (!output_format)
    {
        wrbuf_printf(wr_error,
                     "Element <marc>: attribute 'outputformat' required");
        nmem_destroy(info->nmem);
        return 0;
    }
    else if (!strcmp(output_format, "line"))
    {
        info->output_format_mode = YAZ_MARC_LINE;
    }
    else if (!strcmp(output_format, "marcxml"))
    {
        info->output_format_mode = YAZ_MARC_MARCXML;
        if (info->input_charset && !info->output_charset)
            info->output_charset = "utf-8";
    }
    else if (!strcmp(output_format, "turbomarc"))
    {
        info->output_format_mode = YAZ_MARC_TURBOMARC;
        if (info->input_charset && !info->output_charset)
            info->output_charset = "utf-8";
    }
    else if (!strcmp(output_format, "marc"))
    {
        info->output_format_mode = YAZ_MARC_ISO2709;
    }
    else if (!strcmp(output_format, "marcxchange"))
    {
        info->output_format_mode = YAZ_MARC_XCHANGE;
        if (info->input_charset && !info->output_charset)
            info->output_charset = "utf-8";
    }
    else if (!strcmp(output_format, "json"))
    {
        info->output_format_mode = YAZ_MARC_JSON;
        if (info->input_charset && !info->output_charset)
            info->output_charset = "utf-8";
    }
    else
    {
        wrbuf_printf(wr_error, "Element <marc outputformat='%s'>: "
                     " Unsupported output format"
                     " defined by attribute value",
                     output_format);
        nmem_destroy(info->nmem);
        return 0;
    }
    if (info->input_charset && info->output_charset)
    {
        yaz_iconv_t cd = yaz_iconv_open(info->output_charset,
                                        info->input_charset);
        if (!cd)
        {
            wrbuf_printf(wr_error,
                         "Element <marc inputcharset='%s' outputcharset='%s'>:"
                         " Unsupported character set mapping"
                         " defined by attribute values",
                         info->input_charset, info->output_charset);
            nmem_destroy(info->nmem);
            return 0;
        }
        yaz_iconv_close(cd);
    }
    else if (!info->output_charset)
    {
        wrbuf_printf(wr_error, "Element <marc>: "
                     "attribute 'outputcharset' missing");
        nmem_destroy(info->nmem);
        return 0;
    }
    else if (!info->input_charset)
    {
        wrbuf_printf(wr_error, "Element <marc>: "
                     "attribute 'inputcharset' missing");
        nmem_destroy(info->nmem);
        return 0;
    }
    info->input_charset = nmem_strdup(info->nmem, info->input_charset);
    info->output_charset = nmem_strdup(info->nmem, info->output_charset);
    return info;
}
예제 #15
0
파일: icu_chain.c 프로젝트: dcrossleyau/yaz
struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
                                       int sort,
                                       UErrorCode *status)
{
    xmlNode *node = 0;
    int no_errors = 0;
    struct icu_chain *chain = 0;
    NMEM nmem = 0;

    *status = U_ZERO_ERROR;

    if (xml_node && xml_node->type == XML_ELEMENT_NODE)
    {
        const char *xml_locale = yaz_xml_get_prop((xmlNode *) xml_node,
                                                  "locale");
        if (xml_locale)
            chain = icu_chain_create((const char *) xml_locale, sort, status);
    }

    if (!chain)
        return 0;

    nmem = nmem_create();
    for (node = xml_node->children; node; node = node->next)
    {
        char *rule = 0;
        struct icu_chain_step *step = 0;
        const char *attr_str;

        nmem_reset(nmem);
        if (node->type != XML_ELEMENT_NODE)
            continue;
        attr_str = yaz_xml_get_prop(node, "rule%s", &rule);
        if (attr_str)
        {
            yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
                    "element '%s'", attr_str, node->name);
            no_errors++;
        }
        if (!rule && node->children)
            rule = nmem_text_node_cdata(node->children, nmem);

        if (!rule && strcmp((const char *) node->name, "display"))
        {
            yaz_log(YLOG_WARN, "Missing attribute 'rule' for element %s",
                    (const char *) node->name);
            no_errors++;
            continue;
        }
        if (!strcmp((const char *) node->name, "casemap"))
            step = icu_chain_insert_step(chain,
                                         ICU_chain_step_type_casemap,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "transform"))
            step = icu_chain_insert_step(chain,
                                         ICU_chain_step_type_transform,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "transliterate"))
            step = icu_chain_insert_step(chain,
                                         ICU_chain_step_type_transliterate,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "tokenize"))
            step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "display"))
            step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "stemming"))
            step = icu_chain_insert_step(chain, YAZ_chain_step_type_stemming,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "join"))
            step = icu_chain_insert_step(chain, ICU_chain_step_type_join,
                                         rule, status);
        else if (!strcmp((const char *) node->name, "normalize"))
        {
            yaz_log(YLOG_WARN, "Element %s is deprecated. "
                    "Use transform instead", node->name);
            step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
                                         rule, status);
        }
        else if (!strcmp((const char *) node->name, "index")
                 || !strcmp((const char *) node->name, "sortkey"))
        {
            yaz_log(YLOG_WARN, "Element %s is no longer needed. "
                    "Remove it from the configuration", node->name);
        }
        else
        {
            yaz_log(YLOG_WARN, "Unknown element %s", node->name);
            no_errors++;
            continue;
        }
        if (!step)
        {
            yaz_log(YLOG_WARN, "Step not created for %s", node->name);
            no_errors++;
        }
        if (step && U_FAILURE(*status))
        {
            yaz_log(YLOG_WARN, "ICU Error %d %s for element %s, rule %s",
                    *status, u_errorName(*status), node->name, rule ?
                    rule : "");
            no_errors++;
            break;
        }
    }
    nmem_destroy(nmem);
    if (no_errors)
    {
        icu_chain_destroy(chain);
        return 0;
    }
    return chain;
}
예제 #16
0
static void *construct_xslt(const xmlNode *ptr,
                            const char *path, WRBUF wr_error)
{
    struct _xmlAttr *attr;
    const char *stylesheet = 0;
    struct xslt_info *info = 0;
    NMEM nmem = 0;
    int max_parms = 10;
    int no_parms = 0;

    if (strcmp((const char *) ptr->name, "xslt"))
        return 0;

    for (attr = ptr->properties; attr; attr = attr->next)
    {
        if (!xmlStrcmp(attr->name, BAD_CAST "stylesheet") &&
            attr->children && attr->children->type == XML_TEXT_NODE)
            stylesheet = (const char *) attr->children->content;
        else
        {
            wrbuf_printf(wr_error, "Bad attribute '%s'"
                         "Expected stylesheet.", attr->name);
            return 0;
        }
    }
    nmem = nmem_create();
    info = nmem_malloc(nmem, sizeof(*info));
    info->nmem = nmem;
    info->xsl_parms = nmem_malloc(
        nmem, (2 * max_parms + 1) * sizeof(*info->xsl_parms));

    for (ptr = ptr->children; ptr; ptr = ptr->next)
    {
        const char *name = 0;
        const char *value = 0;
        char *qvalue = 0;
        if (ptr->type != XML_ELEMENT_NODE)
            continue;
        if (strcmp((const char *) ptr->name, "param"))
        {
            wrbuf_printf(wr_error, "Bad element '%s'"
                         "Expected param.", ptr->name);
            nmem_destroy(nmem);
            return 0;
        }
        for (attr = ptr->properties; attr; attr = attr->next)
        {
            if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
                attr->children && attr->children->type == XML_TEXT_NODE)
                name = (const char *) attr->children->content;
            else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
                attr->children && attr->children->type == XML_TEXT_NODE)
                value = (const char *) attr->children->content;
            else
            {
                wrbuf_printf(wr_error, "Bad attribute '%s'"
                             "Expected name or value.", attr->name);
                nmem_destroy(nmem);
                return 0;
            }
        }
        if (!name || !value)
        {
            wrbuf_printf(wr_error, "Missing attributes name or value");
            nmem_destroy(nmem);
            return 0;
        }
        if (no_parms >= max_parms)
        {
            wrbuf_printf(wr_error, "Too many parameters given");
            nmem_destroy(nmem);
            return 0;
        }

        qvalue = nmem_malloc(nmem, strlen(value) + 3);
        strcpy(qvalue, "\'");
        strcat(qvalue, value);
        strcat(qvalue, "\'");

        info->xsl_parms[2 * no_parms] = nmem_strdup(nmem, name);
        info->xsl_parms[2 * no_parms + 1] = qvalue;
        no_parms++;
    }
    info->xsl_parms[2 * no_parms] = 0;

    if (!stylesheet)
    {
        wrbuf_printf(wr_error, "Element <xslt>: "
                     "attribute 'stylesheet' expected");
        nmem_destroy(nmem);
    }
    else
    {
        char fullpath[1024];
        xsltStylesheetPtr xsp;
        if (!yaz_filepath_resolve(stylesheet, path, 0, fullpath))
        {
            wrbuf_printf(wr_error, "Element <xslt stylesheet=\"%s\"/>:"
                         " could not locate stylesheet '%s'",
                         stylesheet, stylesheet);
            if (path)
                wrbuf_printf(wr_error, " with path '%s'", path);

            nmem_destroy(nmem);
            return 0;
        }
        info->xsp_doc = xmlParseFile(fullpath);
        if (!info->xsp_doc)
        {
            wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
                         " xml parse failed: %s", stylesheet, fullpath);
            if (path)
                wrbuf_printf(wr_error, " with path '%s'", path);
            nmem_destroy(nmem);
            return 0;
        }
        /* need to copy this before passing it to the processor. It will
           be encapsulated in the xsp and destroyed by xsltFreeStylesheet */
        xsp = xsltParseStylesheetDoc(xmlCopyDoc(info->xsp_doc, 1));
        if (!xsp)
        {
            wrbuf_printf(wr_error, "Element: <xslt stylesheet=\"%s\"/>:"
                         " xslt parse failed: %s", stylesheet, fullpath);
            if (path)
                wrbuf_printf(wr_error, " with path '%s'", path);
            wrbuf_printf(wr_error, " ("
#if YAZ_HAVE_EXSLT

                         "EXSLT enabled"
#else
                         "EXSLT not supported"
#endif
                         ")");
            xmlFreeDoc(info->xsp_doc);
            nmem_destroy(info->nmem);
        }
        else
        {
            xsltFreeStylesheet(xsp);
            return info;
        }
    }
    return 0;
}
예제 #17
0
static void tst_convert3(void)
{
    NMEM nmem = nmem_create();
    int ret;
    yaz_record_conv_t p = 0;

    const char *iso2709_rec =
        "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x20\x32\x32\x30\x30\x30\x34"
        "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
        "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
        "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
        "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";

    const char *opacxml_rec =
        "<opacRecord>\n"
        "  <bibliographicRecord>\n"
        "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
        "  <leader>00077nam a22000498a 4500</leader>\n"
        "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
        "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
        "    <subfield code=\"a\">k" "\xc3" "\xb8" /* oslash in UTF_8 */
        "benhavn</subfield>\n"
        "  </datafield>\n"
        "</record>\n"
        "  </bibliographicRecord>\n"
        "<holdings>\n"
        " <holding>\n"
        "  <typeOfRecord>u</typeOfRecord>\n"
        "  <encodingLevel>U</encodingLevel>\n"
        "  <receiptAcqStatus>0</receiptAcqStatus>\n"
        "  <dateOfReport>000000</dateOfReport>\n"
        "  <nucCode>s-FM/GC</nucCode>\n"
        "  <localLocation>Main or Science/Business Reading Rms - STORED OFFSITE</localLocation>\n"
        "  <callNumber>MLCM 89/00602 (N)</callNumber>\n"
        "  <shelvingData>FT MEADE</shelvingData>\n"
        "  <copyNumber>Copy 1</copyNumber>\n"
        "  <volumes>\n"
        "   <volume>\n"
        "    <enumeration>1</enumeration>\n"
        "    <chronology>2</chronology>\n"
        "    <enumAndChron>3</enumAndChron>\n"
        "   </volume>\n"
        "   <volume>\n"
        "    <enumeration>1</enumeration>\n"
        "    <chronology>2</chronology>\n"
        "    <enumAndChron>3</enumAndChron>\n"
        "   </volume>\n"
        "  </volumes>\n"
        "  <circulations>\n"
        "   <circulation>\n"
        "    <availableNow value=\"1\"/>\n"
        "    <availabilityDate>20130129</availabilityDate>\n"
        "    <itemId>1226176</itemId>\n"
        "    <renewable value=\"0\"/>\n"
        "    <onHold value=\"0\"/>\n"
        "   </circulation>\n"
        "  </circulations>\n"
        " </holding>\n"
        "</holdings>\n"
        "</opacRecord>\n";

    Z_OPACRecord *z_opac = nmem_malloc(nmem, sizeof(*z_opac));
    Z_HoldingsAndCircData *h;
    Z_CircRecord *circ;

    z_opac->bibliographicRecord =
        z_ext_record_oid_nmem(nmem, yaz_oid_recsyn_usmarc,
                              iso2709_rec, strlen(iso2709_rec));
    z_opac->num_holdingsData = 1;
    z_opac->holdingsData = (Z_HoldingsRecord **)
        nmem_malloc(nmem, sizeof(Z_HoldingsRecord *) * 1);
    z_opac->holdingsData[0] = (Z_HoldingsRecord *)
        nmem_malloc(nmem, sizeof(Z_HoldingsRecord));
    z_opac->holdingsData[0]->which = Z_HoldingsRecord_holdingsAndCirc;
    h = z_opac->holdingsData[0]->u.holdingsAndCirc = (Z_HoldingsAndCircData *)
         nmem_malloc(nmem, sizeof(*h));
    h->typeOfRecord = nmem_strdup(nmem, "u");
    h->encodingLevel = nmem_strdup(nmem, "U");
    h->format = 0;
    h->receiptAcqStatus = nmem_strdup(nmem, "0");
    h->generalRetention = 0;
    h->completeness = 0;
    h->dateOfReport = nmem_strdup(nmem, "000000");
    h->nucCode = nmem_strdup(nmem, "s-FM/GC");
    h->localLocation = nmem_strdup(nmem,
                                   "Main or Science/Business Reading "
                                   "Rms - STORED OFFSITE");
    h->shelvingLocation = 0;
    h->callNumber = nmem_strdup(nmem, "MLCM 89/00602 (N)");
    h->shelvingData = nmem_strdup(nmem, "FT MEADE");
    h->copyNumber = nmem_strdup(nmem, "Copy 1");
    h->publicNote = 0;
    h->reproductionNote = 0;
    h->termsUseRepro = 0;
    h->enumAndChron = 0;
    h->num_volumes = 2;
    h->volumes = 0;

    h->volumes = (Z_Volume **)
        nmem_malloc(nmem, 2 * sizeof(Z_Volume *));

    h->volumes[0] = (Z_Volume *)
        nmem_malloc(nmem, sizeof(Z_Volume));
    h->volumes[1] = h->volumes[0];

    h->volumes[0]->enumeration = nmem_strdup(nmem, "1");
    h->volumes[0]->chronology = nmem_strdup(nmem, "2");
    h->volumes[0]->enumAndChron = nmem_strdup(nmem, "3");

    h->num_circulationData = 1;
    h->circulationData = (Z_CircRecord **)
        nmem_malloc(nmem, 1 * sizeof(Z_CircRecord *));
    circ = h->circulationData[0] = (Z_CircRecord *)
        nmem_malloc(nmem, sizeof(Z_CircRecord));
    circ->availableNow = nmem_booldup(nmem, 1);
    circ->availablityDate = nmem_strdup(nmem, "20130129");
    circ->availableThru = 0;
    circ->restrictions = 0;
    circ->itemId = nmem_strdup(nmem, "1226176");
    circ->renewable = nmem_booldup(nmem, 0);
    circ->onHold = nmem_booldup(nmem, 0);
    circ->enumAndChron = 0;
    circ->midspine = 0;
    circ->temporaryLocation = 0;

    YAZ_CHECK(conv_configure_test("<backend>"
                                  "<marc"
                                  " inputcharset=\"marc-8\""
                                  " outputcharset=\"utf-8\""
                                  " inputformat=\"marc\""
                                  " outputformat=\"marcxml\""
                                  "/>"
                                  "</backend>",
                                  0, &p));

    if (p)
    {
        WRBUF output_record = wrbuf_alloc();
        ret = yaz_record_conv_opac_record(p, z_opac, output_record);
        YAZ_CHECK(ret == 0);
        if (ret == 0)
        {
            ret = strcmp(wrbuf_cstr(output_record), opacxml_rec);
            YAZ_CHECK(ret == 0);
            if (ret)
            {
                printf("got-output_record len=%ld: %s\n",
                       (long) wrbuf_len(output_record),
                       wrbuf_cstr(output_record));
                printf("output_expect_record len=%ld %s\n",
                       (long) strlen(opacxml_rec),
                       opacxml_rec);
            }
        }
        yaz_record_conv_destroy(p);
        wrbuf_destroy(output_record);
    }
    {
        Z_OPACRecord *opac = 0;
        yaz_marc_t mt =  yaz_marc_create();
        ret = yaz_xml_to_opac(mt, opacxml_rec, strlen(opacxml_rec),
                              &opac, 0 /* iconv */, nmem, 0);
        YAZ_CHECK(ret);
        YAZ_CHECK(opac);

        if (opac)
        {
            WRBUF output_record = wrbuf_alloc();
            char *p;

            yaz_marc_xml(mt, YAZ_MARC_MARCXML);
            yaz_opac_decode_wrbuf(mt, opac, output_record);

            /* change MARC size to 00077 from 00078, due to
               encoding of the aring (two bytes in UTF-8) */
            p = strstr(wrbuf_buf(output_record), "00078");
            YAZ_CHECK(p);
            if (p)
                p[4] = '7';

            ret = strcmp(wrbuf_cstr(output_record), opacxml_rec);
            YAZ_CHECK(ret == 0);
            if (ret)
            {
                printf("got-output_record len=%ld: %s\n",
                       (long) wrbuf_len(output_record),
                       wrbuf_cstr(output_record));
                printf("output_expect_record len=%ld %s\n",
                       (long) strlen(opacxml_rec),
                       opacxml_rec);
            }
            wrbuf_destroy(output_record);
        }
        yaz_marc_destroy(mt);
    }
    nmem_destroy(nmem);
}
예제 #18
0
normalize_record_t normalize_record_create(struct conf_service *service,
                                           const char *spec)
{
    NMEM nmem = nmem_create();
    normalize_record_t nt = nmem_malloc(nmem, sizeof(*nt));
    struct normalize_step **m = &nt->steps;
    int no_errors = 0;
    int embed = 0;

    if (*spec == '<')
        embed = 1;

    nt->nmem = nmem;

    if (embed)
    {
        xmlDoc *xsp_doc = xmlParseMemory(spec, strlen(spec));

        if (!xsp_doc)
            no_errors++;
        {
            *m = nmem_malloc(nt->nmem, sizeof(**m));
            (*m)->marcmap = NULL;
            (*m)->stylesheet = NULL;
            (*m)->stylesheet2 = NULL;


            (*m)->stylesheet = xsltParseStylesheetDoc(xsp_doc);
            if (!(*m)->stylesheet)
                no_errors++;
            m = &(*m)->next;
        }
    }
    else
    {
        struct conf_config *conf = service->server->config;
        int i, num;
        char **stylesheets;
        nmem_strsplit(nt->nmem, ",", spec, &stylesheets, &num);

        for (i = 0; i < num; i++)
        {
            WRBUF fname = conf_get_fname(conf, stylesheets[i]);

            *m = nmem_malloc(nt->nmem, sizeof(**m));
            (*m)->marcmap = NULL;
            (*m)->stylesheet = NULL;

            (*m)->stylesheet2 = service_xslt_get(service, stylesheets[i]);
            if ((*m)->stylesheet2)
                ;
            else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
            {
                if (!((*m)->stylesheet =
                      xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(fname))))
                {
                    yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
                            stylesheets[i]);
                    no_errors++;
                }
            }
            else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
            {
                if (!((*m)->marcmap = marcmap_load(wrbuf_cstr(fname), nt->nmem)))
                {
                    yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
                            stylesheets[i]);
                    no_errors++;
                }
            }
            else
            {
                yaz_log(YLOG_FATAL, "Cannot handle stylesheet: %s", stylesheets[i]);
                no_errors++;
            }

            wrbuf_destroy(fname);
            m = &(*m)->next;
        }
    }
    *m = 0;  /* terminate list of steps */

    if (no_errors)
    {
        normalize_record_destroy(nt);
        nt = 0;
    }
    return nt;
}
예제 #19
0
파일: marcmap.c 프로젝트: saiful76/pazpar2
xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in)
{
    char mergekey[1024];
    char medium[32];
    char *s;
    NMEM nmem;
    xmlNsPtr ns_pz;
    xmlDocPtr xml_out;
    xmlNodePtr xml_out_root;
    xmlNodePtr rec_node;
    xmlNodePtr meta_node; 
    struct marchash *marchash;
    struct marcfield *field;
    struct marcsubfield *subfield;
    struct marcmap *mmcur;
     
    xml_out = xmlNewDoc(BAD_CAST "1.0");
    xml_out->encoding = xmlCharStrdup("UTF-8");
    xml_out_root = xmlNewNode(NULL, BAD_CAST "record");
    xmlDocSetRootElement(xml_out, xml_out_root);
    ns_pz = xmlNewNs(xml_out_root, BAD_CAST "http://www.indexdata.com/pazpar2/1.0", BAD_CAST "pz"); 
    xmlSetNs(xml_out_root, ns_pz);
    nmem = nmem_create();
    rec_node = xmlDocGetRootElement(xml_in);
    marchash = marchash_create(nmem);
    marchash_ingest_marcxml(marchash, rec_node);

    mmcur = marcmap;
    while (mmcur != NULL)
    {
        field = 0;
        while ((field = marchash_get_field(marchash, mmcur->field, field)) != 0)
        {
            // field value
            if ((mmcur->subfield == '$') && (s = field->val))
            {
                meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
                xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz); 
            }
            // catenate all subfields
            else if ((mmcur->subfield == '*') && (s = marchash_catenate_subfields(field, " ", nmem)))
            {
                meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
                xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
            }
            // subfield value
            else if (mmcur->subfield) 
            {
                subfield = 0;
                while ((subfield = 
                        marchash_get_subfield(mmcur->subfield,
                                              field, subfield)) != 0)
                {
                    if ((s = subfield->val) != 0)
                    {
                        meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
                        xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
                    }
                }
            }
            
        }
        mmcur = mmcur->next;
    }

    // hard coded mappings

    // medium
    if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('h', field, NULL)))
    {
       strncpy(medium, subfield->val, 32);
    }
    else if ((field = marchash_get_field(marchash, "900", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
       strcpy(medium, "electronic resource");
    else if ((field = marchash_get_field(marchash, "900", NULL)) && (subfield = marchash_get_subfield('b', field, NULL)))
       strcpy(medium, "electronic resource");
    else if ((field = marchash_get_field(marchash, "773", NULL)) && (subfield = marchash_get_subfield('t', field, NULL)))
       strcpy(medium, "article");
    else
       strcpy(medium, "book");

    meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST medium);
    xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST "medium");

    // merge key
    memset(mergekey, 0, 1024);
    strcpy(mergekey, "title ");
    if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
        strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
    strncat(mergekey, " author ", 1023 - strlen(mergekey));
    if ((field = marchash_get_field(marchash, "100", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
        strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
    strncat(mergekey, " medium ", 1023 - strlen(mergekey));
    strncat(mergekey, medium, 1023 - strlen(mergekey));

//    xmlSetProp(xml_out_root, BAD_CAST "mergekey", BAD_CAST mergekey);

    nmem_destroy(nmem);
    return xml_out;
}