Ignore:
Timestamp:
2000-08-25T12:57:05+12:00 (24 years ago)
Author:
davidb
Message:

Changes to support nested {If} and {Or} statements in macro files.
Also expansion of metadata if it includes further metadata. The two
can be intermixed -- metadata can include metadata that has {If}
and {Or} statements.

File:
1 edited

Legend:

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

    r1285 r1443  
    2626#include "formattools.h"
    2727#include "cgiutils.h"
     28#include "OIDtools.h"
     29
    2830#include <assert.h>
    2931
    3032// a few function prototypes
    31 static text_t format_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
     33static text_t format_string (const text_t& collection, recptproto* collectproto,
     34                 ResultDocInfo_t &docinfo, format_t *formatlistptr,
    3235                 const text_t &link, const text_t &icon,
    33                  const text_t &text, bool highlight);
     36                 const text_t &text, bool highlight, ostream& logout);
    3437
    3538static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
     
    269272      }
    270273      if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
     274
    271275    formatlistptr->nextptr = new format_t();
    272276    formatlistptr = formatlistptr->nextptr;
     
    324328  else return false;
    325329
    326   int curlycount = 0;
    327330  int commacount = 0;
    328331  text_t text;
     
    333336      if (here != end) text.push_back(*here);
    334337     
    335     } else if (*here == '{') {curlycount ++; text.push_back(*here);}
    336     else if (*here == '}' && curlycount > 0) {
    337       curlycount --;
    338       text.push_back(*here);
    339     }
    340 
    341     else if ((*here == ',' || *here == '}') && curlycount <= 0) {
     338    }
     339 
     340    else if (*here == ',' || *here == '}' || *here == '{') {
    342341
    343342      if (formatlistptr->command == comOr) {
    344343    // the {Or}{this, or this, or this, or this} statement
    345     // or'ed statements may be either [metadata] or plain text
    346344    format_t *or_ptr;
    347345   
     
    358356    }
    359357
    360     text_t::const_iterator beginbracket = text.begin();
    361     text_t::const_iterator endbracket = (text.end() - 1);
    362     if ((*beginbracket == '[') && (*endbracket == ']')) {
    363       // it's metadata
    364       text_t meta = substr (beginbracket+1, endbracket);
    365       parse_meta (meta, or_ptr, metadata, getParents);
    366 
    367     } else {
    368       parse_string (text, or_ptr, metadata, getParents);
    369     }
     358    if (!text.empty())
     359      {
     360        if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
     361      }
     362
     363    if (*here == '{')
     364      {
     365        // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
     366        // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
     367        // The latter can always be re-written:
     368        // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
     369       
     370        if (!text.empty()) // already used up allocated format_t
     371          {
     372        // => allocate new one for detected action
     373        or_ptr->nextptr = new format_t();
     374        or_ptr = or_ptr->nextptr;
     375          }
     376        if (!parse_action(++here, end, or_ptr, metadata, getParents))
     377          {
     378        return false;
     379          }
     380      }
     381    else
     382      {
     383        if (*here == '}') break;
     384      }
    370385    text.clear();
    371386
     
    374389    if (commacount == 0) {
    375390      // If decision only supports metadata at present
    376      
     391
    377392      // remove the surrounding square brackets
    378393      text_t::const_iterator beginbracket = text.begin();
     
    385400      }
    386401     
    387     } else if (commacount == 1) {
    388       formatlistptr->ifptr = new format_t();
    389       parse_string (text, formatlistptr->ifptr, metadata, getParents);
    390       commacount ++;
    391       text.clear();
     402    } else {
     403      format_t** nextlistptr = NULL;
     404      if (commacount == 1) {
     405          nextlistptr = &formatlistptr->ifptr;
     406      } else if (commacount == 2 ) {
     407        nextlistptr = &formatlistptr->elseptr;
     408      } else {
     409        return false;
     410      }
     411
     412      if (!text.empty()) {
     413        if (*nextlistptr == NULL) {
     414          *nextlistptr = new format_t();
     415        } else {
     416
     417          // skip to the end of any format_t statements already added
     418          while ((*nextlistptr)->nextptr != NULL)
     419          {
     420        nextlistptr = &(*nextlistptr)->nextptr;
     421          }
     422
     423          (*nextlistptr)->nextptr = new format_t();
     424          nextlistptr = &(*nextlistptr)->nextptr;
     425        }
     426
     427        if (!parse_string (text, *nextlistptr, metadata, getParents))
     428          {
     429        return false;
     430          }
     431        text.clear();
     432      }
    392433     
    393     } else if (commacount == 2) {
    394       formatlistptr->elseptr = new format_t();
    395       parse_string (text, formatlistptr->elseptr, metadata, getParents);
    396       commacount ++;
    397       text.clear();
     434      if (*here == '{')
     435        {
     436          if (*nextlistptr == NULL) {
     437        *nextlistptr = new format_t();
     438          } else {
     439        (*nextlistptr)->nextptr = new format_t();
     440        nextlistptr = &(*nextlistptr)->nextptr;
     441          }
     442
     443          if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
     444        {
     445          return false;
     446        }
     447        }
     448      else
     449        {
     450          if (*here == '}') break;
     451          commacount ++;
     452        }
    398453    }
    399454      }
    400       if (*here == '}') break;
    401455     
    402456    } else text.push_back(*here);
     
    493547}
    494548
    495 static text_t get_or (ResultDocInfo_t &docinfo, format_t *orptr,
     549static text_t get_or (const text_t& collection, recptproto* collectproto,
     550              ResultDocInfo_t &docinfo, format_t *orptr,
    496551              const text_t &link, const text_t &icon,
    497               const text_t &text, bool highlight) {
     552              const text_t &text, bool highlight,
     553              ostream& logout) {
    498554
    499555  text_t tmp;
    500556  while (orptr != NULL) {
    501557
    502     tmp = format_string (docinfo, orptr, link, icon, text, highlight);
     558    tmp = format_string (collection,collectproto, docinfo,orptr,
     559             link,icon,text,highlight, logout);
    503560    if (!tmp.empty()) return tmp;
    504561
     
    508565}
    509566
    510 static text_t get_if (ResultDocInfo_t &docinfo, const decision_t &decision,
     567static text_t get_if (const text_t& collection, recptproto* collectproto,
     568              ResultDocInfo_t &docinfo, const decision_t &decision,
    511569              format_t *ifptr, format_t *elseptr, const text_t &link,
    512               const text_t &icon, const text_t &text, bool highlight) {
     570              const text_t &icon, const text_t &text, bool highlight,
     571              ostream& logout)
     572{
    513573
    514574  // not much of a choice yet ...
     
    516576    if (get_meta (docinfo, decision.meta) != "") {
    517577      if (ifptr != NULL)
    518     return get_formatted_string (docinfo, ifptr, link, icon, text, highlight);
     578    return get_formatted_string (collection,collectproto, docinfo,ifptr,
     579                     link,icon,text,highlight, logout);
    519580    }
    520581    else {
    521582      if (elseptr != NULL)
    522     return get_formatted_string (docinfo, elseptr, link, icon, text, highlight);
     583    return get_formatted_string (collection,collectproto, docinfo,elseptr,
     584                     link,icon,text,highlight, logout);
    523585    }
    524586  }
     
    526588}
    527589
    528 text_t format_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
     590bool includes_metadata(const text_t& text)
     591{
     592  text_t::const_iterator here = text.begin();
     593  text_t::const_iterator end = text.end();
     594  while (here != end) {
     595    if (*here == '[') return true;
     596    here ++;
     597  }
     598
     599  return false;
     600}
     601
     602text_t format_string (const text_t& collection, recptproto* collectproto,
     603              ResultDocInfo_t &docinfo, format_t *formatlistptr,
    529604              const text_t &link, const text_t &icon,
    530               const text_t &text, bool highlight) {
     605              const text_t &text, bool highlight,
     606              ostream& logout) {
    531607
    532608  if (formatlistptr == NULL) return "";
     
    545621    return docinfo.result_num;
    546622  case comMeta:
    547     return get_meta (docinfo, formatlistptr->meta);
     623
     624    {
     625      const text_t& metavalue =  get_meta (docinfo, formatlistptr->meta);
     626
     627      if (includes_metadata(metavalue))
     628    {
     629      // text has embedded metadata in it => expand it
     630      FilterRequest_t request;
     631      FilterResponse_t response;
     632
     633      request.getParents = false;
     634     
     635      format_t *expanded_formatlistptr = new format_t();
     636      parse_formatstring (metavalue, expanded_formatlistptr,
     637                  request.fields, request.getParents);
     638     
     639      // retrieve metadata
     640      get_info(docinfo.OID, collection, request.fields, request.getParents,
     641           collectproto, response, logout);
     642     
     643      if (!response.docInfo.empty())
     644        {
     645          text_t expanded_metavalue
     646        = get_formatted_string(collection,collectproto,
     647                       response.docInfo[0],expanded_formatlistptr,
     648                       link,icon,highlight,
     649                       logout);
     650         
     651          return expanded_metavalue;
     652        }
     653      else
     654        {
     655          return metavalue;
     656        }
     657    }
     658      else
     659    {
     660      return metavalue;
     661    }
     662    }
    548663  case comDoc:
    549664    return text;
     
    555670    break;
    556671  case comIf:
    557     return get_if (docinfo, formatlistptr->decision, formatlistptr->ifptr,
    558            formatlistptr->elseptr, link, icon, text, highlight);
     672    return get_if (collection, collectproto,
     673           docinfo, formatlistptr->decision, formatlistptr->ifptr,
     674           formatlistptr->elseptr, link, icon, text, highlight,
     675           logout);
    559676  case comOr:
    560     return get_or (docinfo, formatlistptr->orptr, link, icon, text, highlight);
     677    return get_or (collection,collectproto, docinfo,formatlistptr->orptr,
     678           link,icon,text,highlight, logout);
    561679  }
    562680  return "";
     
    564682
    565683
    566 text_t get_formatted_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
    567                  const text_t &link, const text_t &icon) {
    568 
    569   text_t text;
     684
     685
     686text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     687                 ResultDocInfo_t& docinfo, format_t* formatlistptr,
     688                 const text_t& link, const text_t& icon,
     689                 const text_t& text, const bool highlight,
     690                 ostream& logout) {
    570691
    571692  text_t ft;
    572   while (formatlistptr != NULL) {
    573     ft += format_string (docinfo, formatlistptr, link, icon, text, false);
    574     formatlistptr = formatlistptr->nextptr;
    575   }
     693  while (formatlistptr != NULL)
     694    {
     695      ft += format_string (collection, collectproto, docinfo, formatlistptr,
     696               link, icon, text, highlight, logout);
     697      formatlistptr = formatlistptr->nextptr;
     698    }
     699
    576700  return ft;
    577701}
    578702
    579 
    580 text_t get_formatted_string (ResultDocInfo_t &docinfo, format_t *formatlistptr) {
     703text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     704                 ResultDocInfo_t &docinfo, format_t *formatlistptr,
     705                 const text_t &link, const text_t &icon,
     706                 const bool highlight,
     707                 ostream& logout) {
     708
     709  text_t text = "";
     710
     711  return get_formatted_string(collection, collectproto, docinfo, formatlistptr,
     712                              link, icon, text, highlight, logout);
     713}
     714
     715text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     716                 ResultDocInfo_t &docinfo, format_t *formatlistptr,
     717                 const text_t& text,
     718                 ostream& logout) {
    581719
    582720  text_t link = "<a href=\"_httpdocument_&cl=search&d=" + docinfo.OID + "\">";
    583721  text_t icon = "_icontext_";
    584   text_t text;
    585 
    586   text_t ft;
    587   while (formatlistptr != NULL) {
    588     ft += format_string (docinfo, formatlistptr, link, icon, text, false);
    589     formatlistptr = formatlistptr->nextptr;
    590   }
    591   return ft;
    592 }
    593 
    594 
    595 text_t get_formatted_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
    596                  const text_t &text) {
    597 
    598   text_t link = "<a href=\"_httpdocument_&cl=search&d=" + docinfo.OID + "\">";
    599   text_t icon = "_icontext_";
    600 
    601   text_t ft;
    602   while (formatlistptr != NULL) {
    603     ft += format_string (docinfo, formatlistptr, link, icon, text, false);
    604     formatlistptr = formatlistptr->nextptr;
    605   }
    606   return ft;
    607 }
    608 
    609 
    610 text_t get_formatted_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
    611                  const text_t &link, const text_t &icon, const text_t &text) {
    612 
    613   text_t ft;
    614   while (formatlistptr != NULL) {
    615     ft += format_string (docinfo, formatlistptr, link, icon, text, false);
    616     formatlistptr = formatlistptr->nextptr;
    617   }
    618   return ft;
    619 }
    620 
    621 text_t get_formatted_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
    622                  const text_t &link, const text_t &icon, bool highlight) {
    623 
    624   text_t text, ft;
    625   while (formatlistptr != NULL) {
    626     ft += format_string (docinfo, formatlistptr, link, icon, text, highlight);
    627     formatlistptr = formatlistptr->nextptr;
    628   }
    629   return ft;
    630 }
    631 
    632 text_t get_formatted_string (ResultDocInfo_t &docinfo, format_t *formatlistptr,
    633                  const text_t &link, const text_t &icon,
    634                  const text_t &text, bool highlight) {
    635 
    636   text_t ft;
    637   while (formatlistptr != NULL) {
    638     ft += format_string (docinfo, formatlistptr, link, icon, text, highlight);
    639     formatlistptr = formatlistptr->nextptr;
    640   }
    641   return ft;
    642 }
     722  bool highlight = false;
     723
     724  return get_formatted_string(collection, collectproto, docinfo, formatlistptr,
     725                  link, icon, text, highlight, logout);
     726}
     727
     728text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     729                 ResultDocInfo_t &docinfo, format_t *formatlistptr,
     730                 ostream& logout) {
     731
     732  text_t text = "";
     733
     734  return get_formatted_string(collection, collectproto, docinfo, formatlistptr,
     735                  text, logout);
     736}
     737
Note: See TracChangeset for help on using the changeset viewer.