Change config; use name attribute wherewhere
[mp-xquery-moved-to-github.git] / src / metaproxy_filter_xquery.cpp
index 51afacd..466164c 100644 (file)
@@ -20,8 +20,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <metaproxy/util.hpp>
 #include <yaz/log.h>
 #include <yaz/oid_db.h>
+#include <yaz/diagbib1.h>
 #include <map>
-
+#include <string>
 #include <iostream>
 #include <fstream>
 #include <sstream>
@@ -102,10 +103,16 @@ bool yf::XQuery::convert_one_record(const char *input_buf,
     lItem = lZorba->getItemFactory()->createString(rec_content);
     lDynamicContext->setVariable(zorba_record_variable, lItem);
 
-    std::stringstream ss;
-    tQuery->execute(ss);
-    result = ss.str();
-    return true;
+    try {
+        std::stringstream ss;
+        tQuery->execute(ss);
+        result = ss.str();
+        return true;
+    } catch ( ZorbaException &e) {
+        result = e.what();
+        yaz_log(YLOG_WARN, "XQuery execute: %s", result.c_str());
+        return false;
+    }
 }
 
 void yf::XQuery::process(Package &package) const
@@ -272,11 +279,11 @@ void yf::XQuery::process(Package &package) const
         int i;
         for (i = 0; i < records->num_records; i++)
         {
-            Z_NamePlusRecord *npr = records->records[i];
-            if (npr->which == Z_NamePlusRecord_databaseRecord)
+            Z_NamePlusRecord **npr = &records->records[i];
+            if ((*npr)->which == Z_NamePlusRecord_databaseRecord)
             {
                 const char *details = 0;
-                Z_External *r = npr->u.databaseRecord;
+                Z_External *r = (*npr)->u.databaseRecord;
                 int ret_trans = -1;
                 if (r->which == Z_External_octet &&
                     !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
@@ -286,11 +293,18 @@ void yf::XQuery::process(Package &package) const
                         r->u.octet_aligned->buf, r->u.octet_aligned->len,
                         result))
                     {
-                        npr->u.databaseRecord =
+                        (*npr)->u.databaseRecord =
                             z_ext_record_oid(odr_en, yaz_oid_recsyn_xml,
                                              result.c_str(),
                                              result.length());
                     }
+                    else
+                    {
+                        *npr = zget_surrogateDiagRec(
+                            odr_en, (*npr)->databaseName,
+                            YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+                            result.c_str());
+                    }
                 }
             }
         }
@@ -305,7 +319,7 @@ void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
     {
         if (ptr->type != XML_ELEMENT_NODE)
             continue;
-        if (!strcmp((const char *) ptr->name, "setVariable"))
+        if (!strcmp((const char *) ptr->name, "variable"))
         {
             std::string name;
             std::string value;
@@ -322,44 +336,31 @@ void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
             if (name.length() > 0)
                 zorba_variables[name] = value;
         }
-        else if (!strcmp((const char *) ptr->name, "filename"))
-        {
-            std::string value;
-            struct _xmlAttr *attr;
-            for (attr = ptr->properties; attr; attr = attr->next)
-                if (!strcmp((const char *) attr->name, "value"))
-                    value = mp::xml::get_text(attr->children);
-                else
-                    throw mp::filter::FilterException(
-                        "Bad attribute " + std::string((const char *)
-                                                       attr->name));
-            zorba_filename = value;
-        }
         else if (!strcmp((const char *) ptr->name, "script"))
         {
-            std::string value;
+            std::string name;
             struct _xmlAttr *attr;
             for (attr = ptr->properties; attr; attr = attr->next)
-                if (!strcmp((const char *) attr->name, "value"))
-                    value = mp::xml::get_text(attr->children);
+                if (!strcmp((const char *) attr->name, "name"))
+                    name = mp::xml::get_text(attr->children);
                 else
                     throw mp::filter::FilterException(
                         "Bad attribute " + std::string((const char *)
                                                        attr->name));
-            zorba_script = value;
+            zorba_script = name;
         }
         else if (!strcmp((const char *) ptr->name, "record"))
         {
-            std::string value;
+            std::string name;
             struct _xmlAttr *attr;
             for (attr = ptr->properties; attr; attr = attr->next)
-                if (!strcmp((const char *) attr->name, "value"))
-                    value = mp::xml::get_text(attr->children);
+                if (!strcmp((const char *) attr->name, "name"))
+                    name = mp::xml::get_text(attr->children);
                 else
                     throw mp::filter::FilterException(
                         "Bad attribute " + std::string((const char *)
                                                        attr->name));
-            zorba_record_variable = value;
+            zorba_record_variable = name;
         }
         else
         {
@@ -372,8 +373,6 @@ void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
         throw mp::filter::FilterException("Missing element script");
     if (zorba_record_variable.length() == 0)
         throw mp::filter::FilterException("Missing element record");
-    if (zorba_filename.length() == 0)
-        throw mp::filter::FilterException("Missing element filename");
     if (!test_only)
     {
         void* lStore = StoreManager::getStore();
@@ -381,13 +380,19 @@ void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
 
         lQuery = lZorba->createQuery();
 
-        lQuery->setFileName(zorba_filename);
-
-        std::unique_ptr<std::istream> qfile;
-        qfile.reset(new std::ifstream(zorba_script.c_str()));
-
-        Zorba_CompilerHints lHints;
-        lQuery->compile(*qfile, lHints);
+        try {
+            size_t t = zorba_script.find_last_of('/');
+            if (t != std::string::npos)
+                lQuery->setFileName(zorba_script.substr(0, t + 1));
+            std::unique_ptr<std::istream> qfile(
+                new std::ifstream(zorba_script.c_str()));
+            Zorba_CompilerHints lHints;
+            lQuery->compile(*qfile, lHints);
+        } catch ( ZorbaException &e) {
+            std::string msg = "XQuery compile: ";
+            msg += e.what();
+            throw mp::filter::FilterException(msg);
+        }
     }
 }