root/main/trunk/greenstone2/runtime-src/src/recpt/formattools.cpp @ 24226

Revision 24226, 61.0 KB (checked in by ak19, 9 years ago)

Still on ticket 449. Now srclink_file metadata (contains an underscore that makes things difficult for GS3) is renamed to srclinkFile. Related commits are in perllib and GS3's default/transform/config_format.xsl and Action.java.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * formattools.cpp --
4 * Copyright (C) 1999  The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "formattools.h"
27#include "cgiutils.h"
28#include "gsdltools.h"
29#include "recptprototools.h"
30#include "OIDtools.h"
31#include "summarise.h"
32
33#include <assert.h>
34
35static bool metadata_wrap = false;
36static text_t metadata_wrap_type = "";
37
38// a few function prototypes
39
40static text_t format_string (const text_t& collection, recptproto* collectproto,
41                 ResultDocInfo_t &docinfo, displayclass &disp,
42                 format_t *formatlistptr, text_tmap &options,
43                 ostream& logout);
44
45static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
46              format_t *formatlistptr, text_tset &metadata, bool &getParents);
47
48static text_t format_summary (const text_t& collection, recptproto* collectproto,
49                  ResultDocInfo_t &docinfo, displayclass &disp,
50                  text_tmap &options, ostream& logout);
51static text_t format_text (const text_t& collection, recptproto* collectproto,
52                  ResultDocInfo_t &docinfo, displayclass &disp,
53                  text_tmap &options, ostream& logout);
54
55static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
56                  recptproto* collectproto, ResultDocInfo_t &docinfo,
57                  displayclass &disp, text_tmap &options,
58                  ostream &logout);
59
60
61void metadata_t::clear() {
62  metaname.clear();
63  metacommand = mNone;
64  mqualifier.parent  = pNone;
65  mqualifier.sibling = sNone;
66  mqualifier.child   = cNone;
67  pre_tree_traverse.clear();
68  parentoptions.clear();
69  siblingoptions.clear();
70  childoptions.clear();
71}
72
73void decision_t::clear() {
74  command = dMeta;
75  meta.clear();
76  text.clear();
77}
78
79format_t::~format_t()
80{
81  if (nextptr != NULL) delete nextptr;
82  if (ifptr != NULL) delete ifptr;
83  if (elseptr != NULL) delete elseptr;
84  if (orptr != NULL) delete orptr;
85}
86
87void format_t::clear() {
88  command = comText;
89  decision.clear();
90  text.clear();
91  meta.clear();
92  nextptr = NULL;
93  ifptr = NULL;
94  elseptr = NULL;
95  orptr = NULL;
96}
97
98void formatinfo_t::clear() {
99  DocumentImages = false;
100  DocumentTitles = true;
101  DocumentHeading = "{Or}{[parent(Top):Title],[Title],untitled}<br>";
102  DocumentContents = true;
103  DocumentArrowsBottom = true;
104  DocumentArrowsTop = false;
105  DocumentSearchResultLinks = false;
106  DocumentButtons.erase (DocumentButtons.begin(), DocumentButtons.end());
107  //  DocumentButtons.push_back ("Expand Text");
108  //  DocumentButtons.push_back ("Expand Contents");
109  DocumentButtons.push_back ("Detach");
110  DocumentButtons.push_back ("Highlight");
111  RelatedDocuments = "";
112  DocumentText = "[Text]";
113  formatstrings.erase (formatstrings.begin(), formatstrings.end());
114  DocumentUseHTML = false;
115  AllowExtendedOptions = false;
116}
117
118// simply checks to see if formatstring begins with a <td> tag
119bool is_table_content (const text_t &formatstring) {
120  text_t::const_iterator here = formatstring.begin();
121  text_t::const_iterator end = formatstring.end();
122 
123  while (here != end) {
124    if (*here != ' ') {
125      if ((*here == '<') && ((here+3) < end)) {
126    if ((*(here+1) == 't' || *(here+1) == 'T') &&
127        (*(here+2) == 'd' || *(here+2) == 'D') &&
128        (*(here+3) == '>' || *(here+3) == ' '))
129      return true;
130      } else return false;
131    }
132    ++here;
133  }
134  return false;
135}
136
137bool is_table_content (const format_t *formatlistptr) {
138
139  if (formatlistptr == NULL) return false;
140 
141  if (formatlistptr->command == comText)
142    return is_table_content (formatlistptr->text);
143   
144  return false;
145}
146
147// returns false if key isn't in formatstringmap
148bool get_formatstring (const text_t &key, const text_tmap &formatstringmap,
149               text_t &formatstring) {
150
151  formatstring.clear();
152  text_tmap::const_iterator it = formatstringmap.find(key);
153  if (it == formatstringmap.end()) return false;
154  formatstring = (*it).second;
155  return true;
156}
157
158// tries to find "key1key2" then "key1" then "key2"
159bool get_formatstring (const text_t &key1, const text_t &key2, 
160               const text_tmap &formatstringmap,
161               text_t &formatstring) {
162
163  formatstring.clear();
164  text_tmap::const_iterator it = formatstringmap.find(key1 + key2);
165  if (it != formatstringmap.end()) {
166    formatstring = (*it).second;
167    return true;
168  }
169  it = formatstringmap.find(key1);
170  if (it != formatstringmap.end()) {
171    formatstring = (*it).second;
172    return true;
173  }
174  it = formatstringmap.find(key2);
175  if (it != formatstringmap.end()) {
176    formatstring = (*it).second;
177    return true;
178  }
179  return false;
180}
181
182
183text_t remove_namespace(const text_t &meta_name) {
184  text_t::const_iterator end = meta_name.end();
185  text_t::const_iterator it = findchar(meta_name.begin(), end, '.');
186  if (it != end) {
187    return substr(it+1, end);
188  }
189
190  return meta_name;
191
192}
193// returns a date of form _format:date_(year, month, day)
194// input is date of type yyyy-?mm-?dd
195// at least the year must be present in date
196text_t format_date (const text_t &date) {
197
198  if (date.size() < 4) return "";
199
200  text_t::const_iterator datebegin = date.begin();
201
202  text_t year = substr (datebegin, datebegin+4);
203  int chars_seen_so_far = 4;
204  if (chars_seen_so_far == date.size()) return "_format:date_("+year+")";
205
206  if (date[chars_seen_so_far] == '-') ++chars_seen_so_far ;
207  if (date.size() < chars_seen_so_far+2) return "_format:date_("+year+")";
208 
209  text_t month = substr (datebegin+chars_seen_so_far, datebegin+chars_seen_so_far+2);
210  int imonth = month.getint();
211  if (imonth <= 0 || imonth > 12) return "_format:date_("+year+")";
212 
213  chars_seen_so_far += 2;
214  if (chars_seen_so_far == date.size()) return "_format:date_("+year+","+month+")";
215
216  if (date[chars_seen_so_far] == '-') ++chars_seen_so_far ;
217  if (date.size() < chars_seen_so_far+2) return "_format:date_("+year+","+month+")";
218
219  text_t day = substr (datebegin+chars_seen_so_far, datebegin+chars_seen_so_far+2);
220  if (day[0] == '0') day = substr (day.begin()+1, day.end());
221  int iday = day.getint();
222  if (iday <= 0 || iday > 31) return "_format:date_("+year+","+month+")";
223   
224  return "_format:date_("+year+","+month+","+day+")";
225
226
227// converts an iso639 language code to its English equivalent
228// should we be checking that the macro exists??
229text_t iso639 (const text_t &langcode) {
230  if (langcode.empty()) return "";
231  return "_iso639:iso639"+langcode+"_";
232}
233
234
235text_t get_href (const text_t &link) {
236
237  text_t href;
238
239  text_t::const_iterator here = findchar(link.begin(), link.end(), '"');
240  text_t::const_iterator end = link.end();
241  if (here == end) return g_EmptyText;
242 
243  ++here;
244  while (here != end) {
245    if (*here == '"') break;
246    href.push_back(*here);
247    ++here;
248  }
249
250  return href;
251}
252
253//this function gets the information associated with the relation
254//metadata for the document associated with 'docinfo'. This relation
255//metadata consists of a line of pairs containing 'collection, document OID'
256//(this is the OID of the document related to the current document, and
257//the collection the related document belongs to). For each of these pairs
258//the title metadata is obtained and then an html link between the title
259//of the related doc and the document's position (the document will be
260//found in "<a href=\"_httpdocument_&c=collection&cl=search&d=OID">
261//(where collection is the related documents collection, and OID is the
262//related documents OID).  A list of these html links are made for as many
263//related documents as there are. This list is then returned. If there are
264//no related documents available for the current document then the string
265//'.. no related documents .. ' is returned.
266text_t get_related_docs(const text_t& collection, recptproto* collectproto,
267            ResultDocInfo_t &docinfo, ostream& logout){
268 
269  text_tset metadata;
270
271  //insert the metadata we wish to collect
272  metadata.insert("dc.Relation");
273  metadata.insert("Title"); 
274  metadata.insert("Subject"); //for emails, where title data doesn't apply
275 
276  FilterResponse_t response;
277  text_t relation = ""; //string for displaying relation metadata
278  text_t relationTitle = ""; //the related documents Title (or subject)
279  text_t relationOID = ""; //the related documents OID 
280
281  //get the information associated with the metadata for current doc
282  if (get_info (docinfo.OID, collection, "", metadata,
283        false, collectproto, response, logout)) {
284   
285    //if the relation metadata exists, store for displaying
286    if(!response.docInfo[0].metadata["dc.Relation"].values.empty()){
287      relationOID += response.docInfo[0].metadata["dc.Relation"].values[0];
288
289      //split relation data into pairs of collectionname,ID number
290      text_tarray relationpairs;
291      splitchar (relationOID.begin(), relationOID.end(), ' ', relationpairs);
292     
293      text_tarray::const_iterator currDoc = relationpairs.begin(); 
294      text_tarray::const_iterator lastDoc = relationpairs.end();
295
296      //iterate through the pairs to split and display
297      while(currDoc != lastDoc){
298   
299    //split pairs into collectionname and ID
300    text_tarray relationdata;
301    splitchar ((*currDoc).begin(), (*currDoc).end(), ',', relationdata);
302   
303    //get first element in the array (collection)
304    text_tarray::const_iterator doc_data = relationdata.begin();
305    text_t document_collection = *doc_data;
306    ++doc_data; //increment to get next item in array (oid)
307    text_t document_OID = *doc_data;
308   
309    //create html link to related document
310    relation += "<a href=\"_httpdocument_&amp;c=" + document_collection;
311    relation += "&amp;cl=search&amp;d=" + document_OID;
312       
313    //get the information associated with the metadata for related doc
314    if (get_info (document_OID, document_collection, "", metadata,
315              false, collectproto, response, logout)) {
316     
317      //if title metadata doesn't exist, collect subject metadata
318      //if that doesn't exist, just call it 'related document'
319      if (!response.docInfo[0].metadata["Title"].values[0].empty())
320        relationTitle = response.docInfo[0].metadata["Title"].values[0];
321      else if (!response.docInfo[0].metadata["Subject"].values.empty())
322        relationTitle = response.docInfo[0].metadata["Subject"].values[0];
323      else relationTitle =  "RELATED DOCUMENT";
324     
325    }
326   
327    //link the related document's title to its page
328    relation += "\">" + relationTitle + "</a>";
329    relation += "  (" + document_collection + ")<br>";
330   
331    ++currDoc;
332      }
333    }
334   
335  }
336
337  if(relation.empty()) //no relation data for documnet
338    relation = ".. no related documents .. ";
339
340  return relation;
341}
342
343
344
345static void get_parent_options (text_t &instring, metadata_t &metaoption) {
346
347  assert (instring.size() > 7);
348  if (instring.size() <= 7) return;
349
350  text_t meta, com, op;
351  bool inbraces = false;
352  bool inquotes = false;
353  bool foundcolon = false;
354  text_t::const_iterator here = instring.begin()+6;
355  text_t::const_iterator end = instring.end();
356  while (here != end) {
357    if (foundcolon) meta.push_back (*here);
358    else if (*here == '(') inbraces = true;
359    else if (*here == ')') inbraces = false;
360    else if (*here == '\'' && !inquotes) inquotes = true;
361    else if (*here == '\'' && inquotes) inquotes = false;
362    else if (*here == ':' && !inbraces) foundcolon = true;
363    else if (inquotes) op.push_back (*here);
364    else com.push_back (*here);
365    ++here;
366  }
367
368  instring = meta;
369  if (com.empty())
370    metaoption.mqualifier.parent = pImmediate;
371  else if (com == "Top")
372    metaoption.mqualifier.parent = pTop;
373  else if (com == "All") {
374    metaoption.mqualifier.parent = pAll;
375    metaoption.parentoptions = op;
376  }
377}
378
379
380static void get_sibling_options (text_t &instring, metadata_t &metaoption) {
381
382  assert (instring.size() > 8);
383  if (instring.size() <= 8) return;
384  text_t meta, com, op;
385  bool inbraces = false;
386  bool inquotes = false;
387  bool foundcolon = false;
388  text_t::const_iterator here = instring.begin()+7;
389  text_t::const_iterator end = instring.end();
390  while (here != end) {
391    if (foundcolon) meta.push_back (*here);
392    else if (*here == '(') inbraces = true;
393    else if (*here == ')') inbraces = false;
394    else if (*here == '\'' && !inquotes) inquotes = true;
395    else if (*here == '\'' && inquotes) inquotes = false;
396    else if (*here == ':' && !inbraces) foundcolon = true;   
397    else if (inquotes) op.push_back (*here);
398    else com.push_back (*here);
399    ++here;
400  }
401
402  instring = meta;
403  metaoption.siblingoptions.clear();
404
405  if (com.empty()) {
406    metaoption.mqualifier.sibling = sAll;
407    metaoption.siblingoptions = " ";
408  }
409  else if (com == "first") {
410    metaoption.mqualifier.sibling = sNum;
411    metaoption.siblingoptions = "0";
412  }
413  else if (com == "last") {
414    metaoption.mqualifier.sibling = sNum;
415    metaoption.siblingoptions = "-2"; // == last
416  }
417  else if (com.getint()>0) {
418    metaoption.mqualifier.sibling = sNum;
419    int pos = com.getint()-1;
420    metaoption.siblingoptions +=pos;
421  }
422  else {
423    metaoption.mqualifier.sibling = sAll;
424    metaoption.siblingoptions = op;
425  }
426}
427
428static void get_child_options (text_t &instring, metadata_t &metaoption) {
429
430  assert (instring.size() > 6);
431  if (instring.size() <= 6) return;
432  text_t meta, com, op;
433  bool inbraces = false;
434  bool inquotes = false;
435  bool foundcolon = false;
436  text_t::const_iterator here = instring.begin()+5;
437  text_t::const_iterator end = instring.end();
438  while (here != end) {
439    if (foundcolon) meta.push_back (*here);
440    else if (*here == '(') inbraces = true;
441    else if (*here == ')') inbraces = false;
442    else if (*here == '\'' && !inquotes) inquotes = true;
443    else if (*here == '\'' && inquotes) inquotes = false;
444    else if (*here == ':' && !inbraces) foundcolon = true;
445    else if (inquotes) op.push_back (*here);
446    else com.push_back (*here);
447    ++here;
448  }
449
450  instring = meta;
451  if (com.empty()) {
452    metaoption.mqualifier.child = cAll;
453    metaoption.childoptions = " ";
454  }
455  else if (com == "first") {
456    metaoption.mqualifier.child = cNum;
457    metaoption.childoptions = ".fc";
458  }
459  else if (com == "last") {
460    metaoption.mqualifier.child = cNum;
461    metaoption.childoptions = ".lc";
462  }
463  else if (com.getint()>0) {
464    metaoption.mqualifier.child = cNum;
465    metaoption.childoptions = "."+com;
466  }
467  else {
468    metaoption.mqualifier.child = cAll;
469    metaoption.childoptions = op;
470  }
471}
472
473
474static void get_truncate_options (text_t &instring, metadata_t &metaoption)
475{
476  assert (instring.size() > ((text_t) "truncate").size());
477  if (instring.size() <= ((text_t) "truncate").size()) return;
478  text_t meta, com;
479  bool inbraces = false;
480  bool foundcolon = false;
481  text_t::const_iterator here = instring.begin() + ((text_t) "truncate").size();
482  text_t::const_iterator end = instring.end();
483  while (here != end) {
484    if (foundcolon) meta.push_back (*here);
485    else if (*here == '(') inbraces = true;
486    else if (*here == ')') inbraces = false;
487    else if (*here == ':' && !inbraces) foundcolon = true;   
488    else com.push_back (*here);
489    ++here;
490  }
491
492  instring = meta;
493
494  if (!com.empty())
495  {
496    metaoption.siblingoptions = com;
497  }
498  else
499  {
500    // Default is 100 characters if not specified
501    metaoption.siblingoptions = "100";
502  }
503}
504
505
506
507static void parse_meta (text_t &meta, metadata_t &metaoption,
508            text_tset &metadata, bool &getParents) {
509
510  // Look for the various format statement modifiers
511  // This needs to be done in a loop otherwise not all combinations will be accepted, but actually the order
512  //   is irrelevant because this is not stored in metaoption.metacommand anyway
513  bool keep_trying = true;
514  while (keep_trying)
515  {
516    keep_trying = false;
517
518    if (meta.size() > ((text_t) "cgisafe:").size() && starts_with(meta, "cgisafe:"))
519    {
520      metaoption.metacommand |= mCgiSafe;
521      meta = substr(meta.begin() + ((text_t) "cgisafe:").size(), meta.end());
522      keep_trying = true;
523    }
524    if (meta.size() > ((text_t) "format:").size() && starts_with(meta, "format:"))
525    {   
526      metaoption.metacommand |= mSpecial;
527      meta = substr(meta.begin() + ((text_t) "format:").size(), meta.end());
528      keep_trying = true;
529    }
530
531    // New "truncate" special formatting option
532    if (meta.size() > ((text_t) "truncate").size() && starts_with(meta, "truncate"))  // No colons due to truncate(X)
533    {
534      metaoption.metacommand |= mTruncate;
535      get_truncate_options (meta, metaoption);
536      keep_trying = true;
537    }
538    // New "htmlsafe" special formatting option
539    if (meta.size() > ((text_t) "htmlsafe:").size() && starts_with(meta, "htmlsafe:"))
540    {
541      metaoption.metacommand |= mHTMLSafe;
542      meta = substr(meta.begin() + ((text_t) "htmlsafe:").size(), meta.end());
543      keep_trying = true;
544    }
545    // New "xmlsafe" special formatting option
546    if (meta.size() > ((text_t) "xmlsafe:").size() && starts_with(meta, "xmlsafe:"))
547    {
548      metaoption.metacommand |= mXMLSafe;
549      meta = substr(meta.begin() + ((text_t) "xmlsafe:").size(), meta.end());
550      keep_trying = true;
551    }
552    // New "dmsafe" special formatting option
553    if (meta.size() > ((text_t) "dmsafe:").size() && starts_with(meta, "dmsafe:"))
554    {
555      metaoption.metacommand |= mDMSafe;
556      meta = substr(meta.begin() + ((text_t) "dmsafe:").size(), meta.end());
557      keep_trying = true;
558    }
559  }
560
561  bool had_parent_or_child = true;
562  bool prev_was_parent = false;
563  bool prev_was_child  = false;
564
565  while (had_parent_or_child) {
566    if (meta.size() > 7
567    && (substr (meta.begin(), meta.begin()+6) == "parent")) {
568
569      // clear out sibling and child (cmd and options)
570      metaoption.metacommand &= ~(mChild|mSibling);
571      metaoption.childoptions.clear();
572      metaoption.siblingoptions.clear();
573
574      getParents = true;
575      metaoption.metacommand |= mParent;
576      get_parent_options (meta, metaoption);
577
578      if (prev_was_parent) {
579    metaoption.pre_tree_traverse += ".pr";
580      }
581      else if (prev_was_child) {
582    metaoption.pre_tree_traverse += ".fc";
583      }
584
585      prev_was_parent = true;
586      prev_was_child  = false;
587    }
588    else if (meta.size() > 6
589         && (substr (meta.begin(), meta.begin()+5) == "child")) {
590
591      // clear out sibling and parent (cmd and options)
592      metaoption.metacommand &= ~(mParent|mSibling);
593      metaoption.parentoptions.clear();
594      metaoption.siblingoptions.clear();
595
596      metaoption.metacommand |= mChild;
597      get_child_options (meta, metaoption);
598      metadata.insert("contains");
599
600      if (prev_was_parent) {
601    metaoption.pre_tree_traverse += ".pr";
602      }
603      else if (prev_was_child) {
604    metaoption.pre_tree_traverse += ".fc";
605      }
606
607      prev_was_child  = true;
608      prev_was_parent = false;
609    }
610    else {
611      prev_was_child  = false;
612      prev_was_parent = false;
613      had_parent_or_child = false;
614    }
615  }
616
617  // parent/child can have sibling tacked on end also
618  if (meta.size() > 8 && (substr (meta.begin(), meta.begin()+7) == "sibling")) {
619    metaoption.metacommand |= mSibling;
620    get_sibling_options (meta, metaoption);
621  }
622 
623  // check for ex. which may occur in format statements
624  if (meta.size()>3 && (substr(meta.begin(), meta.begin()+3) == "ex.")) {
625    meta = substr (meta.begin()+3, meta.end());
626  }
627  metadata.insert (meta);
628  metaoption.metaname = meta;
629}
630
631static void parse_coll_meta(text_t &meta, metadata_t &metaoption) {
632  if (meta == "collection") {
633    // no qualifiers
634    metaoption.metaname = g_EmptyText;
635    return;
636  }
637  meta = substr (meta.begin()+11, meta.end());
638  metaoption.metaname = meta;
639 
640}
641
642static void parse_meta (text_t &meta, format_t *formatlistptr,
643            text_tset &metadata, bool &getParents) {
644 
645  // check for ex. which may occur in format statements
646  if (meta.size()>3 && (substr(meta.begin(), meta.begin()+3) == "ex.")) {
647    meta = substr (meta.begin()+3, meta.end());
648  }
649  if (meta == "link")
650    formatlistptr->command = comLink;
651  else if (meta == "/link")
652    formatlistptr->command = comEndLink;
653
654  // the metaname "srclink_file" is deprecated, use "srclinkFile"
655  else if (meta == "srclink") {
656    formatlistptr->command = comAssocLink;
657    formatlistptr->meta.metaname = "srclinkFile";
658    metadata.insert("srclinkFile");
659  }
660  else if (meta == "srchref") {
661    formatlistptr->command = comAssocLink;
662    formatlistptr->text = "href";
663    formatlistptr->meta.metaname = "srclinkFile";
664    metadata.insert("srclinkFile");
665  }
666  else if (meta == "/srclink") {
667    formatlistptr->command = comEndAssocLink;
668    formatlistptr->meta.metaname = "srclinkFile";
669  }
670  // and weblink etc
671  else if (meta == "href")
672    formatlistptr->command = comHref;
673
674  else if (meta == "num")
675    formatlistptr->command = comNum;
676
677  else if (meta == "icon")
678    formatlistptr->command = comIcon;
679
680  else if (meta == "Text")
681    formatlistptr->command = comDoc;
682 
683  else if (meta == "RelatedDocuments")
684   formatlistptr->command = comRel;
685
686  else if (meta == "highlight")
687    formatlistptr->command = comHighlight;
688
689  else if (meta == "/highlight")
690    formatlistptr->command = comEndHighlight;
691
692  else if (meta == "metadata-spanwrap")
693    formatlistptr->command = comMetadataSpanWrap;
694
695  else if (meta == "/metadata-spanwrap")
696    formatlistptr->command = comEndMetadataSpanWrap;
697
698  else if (meta == "metadata-divwrap")
699    formatlistptr->command = comMetadataDivWrap;
700
701  else if (meta == "/metadata-divwrap")
702    formatlistptr->command = comEndMetadataDivWrap;
703
704  else if (meta == "Summary")
705    formatlistptr->command = comSummary;
706
707  else if (meta == "DocImage")
708    formatlistptr->command = comImage;
709
710  else if (meta == "DocTOC")
711    formatlistptr->command = comTOC;
712
713  else if (meta == "DocumentButtonDetach")
714     formatlistptr->command = comDocumentButtonDetach;
715 
716  else if (meta == "DocumentButtonHighlight")
717     formatlistptr->command = comDocumentButtonHighlight;
718 
719  else if (meta == "DocumentButtonExpandContents")
720    formatlistptr->command = comDocumentButtonExpandContents;
721
722  else if (meta == "DocumentButtonExpandText")
723     formatlistptr->command = comDocumentButtonExpandText;
724
725  else if (meta == "DocOID")
726     formatlistptr->command = comOID;
727  else if (meta == "DocTopOID")
728    formatlistptr->command = comTopOID;
729  else if (meta == "DocRank")
730    formatlistptr->command = comRank;
731  else if (meta == "DocTermsFreqTotal")
732    formatlistptr->command = comDocTermsFreqTotal;
733  else if (meta.size() >= 10 && (substr(meta.begin(), meta.begin()+10) == "collection")) {
734    formatlistptr->command = comCollection;
735    parse_coll_meta(meta, formatlistptr->meta);
736  }
737  else {
738    formatlistptr->command = comMeta;
739    parse_meta (meta, formatlistptr->meta, metadata, getParents);
740  }
741}
742
743
744static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
745              text_tset &metadata, bool &getParents) {
746
747  text_t text;
748  text_t::const_iterator here = formatstring.begin();
749  text_t::const_iterator end = formatstring.end();
750
751  while (here != end) {
752
753    if (*here == '\\') {
754      ++here;
755      if (here != end) text.push_back (*here);
756
757    } else if (*here == '{') {
758      if (!text.empty()) {
759    formatlistptr->command = comText;
760    formatlistptr->text = text;
761    formatlistptr->nextptr = new format_t();
762    formatlistptr = formatlistptr->nextptr;
763   
764    text.clear();
765      }
766      if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
767
768    formatlistptr->nextptr = new format_t();
769    formatlistptr = formatlistptr->nextptr;
770    if (here == end) break;
771      }
772    } else if (*here == '[') {
773      if (!text.empty()) {
774    formatlistptr->command = comText;
775    formatlistptr->text = text;
776    formatlistptr->nextptr = new format_t();
777    formatlistptr = formatlistptr->nextptr;
778
779    text.clear();
780      }
781      text_t meta;
782      ++here;
783      while (*here != ']') {
784    if (here == end) return false;
785    meta.push_back (*here);
786    ++here;
787      }
788      parse_meta (meta, formatlistptr, metadata, getParents);
789      formatlistptr->nextptr = new format_t();
790      formatlistptr = formatlistptr->nextptr;
791
792    } else
793      text.push_back (*here);
794
795    if (here != end) ++here;
796  }
797  if (!text.empty()) {
798    formatlistptr->command = comText;
799    formatlistptr->text = text;
800    formatlistptr->nextptr = new format_t();
801    formatlistptr = formatlistptr->nextptr;
802
803  }
804  return true;
805}
806
807
808static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
809              format_t *formatlistptr, text_tset &metadata, bool &getParents) {
810
811  text_t::const_iterator it = findchar (here, end, '}');
812  if (it == end) return false;
813
814  text_t com = substr (here, it);
815  here = findchar (it, end, '{');
816  if (here == end) return false;
817  else ++here;
818
819  if (com == "If" || com == "if" || com == "IF") formatlistptr->command = comIf;
820  else if (com == "Or" || com == "or" || com == "OR") formatlistptr->command = comOr;
821  else return false;
822
823  int commacount = 0;
824  text_t text;
825  while (here != end) {
826
827    if (*here == '\\') {
828      ++here;
829      if (here != end) text.push_back(*here);
830     
831    }
832 
833    else if (*here == ',' || *here == '}' || *here == '{') {
834
835      if (formatlistptr->command == comOr) {
836    // the {Or}{this, or this, or this, or this} statement
837    format_t *or_ptr;
838   
839    // find the next unused orptr
840    if (formatlistptr->orptr == NULL) {
841      formatlistptr->orptr = new format_t();
842      or_ptr = formatlistptr->orptr;
843    } else {
844      or_ptr = formatlistptr->orptr;
845      while (or_ptr->nextptr != NULL)
846        or_ptr = or_ptr->nextptr;
847      or_ptr->nextptr = new format_t();
848      or_ptr = or_ptr->nextptr;
849    }
850
851    if (!text.empty())
852      {
853        if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
854      }
855
856    if (*here == '{')
857      {
858        // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
859        // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
860        // The latter can always be re-written:
861        // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
862       
863        if (!text.empty()) // already used up allocated format_t
864          {
865        // => allocate new one for detected action
866        or_ptr->nextptr = new format_t();
867        or_ptr = or_ptr->nextptr;
868          }
869        if (!parse_action(++here, end, or_ptr, metadata, getParents))
870          {
871        return false;
872          }
873      }
874    else
875      {
876        if (*here == '}') break;
877      }
878    text.clear();
879
880      }
881
882      // Parse an {If}{decide,do,else} statement
883      else {
884   
885    // Read the decision component. 
886    if (commacount == 0) {
887      // Decsion can be a metadata element, or a piece of text.
888      // Originally Stefan's code, updated 25/10/2000 by Gordon.
889
890      text_t::const_iterator beginbracket = text.begin();
891      text_t::const_iterator endbracket = (text.end() - 1);
892
893      // Decision is based on a metadata element
894      if ((*beginbracket == '[') && (*endbracket == ']')) {
895        // Ignore the surrounding square brackets
896        text_t meta = substr (beginbracket+1, endbracket);
897        parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
898        ++commacount;
899        text.clear();
900      }
901
902      // Decision is a piece of text (probably a macro like _cgiargmode_).
903      else {
904
905        // hunt for any metadata in string, which might be uses in
906        // to test a condition, e.g. [Format] eq 'PDF'
907        format_t* dummyformat = new format_t();
908        // update which metadata fields needed
909        // (not interested in updatng formatlistptr)
910        parse_string (text, dummyformat, metadata, getParents);
911        delete dummyformat;
912
913        formatlistptr->decision.command = dText;
914        formatlistptr->decision.text = text;
915        ++commacount;
916        text.clear();
917      }
918    }
919
920    // Read the "then" and "else" components of the {If} statement.
921    else {
922      format_t** nextlistptr = NULL;
923      if (commacount == 1) {
924        nextlistptr = &formatlistptr->ifptr;
925      } else if (commacount == 2 ) {
926        nextlistptr = &formatlistptr->elseptr;
927      } else {
928        return false;
929      }
930
931      if (!text.empty()) {
932        if (*nextlistptr == NULL) {
933          *nextlistptr = new format_t();
934        } else {
935
936          // skip to the end of any format_t statements already added
937          while ((*nextlistptr)->nextptr != NULL)
938          {
939        nextlistptr = &(*nextlistptr)->nextptr;
940          }
941
942          (*nextlistptr)->nextptr = new format_t();
943          nextlistptr = &(*nextlistptr)->nextptr;
944        }
945
946        if (!parse_string (text, *nextlistptr, metadata, getParents))
947          {
948        return false;
949          }
950        text.clear();
951      }
952     
953      if (*here == '{')
954        {
955          if (*nextlistptr == NULL) {
956        *nextlistptr = new format_t();
957          } else {
958        // skip to the end of any format_t statements already added
959        while ((*nextlistptr)->nextptr != NULL)
960          {
961            nextlistptr = &(*nextlistptr)->nextptr;
962          }
963
964        (*nextlistptr)->nextptr = new format_t();
965        nextlistptr = &(*nextlistptr)->nextptr;
966          }
967
968          if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
969        {
970          return false;
971        }
972        }
973      else
974        {
975          if (*here == '}') break;
976          ++commacount;
977        }
978    }
979      }
980     
981    } else text.push_back(*here);
982   
983    if (here != end) ++here;
984  }
985
986  return true;
987}
988
989
990static text_t wrap_metatext(const text_t& metatext, const text_t& OID,
991                const text_t metaname, int metapos=-1)
992{
993
994  text_t tag_type = metadata_wrap_type;
995  text_t editable_type = (metaname == "Text") ? "text" : "metadata";
996
997  text_t wrapped_metatext = "<" + tag_type + " ";
998  wrapped_metatext += "class=\"editable-" + editable_type + "\" ";
999
1000  wrapped_metatext += "docoid=\"" + OID + "\" "; 
1001  wrapped_metatext += "metaname=\"" + metaname + "\"";
1002
1003  if (metapos>=0) {
1004    text_t metapos_str = metapos;
1005    wrapped_metatext += " metapos=\"" + metapos_str + "\"";
1006  }
1007
1008  wrapped_metatext += ">" + metatext + "</" + tag_type + ">";
1009
1010  return wrapped_metatext;
1011}
1012
1013   
1014
1015bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
1016             text_tset &metadata, bool &getParents) {
1017
1018  formatlistptr->clear();
1019  getParents = false;
1020
1021  return (parse_string (formatstring, formatlistptr, metadata, getParents));
1022}
1023
1024// position -1 for all, -2 for the last, 0 for the first, or x for a particular piece
1025// metainfo has all the values for the metadata, meta has the request metadata name and options, position tells which values to get
1026
1027static text_t get_formatted_meta_text(const text_t& OID, MetadataInfo_t &metainfo, const metadata_t &meta, int position, bool no_cgisafe = false)
1028{
1029  text_t no_ns_metaname = remove_namespace(meta.metaname);
1030  text_t formatted_metatext;
1031  bool first = true;
1032 
1033  const int start_i=0;
1034  const int end_i = metainfo.values.size()-1;
1035 
1036  if (position == -1) { // all
1037    for (int i=start_i; i<=end_i; ++i) {
1038      if (!first) formatted_metatext += meta.siblingoptions;
1039     
1040      text_t fresh_metatext;
1041
1042      if (meta.metacommand & mSpecial) {
1043    // special formatting
1044    if (no_ns_metaname == "Date") fresh_metatext = format_date (metainfo.values[i]);
1045    else if (no_ns_metaname == "Language") fresh_metatext = iso639(metainfo.values[i]);
1046    else fresh_metatext = "_format:"+meta.metaname+"_("+metainfo.values[i]+")";
1047      }
1048      else fresh_metatext = metainfo.values[i];
1049
1050      // New "truncate" special formatting option
1051      if (meta.metacommand & mTruncate)
1052      {
1053    int truncate_length = meta.siblingoptions.getint();
1054    text_t truncated_value = fresh_metatext;
1055    if (truncated_value.size() > truncate_length)
1056    {
1057      truncated_value = substr(truncated_value.begin(), truncated_value.begin() + truncate_length) + "... _texttruncated_";
1058    }
1059    fresh_metatext = truncated_value;
1060      }
1061      // New "xmlsafe" special formatting option
1062      if (meta.metacommand & mXMLSafe)
1063      {
1064    // Make it XML-safe
1065    text_t text_xml_safe = "";
1066    text_t::const_iterator text_iterator = fresh_metatext.begin();
1067    while (text_iterator != fresh_metatext.end())
1068    {
1069      if (*text_iterator == '&') text_xml_safe += "&amp;";
1070      else if (*text_iterator == '<') text_xml_safe += "&lt;";
1071      else if (*text_iterator == '>') text_xml_safe += "&gt;";
1072      else text_xml_safe.push_back(*text_iterator);
1073      text_iterator++;
1074    }
1075    fresh_metatext = text_xml_safe;
1076      }
1077      // New "htmlsafe" special formatting option
1078      if (meta.metacommand & mHTMLSafe)
1079      {
1080    // Make it HTML-safe
1081    text_t text_html_safe = "";
1082    text_t::const_iterator text_iterator = fresh_metatext.begin();
1083    while (text_iterator != fresh_metatext.end())
1084    {
1085      if (*text_iterator == '&') text_html_safe += "&amp;";
1086      else if (*text_iterator == '<') text_html_safe += "&lt;";
1087      else if (*text_iterator == '>') text_html_safe += "&gt;";
1088      else if (*text_iterator == '"') text_html_safe += "&quot;";
1089      else text_html_safe.push_back(*text_iterator);
1090      text_iterator++;
1091    }
1092    fresh_metatext = text_html_safe;
1093      }
1094      // New "dmsafe" special formatting option (always apply to "srclinkFile" metadata)
1095      // (The metaname "srclink_file" is deprecated, use "srclinkFile")
1096      if (meta.metacommand & mDMSafe || meta.metaname == "srclinkFile")
1097      {
1098    // Make it macro-safe
1099    text_t text_dm_safe = dm_safe(fresh_metatext);
1100    fresh_metatext = text_dm_safe;
1101      }
1102
1103      if (metadata_wrap) {
1104    fresh_metatext = wrap_metatext(fresh_metatext,OID,meta.metaname,i);
1105      }
1106      formatted_metatext += fresh_metatext;
1107
1108      first = false;
1109     
1110    }
1111  } else {
1112    if (position == -2) { // end
1113      position = end_i;
1114    } else if (position < start_i || position > end_i) {
1115      return "";
1116    }
1117
1118    text_t fresh_metatext;
1119    if (meta.metacommand & mSpecial) {
1120
1121      // special formatting
1122      if (no_ns_metaname == "Date") fresh_metatext = format_date (metainfo.values[position]);
1123      else if (no_ns_metaname == "Language") fresh_metatext = iso639(metainfo.values[position]);
1124      else fresh_metatext = "_format:"+meta.metaname+"_("+metainfo.values[position]+")";
1125    }
1126    else fresh_metatext = metainfo.values[position];
1127
1128    // New "truncate" special formatting option
1129    if (meta.metacommand & mTruncate)
1130    {
1131      int truncate_length = meta.siblingoptions.getint();
1132      text_t truncated_value = fresh_metatext;
1133      if (truncated_value.size() > truncate_length)
1134      {
1135    truncated_value = substr(truncated_value.begin(), truncated_value.begin() + truncate_length) + "... _texttruncated_";
1136      }
1137      fresh_metatext = truncated_value;
1138    }
1139    // New "xmlsafe" special formatting option
1140    if (meta.metacommand & mXMLSafe)
1141    {
1142      // Make it XML-safe
1143      text_t text_xml_safe = "";
1144      text_t::const_iterator text_iterator = fresh_metatext.begin();
1145      while (text_iterator != fresh_metatext.end())
1146      {
1147    if (*text_iterator == '&') text_xml_safe += "&amp;";
1148    else if (*text_iterator == '<') text_xml_safe += "&lt;";
1149    else if (*text_iterator == '>') text_xml_safe += "&gt;";
1150    else text_xml_safe.push_back(*text_iterator);
1151    text_iterator++;
1152      }
1153      fresh_metatext = text_xml_safe;
1154    }
1155    // New "htmlsafe" special formatting option
1156    if (meta.metacommand & mHTMLSafe)
1157    {
1158      // Make it HTML-safe
1159      text_t text_html_safe = "";
1160      text_t::const_iterator text_iterator = fresh_metatext.begin();
1161      while (text_iterator != fresh_metatext.end())
1162      {
1163    if (*text_iterator == '&') text_html_safe += "&amp;";
1164    else if (*text_iterator == '<') text_html_safe += "&lt;";
1165    else if (*text_iterator == '>') text_html_safe += "&gt;";
1166    else if (*text_iterator == '"') text_html_safe += "&quot;";
1167    else if (*text_iterator == '\'') text_html_safe += "&#39;";
1168    else if (*text_iterator == ',') text_html_safe += "&#44;";
1169    else text_html_safe.push_back(*text_iterator);
1170    text_iterator++;
1171      }
1172      fresh_metatext = text_html_safe;
1173    }
1174    // New "dmsafe" special formatting option (always apply to "srclinkFile" metadata)
1175    // (The metaname "srclink_file" is deprecated, use "srclinkFile")
1176    if (meta.metacommand & mDMSafe || meta.metaname == "srclinkFile")
1177    {
1178      // Make it macro-safe
1179      text_t text_dm_safe = dm_safe(fresh_metatext);
1180      fresh_metatext = text_dm_safe;
1181    }
1182
1183    if (metadata_wrap) {
1184      fresh_metatext = wrap_metatext(fresh_metatext,OID,meta.metaname,position);
1185    }
1186
1187    formatted_metatext += fresh_metatext;
1188  }
1189
1190  if (meta.metacommand & mCgiSafe && !no_cgisafe) return cgi_safe_unicode (formatted_metatext);
1191  else return formatted_metatext;
1192}
1193
1194static text_t get_parent_meta (ResultDocInfo_t &docinfo, const metadata_t &meta, int siblings_values)
1195{
1196 
1197  MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
1198
1199  switch (meta.mqualifier.parent) {
1200  case pNone:
1201    return "Nothing!!";
1202    break;
1203
1204  case pImmediate:
1205    if (parent != NULL) {
1206      text_t parent_oid = get_parent(docinfo.OID);
1207      return get_formatted_meta_text(parent_oid,*parent, meta, siblings_values);
1208    }
1209    break;
1210
1211  case pTop:
1212    if (parent != NULL) {
1213      text_t parent_oid = get_parent(docinfo.OID);
1214
1215      while (parent->parent != NULL) {
1216    parent = parent->parent;
1217    parent_oid = get_parent(parent_oid);
1218      }
1219      return get_formatted_meta_text(parent_oid,*parent, meta, siblings_values);
1220    }
1221    break;
1222
1223  case pAll:
1224    MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
1225    if (parent != NULL) {
1226      text_t parent_oid = get_parent(docinfo.OID);
1227
1228      text_tarray tmparray;
1229      while (parent != NULL) {
1230    tmparray.push_back (get_formatted_meta_text(parent_oid,*parent, meta, siblings_values, true)); // set no_cgisafe to true, as we'll do it once we have all the metadata
1231    parent = parent->parent;
1232    parent_oid = get_parent(parent_oid);
1233
1234      }
1235      // now join them up - use teh parent separator
1236      bool first = true;
1237      text_t tmp;
1238      text_tarray::reverse_iterator here = tmparray.rbegin();
1239      text_tarray::reverse_iterator end = tmparray.rend();
1240      while (here != end) {
1241    if (!first) tmp += meta.parentoptions;
1242    tmp += *here;
1243    first = false;
1244    ++here;
1245      }
1246      if (meta.metacommand & mCgiSafe) return cgi_safe_unicode (tmp);
1247      else return tmp;
1248    }
1249  }
1250  return "";
1251
1252}
1253
1254static text_t get_child_meta (const text_t& collection,
1255                  recptproto* collectproto,
1256                  ResultDocInfo_t &docinfo, displayclass &disp,
1257                  const metadata_t &meta, text_tmap &options,
1258                  ostream& logout, int siblings_values)
1259{
1260  if (docinfo.metadata["contains"].values[0].size()==0) return ""; // no children
1261 
1262  const text_t& pre_tree_trav = meta.pre_tree_traverse;
1263  const text_t& child_metaname = meta.metaname;
1264  const text_t& child_field = meta.childoptions;
1265  text_tset child_metadata;
1266  child_metadata.insert(child_metaname);
1267
1268  FilterResponse_t child_response;
1269  if (meta.mqualifier.child == cNum) {
1270    // just one child
1271    //get the information associated with the metadata for child doc
1272    if (!get_info (docinfo.OID+pre_tree_trav+child_field, collection, "",
1273           child_metadata, false, collectproto, child_response,
1274           logout)) return ""; // invalid child number
1275
1276      if (child_response.docInfo.empty()) return false; // no info for the child
1277 
1278      ResultDocInfo_t& child_docinfo = child_response.docInfo[0];
1279      MetadataInfo_t& metaname_rec = child_docinfo.metadata[child_metaname];
1280 
1281      text_t child_metavalue
1282    = get_formatted_meta_text(child_docinfo.OID,metaname_rec,meta,siblings_values);
1283      return expand_metadata(child_metavalue,collection,collectproto,
1284             child_docinfo,disp,options,logout);
1285  }
1286 
1287   
1288  if (meta.mqualifier.child != cAll) return false; // invalid qualifier
1289
1290
1291  if (!pre_tree_trav.empty()) {
1292    // need to get relevant "contains" metadata for new (e.g. pre tree trav) node
1293    FilterResponse_t trav_response;
1294
1295    text_tset trav_metadata;
1296    trav_metadata.insert("contains");
1297
1298    if (!get_info (docinfo.OID+pre_tree_trav, collection, "",
1299           trav_metadata, false, collectproto, trav_response,
1300           logout)) return ""; // invalid pre_tree_trav
1301
1302    if (trav_response.docInfo.empty()) return false; // no info for the pre_tree_trav OID
1303 
1304    ResultDocInfo_t& trav_docinfo = trav_response.docInfo[0];
1305
1306    // use this for rest of routine
1307    docinfo = trav_docinfo;
1308  }
1309 
1310  // we need to get all children
1311  text_t result = "";
1312  text_tarray children;
1313  text_t contains = docinfo.metadata["contains"].values[0];
1314  splitchar (contains.begin(), contains.end(), ';', children);
1315  text_tarray::const_iterator here = children.begin();
1316  text_tarray::const_iterator end = children.end();
1317  bool first = true;
1318  while (here !=end) {
1319    text_t oid = *here;
1320    here++;
1321    if (*(oid.begin()) == '"') translate_parent (oid, docinfo.OID);
1322
1323    //get the information associated with the metadata for child doc
1324    if (!get_info (oid, collection, "", child_metadata,
1325           false, collectproto, child_response, logout) ||
1326    child_response.docInfo.empty()) {
1327      first = false;
1328      continue;
1329    }
1330   
1331   
1332    ResultDocInfo_t& child_docinfo = child_response.docInfo[0];
1333    MetadataInfo_t& metaname_rec = child_docinfo.metadata[child_metaname];
1334   
1335    text_t child_metavalue
1336      = get_formatted_meta_text(child_docinfo.OID,metaname_rec,meta,siblings_values);
1337
1338
1339    if (!first) result += child_field;
1340    first = false;
1341    // need to do this here cos otherwise we are in the wrong document
1342    text_t em =  expand_metadata(child_metavalue,collection,collectproto,
1343                 child_docinfo,disp,options,logout);
1344
1345    result += em;
1346  }
1347  return result;
1348   
1349}
1350
1351static text_t get_meta (const text_t& collection, recptproto* collectproto,
1352            ResultDocInfo_t &docinfo, displayclass &disp,
1353            const metadata_t &meta, text_tmap &options,
1354            ostream& logout) {
1355 
1356  // make sure we have the requested metadata
1357  MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
1358  if (it == docinfo.metadata.end()) return "";
1359
1360  int siblings_values = 0; // default is no siblings, just the first metadata available
1361  if (meta.metacommand & mSibling) {
1362    if (meta.mqualifier.sibling == sAll) {
1363      siblings_values = -1; //all
1364    } else if (meta.mqualifier.sibling == sNum) {
1365      siblings_values = meta.siblingoptions.getint();
1366    }
1367  }
1368  if (meta.metacommand & mParent) {
1369    return get_parent_meta(docinfo,meta,siblings_values);
1370  }
1371
1372  else if (meta.metacommand & mChild) {
1373    return get_child_meta(collection,collectproto,docinfo,disp,meta,
1374                options,logout, siblings_values);
1375  }
1376  else if (meta.metacommand & mSibling) { // only siblings
1377    MetadataInfo_t& metaname_rec = docinfo.metadata[meta.metaname];
1378    return get_formatted_meta_text(docinfo.OID,docinfo.metadata[meta.metaname],meta, siblings_values);
1379  }
1380  else {
1381   
1382    // straightforward metadata request (nothing fancy)
1383
1384    text_t classifier_metaname = docinfo.classifier_metadata_type;
1385    int metaname_index
1386      = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
1387    return get_formatted_meta_text(docinfo.OID,docinfo.metadata[meta.metaname], meta, metaname_index);
1388  }
1389 
1390  return "";
1391}
1392
1393static text_t get_or (const text_t& collection, recptproto* collectproto,
1394              ResultDocInfo_t &docinfo, displayclass &disp,
1395              format_t *orptr, text_tmap &options,
1396              ostream& logout) {
1397
1398  while (orptr != NULL) {
1399
1400    if (metadata_wrap) {
1401      // need to be a bit more careful about this
1402      // => test for it *without* spanwrap or divwrap, and if defined, then
1403      // got back and generate it again, this time with spanwrap/divwrap on
1404
1405      metadata_wrap = false;
1406      text_t test_tmp = format_string (collection,collectproto,docinfo, disp, orptr,
1407                       options, logout);
1408      metadata_wrap = true;
1409      if (!test_tmp.empty()) {
1410
1411    return format_string (collection,collectproto,docinfo, disp, orptr,
1412                  options, logout);
1413      }
1414    }
1415    else {
1416      text_t tmp = format_string (collection,collectproto,docinfo, disp, orptr,
1417                  options, logout);
1418      if (!tmp.empty()) return tmp;
1419    }
1420
1421    orptr = orptr->nextptr;
1422  }
1423  return "";
1424}
1425
1426static bool char_is_whitespace(const char c)
1427{
1428  return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
1429
1430}
1431
1432static int scan_over_whitespace(const text_t& outstring, const int start_pos)
1433{
1434  int pos = start_pos;
1435  while (pos<outstring.size()) {
1436    if (!char_is_whitespace(outstring[pos])) {
1437      break;
1438    }
1439    ++pos;
1440  }
1441
1442  return pos;
1443}
1444
1445static int rscan_over_whitespace(const text_t& outstring, const int start_pos)
1446{
1447  int pos = start_pos;
1448  while (pos>=0) {
1449    if (!char_is_whitespace(outstring[pos])) {
1450      break;
1451    }
1452    --pos;
1453  }
1454
1455  return pos;
1456}
1457
1458static int rscan_for_whitespace(const text_t& outstring, const int start_pos)
1459{
1460  int pos = start_pos;
1461  while (pos>=0) {
1462    if (char_is_whitespace(outstring[pos])) {
1463      break;
1464    }
1465    --pos;
1466  }
1467
1468  return pos;
1469}
1470
1471
1472static int rscan_for(const text_t& outstring, const int start_pos,
1473             const char find_c)
1474{
1475  int pos = start_pos;
1476  while (pos>=0) {
1477    char c = outstring[pos];
1478    if (outstring[pos] == find_c) {
1479      break;
1480    }
1481    --pos;
1482  }
1483
1484  return pos;
1485}
1486
1487text_t extract_substr(const text_t& outstring, const int start_pos,
1488              const int end_pos)
1489{
1490  text_t extracted_str;
1491  extracted_str.clear();
1492
1493  for (int pos=start_pos; pos<=end_pos; ++pos) {
1494    extracted_str.push_back(outstring[pos]);
1495  }
1496
1497  return extracted_str;
1498}
1499
1500
1501static text_t expand_potential_metadata(const text_t& collection,
1502                    recptproto* collectproto,
1503                    ResultDocInfo_t &docinfo,
1504                    displayclass &disp,
1505                    const text_t& intext,
1506                    text_tmap &options,
1507                    ostream& logout)
1508{
1509  text_t outtext;
1510
1511  // decide if dealing with metadata or text
1512
1513  text_t::const_iterator beginbracket = intext.begin();
1514  text_t::const_iterator endbracket = (intext.end() - 1);
1515
1516  // Decision is based on a metadata element
1517  if ((*beginbracket == '[') && (*endbracket == ']')) {
1518    // Ignore the surrounding square brackets
1519    text_t meta_text = substr (beginbracket+1, endbracket);
1520
1521    if (meta_text == "Text") {
1522      outtext = format_text(collection, collectproto, docinfo, disp, options, logout);
1523    }
1524    else {
1525
1526      text_tset metadata;
1527      bool getParents =false;
1528      metadata_t meta;
1529     
1530      parse_meta (meta_text, meta, metadata, getParents);   
1531      outtext
1532    = get_meta (collection,collectproto,docinfo,disp,meta,options,logout);
1533    }
1534
1535  }
1536  else {
1537    outtext = intext;
1538  }
1539
1540  return outtext;
1541}
1542
1543
1544
1545
1546static bool uses_expression(const text_t& collection, recptproto* collectproto,
1547                ResultDocInfo_t &docinfo,
1548                displayclass &disp,
1549                const text_t& outstring, text_t& lhs_expr,
1550                text_t& op_expr, text_t& rhs_expr,
1551                text_tmap &options,
1552                ostream& logout)
1553{
1554  // Note: the string may not be of the form: str1 op str2, however
1555  // to deterine this we have to process it on the assumption it is,
1556  // and if at any point an 'erroneous' value is encountered, return
1557  // false and let something else have a go at evaluating it
1558
1559  // Starting at the end of the string and working backwards ..
1560
1561  const int outstring_len = outstring.size();
1562
1563  // skip over white space
1564  int rhs_end = rscan_over_whitespace(outstring,outstring_len-1);
1565
1566  if (rhs_end<=0) {
1567    // no meaningful text or (rhs_end==0) no room for operator
1568    return false;
1569  }
1570
1571  // check for ' or " and then scan over token
1572  const char potential_quote = outstring[rhs_end];
1573  int rhs_start=rhs_end;
1574  bool quoted = false;
1575
1576  if ((potential_quote == '\'') || (potential_quote == '\"')) {
1577    --rhs_end;
1578    rhs_start = rscan_for(outstring,rhs_end-1,potential_quote) +1;
1579    quoted = true;
1580  }
1581  else {
1582    rhs_start = rscan_for_whitespace(outstring,rhs_end-1) +1;
1583  }
1584
1585  if ((rhs_end-rhs_start)<0) {
1586    // no meaningful rhs expression
1587    return false;
1588  }
1589
1590  // form rhs_expr
1591  rhs_expr = extract_substr(outstring,rhs_start,rhs_end);
1592
1593  // skip over white space
1594  const int to_whitespace = (quoted) ? 2 : 1;
1595
1596  int op_end = rscan_over_whitespace(outstring,rhs_start-to_whitespace);
1597  int op_start = rscan_for_whitespace(outstring,op_end-1)+1;
1598
1599  if ((op_end<0) && (op_start<0)) {
1600    // no meaningful expression operator
1601    return false;
1602  }
1603
1604  if (op_end-op_start<0) {
1605    // no meaningful expression operator
1606    return false;
1607  }
1608
1609  op_expr = extract_substr(outstring,op_start,op_end);
1610
1611
1612  // check for operator
1613  if ((op_expr != "eq") && (op_expr != "ne") && (op_expr != "gt") &&
1614     (op_expr != "ge") && (op_expr != "lt") && (op_expr != "le") && (op_expr != "==") && (op_expr != "!=") &&  (op_expr != ">") && (op_expr != ">=") && (op_expr != "<") && (op_expr != "<=") && (op_expr != "sw") && (op_expr != "ew")) {
1615
1616    // not a valid operator
1617    return false;
1618  }
1619
1620  int lhs_end = rscan_over_whitespace(outstring,op_start-1);
1621  if (lhs_end<0) {
1622    // no meaningful lhs expression
1623    return false;
1624  }
1625
1626  int lhs_start = scan_over_whitespace(outstring,0);
1627
1628  // form lhs_expr from remainder of string
1629  lhs_expr = extract_substr(outstring,lhs_start,lhs_end);
1630
1631  // Now we know we have a valid expression, look up any
1632  // metadata terms
1633
1634  rhs_expr = expand_potential_metadata(collection,collectproto,docinfo,
1635                       disp,rhs_expr,options,logout);
1636  lhs_expr = expand_potential_metadata(collection,collectproto,docinfo,
1637                       disp,lhs_expr,options,logout);
1638
1639  return true;
1640}
1641
1642static bool eval_expression_true(const text_t& lhs_expr,const text_t& op_expr,
1643                 const text_t& rhs_expr, ostream& logout)
1644{
1645  if (op_expr == "eq") return (lhs_expr == rhs_expr);
1646  else if (op_expr == "ne" ) return (lhs_expr != rhs_expr);
1647  else if (op_expr == "gt") return (lhs_expr > rhs_expr);
1648  else if (op_expr == "ge") return (lhs_expr >= rhs_expr);
1649  else if (op_expr == "lt") return (lhs_expr < rhs_expr);
1650  else if (op_expr == "le") return (lhs_expr <= rhs_expr);
1651  else if (op_expr == "==") return (lhs_expr.getint() == rhs_expr.getint());
1652  else if (op_expr == "!=") return (lhs_expr.getint() != rhs_expr.getint());
1653  else if (op_expr == ">") return (lhs_expr.getint() > rhs_expr.getint());
1654  else if (op_expr == ">=") return (lhs_expr.getint() >= rhs_expr.getint());
1655  else if (op_expr == "<") return (lhs_expr.getint() < rhs_expr.getint());
1656  else if (op_expr == "<=") return (lhs_expr.getint() <= rhs_expr.getint());
1657  else if (op_expr == "sw") return (starts_with(lhs_expr,rhs_expr));
1658  else if (op_expr == "ew") return (ends_with(lhs_expr,rhs_expr));
1659  else {
1660    logout << "Error: '" << op_expr << "' is not a recognised operator." << endl;
1661  }
1662 
1663  return false;
1664}
1665
1666
1667static text_t get_if (const text_t& collection, recptproto* collectproto,
1668              ResultDocInfo_t &docinfo, displayclass &disp,
1669              const decision_t &decision,
1670              format_t *ifptr, format_t *elseptr,
1671              text_tmap &options, ostream& logout)
1672{
1673  // If the decision component is a metadata element, then evaluate it
1674  // to see whether we output the "then" or the "else" clause
1675  if (decision.command == dMeta) {
1676
1677    bool store_metadata_wrap = metadata_wrap;
1678    metadata_wrap = 0;
1679
1680    // temporarily suspend metadata-XXXwrap (if on) so can test if metadata item really exits or not
1681    bool metadata_exists
1682      = (get_meta (collection,collectproto,docinfo,disp,decision.meta,options,
1683           logout) != "");
1684
1685    metadata_wrap = store_metadata_wrap;
1686
1687    if (metadata_exists) {
1688      if (ifptr != NULL)
1689    return get_formatted_string (collection,collectproto,docinfo, disp, ifptr,
1690                     options, logout);
1691    }
1692    else {
1693      if (elseptr != NULL)
1694    return get_formatted_string (collection,collectproto,docinfo, disp, elseptr,
1695                     options, logout);
1696    }
1697  }
1698
1699  // If the decision component is text, then evaluate it (it is probably a
1700  // macro like _cgiargmode_) to decide what to output.
1701  else if (decision.command == dText) {
1702
1703    text_t outstring;
1704    disp.expandstring (decision.text, outstring);
1705
1706    // Check for if expression in form: str1 op str2
1707    // (such as [x] eq "y")
1708    text_t lhs_expr, op_expr, rhs_expr;
1709    if (uses_expression(collection,collectproto,docinfo, disp, outstring,lhs_expr,op_expr,rhs_expr, options,logout)) {
1710      if (eval_expression_true(lhs_expr,op_expr,rhs_expr,logout)) {
1711    if (ifptr != NULL) {
1712      return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1713                       options, logout);
1714    }
1715    else {
1716      return "";
1717    }
1718      } else {
1719    if (elseptr != NULL) {
1720      return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1721                       options, logout);
1722    }
1723    else {
1724      return "";
1725    }
1726      }
1727    }
1728
1729
1730    // This is a tad tricky.  When we expand a string like _cgiargmode_, that is
1731    // a cgi argument macro that has not been set, it evaluates to itself.
1732    // Therefore, were have to say that a piece of text evalautes true if
1733    // it is non-empty and if it is a cgi argument evaulating to itself.
1734
1735    if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
1736      if (ifptr != NULL)
1737    return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1738                     options, logout);
1739    } else {
1740      if (elseptr != NULL)
1741    return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1742                     options, logout);
1743    }
1744  }
1745 
1746  return "";
1747}
1748
1749bool includes_metadata(const text_t& text)
1750{
1751  text_t::const_iterator here = text.begin();
1752  text_t::const_iterator end = text.end();
1753
1754  char startbracket = '[';
1755  char endbracket = ']';
1756
1757  char bracket = startbracket;
1758  while (here != end) {
1759      if (*here == bracket) {
1760          if(bracket == startbracket) {
1761              // seen a [, next look for a ] to confirm it's metadata
1762              bracket = endbracket;
1763          } else if(bracket == endbracket) {
1764              // found [ ... ] in text, so we think it includes metadata
1765              return true;
1766          }
1767      }
1768    ++here;
1769  }
1770
1771  return false;
1772}
1773
1774static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
1775                  recptproto* collectproto,
1776                  ResultDocInfo_t &docinfo,
1777                  displayclass &disp, text_tmap &options,
1778                  ostream &logout) {
1779     
1780  if (includes_metadata(metavalue)) {
1781   
1782    // text has embedded metadata in it => expand it
1783    FilterRequest_t request;
1784    FilterResponse_t response;
1785   
1786    request.getParents = false;
1787   
1788    format_t *expanded_formatlistptr = new format_t();
1789    parse_formatstring (metavalue, expanded_formatlistptr,
1790            request.fields, request.getParents);
1791   
1792    // retrieve metadata
1793    get_info(docinfo.OID, collection, "", request.fields, request.getParents,
1794         collectproto, response, logout);
1795   
1796    if (!response.docInfo.empty()) {
1797     
1798      text_t expanded_metavalue
1799    = get_formatted_string(collection, collectproto,
1800                   response.docInfo[0], disp, expanded_formatlistptr,
1801                   options, logout);
1802     
1803      return expanded_metavalue;
1804    }
1805    else {
1806      return metavalue;
1807    }
1808  }
1809  else {
1810   
1811    return metavalue;
1812  }
1813}
1814
1815text_t get_collection_meta(const text_t& collection, recptproto* collectproto,
1816               displayclass &disp,
1817               text_t meta_name, ostream& logout) {
1818 
1819  ColInfoResponse_t collectinfo;
1820  comerror_t err;
1821  collectproto->get_collectinfo (collection, collectinfo,err,logout);
1822  text_t meta_value = "";
1823  text_t lang;
1824  disp.expandstring("_cgiargl_",lang);
1825  if (lang.empty()) {
1826    lang = "en";
1827  }
1828
1829  if (err == noError) {
1830    meta_value = collectinfo.get_collectionmeta(meta_name, lang);
1831  }
1832  return meta_value;
1833 
1834
1835}
1836text_t format_string (const text_t& collection, recptproto* collectproto,
1837              ResultDocInfo_t &docinfo, displayclass &disp,
1838              format_t *formatlistptr, text_tmap &options,
1839              ostream& logout) {
1840
1841  if (formatlistptr == NULL) return "";
1842
1843  switch (formatlistptr->command) {
1844     case comOID:
1845    return docinfo.OID;
1846  case comTopOID:
1847    {
1848      text_t top_id;
1849      get_top(docinfo.OID, top_id);
1850      return top_id;
1851    }
1852  case comRank:
1853    return text_t(docinfo.ranking);
1854     case comText:
1855    return formatlistptr->text;
1856     case comLink:
1857    return options["link"];
1858     case comEndLink:
1859       {
1860     if (options["link"].empty()) return "";
1861    else return "</a>";
1862       }
1863     case comHref:
1864    return get_href(options["link"]);
1865     case comIcon:
1866    return options["icon"];
1867     case comNum:
1868    return docinfo.result_num;
1869     case comRel: //if [RelatedDocuments] appears in format string, collect relation data
1870    return get_related_docs(collection, collectproto, docinfo, logout);
1871
1872     case comSummary:
1873       return format_summary(collection, collectproto, docinfo, disp, options, logout);
1874     case comAssocLink:
1875       {
1876     text_t link_filename = get_meta(collection, collectproto, docinfo, disp, formatlistptr->meta, options, logout);
1877         if (!link_filename.empty()) {
1878       text_t href= expand_metadata(options["assocfilepath"], collection, collectproto, docinfo, disp, options, logout) + link_filename;
1879       if (formatlistptr->text == "href") {
1880         return href;
1881       }
1882       return "<a href=\""+ href + "\">";
1883     }
1884     return "";
1885       }
1886  case comEndAssocLink:
1887    {
1888    text_t link_filename = get_meta(collection, collectproto, docinfo, disp, formatlistptr->meta, options, logout);
1889    if (!link_filename.empty()) {
1890      return "</a>";
1891    }
1892    return "";
1893    }
1894     case comMeta:
1895    {
1896       const text_t& metavalue =  get_meta (collection,collectproto, docinfo, disp,formatlistptr->meta,options,logout);
1897       return expand_metadata(metavalue, collection, collectproto, docinfo, disp, options, logout);
1898    }
1899
1900     case comDoc:
1901       return format_text(collection, collectproto, docinfo, disp, options, logout);
1902
1903     case comImage:
1904    return expand_metadata(options["DocImage"], collection, collectproto, docinfo, disp, options, logout);
1905     case comTOC:
1906    return options["DocTOC"];
1907     case comDocumentButtonDetach:
1908    return options["DocumentButtonDetach"];
1909     case comDocumentButtonHighlight:
1910    return options["DocumentButtonHighlight"];
1911     case comDocumentButtonExpandContents:
1912    return options["DocumentButtonExpandContents"];
1913     case comDocumentButtonExpandText:
1914    return options["DocumentButtonExpandText"];
1915     case comHighlight:
1916    if (options["highlight"] == "1") return "<b>";
1917    break;
1918     case comEndHighlight:
1919    if (options["highlight"] == "1") return "</b>";
1920    break;
1921     case comMetadataSpanWrap:
1922        metadata_wrap=true;  metadata_wrap_type="span"; return "";
1923    break;
1924     case comEndMetadataSpanWrap:
1925    metadata_wrap=false; metadata_wrap_type="";     return "";
1926    break;
1927     case comMetadataDivWrap:
1928        metadata_wrap=true;  metadata_wrap_type="div";  return "";
1929    break;
1930     case comEndMetadataDivWrap:
1931    metadata_wrap=false; metadata_wrap_type="";     return "";
1932    break;
1933     case comIf:
1934    return get_if (collection, collectproto, docinfo, disp,
1935               formatlistptr->decision, formatlistptr->ifptr,
1936               formatlistptr->elseptr, options, logout);
1937     case comOr:
1938    return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
1939               options, logout);
1940     case comDocTermsFreqTotal:
1941       return docinfo.num_terms_matched;
1942     case comCollection:
1943       if (formatlistptr->meta.metaname == g_EmptyText) {
1944     return collection;
1945       }
1946       return get_collection_meta(collection, collectproto, disp, formatlistptr->meta.metaname, logout);
1947   
1948  }
1949  return "";
1950}
1951
1952text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1953                 ResultDocInfo_t &docinfo, displayclass &disp,
1954                 format_t *formatlistptr, text_tmap &options,
1955                 ostream& logout) {
1956
1957   text_t ft;
1958   while (formatlistptr != NULL)
1959      {
1960     ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
1961                  options, logout);
1962     formatlistptr = formatlistptr->nextptr;
1963      }
1964   
1965   return ft;
1966}
1967
1968
1969// we have only preloaded the text in DocumentAction. But you may want
1970// to get the text in query, so copy what we have done with
1971// format_summary and get the text here. Probably is quite expensive?
1972text_t format_text (const text_t& collection, recptproto* collectproto,
1973            ResultDocInfo_t &docinfo, displayclass &disp,
1974            text_tmap &options, ostream& logout)
1975{
1976  text_t text;
1977
1978  if (!options["text"].empty()) {
1979    text = options["text"];
1980  }
1981  else {
1982    // get document text here
1983    DocumentRequest_t docrequest;
1984    DocumentResponse_t docresponse;
1985    comerror_t err;
1986    docrequest.OID = docinfo.OID;
1987    collectproto->get_document (collection, docrequest, docresponse, err, logout);
1988    text = docresponse.doc;
1989  }
1990
1991  if (metadata_wrap) {
1992    text = wrap_metatext(text,docinfo.OID,"Text");
1993  }
1994
1995  return text;
1996}
1997 
1998/* FUNCTION NAME: format_summary
1999 * DESC: this is invoked when a [Summary] special metadata is processed.
2000 * RETURNS: a query-biased summary for the document */
2001
2002text_t format_summary (const text_t& collection, recptproto* collectproto,
2003               ResultDocInfo_t &docinfo, displayclass &disp,
2004               text_tmap &options, ostream& logout) {
2005
2006  // GRB: added code here to ensure that the cstr (and other collections)
2007  //      uses the document metadata item Summary, rather than compressing
2008  //      the text of the document, processed via the methods in
2009  //      summarise.cpp
2010
2011  text_t summary;
2012
2013  if (docinfo.metadata.count("Summary") > 0 &&
2014      docinfo.metadata["Summary"].values.size() > 0) {
2015    summary = docinfo.metadata["Summary"].values[0];
2016  }
2017  else {
2018 
2019    text_t textToSummarise, query;
2020
2021    if(options["text"].empty()) { // get document text
2022      DocumentRequest_t docrequest;
2023      DocumentResponse_t docresponse;
2024      comerror_t err;
2025      docrequest.OID = docinfo.OID;
2026      collectproto->get_document (collection, docrequest, docresponse, err, logout);
2027      textToSummarise = docresponse.doc;
2028    }
2029    else {
2030      // in practice, this would not happen, because text is only
2031      // loaded with the [Text] command
2032      textToSummarise = options["text"];
2033    }
2034   
2035    disp.expandstring("_cgiargq_",query);
2036    summary = summarise(textToSummarise,query,80);
2037    //summary = substr(textToSummarise.begin(),textToSummarise.begin()+80);
2038  }
2039
2040  summary.replace("'","&#039;");
2041  summary.replace("\n","&#013;");
2042
2043  if (metadata_wrap) {
2044    summary = wrap_metatext(summary,docinfo.OID,"Summary");
2045  }
2046
2047  return summary;
2048}
Note: See TracBrowser for help on using the browser.