Ignore:
Timestamp:
1999-07-07T17:49:35+12:00 (25 years ago)
Author:
sjboddie
Message:

had another crack at the format string code - created a new formattools
module. It can now handle {If} and {Or} statements although there's a
bug preventing nested if's and or's.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/recpt/queryaction.cpp

    r337 r347  
    1212/*
    1313   $Log$
     14   Revision 1.10  1999/07/07 05:49:35  sjboddie
     15   had another crack at the format string code - created a new formattools
     16   module. It can now handle {If} and {Or} statements although there's a
     17   bug preventing nested if's and or's.
     18
    1419   Revision 1.9  1999/07/01 22:48:46  sjboddie
    1520   had a go at getting a query result format string working
     
    5459#include "queryaction.h"
    5560#include "querytools.h"
     61#include "formattools.h"
    5662
    5763queryaction::queryaction () {
     
    310316      if (it != end) set_option_macro ("n", args["n"], (*it).second, disp);
    311317  }
    312 }
    313 
    314 // returns false if formatstring has errors
    315 bool queryaction::parse_formatstring (const text_t &formatstring, text_tarray &formatarray,
    316                       text_tmap &metamap, text_tarray &metadata, bool &getParents) {
    317 
    318   formatarray.erase (formatarray.begin(), formatarray.end());
    319   metadata.erase (metadata.begin(), metadata.end());
    320   getParents = false;
    321 
    322   text_t::const_iterator here = formatstring.begin();
    323   text_t::const_iterator end = formatstring.end();
    324 
    325   int metacount = 0;
    326   text_t text;
    327   while (here != end) {
    328 
    329     if (*here == '\\')
    330       text.push_back (*(++here));
    331 
    332     else if (*here == '<') {
    333       if (!text.empty()) formatarray.push_back (text);
    334       text.clear();
    335       text_t meta;
    336       here ++;
    337       while (*here != '>') {
    338     if (here == end) return false;
    339     meta.push_back (*here);
    340     here ++;
    341       }
    342       parse_meta (meta, metacount, formatarray, metadata, getParents, metamap);
    343 
    344     } else
    345       text.push_back (*here);
    346 
    347     here ++;
    348   }
    349   if (!text.empty()) formatarray.push_back (text);
    350 
    351   return true;
    352 }
    353 
    354 void queryaction::get_parent_options (const text_t &instring, text_t &options,
    355                       text_t &meta) {
    356 
    357   options.clear();
    358   meta.clear();
    359   bool inquotes = false;
    360   bool foundcolon = false;
    361   text_t::const_iterator here = instring.begin()+6;
    362   text_t::const_iterator end = instring.end();
    363   while (here != end) {
    364     if (*here == '(') inquotes = true;
    365     else if (*here == ')') inquotes = false;
    366     else if (*here == ':' && !inquotes) foundcolon = true;
    367     else if (foundcolon) meta.push_back (*here);
    368     else options.push_back (*here);
    369     here ++;
    370   }
    371 }
    372 
    373 void queryaction::parse_meta (const text_t &meta, int &count, text_tarray &formatarray,
    374                   text_tarray &metadata, bool &getParents, text_tmap &metamap) {
    375  
    376   if (meta == "link" || meta == "/link") formatarray.push_back ("<" + meta + ">");
    377   else {
    378     text_t met = meta;
    379 
    380     if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
    381       getParents = true;
    382       text_t ops;
    383       get_parent_options (meta, ops, met);
    384     }
    385     text_tmap::const_iterator it;
    386     if ((it = metamap.find(met)) != metamap.end())
    387       formatarray.push_back(meta);
    388     else {
    389       metamap[met] = count;
    390       metadata.push_back (met);
    391       formatarray.push_back (meta);
    392       count ++;
    393     }
    394   }
    395 }
    396 
    397 void queryaction::output_results (const ResultDocInfo_tarray &results, const text_tarray &formatarray,
    398                   const text_tmap &metamap, displayclass &disp, outconvertclass &outconvert,
    399                   ostream &textout) {
    400 
    401   ResultDocInfo_tarray::const_iterator this_doc = results.begin();
    402   ResultDocInfo_tarray::const_iterator end_doc = results.end();
    403   text_tmap::const_iterator it;
    404 
    405   textout << "<table cellspacing=4>\n";
    406   while (this_doc != end_doc) {
    407     textout << "<tr>\n";
    408 
    409     text_tarray::const_iterator this_format = formatarray.begin();
    410     text_tarray::const_iterator end_format = formatarray.end();
    411 
    412     while (this_format != end_format) {
    413 
    414       if (*this_format == "<link>")
    415     textout << outconvert << disp << "<a href=\"_httpdocument_&cl=search&d=" + (*this_doc).OID + "\">";
    416       else if (*this_format == "</link>")
    417     textout << "</a>";
    418       else if ((it = metamap.find(*this_format)) != metamap.end())
    419     textout << outconvert << disp
    420         << (*this_doc).metadata[(*it).second.getint()].values.back();
    421       else if ((*this_format).size() > 7 &&
    422            (substr ((*this_format).begin(), (*this_format).begin()+6) == "parent")) {
    423     text_t options, meta;
    424     get_parent_options (*this_format, options, meta);
    425     if ((it = metamap.find(meta)) != metamap.end()) {
    426       int metanum = (*it).second.getint();
    427       if (options.empty()) {
    428         // no options - we just want the immediate parent
    429         int num = (*this_doc).metadata[metanum].values.size();
    430         if (num > 1)
    431           textout << outconvert << disp
    432               << (*this_doc).metadata[metanum].values[num-2];
    433       } else if (options == "top") {
    434         // want the top parent
    435         if ((*this_doc).metadata[metanum].values.size() > 1)
    436           textout << outconvert << disp
    437               << (*this_doc).metadata[metanum].values[0];
    438       } else {
    439         // the 'all' option
    440         if (substr (options.begin(), options.begin()+3) != "all")
    441           // bad format or unknown option
    442           textout << outconvert << disp << *this_format;
    443         else {
    444           text_t::const_iterator fquote = findchar (options.begin(), options.end(), '\'');
    445           text_t separator = substr (fquote+1, findchar (fquote+1, options.end(), '\''));
    446           text_tarray::const_iterator here = (*this_doc).metadata[metanum].values.begin();
    447           // the last metadata value is for the current OID (i.e. it's not a parent)
    448           text_tarray::const_iterator end = ((*this_doc).metadata[metanum].values.end()) - 1;
    449           bool first = true;
    450           while (here != end) {
    451         if (!first) textout << outconvert << disp << separator;
    452         textout << outconvert << disp << *here;
    453         first = false;
    454         here ++;
    455           }
    456         }
    457       }
    458     } else {
    459       // Didn't get any metadata by this name, maybe
    460       // it's just plain text that happened to match (oops!!)
    461       textout << outconvert << disp << *this_format;
    462     }     
    463       } else
    464     textout << outconvert << disp << *this_format;
    465      
    466       this_format ++;
    467      
    468     }
    469     textout << "</tr>\n";
    470     this_doc ++;
    471   }
    472 
    473   textout << "</table>\n";
    474318}
    475319
     
    491335  // if we still don't have a format string use the default
    492336  if (formatstring.empty())
    493     formatstring = "\\<td valign=top nowrap\\><link>_icontext_</link>\\</td\\>\\<td\\><Title>\\</td\\>";
     337    formatstring = "<td valign=top nowrap>[link]_icontext_[/link]</td><td>[Title]</td>";
    494338
    495339  if (collectproto == NULL) {
     
    503347    FilterResponse_t response;
    504348    text_t quotedstring;
    505     text_tarray formatarray;
    506     text_tmap metamap;
    507     parse_formatstring (formatstring, formatarray, metamap,
    508             request.fields, request.getParents);
     349    format_t *formatlistptr = new format_t();
     350
     351    parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
    509352
    510353    // do the query
    511354    request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
    512     //    request.fields.push_back ("Title");
    513     //    request.fields.push_back ("Creator");
    514     //    request.getParents = true;
    515355    if (!do_query (request, args, collectproto, quotedstring, response, logout))
    516356      return false;
     
    520360   
    521361    // output the header
    522     textout << outconvert << disp << "_query:header_\n_query:content_";
    523 
    524     output_results (response.docInfo, formatarray, metamap, disp, outconvert, textout);
    525 
     362    textout << outconvert << disp << "_query:header_\n"
     363        << "_query:content_";
     364
     365    // output the results
     366    textout << "<table cellspacing=4>\n";
     367    ResultDocInfo_tarray::const_iterator this_doc = response.docInfo.begin();
     368    ResultDocInfo_tarray::const_iterator end_doc = response.docInfo.end();
     369
     370    while (this_doc != end_doc) {
     371      textout << outconvert << disp << "<tr>\n"
     372          << get_formatted_string (*this_doc, formatlistptr) << "\n"
     373          << "</tr>\n";
     374      this_doc ++;
     375    }
     376    textout << "</table>\n";
     377
     378    delete (formatlistptr);
    526379   
    527380    // output the footer
Note: See TracChangeset for help on using the changeset viewer.