source: trunk/gsdl/src/recpt/formattools.cpp@ 10614

Last change on this file since 10614 was 10614, checked in by kjdon, 19 years ago

now allow [Text] in an if statement - can do {If}{[Text] ne 'This document has no text. ',[Text]}

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 51.7 KB
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 "OIDtools.h"
29#include "summarise.h"
30
31#include <assert.h>
32
33// a few function prototypes
34
35static text_t format_string (const text_t& collection, recptproto* collectproto,
36 ResultDocInfo_t &docinfo, displayclass &disp,
37 format_t *formatlistptr, text_tmap &options,
38 ostream& logout);
39
40static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
41 format_t *formatlistptr, text_tset &metadata, bool &getParents);
42
43static text_t format_summary (const text_t& collection, recptproto* collectproto,
44 ResultDocInfo_t &docinfo, displayclass &disp,
45 text_tmap &options, ostream& logout);
46static text_t format_text (const text_t& collection, recptproto* collectproto,
47 ResultDocInfo_t &docinfo, displayclass &disp,
48 text_tmap &options, ostream& logout);
49
50static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
51 recptproto* collectproto, ResultDocInfo_t &docinfo,
52 displayclass &disp, text_tmap &options,
53 ostream &logout);
54
55
56void metadata_t::clear() {
57 metaname.clear();
58 metacommand = mNone;
59 mqualifier.parent = pNone;
60 mqualifier.sibling = sNone;
61 mqualifier.child = cNone;
62 parentoptions.clear();
63 siblingoptions.clear();
64 childoptions.clear();
65}
66
67void decision_t::clear() {
68 command = dMeta;
69 meta.clear();
70 text.clear();
71}
72
73void format_t::clear() {
74 command = comText;
75 decision.clear();
76 text.clear();
77 meta.clear();
78 nextptr = NULL;
79 ifptr = NULL;
80 elseptr = NULL;
81 orptr = NULL;
82}
83
84void formatinfo_t::clear() {
85 DocumentImages = false;
86 DocumentTitles = true;
87 DocumentHeading = "{Or}{[parent(Top):Title],[Title],untitled}<br>";
88 DocumentContents = true;
89 DocumentArrowsBottom = true;
90 DocumentArrowsTop = false;
91 DocumentButtons.erase (DocumentButtons.begin(), DocumentButtons.end());
92 // DocumentButtons.push_back ("Expand Text");
93 // DocumentButtons.push_back ("Expand Contents");
94 DocumentButtons.push_back ("Detach");
95 DocumentButtons.push_back ("Highlight");
96 RelatedDocuments = "";
97 DocumentText = "<center><table width=_pagewidth_><tr><td>[Text]</td></tr></table></center>";
98 formatstrings.erase (formatstrings.begin(), formatstrings.end());
99 DocumentUseHTML = false;
100 AllowExtendedOptions = false;
101}
102
103// simply checks to see if formatstring begins with a <td> tag
104bool is_table_content (const text_t &formatstring) {
105 text_t::const_iterator here = formatstring.begin();
106 text_t::const_iterator end = formatstring.end();
107
108 while (here != end) {
109 if (*here != ' ') {
110 if ((*here == '<') && ((here+3) < end)) {
111 if ((*(here+1) == 't' || *(here+1) == 'T') &&
112 (*(here+2) == 'd' || *(here+2) == 'D') &&
113 (*(here+3) == '>' || *(here+3) == ' '))
114 return true;
115 } else return false;
116 }
117 ++here;
118 }
119 return false;
120}
121
122bool is_table_content (const format_t *formatlistptr) {
123
124 if (formatlistptr == NULL) return false;
125
126 if (formatlistptr->command == comText)
127 return is_table_content (formatlistptr->text);
128
129 return false;
130}
131
132// returns false if key isn't in formatstringmap
133bool get_formatstring (const text_t &key, const text_tmap &formatstringmap,
134 text_t &formatstring) {
135
136 formatstring.clear();
137 text_tmap::const_iterator it = formatstringmap.find(key);
138 if (it == formatstringmap.end()) return false;
139 formatstring = (*it).second;
140 return true;
141}
142
143// tries to find "key1key2" then "key1" then "key2"
144bool get_formatstring (const text_t &key1, const text_t &key2,
145 const text_tmap &formatstringmap,
146 text_t &formatstring) {
147
148 formatstring.clear();
149 text_tmap::const_iterator it = formatstringmap.find(key1 + key2);
150 if (it != formatstringmap.end()) {
151 formatstring = (*it).second;
152 return true;
153 }
154 it = formatstringmap.find(key1);
155 if (it != formatstringmap.end()) {
156 formatstring = (*it).second;
157 return true;
158 }
159 it = formatstringmap.find(key2);
160 if (it != formatstringmap.end()) {
161 formatstring = (*it).second;
162 return true;
163 }
164 return false;
165}
166
167
168text_t remove_namespace(const text_t &meta_name) {
169 text_t::const_iterator end = meta_name.end();
170 text_t::const_iterator it = findchar(meta_name.begin(), end, '.');
171 if (it != end) {
172 return substr(it+1, end);
173 }
174 return meta_name;
175
176}
177// returns a date of form 31 _textmonthnn_ 1999
178// input is date of type 19991231
179// at least the year must be present in date
180text_t format_date (const text_t &date) {
181
182 if (date.size() < 4) return "";
183
184 text_t::const_iterator datebegin = date.begin();
185
186 text_t year = substr (datebegin, datebegin+4);
187
188 if (date.size() < 6) return year;
189
190 text_t month = "_textmonth" + substr (datebegin+4, datebegin+6) + "_";
191 int imonth = month.getint();
192 if (imonth < 0 || imonth > 12) return year;
193
194 if (date.size() < 8) return month + " " + year;
195
196 text_t day = substr (datebegin+6, datebegin+8);
197 if (day[0] == '0') day = substr (day.begin()+1, day.end());
198 int iday = day.getint();
199 if (iday < 0 || iday > 31) return month + " " + year;
200
201 return day + " " + month + " " + year;
202}
203
204// converts an iso639 language code to its English equivalent
205// I realize that this isn't the pretiest or most efficient implementation,
206// hopefully this ugly Language (and Date too) formatting won't survive to
207// see gsdl-3.0
208text_t iso639 (const text_t &langcode) {
209
210 if (langcode == "aa") return "Afar";
211 if (langcode == "ab") return "Abkhazian";
212 if (langcode == "af") return "Afrikaans";
213 if (langcode == "am") return "Amharic";
214 if (langcode == "ar") return "Arabic";
215 if (langcode == "as") return "Assamese";
216 if (langcode == "ay") return "Aymara";
217 if (langcode == "az") return "Azerbaijani";
218
219 if (langcode == "ba") return "Bashkir";
220 if (langcode == "be") return "Byelorussian";
221 if (langcode == "bg") return "Bulgarian";
222 if (langcode == "bh") return "Bihari";
223 if (langcode == "bi") return "Bislama";
224 if (langcode == "bn") return "Bengali; Bangla";
225 if (langcode == "bo") return "Tibetan";
226 if (langcode == "br") return "Breton";
227
228 if (langcode == "ca") return "Catalan";
229 if (langcode == "co") return "Corsican";
230 if (langcode == "cs") return "Czech";
231 if (langcode == "cy") return "Welsh";
232
233 if (langcode == "da") return "Danish";
234 if (langcode == "de") return "German";
235 if (langcode == "dz") return "Bhutani";
236
237 if (langcode == "el") return "Greek";
238 if (langcode == "en") return "English";
239 if (langcode == "eo") return "Esperanto";
240 if (langcode == "es") return "Spanish";
241 if (langcode == "et") return "Estonian";
242 if (langcode == "eu") return "Basque";
243
244 if (langcode == "fa") return "Persian";
245 if (langcode == "fi") return "Finnish";
246 if (langcode == "fj") return "Fiji";
247 if (langcode == "fo") return "Faroese";
248 if (langcode == "fr") return "French";
249 if (langcode == "fy") return "Frisian";
250
251 if (langcode == "ga") return "Irish";
252 if (langcode == "gd") return "Scots Gaelic";
253 if (langcode == "gl") return "Galician";
254 if (langcode == "gn") return "Guarani";
255 if (langcode == "gu") return "Gujarati";
256
257 if (langcode == "ha") return "Hausa";
258 if (langcode == "hi") return "Hindi";
259 if (langcode == "hr") return "Croatian";
260 if (langcode == "hu") return "Hungarian";
261 if (langcode == "hy") return "Armenian";
262
263 if (langcode == "ia") return "Interlingua";
264 if (langcode == "ie") return "Interlingue";
265 if (langcode == "ik") return "Inupiak";
266 if (langcode == "in") return "Indonesian";
267 if (langcode == "is") return "Icelandic";
268 if (langcode == "it") return "Italian";
269 if (langcode == "iw") return "Hebrew";
270
271 if (langcode == "ja") return "Japanese";
272 if (langcode == "ji") return "Yiddish";
273 if (langcode == "jw") return "Javanese";
274
275 if (langcode == "ka") return "Georgian";
276 if (langcode == "kk") return "Kazakh";
277 if (langcode == "kl") return "Greenlandic";
278 if (langcode == "km") return "Cambodian";
279 if (langcode == "kn") return "Kannada";
280 if (langcode == "ko") return "Korean";
281 if (langcode == "ks") return "Kashmiri";
282 if (langcode == "ku") return "Kurdish";
283 if (langcode == "ky") return "Kirghiz";
284
285 if (langcode == "la") return "Latin";
286 if (langcode == "ln") return "Lingala";
287 if (langcode == "lo") return "Laothian";
288 if (langcode == "lt") return "Lithuanian";
289 if (langcode == "lv") return "Latvian, Lettish";
290
291 if (langcode == "mg") return "Malagasy";
292 if (langcode == "mi") return "Maori";
293 if (langcode == "mk") return "Macedonian";
294 if (langcode == "ml") return "Malayalam";
295 if (langcode == "mn") return "Mongolian";
296 if (langcode == "mo") return "Moldavian";
297 if (langcode == "mr") return "Marathi";
298 if (langcode == "ms") return "Malay";
299 if (langcode == "mt") return "Maltese";
300 if (langcode == "my") return "Burmese";
301
302 if (langcode == "na") return "Nauru";
303 if (langcode == "ne") return "Nepali";
304 if (langcode == "nl") return "Dutch";
305 if (langcode == "no") return "Norwegian";
306
307 if (langcode == "oc") return "Occitan";
308 if (langcode == "om") return "(Afan) Oromo";
309 if (langcode == "or") return "Oriya";
310
311 if (langcode == "pa") return "Punjabi";
312 if (langcode == "pl") return "Polish";
313 if (langcode == "ps") return "Pashto, Pushto";
314 if (langcode == "pt") return "Portuguese";
315
316 if (langcode == "qu") return "Quechua";
317 if (langcode == "rm") return "Rhaeto-Romance";
318 if (langcode == "rn") return "Kirundi";
319 if (langcode == "ro") return "Romanian";
320 if (langcode == "ru") return "Russian";
321 if (langcode == "rw") return "Kinyarwanda";
322
323 if (langcode == "sa") return "Sanskrit";
324 if (langcode == "sd") return "Sindhi";
325 if (langcode == "sg") return "Sangro";
326 if (langcode == "sh") return "Serbo-Croatian";
327 if (langcode == "si") return "Singhalese";
328 if (langcode == "sk") return "Slovak";
329 if (langcode == "sl") return "Slovenian";
330 if (langcode == "sm") return "Samoan";
331 if (langcode == "sn") return "Shona";
332 if (langcode == "so") return "Somali";
333 if (langcode == "sq") return "Albanian";
334 if (langcode == "sr") return "Serbian";
335 if (langcode == "ss") return "Siswati";
336 if (langcode == "st") return "Sesotho";
337 if (langcode == "su") return "Sudanese";
338 if (langcode == "sv") return "Swedish";
339 if (langcode == "sw") return "Swahili";
340
341 if (langcode == "ta") return "Tamil";
342 if (langcode == "te") return "Tegulu";
343 if (langcode == "tg") return "Tajik";
344 if (langcode == "th") return "Thai";
345 if (langcode == "ti") return "Tigrinya";
346 if (langcode == "tk") return "Turkmen";
347 if (langcode == "tl") return "Tagalog";
348 if (langcode == "tn") return "Setswana";
349 if (langcode == "to") return "Tonga";
350 if (langcode == "tr") return "Turkish";
351 if (langcode == "ts") return "Tsonga";
352 if (langcode == "tt") return "Tatar";
353 if (langcode == "tw") return "Twi";
354
355 if (langcode == "uk") return "Ukrainian";
356 if (langcode == "ur") return "Urdu";
357 if (langcode == "uz") return "Uzbek";
358
359 if (langcode == "vi") return "Vietnamese";
360 if (langcode == "vo") return "Volapuk";
361
362 if (langcode == "wo") return "Wolof";
363
364 if (langcode == "xh") return "Xhosa";
365
366 if (langcode == "yo") return "Yoruba";
367
368 if (langcode == "zh") return "Chinese";
369 if (langcode == "zu") return "Zulu";
370 return "";
371}
372
373text_t get_href (const text_t &link) {
374
375 text_t href;
376
377 text_t::const_iterator here = findchar(link.begin(), link.end(), '"');
378 text_t::const_iterator end = link.end();
379
380 ++here;
381 while (here != end) {
382 if (*here == '"') break;
383 href.push_back(*here);
384 ++here;
385 }
386
387 return href;
388}
389
390//this function gets the information associated with the relation
391//metadata for the document associated with 'docinfo'. This relation
392//metadata consists of a line of pairs containing 'collection, document OID'
393//(this is the OID of the document related to the current document, and
394//the collection the related document belongs to). For each of these pairs
395//the title metadata is obtained and then an html link between the title
396//of the related doc and the document's position (the document will be
397//found in "<a href=\"_httpdocument_&c=collection&cl=search&d=OID">
398//(where collection is the related documents collection, and OID is the
399//related documents OID). A list of these html links are made for as many
400//related documents as there are. This list is then returned. If there are
401//no related documents available for the current document then the string
402//'.. no related documents .. ' is returned.
403text_t get_related_docs(const text_t& collection, recptproto* collectproto,
404 ResultDocInfo_t &docinfo, ostream& logout){
405
406 text_tset metadata;
407
408 //insert the metadata we wish to collect
409 metadata.insert("relation");
410 metadata.insert("Title");
411 metadata.insert("Subject"); //for emails, where title data doesn't apply
412
413 FilterResponse_t response;
414 text_t relation = ""; //string for displaying relation metadata
415 text_t relationTitle = ""; //the related documents Title (or subject)
416 text_t relationOID = ""; //the related documents OID
417
418 //get the information associated with the metadata for current doc
419 if (get_info (docinfo.OID, collection, "", metadata,
420 false, collectproto, response, logout)) {
421
422 //if the relation metadata exists, store for displaying
423 if(!response.docInfo[0].metadata["relation"].values.empty()){
424 relationOID += response.docInfo[0].metadata["relation"].values[0];
425
426 //split relation data into pairs of collectionname,ID number
427 text_tarray relationpairs;
428 splitchar (relationOID.begin(), relationOID.end(), ' ', relationpairs);
429
430 text_tarray::const_iterator currDoc = relationpairs.begin();
431 text_tarray::const_iterator lastDoc = relationpairs.end();
432
433 //iterate through the pairs to split and display
434 while(currDoc != lastDoc){
435
436 //split pairs into collectionname and ID
437 text_tarray relationdata;
438 splitchar ((*currDoc).begin(), (*currDoc).end(), ',', relationdata);
439
440 //get first element in the array (collection)
441 text_tarray::const_iterator doc_data = relationdata.begin();
442 text_t document_collection = *doc_data;
443 ++doc_data; //increment to get next item in array (oid)
444 text_t document_OID = *doc_data;
445
446 //create html link to related document
447 relation += "<a href=\"_httpdocument_&c=" + document_collection;
448 relation += "&cl=search&d=" + document_OID;
449
450 //get the information associated with the metadata for related doc
451 if (get_info (document_OID, document_collection, "", metadata,
452 false, collectproto, response, logout)) {
453
454 //if title metadata doesn't exist, collect subject metadata
455 //if that doesn't exist, just call it 'related document'
456 if (!response.docInfo[0].metadata["Title"].values[0].empty())
457 relationTitle = response.docInfo[0].metadata["Title"].values[0];
458 else if (!response.docInfo[0].metadata["Subject"].values.empty())
459 relationTitle = response.docInfo[0].metadata["Subject"].values[0];
460 else relationTitle = "RELATED DOCUMENT";
461
462 }
463
464 //link the related document's title to its page
465 relation += "\">" + relationTitle + "</a>";
466 relation += " (" + document_collection + ")<br>";
467
468 ++currDoc;
469 }
470 }
471
472 }
473
474 if(relation.empty()) //no relation data for documnet
475 relation = ".. no related documents .. ";
476
477 return relation;
478}
479
480
481
482static void get_parent_options (text_t &instring, metadata_t &metaoption) {
483
484 assert (instring.size() > 7);
485 if (instring.size() <= 7) return;
486
487 text_t meta, com, op;
488 bool inbraces = false;
489 bool inquotes = false;
490 bool foundcolon = false;
491 text_t::const_iterator here = instring.begin()+6;
492 text_t::const_iterator end = instring.end();
493 while (here != end) {
494 if (foundcolon) meta.push_back (*here);
495 else if (*here == '(') inbraces = true;
496 else if (*here == ')') inbraces = false;
497 else if (*here == '\'' && !inquotes) inquotes = true;
498 else if (*here == '\'' && inquotes) inquotes = false;
499 else if (*here == ':' && !inbraces) foundcolon = true;
500 else if (inquotes) op.push_back (*here);
501 else com.push_back (*here);
502 ++here;
503 }
504
505 instring = meta;
506 if (com.empty())
507 metaoption.mqualifier.parent = pImmediate;
508 else if (com == "Top")
509 metaoption.mqualifier.parent = pTop;
510 else if (com == "All") {
511 metaoption.mqualifier.parent = pAll;
512 metaoption.parentoptions = op;
513 }
514}
515
516
517static void get_sibling_options (text_t &instring, metadata_t &metaoption) {
518
519 assert (instring.size() > 8);
520 if (instring.size() <= 8) return;
521 text_t meta, com, op;
522 bool inbraces = false;
523 bool inquotes = false;
524 bool foundcolon = false;
525 text_t::const_iterator here = instring.begin()+7;
526 text_t::const_iterator end = instring.end();
527 while (here != end) {
528 if (foundcolon) meta.push_back (*here);
529 else if (*here == '(') inbraces = true;
530 else if (*here == ')') inbraces = false;
531 else if (*here == '\'' && !inquotes) inquotes = true;
532 else if (*here == '\'' && inquotes) inquotes = false;
533 else if (*here == ':' && !inbraces) foundcolon = true;
534 else if (inquotes) op.push_back (*here);
535 else com.push_back (*here);
536 ++here;
537 }
538
539 instring = meta;
540 metaoption.siblingoptions.clear();
541
542 if (com.empty()) {
543 metaoption.mqualifier.sibling = sAll;
544 metaoption.siblingoptions = " ";
545 }
546 else if (com == "first") {
547 metaoption.mqualifier.sibling = sNum;
548 metaoption.siblingoptions = "0";
549 }
550 else if (com == "last") {
551 metaoption.mqualifier.sibling = sNum;
552 metaoption.siblingoptions = "-2"; // == last
553 }
554 else if (com.getint()>0) {
555 metaoption.mqualifier.sibling = sNum;
556 int pos = com.getint()-1;
557 metaoption.siblingoptions +=pos;
558 }
559 else {
560 metaoption.mqualifier.sibling = sAll;
561 metaoption.siblingoptions = op;
562 }
563}
564
565static void get_child_options (text_t &instring, metadata_t &metaoption) {
566
567 assert (instring.size() > 6);
568 if (instring.size() <= 6) return;
569 text_t meta, com, op;
570 bool inbraces = false;
571 bool inquotes = false;
572 bool foundcolon = false;
573 text_t::const_iterator here = instring.begin()+5;
574 text_t::const_iterator end = instring.end();
575 while (here != end) {
576 if (foundcolon) meta.push_back (*here);
577 else if (*here == '(') inbraces = true;
578 else if (*here == ')') inbraces = false;
579 else if (*here == '\'' && !inquotes) inquotes = true;
580 else if (*here == '\'' && inquotes) inquotes = false;
581 else if (*here == ':' && !inbraces) foundcolon = true;
582 else if (inquotes) op.push_back (*here);
583 else com.push_back (*here);
584 ++here;
585 }
586
587 instring = meta;
588 if (com.empty()) {
589 metaoption.mqualifier.child = cAll;
590 metaoption.childoptions = " ";
591 }
592 else if (com == "first") {
593 metaoption.mqualifier.child = cNum;
594 metaoption.childoptions = ".fc";
595 }
596 else if (com == "last") {
597 metaoption.mqualifier.child = cNum;
598 metaoption.childoptions = ".lc";
599 }
600 else if (com.getint()>0) {
601 metaoption.mqualifier.child = cNum;
602 metaoption.childoptions = "."+com;
603 }
604 else {
605 metaoption.mqualifier.child = cAll;
606 metaoption.childoptions = op;
607 }
608}
609
610
611
612static void parse_meta (text_t &meta, metadata_t &metaoption,
613 text_tset &metadata, bool &getParents) {
614
615 if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
616 metaoption.metacommand |= mCgiSafe;
617 meta = substr (meta.begin()+8, meta.end());
618 }
619
620 if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
621 getParents = true;
622 metaoption.metacommand |= mParent;
623 get_parent_options (meta, metaoption);
624 }
625 else if (meta.size() > 6 && (substr (meta.begin(), meta.begin()+5) == "child")) {
626 metaoption.metacommand |= mChild;
627 get_child_options (meta, metaoption);
628 metadata.insert("contains");
629 }
630 // parent and child can have sibling also
631 if (meta.size() > 8 && (substr (meta.begin(), meta.begin()+7) == "sibling")) {
632 metaoption.metacommand |= mSibling;
633 get_sibling_options (meta, metaoption);
634 }
635
636 // check for ex. which may occur in format statements
637 if (meta.size()>3 && (substr(meta.begin(), meta.begin()+3) == "ex.")) {
638 meta = substr (meta.begin()+3, meta.end());
639 }
640 metadata.insert (meta);
641 metaoption.metaname = meta;
642}
643
644static void parse_coll_meta(text_t &meta, metadata_t &metaoption) {
645 if (meta == "collection") {
646 // no qualifiers
647 metaoption.metaname = g_EmptyText;
648 return;
649 }
650 meta = substr (meta.begin()+11, meta.end());
651 metaoption.metaname = meta;
652
653}
654
655static void parse_meta (text_t &meta, format_t *formatlistptr,
656 text_tset &metadata, bool &getParents) {
657
658 if (meta == "link")
659 formatlistptr->command = comLink;
660 else if (meta == "/link")
661 formatlistptr->command = comEndLink;
662
663 else if (meta == "href")
664 formatlistptr->command = comHref;
665
666 else if (meta == "num")
667 formatlistptr->command = comNum;
668
669 else if (meta == "icon")
670 formatlistptr->command = comIcon;
671
672 else if (meta == "Text")
673 formatlistptr->command = comDoc;
674
675 else if (meta == "RelatedDocuments")
676 formatlistptr->command = comRel;
677
678 else if (meta == "highlight")
679 formatlistptr->command = comHighlight;
680
681 else if (meta == "/highlight")
682 formatlistptr->command = comEndHighlight;
683
684 else if (meta == "Summary")
685 formatlistptr->command = comSummary;
686
687 else if (meta == "DocImage")
688 formatlistptr->command = comImage;
689
690 else if (meta == "DocTOC")
691 formatlistptr->command = comTOC;
692
693 else if (meta == "DocumentButtonDetach")
694 formatlistptr->command = comDocumentButtonDetach;
695
696 else if (meta == "DocumentButtonHighlight")
697 formatlistptr->command = comDocumentButtonHighlight;
698
699 else if (meta == "DocumentButtonExpandContents")
700 formatlistptr->command = comDocumentButtonExpandContents;
701
702 else if (meta == "DocumentButtonExpandText")
703 formatlistptr->command = comDocumentButtonExpandText;
704
705 else if (meta == "DocOID")
706 formatlistptr->command = comOID;
707 else if (meta == "DocRank")
708 formatlistptr->command = comRank;
709 else if (meta.size() >= 10 && (substr(meta.begin(), meta.begin()+10) == "collection")) {
710 formatlistptr->command = comCollection;
711 parse_coll_meta(meta, formatlistptr->meta);
712 }
713 else {
714 formatlistptr->command = comMeta;
715 parse_meta (meta, formatlistptr->meta, metadata, getParents);
716 }
717}
718
719
720static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
721 text_tset &metadata, bool &getParents) {
722
723 text_t text;
724 text_t::const_iterator here = formatstring.begin();
725 text_t::const_iterator end = formatstring.end();
726
727 while (here != end) {
728
729 if (*here == '\\') {
730 ++here;
731 if (here != end) text.push_back (*here);
732
733 } else if (*here == '{') {
734 if (!text.empty()) {
735 formatlistptr->command = comText;
736 formatlistptr->text = text;
737 formatlistptr->nextptr = new format_t();
738 formatlistptr = formatlistptr->nextptr;
739
740 text.clear();
741 }
742 if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
743
744 formatlistptr->nextptr = new format_t();
745 formatlistptr = formatlistptr->nextptr;
746 if (here == end) break;
747 }
748 } else if (*here == '[') {
749 if (!text.empty()) {
750 formatlistptr->command = comText;
751 formatlistptr->text = text;
752 formatlistptr->nextptr = new format_t();
753 formatlistptr = formatlistptr->nextptr;
754
755 text.clear();
756 }
757 text_t meta;
758 ++here;
759 while (*here != ']') {
760 if (here == end) return false;
761 meta.push_back (*here);
762 ++here;
763 }
764 parse_meta (meta, formatlistptr, metadata, getParents);
765 formatlistptr->nextptr = new format_t();
766 formatlistptr = formatlistptr->nextptr;
767
768 } else
769 text.push_back (*here);
770
771 if (here != end) ++here;
772 }
773 if (!text.empty()) {
774 formatlistptr->command = comText;
775 formatlistptr->text = text;
776 formatlistptr->nextptr = new format_t();
777 formatlistptr = formatlistptr->nextptr;
778
779 }
780 return true;
781}
782
783
784static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
785 format_t *formatlistptr, text_tset &metadata, bool &getParents) {
786
787 text_t::const_iterator it = findchar (here, end, '}');
788 if (it == end) return false;
789
790 text_t com = substr (here, it);
791 here = findchar (it, end, '{');
792 if (here == end) return false;
793 else ++here;
794
795 if (com == "If" || com == "if" || com == "IF") formatlistptr->command = comIf;
796 else if (com == "Or" || com == "or" || com == "OR") formatlistptr->command = comOr;
797 else return false;
798
799 int commacount = 0;
800 text_t text;
801 while (here != end) {
802
803 if (*here == '\\') {
804 ++here;
805 if (here != end) text.push_back(*here);
806
807 }
808
809 else if (*here == ',' || *here == '}' || *here == '{') {
810
811 if (formatlistptr->command == comOr) {
812 // the {Or}{this, or this, or this, or this} statement
813 format_t *or_ptr;
814
815 // find the next unused orptr
816 if (formatlistptr->orptr == NULL) {
817 formatlistptr->orptr = new format_t();
818 or_ptr = formatlistptr->orptr;
819 } else {
820 or_ptr = formatlistptr->orptr;
821 while (or_ptr->nextptr != NULL)
822 or_ptr = or_ptr->nextptr;
823 or_ptr->nextptr = new format_t();
824 or_ptr = or_ptr->nextptr;
825 }
826
827 if (!text.empty())
828 {
829 if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
830 }
831
832 if (*here == '{')
833 {
834 // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
835 // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
836 // The latter can always be re-written:
837 // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
838
839 if (!text.empty()) // already used up allocated format_t
840 {
841 // => allocate new one for detected action
842 or_ptr->nextptr = new format_t();
843 or_ptr = or_ptr->nextptr;
844 }
845 if (!parse_action(++here, end, or_ptr, metadata, getParents))
846 {
847 return false;
848 }
849 }
850 else
851 {
852 if (*here == '}') break;
853 }
854 text.clear();
855
856 }
857
858 // Parse an {If}{decide,do,else} statement
859 else {
860
861 // Read the decision component.
862 if (commacount == 0) {
863 // Decsion can be a metadata element, or a piece of text.
864 // Originally Stefan's code, updated 25/10/2000 by Gordon.
865
866 text_t::const_iterator beginbracket = text.begin();
867 text_t::const_iterator endbracket = (text.end() - 1);
868
869 // Decision is based on a metadata element
870 if ((*beginbracket == '[') && (*endbracket == ']')) {
871 // Ignore the surrounding square brackets
872 text_t meta = substr (beginbracket+1, endbracket);
873 parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
874 ++commacount;
875 text.clear();
876 }
877
878 // Decision is a piece of text (probably a macro like _cgiargmode_).
879 else {
880
881 // hunt for any metadata in string, which might be uses in
882 // to test a condition, e.g. [Format] eq 'PDF'
883 format_t* dummyformat = new format_t();
884 // update which metadata fields needed
885 // (not interested in updatng formatlistptr)
886 parse_string (text, dummyformat, metadata, getParents);
887 delete dummyformat;
888
889 formatlistptr->decision.command = dText;
890 formatlistptr->decision.text = text;
891 ++commacount;
892 text.clear();
893 }
894 }
895
896 // Read the "then" and "else" components of the {If} statement.
897 else {
898 format_t** nextlistptr = NULL;
899 if (commacount == 1) {
900 nextlistptr = &formatlistptr->ifptr;
901 } else if (commacount == 2 ) {
902 nextlistptr = &formatlistptr->elseptr;
903 } else {
904 return false;
905 }
906
907 if (!text.empty()) {
908 if (*nextlistptr == NULL) {
909 *nextlistptr = new format_t();
910 } else {
911
912 // skip to the end of any format_t statements already added
913 while ((*nextlistptr)->nextptr != NULL)
914 {
915 nextlistptr = &(*nextlistptr)->nextptr;
916 }
917
918 (*nextlistptr)->nextptr = new format_t();
919 nextlistptr = &(*nextlistptr)->nextptr;
920 }
921
922 if (!parse_string (text, *nextlistptr, metadata, getParents))
923 {
924 return false;
925 }
926 text.clear();
927 }
928
929 if (*here == '{')
930 {
931 if (*nextlistptr == NULL) {
932 *nextlistptr = new format_t();
933 } else {
934 // skip to the end of any format_t statements already added
935 while ((*nextlistptr)->nextptr != NULL)
936 {
937 nextlistptr = &(*nextlistptr)->nextptr;
938 }
939
940 (*nextlistptr)->nextptr = new format_t();
941 nextlistptr = &(*nextlistptr)->nextptr;
942 }
943
944 if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
945 {
946 return false;
947 }
948 }
949 else
950 {
951 if (*here == '}') break;
952 ++commacount;
953 }
954 }
955 }
956
957 } else text.push_back(*here);
958
959 if (here != end) ++here;
960 }
961
962 return true;
963}
964
965
966bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
967 text_tset &metadata, bool &getParents) {
968
969 formatlistptr->clear();
970 getParents = false;
971
972 return (parse_string (formatstring, formatlistptr, metadata, getParents));
973}
974
975// position -1 for all, -2 for the last, 0 for the first, or x for a particular piece
976// metainfo has all the values for the metadata, meta has the request metadata name and options, position tells which values to get
977static text_t get_formatted_meta_text(MetadataInfo_t &metainfo, const metadata_t &meta, int position, bool no_cgisafe = false)
978{
979 text_t no_ns_metaname = remove_namespace(meta.metaname);
980 text_t tmp;
981 bool first = true;
982
983 const int start_i=0;
984 const int end_i = metainfo.values.size()-1;
985
986 if (position == -1) { // all
987 for (int i=start_i; i<=end_i; ++i) {
988 if (!first) tmp += meta.siblingoptions;
989 if (no_ns_metaname == "Date") tmp += format_date (metainfo.values[i]);
990 else if (no_ns_metaname == "Language") tmp += iso639(metainfo.values[i]);
991 else tmp += metainfo.values[i];
992 first = false;
993
994 }
995 } else {
996 if (position == -2) { // end
997 position = end_i;
998 } else if (position < start_i || position > end_i) {
999 return "";
1000 }
1001 if (no_ns_metaname == "Date") tmp += format_date (metainfo.values[position]);
1002 else if (no_ns_metaname == "Language") tmp += iso639(metainfo.values[position]);
1003 else tmp += metainfo.values[position];
1004 }
1005 if (meta.metacommand & mCgiSafe && !no_cgisafe) return cgi_safe (tmp);
1006 else return tmp;
1007}
1008
1009static text_t get_parent_meta (ResultDocInfo_t &docinfo, const metadata_t &meta, int siblings_values)
1010{
1011
1012 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
1013 switch (meta.mqualifier.parent) {
1014 case pNone:
1015 return "Nothing!!";
1016 break;
1017
1018 case pImmediate:
1019 if (parent != NULL) {
1020 return get_formatted_meta_text(*parent, meta, siblings_values);
1021 }
1022 break;
1023
1024 case pTop:
1025 if (parent != NULL) {
1026 while (parent->parent != NULL) parent = parent->parent;
1027 return get_formatted_meta_text(*parent, meta, siblings_values);
1028 }
1029 break;
1030
1031 case pAll:
1032 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
1033 if (parent != NULL) {
1034 text_tarray tmparray;
1035 while (parent != NULL) {
1036 tmparray.push_back (get_formatted_meta_text(*parent, meta, siblings_values, true)); // set no_cgisafe to true, as we'll do it once we have all the metadata
1037 parent = parent->parent;
1038 }
1039 // now join them up - use teh parent separator
1040 bool first = true;
1041 text_t tmp;
1042 text_tarray::reverse_iterator here = tmparray.rbegin();
1043 text_tarray::reverse_iterator end = tmparray.rend();
1044 while (here != end) {
1045 if (!first) tmp += meta.parentoptions;
1046 tmp += *here;
1047 first = false;
1048 ++here;
1049 }
1050 if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
1051 else return tmp;
1052 }
1053 }
1054 return "";
1055
1056}
1057
1058static text_t get_child_meta (const text_t& collection,
1059 recptproto* collectproto,
1060 ResultDocInfo_t &docinfo, displayclass &disp,
1061 const metadata_t &meta, text_tmap &options,
1062 ostream& logout, int siblings_values)
1063{
1064 if (docinfo.metadata["contains"].values[0].size()==0) return ""; // no children
1065
1066 const text_t& child_metaname = meta.metaname;
1067 const text_t& child_field = meta.childoptions;
1068 text_tset child_metadata;
1069 child_metadata.insert(child_metaname);
1070
1071 FilterResponse_t child_response;
1072 if (meta.mqualifier.child == cNum) {
1073 // just one child
1074 //get the information associated with the metadata for child doc
1075 if (!get_info (docinfo.OID+child_field, collection, "", child_metadata,
1076 false, collectproto, child_response, logout)) return ""; // invalid child number
1077
1078 if (child_response.docInfo.empty()) return false; // no info for the child
1079
1080 ResultDocInfo_t& child_docinfo = child_response.docInfo[0];
1081 MetadataInfo_t& metaname_rec = child_docinfo.metadata[child_metaname];
1082
1083 text_t child_metavalue = get_formatted_meta_text(metaname_rec,meta,siblings_values);
1084 return expand_metadata(child_metavalue,collection,collectproto,
1085 child_docinfo,disp,options,logout);
1086 }
1087
1088
1089 if (meta.mqualifier.child != cAll) return false; // invalid qualifier
1090
1091 // we need to get all children
1092 text_t result = "";
1093 text_tarray children;
1094 text_t contains = docinfo.metadata["contains"].values[0];
1095 splitchar (contains.begin(), contains.end(), ';', children);
1096 text_tarray::const_iterator here = children.begin();
1097 text_tarray::const_iterator end = children.end();
1098 bool first = true;
1099 while (here !=end) {
1100 text_t oid = *here;
1101 here++;
1102 if (*(oid.begin()) == '"') translate_parent (oid, docinfo.OID);
1103
1104
1105 //get the information associated with the metadata for child doc
1106 if (!get_info (oid, collection, "", child_metadata,
1107 false, collectproto, child_response, logout) ||
1108 child_response.docInfo.empty()) {
1109 first = false;
1110 continue;
1111 }
1112
1113
1114 ResultDocInfo_t& child_docinfo = child_response.docInfo[0];
1115 MetadataInfo_t& metaname_rec = child_docinfo.metadata[child_metaname];
1116
1117 text_t child_metavalue = get_formatted_meta_text(metaname_rec,meta,siblings_values);
1118 if (!first) result += child_field;
1119 first = false;
1120 // need to do this here cos otherwise we are in the wrong document
1121 result += expand_metadata(child_metavalue,collection,collectproto,
1122 child_docinfo,disp,options,logout);
1123 }
1124 return result;
1125
1126}
1127
1128
1129// note: all the format_date stuff is assuming that all Date metadata is going to
1130// be of the form yyyymmdd, this is of course, crap ;)
1131
1132static text_t get_meta (const text_t& collection, recptproto* collectproto,
1133 ResultDocInfo_t &docinfo, displayclass &disp,
1134 const metadata_t &meta, text_tmap &options,
1135 ostream& logout) {
1136
1137 // make sure we have the requested metadata
1138 MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
1139 if (it == docinfo.metadata.end()) return "";
1140
1141 int siblings_values = 0; // default is no siblings, just the first metadata available
1142 if (meta.metacommand & mSibling) {
1143 if (meta.mqualifier.sibling == sAll) {
1144 siblings_values = -1; //all
1145 } else if (meta.mqualifier.sibling == sNum) {
1146 siblings_values = meta.siblingoptions.getint();
1147 }
1148 }
1149 if (meta.metacommand & mParent) {
1150 return get_parent_meta(docinfo,meta,siblings_values);
1151 }
1152
1153 else if (meta.metacommand & mChild) {
1154 return get_child_meta(collection,collectproto,docinfo,disp,meta,
1155 options,logout, siblings_values);
1156 }
1157 else if (meta.metacommand & mSibling) { // only siblings
1158 MetadataInfo_t& metaname_rec = docinfo.metadata[meta.metaname];
1159 return get_formatted_meta_text(docinfo.metadata[meta.metaname],meta, siblings_values);
1160 }
1161 else {
1162
1163 // straightforward metadata request (nothing fancy)
1164
1165 text_t classifier_metaname = docinfo.classifier_metadata_type;
1166 int metaname_index
1167 = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
1168 return get_formatted_meta_text(docinfo.metadata[meta.metaname], meta, metaname_index);
1169 }
1170
1171 return "";
1172}
1173
1174static text_t get_or (const text_t& collection, recptproto* collectproto,
1175 ResultDocInfo_t &docinfo, displayclass &disp,
1176 format_t *orptr, text_tmap &options,
1177 ostream& logout) {
1178
1179 text_t tmp;
1180 while (orptr != NULL) {
1181
1182 tmp = format_string (collection,collectproto,docinfo, disp, orptr,
1183 options, logout);
1184 if (!tmp.empty()) return tmp;
1185
1186 orptr = orptr->nextptr;
1187 }
1188 return "";
1189}
1190
1191static bool char_is_whitespace(const char c)
1192{
1193 return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
1194
1195}
1196
1197static int scan_over_whitespace(const text_t& outstring, const int start_pos)
1198{
1199 int pos = start_pos;
1200 while (pos<outstring.size()) {
1201 if (!char_is_whitespace(outstring[pos])) {
1202 break;
1203 }
1204 ++pos;
1205 }
1206
1207 return pos;
1208}
1209
1210static int rscan_over_whitespace(const text_t& outstring, const int start_pos)
1211{
1212 int pos = start_pos;
1213 while (pos>=0) {
1214 if (!char_is_whitespace(outstring[pos])) {
1215 break;
1216 }
1217 --pos;
1218 }
1219
1220 return pos;
1221}
1222
1223static int rscan_for_whitespace(const text_t& outstring, const int start_pos)
1224{
1225 int pos = start_pos;
1226 while (pos>=0) {
1227 if (char_is_whitespace(outstring[pos])) {
1228 break;
1229 }
1230 --pos;
1231 }
1232
1233 return pos;
1234}
1235
1236
1237static int rscan_for(const text_t& outstring, const int start_pos,
1238 const char find_c)
1239{
1240 int pos = start_pos;
1241 while (pos>=0) {
1242 char c = outstring[pos];
1243 if (outstring[pos] == find_c) {
1244 break;
1245 }
1246 --pos;
1247 }
1248
1249 return pos;
1250}
1251
1252text_t extract_substr(const text_t& outstring, const int start_pos,
1253 const int end_pos)
1254{
1255 text_t extracted_str;
1256 extracted_str.clear();
1257
1258 for (int pos=start_pos; pos<=end_pos; ++pos) {
1259 extracted_str.push_back(outstring[pos]);
1260 }
1261
1262 return extracted_str;
1263}
1264
1265
1266static text_t expand_potential_metadata(const text_t& collection,
1267 recptproto* collectproto,
1268 ResultDocInfo_t &docinfo,
1269 displayclass &disp,
1270 const text_t& intext,
1271 text_tmap &options,
1272 ostream& logout)
1273{
1274 text_t outtext;
1275
1276 // decide if dealing with metadata or text
1277
1278 text_t::const_iterator beginbracket = intext.begin();
1279 text_t::const_iterator endbracket = (intext.end() - 1);
1280
1281 // Decision is based on a metadata element
1282 if ((*beginbracket == '[') && (*endbracket == ']')) {
1283 // Ignore the surrounding square brackets
1284 text_t meta_text = substr (beginbracket+1, endbracket);
1285
1286 if (meta_text == "Text") {
1287 outtext = format_text(collection, collectproto, docinfo, disp, options, logout);
1288 } else {
1289
1290 text_tset metadata;
1291 bool getParents =false;
1292 metadata_t meta;
1293
1294 parse_meta (meta_text, meta, metadata, getParents);
1295 outtext
1296 = get_meta (collection,collectproto,docinfo,disp,meta,options,logout);
1297 }
1298
1299 }
1300 else {
1301 outtext = intext;
1302 }
1303
1304 return outtext;
1305}
1306
1307
1308
1309
1310static bool uses_expression(const text_t& collection, recptproto* collectproto,
1311 ResultDocInfo_t &docinfo,
1312 displayclass &disp,
1313 const text_t& outstring, text_t& lhs_expr,
1314 text_t& op_expr, text_t& rhs_expr,
1315 text_tmap &options,
1316 ostream& logout)
1317{
1318 // Note: the string may not be of the form: str1 op str2, however
1319 // to deterine this we have to process it on the assumption it is,
1320 // and if at any point an 'erroneous' value is encountered, return
1321 // false and let something else have a go at evaluating it
1322
1323 // Starting at the end of the string and working backwards ..
1324
1325 const int outstring_len = outstring.size();
1326
1327 // skip over white space
1328 int rhs_end = rscan_over_whitespace(outstring,outstring_len-1);
1329
1330 if (rhs_end<=0) {
1331 // no meaningful text or (rhs_end==0) no room for operator
1332 return false;
1333 }
1334
1335 // check for ' or " and then scan over token
1336 const char potential_quote = outstring[rhs_end];
1337 int rhs_start=rhs_end;
1338 bool quoted = false;
1339
1340 if ((potential_quote == '\'') || (potential_quote == '\"')) {
1341 --rhs_end;
1342 rhs_start = rscan_for(outstring,rhs_end-1,potential_quote) +1;
1343 quoted = true;
1344 }
1345 else {
1346 rhs_start = rscan_for_whitespace(outstring,rhs_end-1) +1;
1347 }
1348
1349 if ((rhs_end-rhs_start)<0) {
1350 // no meaningful rhs expression
1351 return false;
1352 }
1353
1354 // form rhs_expr
1355 rhs_expr = extract_substr(outstring,rhs_start,rhs_end);
1356
1357 // skip over white space
1358 const int to_whitespace = (quoted) ? 2 : 1;
1359
1360 int op_end = rscan_over_whitespace(outstring,rhs_start-to_whitespace);
1361 int op_start = rscan_for_whitespace(outstring,op_end-1)+1;
1362
1363
1364 if (op_end-op_start<0) {
1365 // no meaningful expression operator
1366 return false;
1367 }
1368
1369 op_expr = extract_substr(outstring,op_start,op_end);
1370
1371
1372 // check for operator
1373 if ((op_expr != "eq") && (op_expr != "ne") && (op_expr != "gt") &&
1374 (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")) {
1375
1376 // not a valid operator
1377 return false;
1378 }
1379
1380 int lhs_end = rscan_over_whitespace(outstring,op_start-1);
1381 if (lhs_end<0) {
1382 // no meaningful lhs expression
1383 return false;
1384 }
1385
1386 int lhs_start = scan_over_whitespace(outstring,0);
1387
1388 // form lhs_expr from remainder of string
1389 lhs_expr = extract_substr(outstring,lhs_start,lhs_end);
1390
1391 // Now we know we have a valid expression, look up any
1392 // metadata terms
1393
1394 rhs_expr = expand_potential_metadata(collection,collectproto,docinfo,
1395 disp,rhs_expr,options,logout);
1396 lhs_expr = expand_potential_metadata(collection,collectproto,docinfo,
1397 disp,lhs_expr,options,logout);
1398
1399 return true;
1400}
1401
1402static bool eval_expression_true(const text_t& lhs_expr,const text_t& op_expr,
1403 const text_t& rhs_expr, ostream& logout)
1404{
1405 if (op_expr == "eq") return (lhs_expr == rhs_expr);
1406 else if (op_expr == "ne" ) return (lhs_expr != rhs_expr);
1407 else if (op_expr == "gt") return (lhs_expr > rhs_expr);
1408 else if (op_expr == "ge") return (lhs_expr >= rhs_expr);
1409 else if (op_expr == "lt") return (lhs_expr < rhs_expr);
1410 else if (op_expr == "le") return (lhs_expr <= rhs_expr);
1411 else if (op_expr == "==") return (lhs_expr.getint() == rhs_expr.getint());
1412 else if (op_expr == "!=") return (lhs_expr.getint() != rhs_expr.getint());
1413 else if (op_expr == ">") return (lhs_expr.getint() > rhs_expr.getint());
1414 else if (op_expr == ">=") return (lhs_expr.getint() >= rhs_expr.getint());
1415 else if (op_expr == "<") return (lhs_expr.getint() < rhs_expr.getint());
1416 else if (op_expr == "<=") return (lhs_expr.getint() <= rhs_expr.getint());
1417 else if (op_expr == "sw") return (starts_with(lhs_expr,rhs_expr));
1418 else if (op_expr == "ew") return (ends_with(lhs_expr,rhs_expr));
1419 else {
1420 logout << "Error: '" << op_expr << "' is not a recognised operator." << endl;
1421 }
1422
1423 return false;
1424}
1425
1426
1427static text_t get_if (const text_t& collection, recptproto* collectproto,
1428 ResultDocInfo_t &docinfo, displayclass &disp,
1429 const decision_t &decision,
1430 format_t *ifptr, format_t *elseptr,
1431 text_tmap &options, ostream& logout)
1432{
1433 // If the decision component is a metadata element, then evaluate it
1434 // to see whether we output the "then" or the "else" clause
1435 if (decision.command == dMeta) {
1436 if (get_meta (collection,collectproto,docinfo,disp,decision.meta,options,
1437 logout) != "") {
1438 if (ifptr != NULL)
1439 return get_formatted_string (collection,collectproto,docinfo, disp, ifptr,
1440 options, logout);
1441 }
1442 else {
1443 if (elseptr != NULL)
1444 return get_formatted_string (collection,collectproto,docinfo, disp, elseptr,
1445 options, logout);
1446 }
1447 }
1448
1449 // If the decision component is text, then evaluate it (it is probably a
1450 // macro like _cgiargmode_) to decide what to output.
1451 else if (decision.command == dText) {
1452
1453 text_t outstring;
1454 disp.expandstring (decision.text, outstring);
1455
1456 // Check for if expression in form: str1 op str2
1457 // (such as [x] eq "y")
1458 text_t lhs_expr, op_expr, rhs_expr;
1459 if (uses_expression(collection,collectproto,docinfo, disp, outstring,lhs_expr,op_expr,rhs_expr, options,logout)) {
1460 if (eval_expression_true(lhs_expr,op_expr,rhs_expr,logout)) {
1461 if (ifptr != NULL) {
1462 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1463 options, logout);
1464 }
1465 else {
1466 return "";
1467 }
1468 } else {
1469 if (elseptr != NULL) {
1470 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1471 options, logout);
1472 }
1473 else {
1474 return "";
1475 }
1476 }
1477 }
1478
1479
1480 // This is a tad tricky. When we expand a string like _cgiargmode_, that is
1481 // a cgi argument macro that has not been set, it evaluates to itself.
1482 // Therefore, were have to say that a piece of text evalautes true if
1483 // it is non-empty and if it is a cgi argument evaulating to itself.
1484
1485 if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
1486 if (ifptr != NULL)
1487 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1488 options, logout);
1489 } else {
1490 if (elseptr != NULL)
1491 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1492 options, logout);
1493 }
1494 }
1495
1496 return "";
1497}
1498
1499bool includes_metadata(const text_t& text)
1500{
1501 text_t::const_iterator here = text.begin();
1502 text_t::const_iterator end = text.end();
1503 while (here != end) {
1504 if (*here == '[') return true;
1505 ++here;
1506 }
1507
1508 return false;
1509}
1510
1511static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
1512 recptproto* collectproto,
1513 ResultDocInfo_t &docinfo,
1514 displayclass &disp, text_tmap &options,
1515 ostream &logout) {
1516
1517 if (includes_metadata(metavalue)) {
1518
1519 // text has embedded metadata in it => expand it
1520 FilterRequest_t request;
1521 FilterResponse_t response;
1522
1523 request.getParents = false;
1524
1525 format_t *expanded_formatlistptr = new format_t();
1526 parse_formatstring (metavalue, expanded_formatlistptr,
1527 request.fields, request.getParents);
1528
1529 // retrieve metadata
1530 get_info(docinfo.OID, collection, "", request.fields, request.getParents,
1531 collectproto, response, logout);
1532
1533 if (!response.docInfo.empty()) {
1534
1535 text_t expanded_metavalue
1536 = get_formatted_string(collection, collectproto,
1537 response.docInfo[0], disp, expanded_formatlistptr,
1538 options, logout);
1539
1540 return expanded_metavalue;
1541 }
1542 else {
1543 return metavalue;
1544 }
1545 }
1546 else {
1547
1548 return metavalue;
1549 }
1550}
1551
1552text_t get_collection_meta(const text_t& collection, recptproto* collectproto,
1553 displayclass &disp,
1554 text_t meta_name, ostream& logout) {
1555
1556 ColInfoResponse_t collectinfo;
1557 comerror_t err;
1558 collectproto->get_collectinfo (collection, collectinfo,err,logout);
1559 text_t meta_value = "";
1560 text_t lang;
1561 disp.expandstring("_cgiargl_",lang);
1562 if (lang.empty()) {
1563 lang = "en";
1564 }
1565
1566 if (err == noError) {
1567 meta_value = collectinfo.get_collectionmeta(meta_name, lang);
1568 }
1569 return meta_value;
1570
1571
1572}
1573text_t format_string (const text_t& collection, recptproto* collectproto,
1574 ResultDocInfo_t &docinfo, displayclass &disp,
1575 format_t *formatlistptr, text_tmap &options,
1576 ostream& logout) {
1577
1578 if (formatlistptr == NULL) return "";
1579
1580 switch (formatlistptr->command) {
1581 case comOID:
1582 return docinfo.OID;
1583 case comRank:
1584 return text_t(docinfo.ranking);
1585 case comText:
1586 return formatlistptr->text;
1587 case comLink:
1588 return options["link"];
1589 case comEndLink:
1590 if (options["link"].empty()) return "";
1591 else return "</a>";
1592 case comHref:
1593 return get_href(options["link"]);
1594 case comIcon:
1595 return options["icon"];
1596 case comNum:
1597 return docinfo.result_num;
1598 case comRel: //if [RelatedDocuments] appears in format string, collect relation data
1599 return get_related_docs(collection, collectproto, docinfo, logout);
1600 case comSummary:
1601 return format_summary(collection, collectproto, docinfo, disp, options, logout);
1602 case comMeta:
1603 {
1604 const text_t& metavalue = get_meta (collection,collectproto, docinfo, disp,formatlistptr->meta,options,logout);
1605 return expand_metadata(metavalue, collection, collectproto, docinfo, disp, options, logout);
1606 }
1607 case comDoc:
1608 return format_text(collection, collectproto, docinfo, disp, options, logout);
1609 //return options["text"];
1610 case comImage:
1611 return expand_metadata(options["DocImage"], collection, collectproto, docinfo, disp, options, logout);
1612 case comTOC:
1613 return options["DocTOC"];
1614 case comDocumentButtonDetach:
1615 return options["DocumentButtonDetach"];
1616 case comDocumentButtonHighlight:
1617 return options["DocumentButtonHighlight"];
1618 case comDocumentButtonExpandContents:
1619 return options["DocumentButtonExpandContents"];
1620 case comDocumentButtonExpandText:
1621 return options["DocumentButtonExpandText"];
1622 case comHighlight:
1623 if (options["highlight"] == "1") return "<b>";
1624 break;
1625 case comEndHighlight:
1626 if (options["highlight"] == "1") return "</b>";
1627 break;
1628 case comIf:
1629 return get_if (collection, collectproto, docinfo, disp,
1630 formatlistptr->decision, formatlistptr->ifptr,
1631 formatlistptr->elseptr, options, logout);
1632 case comOr:
1633 return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
1634 options, logout);
1635 case comCollection:
1636 if (formatlistptr->meta.metaname == g_EmptyText) {
1637 return collection;
1638 }
1639 return get_collection_meta(collection, collectproto, disp, formatlistptr->meta.metaname, logout);
1640
1641 }
1642 return "";
1643}
1644
1645text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1646 ResultDocInfo_t &docinfo, displayclass &disp,
1647 format_t *formatlistptr, text_tmap &options,
1648 ostream& logout) {
1649
1650 text_t ft;
1651 while (formatlistptr != NULL)
1652 {
1653 ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
1654 options, logout);
1655 formatlistptr = formatlistptr->nextptr;
1656 }
1657
1658 return ft;
1659}
1660
1661
1662// we have only preloaded the text in DocumentAction. But you may want to get the text in query. so copy what we have done with format_summary and get the text here. probably is quite expensive?
1663text_t format_text (const text_t& collection, recptproto* collectproto,
1664 ResultDocInfo_t &docinfo, displayclass &disp,
1665 text_tmap &options, ostream& logout) {
1666 if(!options["text"].empty()) {
1667 return options["text"];
1668 }
1669 // else get document text here
1670 DocumentRequest_t docrequest;
1671 DocumentResponse_t docresponse;
1672 comerror_t err;
1673 docrequest.OID = docinfo.OID;
1674 collectproto->get_document (collection, docrequest, docresponse, err, logout);
1675 return docresponse.doc;
1676
1677}
1678
1679/* FUNCTION NAME: format_summary
1680 * DESC: this is invoked when a [Summary] special metadata is processed.
1681 * RETURNS: a query-biased summary for the document */
1682
1683text_t format_summary (const text_t& collection, recptproto* collectproto,
1684 ResultDocInfo_t &docinfo, displayclass &disp,
1685 text_tmap &options, ostream& logout) {
1686
1687 // GRB: added code here to ensure that the cstr (and other collections)
1688 // uses the document metadata item Summary, rather than compressing
1689 // the text of the document, processed via the methods in
1690 // summarise.cpp
1691 if (docinfo.metadata.count("Summary") > 0 &&
1692 docinfo.metadata["Summary"].values.size() > 0) {
1693 return docinfo.metadata["Summary"].values[0];
1694 }
1695
1696 text_t textToSummarise, query;
1697 if(options["text"].empty()) { // get document text
1698 DocumentRequest_t docrequest;
1699 DocumentResponse_t docresponse;
1700 comerror_t err;
1701 docrequest.OID = docinfo.OID;
1702 collectproto->get_document (collection, docrequest, docresponse, err, logout);
1703 textToSummarise = docresponse.doc;
1704 } else // in practice, this would not happen, because text is only
1705 // loaded with the [Text] command
1706 textToSummarise = options["text"];
1707 disp.expandstring("_cgiargq_",query);
1708 return summarise(textToSummarise,query,80);
1709}
Note: See TracBrowser for help on using the repository browser.