Changeset 5787


Ignore:
Timestamp:
2003-11-05T15:06:39+13:00 (20 years ago)
Author:
davidb
Message:

Added functionality to format statements. In addition to 'parent(...):Metadata'
syntax, now supports 'sibling(...):Metadata' which retrieve multile values
of the given metadata value at that level in the doc.

Location:
trunk/gsdl/src/recpt
Files:
2 edited

Legend:

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

    r4972 r5787  
    3232
    3333// a few function prototypes
    34 
    3534static text_t format_string (const text_t& collection, recptproto* collectproto,
    3635                 ResultDocInfo_t &docinfo, displayclass &disp,
    37                  format_t *formatlistptr, text_tmap &options,
    38                  ostream& logout);
     36                 format_t *formatlistptr,
     37                 const text_t &link, const text_t &icon,
     38                 const text_t &text, bool highlight, ostream& logout);
    3939
    4040static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
    4141              format_t *formatlistptr, text_tset &metadata, bool &getParents);
    4242
    43 static text_t format_summary (const text_t& collection, recptproto* collectproto,
    44                   ResultDocInfo_t &docinfo, displayclass &disp,
    45                   text_tmap &options, ostream& logout);
     43text_t format_summary (const text_t& collection, recptproto* collectproto,
     44              ResultDocInfo_t &docinfo, displayclass &disp,
     45              const text_t &text, bool highlight,
     46               ostream& logout);
    4647
    4748
     
    5051  metacommand = mNone;
    5152  parentcommand = pNone;
    52   parentoptions.clear();
     53  functionoptions.clear();
    5354}
    5455
     
    7677  DocumentContents = true;
    7778  DocumentArrowsBottom = true;
    78   DocumentArrowsTop = false;
    7979  DocumentButtons.erase (DocumentButtons.begin(), DocumentButtons.end());
    8080  //  DocumentButtons.push_back ("Expand Text");
     
    8686  formatstrings.erase (formatstrings.begin(), formatstrings.end());
    8787  DocumentUseHTML = false;
    88   AllowExtendedOptions = false;
    8988}
    9089
     
    488487  else if (com == "All") {
    489488    metaoption.parentcommand = pAll;
    490     metaoption.parentoptions = op;
     489    metaoption.functionoptions = op;
     490  }
     491}
     492
     493
     494static void get_sibling_options (text_t &instring, metadata_t &metaoption) {
     495
     496  assert (instring.size() > 8);
     497  if (instring.size() <= 8) return;
     498
     499  text_t meta, com, op;
     500  bool inbraces = false;
     501  bool inquotes = false;
     502  bool foundcolon = false;
     503  text_t::const_iterator here = instring.begin()+7;
     504  text_t::const_iterator end = instring.end();
     505  while (here != end) {
     506    if (*here == '(') inbraces = true;
     507    else if (*here == ')') inbraces = false;
     508    else if (*here == '\'' && !inquotes) inquotes = true;
     509    else if (*here == '\'' && inquotes) inquotes = false;
     510    else if (*here == ':' && !inbraces) foundcolon = true;
     511    else if (foundcolon) meta.push_back (*here);
     512    else if (inquotes) op.push_back (*here);
     513    else com.push_back (*here);
     514    here ++;
     515  }
     516
     517  instring = meta;
     518
     519  if (com.empty()) {
     520    metaoption.functionoptions = " ";
     521  }
     522  else {
     523    metaoption.functionoptions = op;
    491524  }
    492525}
     
    496529
    497530  if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
    498     metaoption.metacommand = mCgiSafe;
     531    metaoption.metacommand |= mCgiSafe;
    499532    meta = substr (meta.begin()+8, meta.end());
    500533  }
    501534
    502535  if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
     536    metaoption.metacommand |= mParent;
    503537    getParents = true;
    504538    get_parent_options (meta, metaoption);
     539  }
     540  else if (meta.size() > 8 && (substr (meta.begin(), meta.begin()+7) == "sibling")) {
     541    metaoption.metacommand |= mSibling;
     542    get_sibling_options (meta, metaoption);
    505543  }
    506544
     
    541579    formatlistptr->command = comSummary;
    542580
    543   else if (meta == "DocImage")
    544     formatlistptr->command = comImage;
    545 
    546   else if (meta == "DocTOC")
    547     formatlistptr->command = comTOC;
    548 
    549   else if (meta == "DocumentButtonDetach")
    550      formatlistptr->command = comDocumentButtonDetach;
    551  
    552   else if (meta == "DocumentButtonHighlight")
    553      formatlistptr->command = comDocumentButtonHighlight;
    554  
    555   else if (meta == "DocumentButtonExpandContents")
    556     formatlistptr->command = comDocumentButtonExpandContents;
    557 
    558   else if (meta == "DocumentButtonExpandText")
    559      formatlistptr->command = comDocumentButtonExpandText;
    560  
    561581  else {
    562582    formatlistptr->command = comMeta;
     
    820840  case pNone:
    821841    {
    822       text_t classifier_metaname = docinfo.classifier_metadata_type;
    823        int metaname_index
    824     = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
    825       text_t metadata_item = docinfo.metadata[meta.metaname].values[metaname_index];
    826      
    827       if (meta.metaname == "Date")
    828     return format_date (metadata_item);
    829       else if (meta.metaname == "Language")
    830     return iso639(metadata_item);
    831       if (meta.metacommand == mCgiSafe)
    832     return cgi_safe (metadata_item);
    833       else return metadata_item;
     842      if (meta.metacommand & mSibling) {
     843    text_t tmp;
     844    bool first = true;
     845
     846    MetadataInfo_t& metaname_rec = docinfo.metadata[meta.metaname];
     847
     848    const int start_i=0;
     849    const int end_i = metaname_rec.values.size()-1;
     850
     851    for (int i=start_i; i<=end_i; i++) {
     852      if (!first) tmp += meta.functionoptions;
     853      if (meta.metaname == "Date") tmp += format_date (metaname_rec.values[i]);
     854      else if (meta.metaname == "Language") tmp += iso639(metaname_rec.values[i]);
     855      else tmp += metaname_rec.values[i];
     856      first = false;
     857    }
     858
     859    if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
     860    else return tmp;
     861
     862      }
     863      else {
     864
     865    text_t classifier_metaname = docinfo.classifier_metadata_type;
     866    int metaname_index
     867      = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
     868    text_t metadata_item = docinfo.metadata[meta.metaname].values[metaname_index];
     869   
     870    if (meta.metaname == "Date")
     871      return format_date (metadata_item);
     872    else if (meta.metaname == "Language")
     873      return iso639(metadata_item);
     874    if (meta.metacommand & mCgiSafe)
     875      return cgi_safe (metadata_item);
     876    else return metadata_item;
     877      }
    834878    }
     879
    835880
    836881  case pImmediate:
     
    844889    break;
    845890
     891
    846892  case pTop:
    847893    if (parent != NULL) {
     
    850896      if (meta.metaname == "Date")
    851897    return format_date (parent->values[0]);
    852       if (meta.metacommand == mCgiSafe)
     898      if (meta.metacommand & mCgiSafe)
    853899    return cgi_safe (parent->values[0]);
    854900      else return parent->values[0];
     
    859905    MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
    860906    if (parent != NULL) {
     907
    861908      text_tarray tmparray;
    862909      while (parent != NULL) {
     
    869916      text_tarray::reverse_iterator end = tmparray.rend();
    870917      while (here != end) {
    871     if (!first) tmp += meta.parentoptions;
     918    if (!first) tmp += meta.functionoptions;
    872919    if (meta.metaname == "Date") tmp += format_date (*here);
    873920    else tmp += *here;
     
    875922    here ++;
    876923      }
    877       if (meta.metacommand == mCgiSafe) return cgi_safe (tmp);
     924      if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
    878925      else return tmp;
    879926    }
     
    884931static text_t get_or (const text_t& collection, recptproto* collectproto,
    885932              ResultDocInfo_t &docinfo, displayclass &disp,
    886               format_t *orptr, text_tmap &options,
     933              format_t *orptr,
     934              const text_t &link, const text_t &icon,
     935              const text_t &text, bool highlight,
    887936              ostream& logout) {
    888937
     
    891940
    892941    tmp = format_string (collection,collectproto, docinfo, disp, orptr,
    893              options, logout);
     942             link, icon, text, highlight, logout);
    894943    if (!tmp.empty()) return tmp;
    895944
     
    902951              ResultDocInfo_t &docinfo, displayclass &disp,
    903952              const decision_t &decision,
    904               format_t *ifptr, format_t *elseptr,
    905               text_tmap &options, ostream& logout)
     953              format_t *ifptr, format_t *elseptr, const text_t &link,
     954              const text_t &icon, const text_t &text, bool highlight,
     955              ostream& logout)
    906956{
    907957
     
    912962      if (ifptr != NULL)
    913963    return get_formatted_string (collection,collectproto, docinfo, disp, ifptr,
    914                      options, logout);
     964                     link, icon, text, highlight, logout);
    915965    }
    916966    else {
    917967      if (elseptr != NULL)
    918968    return get_formatted_string (collection,collectproto, docinfo, disp, elseptr,
    919                      options, logout);
     969                     link, icon, text, highlight, logout);
    920970    }
    921971  }
     
    935985      if (ifptr != NULL)
    936986    return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
    937                      options, logout);
     987                     link, icon, text, highlight, logout);
    938988    } else {
    939989      if (elseptr != NULL)
    940990    return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
    941                      options, logout);
     991                     link, icon, text, highlight, logout);
    942992    }
    943993  }
     
    9581008}
    9591009
    960 static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
    961                   recptproto* collectproto, ResultDocInfo_t &docinfo,
    962                   displayclass &disp, text_tmap &options,
    963                   ostream &logout) {
    964      
    965    if (includes_metadata(metavalue))
    966       {
    967      // text has embedded metadata in it => expand it
    968      FilterRequest_t request;
    969      FilterResponse_t response;
    970 
    971      request.getParents = false;
    972      
    973      format_t *expanded_formatlistptr = new format_t();
    974      parse_formatstring (metavalue, expanded_formatlistptr,
    975                  request.fields, request.getParents);
    976      
    977      // retrieve metadata
    978      get_info(docinfo.OID, collection, request.fields, request.getParents,
    979           collectproto, response, logout);
    980      
    981      if (!response.docInfo.empty())
    982         {
    983            text_t expanded_metavalue
    984           = get_formatted_string(collection, collectproto,
    985                      response.docInfo[0], disp, expanded_formatlistptr,
    986                      options, logout);
    987          
    988            return expanded_metavalue;
    989         }
    990      else
    991         {
    992            return metavalue;
    993         }
    994       }
    995    else
    996       {
    997      return metavalue;
    998       }
    999 }
     1010
    10001011
    10011012text_t format_string (const text_t& collection, recptproto* collectproto,
    10021013              ResultDocInfo_t &docinfo, displayclass &disp,
    1003               format_t *formatlistptr, text_tmap &options,
     1014              format_t *formatlistptr,
     1015              const text_t &link, const text_t &icon,
     1016              const text_t &text, bool highlight,
    10041017              ostream& logout) {
    10051018
     
    10071020
    10081021  switch (formatlistptr->command) {
    1009      case comText:
    1010     return formatlistptr->text;
    1011      case comLink:
    1012     return options["link"];
    1013      case comEndLink:
    1014     if (options["link"].empty()) return "";
    1015     else return "</a>";
    1016      case comHref:
    1017     return get_href(options["link"]);
    1018      case comIcon:
    1019     return options["icon"];
    1020      case comNum:
    1021     return docinfo.result_num;
    1022      case comRel: //if [RelatedDocuments] appears in format string, collect relation data
    1023     return get_related_docs(collection, collectproto, docinfo, logout);
    1024      case comSummary:
    1025     return format_summary(collection, collectproto, docinfo, disp, options, logout);
    1026      case comMeta:
     1022  case comText:
     1023    return formatlistptr->text;
     1024  case comLink:
     1025    return link;
     1026  case comEndLink:
     1027    if (link.empty()) return "";
     1028    else return "</a>";
     1029  case comHref:
     1030    return get_href(link);
     1031  case comIcon:
     1032    return icon;
     1033  case comNum:
     1034    return docinfo.result_num;
     1035  case comRel: //if [RelatedDocuments] appears in format string, collect relation data
     1036    return get_related_docs(collection, collectproto, docinfo, logout);
     1037  case comSummary:
     1038    return format_summary(collection,collectproto,docinfo,disp,text,highlight,logout);
     1039  case comMeta:
     1040
     1041    {
     1042      const text_t& metavalue =  get_meta (docinfo, formatlistptr->meta);
     1043
     1044      if (includes_metadata(metavalue))
    10271045    {
    1028        const text_t& metavalue =  get_meta (docinfo, formatlistptr->meta);
    1029        return expand_metadata(metavalue, collection, collectproto, docinfo, disp, options, logout);
     1046      // text has embedded metadata in it => expand it
     1047      FilterRequest_t request;
     1048      FilterResponse_t response;
     1049
     1050      request.getParents = false;
     1051     
     1052      format_t *expanded_formatlistptr = new format_t();
     1053      parse_formatstring (metavalue, expanded_formatlistptr,
     1054                  request.fields, request.getParents);
     1055     
     1056      // retrieve metadata
     1057      get_info(docinfo.OID, collection, request.fields, request.getParents,
     1058           collectproto, response, logout);
     1059     
     1060      if (!response.docInfo.empty())
     1061        {
     1062          text_t expanded_metavalue
     1063        = get_formatted_string(collection, collectproto,
     1064                       response.docInfo[0], disp, expanded_formatlistptr,
     1065                       link, icon, highlight, logout);
     1066         
     1067          return expanded_metavalue;
     1068        }
     1069      else
     1070        {
     1071          return metavalue;
     1072        }
    10301073    }
    1031      case comDoc:
    1032     return options["text"];
    1033      case comImage:
    1034     return expand_metadata(options["DocImage"], collection, collectproto, docinfo, disp, options, logout);
    1035      case comTOC:
    1036     return options["DocTOC"];
    1037      case comDocumentButtonDetach:
    1038     return options["DocumentButtonDetach"];
    1039      case comDocumentButtonHighlight:
    1040     return options["DocumentButtonHighlight"];
    1041      case comDocumentButtonExpandContents:
    1042     return options["DocumentButtonExpandContents"];
    1043      case comDocumentButtonExpandText:
    1044     return options["DocumentButtonExpandText"];
    1045      case comHighlight:
    1046     if (options["highlight"] == "1") return "<b>";
    1047     break;
    1048      case comEndHighlight:
    1049     if (options["highlight"] == "1") return "</b>";
    1050     break;
    1051      case comIf:
    1052     return get_if (collection, collectproto, docinfo, disp,
    1053                formatlistptr->decision, formatlistptr->ifptr,
    1054                formatlistptr->elseptr, options, logout);
    1055      case comOr:
    1056     return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
    1057                options, logout);
     1074      else
     1075    {
     1076      return metavalue;
     1077    }
     1078    }
     1079  case comDoc:
     1080    return text;
     1081  case comHighlight:
     1082    if (highlight) return "<b>";
     1083    break;
     1084  case comEndHighlight:
     1085    if (highlight) return "</b>";
     1086    break;
     1087  case comIf:
     1088    return get_if (collection, collectproto, docinfo, disp,
     1089           formatlistptr->decision, formatlistptr->ifptr,
     1090           formatlistptr->elseptr, link, icon, text, highlight,
     1091           logout);
     1092  case comOr:
     1093    return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
     1094           link, icon, text, highlight, logout);
    10581095  }
    10591096  return "";
     1097}
     1098
     1099
     1100
     1101
     1102text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     1103                 ResultDocInfo_t& docinfo, displayclass &disp,
     1104                 format_t* formatlistptr,
     1105                 const text_t& link, const text_t& icon,
     1106                 const text_t& text, const bool highlight,
     1107                 ostream& logout) {
     1108
     1109  text_t ft;
     1110  while (formatlistptr != NULL)
     1111    {
     1112      ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
     1113               link, icon, text, highlight, logout);
     1114      formatlistptr = formatlistptr->nextptr;
     1115    }
     1116
     1117  return ft;
    10601118}
    10611119
    10621120text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
    10631121                 ResultDocInfo_t &docinfo, displayclass &disp,
    1064                  format_t *formatlistptr, text_tmap &options,
     1122                 format_t *formatlistptr,
     1123                 const text_t &link, const text_t &icon,
     1124                 const bool highlight,
    10651125                 ostream& logout) {
    10661126
    1067    text_t ft;
    1068    while (formatlistptr != NULL)
    1069       {
    1070      ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
    1071                   options, logout);
    1072      formatlistptr = formatlistptr->nextptr;
    1073       }
    1074    
    1075    return ft;
     1127  text_t text = "";
     1128
     1129  return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
     1130                              link, icon, text, highlight, logout);
     1131}
     1132
     1133text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     1134                 ResultDocInfo_t &docinfo, displayclass &disp,
     1135                 format_t *formatlistptr,
     1136                 const text_t& text,
     1137                 ostream& logout) {
     1138
     1139  text_t link = "<a href=\"_httpdocument_&cl=search&d=" + docinfo.OID + "\">";
     1140  text_t icon = "_icontext_";
     1141  bool highlight = false;
     1142
     1143  return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
     1144                  link, icon, text, highlight, logout);
     1145}
     1146
     1147text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
     1148                 ResultDocInfo_t &docinfo, displayclass &disp,
     1149                 format_t *formatlistptr,
     1150                 ostream& logout) {
     1151
     1152  text_t text = "";
     1153
     1154  return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
     1155                  text, logout);
    10761156}
    10771157
     
    10821162
    10831163text_t format_summary (const text_t& collection, recptproto* collectproto,
    1084                ResultDocInfo_t &docinfo, displayclass &disp,
    1085                text_tmap &options, ostream& logout) {
     1164              ResultDocInfo_t &docinfo, displayclass &disp,
     1165              const text_t &text, bool highlight,
     1166              ostream& logout) {
    10861167
    10871168  // GRB: added code here to ensure that the cstr (and other collections)
     
    10951176
    10961177  text_t textToSummarise, query;
    1097   if(options["text"].empty()) { // get document text
    1098      DocumentRequest_t docrequest;
    1099      DocumentResponse_t docresponse;
    1100      comerror_t err;
    1101      docrequest.OID = docinfo.OID;
    1102      collectproto->get_document (collection, docrequest, docresponse, err, logout);
    1103      textToSummarise = docresponse.doc;
     1178  if(text.empty()) { // get document text
     1179      DocumentRequest_t docrequest;
     1180      DocumentResponse_t docresponse;
     1181      comerror_t err;
     1182      docrequest.OID = docinfo.OID;
     1183      collectproto->get_document (collection, docrequest, docresponse, err, logout);
     1184      textToSummarise = docresponse.doc;
    11041185  } else // in practice, this would not happen, because text is only
    11051186         // loaded with the [Text] command
    1106      textToSummarise = options["text"];
     1187      textToSummarise = text;
    11071188  disp.expandstring("_cgiargq_",query);
    11081189  return summarise(textToSummarise,query,80);
  • trunk/gsdl/src/recpt/formattools.h

    r4972 r5787  
    3939enum pcommand_t {pNone, pImmediate, pTop, pAll};
    4040enum dcommand_t {dMeta, dText};
    41 enum mcommand_t {mNone, mCgiSafe};
     41enum mcommand_t {mNone=0, mCgiSafe=1, mParent=2, mSibling=4};
    4242
    4343struct metadata_t {
     
    4646
    4747  text_t metaname;
    48   mcommand_t metacommand;
     48  int metacommand; // used to hold flags from mcommand_t
    4949  pcommand_t parentcommand;
    50   text_t parentoptions;
     50  text_t functionoptions;
    5151};
    5252
Note: See TracChangeset for help on using the changeset viewer.