URL recipe in place
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
index 35512ca..eb9f64b 100644 (file)
@@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/srw.h>
 #include <metaproxy/package.hpp>
 #include <metaproxy/util.hpp>
+#include <metaproxy/xmlutil.hpp>
 #include "torus.hpp"
 
 #include <libxslt/xsltutils.h>
@@ -60,6 +61,7 @@ namespace metaproxy_1 {
             std::string element_set;
             std::string record_encoding;
             std::string transform_xsl_fname;
+            std::string urlRecipe;
             bool use_turbomarc;
             bool piggyback;
             CCL_bibset ccl_bibset;
@@ -400,6 +402,11 @@ yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
             s->transform_xsl_fname = mp::xml::get_text(ptr);
         }
         else if (!strcmp((const char *) ptr->name,
+                         "urlRecipe"))
+        {
+            s->urlRecipe = mp::xml::get_text(ptr);
+        }
+        else if (!strcmp((const char *) ptr->name,
                          "useTurboMarc"))
         {
             ; // useTurboMarc is ignored
@@ -848,6 +855,7 @@ Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
                 }
                 
                 int rec_len;
+                xmlChar *xmlrec_buf = 0;
                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
                                                       &rec_len);
                 if (rec_buf && b->xsp && enable_pz2_transform)
@@ -859,13 +867,39 @@ Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
                         rec_res = xsltApplyStylesheet(b->xsp, rec_doc, 0);
 
                         if (rec_res)
-                            xsltSaveResultToString((xmlChar **) &rec_buf, &rec_len,
+                            xsltSaveResultToString(&xmlrec_buf, &rec_len,
                                                    rec_res, b->xsp);
+                        rec_buf = (const char *) xmlrec_buf;
+                        xmlFreeDoc(rec_doc);
+                        xmlFreeDoc(rec_res);
                     }
                 }
 
                 if (rec_buf)
                 {
+                    xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
+                    std::string res = 
+                        mp::xml::url_recipe_handle(doc, b->sptr->urlRecipe);
+                    if (res.length())
+                    {
+                        xmlNode *ptr = xmlDocGetRootElement(doc);
+                        while (ptr && ptr->type != XML_ELEMENT_NODE)
+                            ptr = ptr->next;
+                        xmlNode *c = 
+                            xmlNewChild(ptr, 0, BAD_CAST "generated-url", 0);
+                        xmlNode * t = xmlNewText(BAD_CAST res.c_str());
+                        xmlAddChild(c, t);
+
+                        if (xmlrec_buf)
+                            xmlFree(xmlrec_buf);
+
+                        xmlDocDumpMemory(doc, &xmlrec_buf, &rec_len);
+                        rec_buf = (const char *) xmlrec_buf;
+                    }
+                    xmlFreeDoc(doc);
+                }
+                if (rec_buf)
+                {
                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
                     npr->databaseName = odr_database;
                     npr->which = Z_NamePlusRecord_databaseRecord;
@@ -879,6 +913,8 @@ Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
                         rec_type_str);
                 }
+                if (xmlrec_buf)
+                    xmlFree(xmlrec_buf);
             }
             else
             {