예제 #1
0
void xmlquerytopqf(const char *xmlstr)
{
    xmlDocPtr doc;

    doc = xmlParseMemory(xmlstr, strlen(xmlstr));
    if (!doc)
    {
	fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
	exit(1);
    }
    else
    {
	int error_code = 0;
	const char *addinfo = 0;
	Z_Query *query = 0;
	ODR odr = odr_createmem(ODR_ENCODE);

	const xmlNode *root_element = xmlDocGetRootElement(doc);
	yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
	if (error_code)
	{
	    fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
		    prog, error_code, addinfo);
	    exit(1);
	}
	else if (!query)
	{
	    fprintf(stderr, "%s: yaz_xml2query no query result\n",
		    prog);
	    exit(1);
	}
	else
	{
	    WRBUF w = wrbuf_alloc();
	    yaz_query_to_wrbuf(w, query);
	    printf("%s\n", wrbuf_cstr(w));
	    wrbuf_destroy(w);
	}
	odr_destroy(odr);
	xmlFreeDoc(doc);
    }
}
void yf::QueryRewrite::Rep::process(mp::Package &package) const
{
    Z_GDU *gdu = package.request().get();

    if (gdu && gdu->which == Z_GDU_Z3950)
    {
        Z_APDU *apdu_req = gdu->u.z3950;
        if (apdu_req->which == Z_APDU_searchRequest)
        {
            int error_code = 0;
            const char *addinfo = 0;
            mp::odr odr;
            Z_SearchRequest *req = apdu_req->u.searchRequest;

            if (m_stylesheet)
            {
                xmlDocPtr doc_input = 0;
                yaz_query2xml(req->query, &doc_input);

                if (doc_input)
                {
                    xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
                                                            doc_input, 0);
                    if (!doc_res)
                    {
                        error_code = YAZ_BIB1_MALFORMED_QUERY;
                        addinfo = "XSLT transform failed for query";
                    }
                    else
                    {
                        const xmlNode *root_element = xmlDocGetRootElement(doc_res);
                        yaz_xml2query(root_element, &req->query, odr,
                                      &error_code, &addinfo);
                        xmlFreeDoc(doc_res);
                    }
                    xmlFreeDoc(doc_input);
                }
            }
            if (!error_code && charset_to.length() && charset_from.length() &&
                (req->query->which == Z_Query_type_1
                 || req->query->which == Z_Query_type_101))
            {
                yaz_iconv_t cd = yaz_iconv_open(charset_to.c_str(),
                                                charset_from.c_str());
                if (cd)
                {
                    int r = yaz_query_charset_convert_rpnquery_check(
                        req->query->u.type_1, odr, cd);
                    yaz_iconv_close(cd);
                    if (r)
                    {  /* query could not be char converted */
                        error_code = YAZ_BIB1_MALFORMED_QUERY;
                        addinfo = "could not convert query to target charset";
                    }
                }
            }
            if (error_code)
            {
                Z_APDU *f_apdu =
                    odr.create_searchResponse(apdu_req, error_code, addinfo);
                package.response() = f_apdu;
                return;
            }
            package.request() = gdu;
        }
    }
    package.move();
}
예제 #3
0
파일: test_xmlquery.c 프로젝트: nla/yaz
enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml,
                                 const char *expect_pqf)
{
    YAZ_PQF_Parser parser = yaz_pqf_create();
    ODR odr = odr_createmem(ODR_ENCODE);
    Z_RPNQuery *rpn;
    enum pqf2xml_status status = XML_NO_ERROR;

    YAZ_CHECK(parser);

    YAZ_CHECK(odr);

    rpn = yaz_pqf_parse(parser, odr, pqf);

    yaz_pqf_destroy(parser);

    if (!rpn)
        status = PQF_FAILED;
    else
    {
#if YAZ_HAVE_XML2
        xmlDocPtr doc = 0;

        yaz_rpnquery2xml(rpn, &doc);

        if (!doc)
            status = QUERY2XML_FAILED;
        else
        {
            char *buf_out;
            int len_out;

            xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);

            if (len_out == (int) strlen(expect_xml)
                && memcmp(buf_out, expect_xml, len_out) == 0)
            {
                Z_Query *query2 = 0;
                int error_code = 0;
                const char *addinfo = 0;
                const xmlNode *root_element = xmlDocGetRootElement(doc);
                ODR odr2 = odr_createmem(ODR_ENCODE);

                yaz_xml2query(root_element, &query2, odr2,
                              &error_code, &addinfo);
                if (error_code || !query2)
                    status = XML_NO_MATCH;
                else
                {
                    WRBUF w = wrbuf_alloc();
                    yaz_query_to_wrbuf(w, query2);
                    if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0)
                        status = XML_MATCH;
                    else
                    {
                        status = XML_NO_MATCH;
                        printf("Result: %s\n", wrbuf_cstr(w));
                    }
                    wrbuf_destroy(w);
                }
                odr_destroy(odr2);
            }
            else
            {
                printf("%.*s\n", len_out, buf_out);
                status = XML_NO_MATCH;
            }
            xmlFreeDoc(doc);
            xmlFree(buf_out);
        }
#else
        status = QUERY2XML_FAILED;
#endif
    }
    odr_destroy(odr);
    return status;
}