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

Last change on this file since 4868 was 4868, checked in by sjboddie, 21 years ago

Made the DocumentArrowsBottom format option work and added a new
DocumentArrowsTop option

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 33.8 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
34static text_t format_string (const text_t& collection, recptproto* collectproto,
35 ResultDocInfo_t &docinfo, displayclass &disp,
36 format_t *formatlistptr,
37 const text_t &link, const text_t &icon,
38 const text_t &text, bool highlight, 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
43text_t format_summary (const text_t& collection, recptproto* collectproto,
44 ResultDocInfo_t &docinfo, displayclass &disp,
45 const text_t &text, bool highlight,
46 ostream& logout);
47
48
49void metadata_t::clear() {
50 metaname.clear();
51 metacommand = mNone;
52 parentcommand = pNone;
53 parentoptions.clear();
54}
55
56void decision_t::clear() {
57 command = dMeta;
58 meta.clear();
59 text.clear();
60}
61
62void format_t::clear() {
63 command = comText;
64 decision.clear();
65 text.clear();
66 meta.clear();
67 nextptr = NULL;
68 ifptr = NULL;
69 elseptr = NULL;
70 orptr = NULL;
71}
72
73void formatinfo_t::clear() {
74 DocumentImages = false;
75 DocumentTitles = true;
76 DocumentHeading = "{Or}{[parent(Top):Title],[Title],untitled}<br>";
77 DocumentContents = true;
78 DocumentArrowsBottom = true;
79 DocumentArrowsTop = false;
80 DocumentButtons.erase (DocumentButtons.begin(), DocumentButtons.end());
81 // DocumentButtons.push_back ("Expand Text");
82 // DocumentButtons.push_back ("Expand Contents");
83 DocumentButtons.push_back ("Detach");
84 DocumentButtons.push_back ("Highlight");
85 RelatedDocuments = "";
86 DocumentText = "<center><table width=_pagewidth_><tr><td>[Text]</td></tr></table></center>";
87 formatstrings.erase (formatstrings.begin(), formatstrings.end());
88 DocumentUseHTML = false;
89}
90
91// simply checks to see if formatstring begins with a <td> tag
92bool is_table_content (const text_t &formatstring) {
93 text_t::const_iterator here = formatstring.begin();
94 text_t::const_iterator end = formatstring.end();
95
96 while (here != end) {
97 if (*here != ' ') {
98 if ((*here == '<') && ((here+3) < end)) {
99 if ((*(here+1) == 't' || *(here+1) == 'T') &&
100 (*(here+2) == 'd' || *(here+2) == 'D') &&
101 (*(here+3) == '>' || *(here+3) == ' '))
102 return true;
103 } else return false;
104 }
105 here ++;
106 }
107 return false;
108}
109
110bool is_table_content (const format_t *formatlistptr) {
111
112 if (formatlistptr == NULL) return false;
113
114 if (formatlistptr->command == comText)
115 return is_table_content (formatlistptr->text);
116
117 return false;
118}
119
120// returns false if key isn't in formatstringmap
121bool get_formatstring (const text_t &key, const text_tmap &formatstringmap,
122 text_t &formatstring) {
123
124 formatstring.clear();
125 text_tmap::const_iterator it = formatstringmap.find(key);
126 if (it == formatstringmap.end()) return false;
127 formatstring = (*it).second;
128 return true;
129}
130
131// tries to find "key1key2" then "key1" then "key2"
132bool get_formatstring (const text_t &key1, const text_t &key2,
133 const text_tmap &formatstringmap,
134 text_t &formatstring) {
135
136 formatstring.clear();
137 text_tmap::const_iterator it = formatstringmap.find(key1 + key2);
138 if (it != formatstringmap.end()) {
139 formatstring = (*it).second;
140 return true;
141 }
142 it = formatstringmap.find(key1);
143 if (it != formatstringmap.end()) {
144 formatstring = (*it).second;
145 return true;
146 }
147 it = formatstringmap.find(key2);
148 if (it != formatstringmap.end()) {
149 formatstring = (*it).second;
150 return true;
151 }
152 return false;
153}
154
155
156// returns a date of form 31 _textmonthnn_ 1999
157// input is date of type 19991231
158// at least the year must be present in date
159text_t format_date (const text_t &date) {
160
161 if (date.size() < 4) return "";
162
163 text_t::const_iterator datebegin = date.begin();
164
165 text_t year = substr (datebegin, datebegin+4);
166
167 if (date.size() < 6) return year;
168
169 text_t month = "_textmonth" + substr (datebegin+4, datebegin+6) + "_";
170 int imonth = month.getint();
171 if (imonth < 0 || imonth > 12) return year;
172
173 if (date.size() < 8) return month + " " + year;
174
175 text_t day = substr (datebegin+6, datebegin+8);
176 if (day[0] == '0') day = substr (day.begin()+1, day.end());
177 int iday = day.getint();
178 if (iday < 0 || iday > 31) return month + " " + year;
179
180 return day + " " + month + " " + year;
181}
182
183// converts an iso639 language code to its English equivalent
184// I realize that this isn't the pretiest or most efficient implementation,
185// hopefully this ugly Language (and Date too) formatting won't survive to
186// see gsdl-3.0
187text_t iso639 (const text_t &langcode) {
188
189 if (langcode == "aa") return "Afar";
190 if (langcode == "ab") return "Abkhazian";
191 if (langcode == "af") return "Afrikaans";
192 if (langcode == "am") return "Amharic";
193 if (langcode == "ar") return "Arabic";
194 if (langcode == "as") return "Assamese";
195 if (langcode == "ay") return "Aymara";
196 if (langcode == "az") return "Azerbaijani";
197
198 if (langcode == "ba") return "Bashkir";
199 if (langcode == "be") return "Byelorussian";
200 if (langcode == "bg") return "Bulgarian";
201 if (langcode == "bh") return "Bihari";
202 if (langcode == "bi") return "Bislama";
203 if (langcode == "bn") return "Bengali; Bangla";
204 if (langcode == "bo") return "Tibetan";
205 if (langcode == "br") return "Breton";
206
207 if (langcode == "ca") return "Catalan";
208 if (langcode == "co") return "Corsican";
209 if (langcode == "cs") return "Czech";
210 if (langcode == "cy") return "Welsh";
211
212 if (langcode == "da") return "Danish";
213 if (langcode == "de") return "German";
214 if (langcode == "dz") return "Bhutani";
215
216 if (langcode == "el") return "Greek";
217 if (langcode == "en") return "English";
218 if (langcode == "eo") return "Esperanto";
219 if (langcode == "es") return "Spanish";
220 if (langcode == "et") return "Estonian";
221 if (langcode == "eu") return "Basque";
222
223 if (langcode == "fa") return "Persian";
224 if (langcode == "fi") return "Finnish";
225 if (langcode == "fj") return "Fiji";
226 if (langcode == "fo") return "Faroese";
227 if (langcode == "fr") return "French";
228 if (langcode == "fy") return "Frisian";
229
230 if (langcode == "ga") return "Irish";
231 if (langcode == "gd") return "Scots Gaelic";
232 if (langcode == "gl") return "Galician";
233 if (langcode == "gn") return "Guarani";
234 if (langcode == "gu") return "Gujarati";
235
236 if (langcode == "ha") return "Hausa";
237 if (langcode == "hi") return "Hindi";
238 if (langcode == "hr") return "Croatian";
239 if (langcode == "hu") return "Hungarian";
240 if (langcode == "hy") return "Armenian";
241
242 if (langcode == "ia") return "Interlingua";
243 if (langcode == "ie") return "Interlingue";
244 if (langcode == "ik") return "Inupiak";
245 if (langcode == "in") return "Indonesian";
246 if (langcode == "is") return "Icelandic";
247 if (langcode == "it") return "Italian";
248 if (langcode == "iw") return "Hebrew";
249
250 if (langcode == "ja") return "Japanese";
251 if (langcode == "ji") return "Yiddish";
252 if (langcode == "jw") return "Javanese";
253
254 if (langcode == "ka") return "Georgian";
255 if (langcode == "kk") return "Kazakh";
256 if (langcode == "kl") return "Greenlandic";
257 if (langcode == "km") return "Cambodian";
258 if (langcode == "kn") return "Kannada";
259 if (langcode == "ko") return "Korean";
260 if (langcode == "ks") return "Kashmiri";
261 if (langcode == "ku") return "Kurdish";
262 if (langcode == "ky") return "Kirghiz";
263
264 if (langcode == "la") return "Latin";
265 if (langcode == "ln") return "Lingala";
266 if (langcode == "lo") return "Laothian";
267 if (langcode == "lt") return "Lithuanian";
268 if (langcode == "lv") return "Latvian, Lettish";
269
270 if (langcode == "mg") return "Malagasy";
271 if (langcode == "mi") return "Maori";
272 if (langcode == "mk") return "Macedonian";
273 if (langcode == "ml") return "Malayalam";
274 if (langcode == "mn") return "Mongolian";
275 if (langcode == "mo") return "Moldavian";
276 if (langcode == "mr") return "Marathi";
277 if (langcode == "ms") return "Malay";
278 if (langcode == "mt") return "Maltese";
279 if (langcode == "my") return "Burmese";
280
281 if (langcode == "na") return "Nauru";
282 if (langcode == "ne") return "Nepali";
283 if (langcode == "nl") return "Dutch";
284 if (langcode == "no") return "Norwegian";
285
286 if (langcode == "oc") return "Occitan";
287 if (langcode == "om") return "(Afan) Oromo";
288 if (langcode == "or") return "Oriya";
289
290 if (langcode == "pa") return "Punjabi";
291 if (langcode == "pl") return "Polish";
292 if (langcode == "ps") return "Pashto, Pushto";
293 if (langcode == "pt") return "Portuguese";
294
295 if (langcode == "qu") return "Quechua";
296 if (langcode == "rm") return "Rhaeto-Romance";
297 if (langcode == "rn") return "Kirundi";
298 if (langcode == "ro") return "Romanian";
299 if (langcode == "ru") return "Russian";
300 if (langcode == "rw") return "Kinyarwanda";
301
302 if (langcode == "sa") return "Sanskrit";
303 if (langcode == "sd") return "Sindhi";
304 if (langcode == "sg") return "Sangro";
305 if (langcode == "sh") return "Serbo-Croatian";
306 if (langcode == "si") return "Singhalese";
307 if (langcode == "sk") return "Slovak";
308 if (langcode == "sl") return "Slovenian";
309 if (langcode == "sm") return "Samoan";
310 if (langcode == "sn") return "Shona";
311 if (langcode == "so") return "Somali";
312 if (langcode == "sq") return "Albanian";
313 if (langcode == "sr") return "Serbian";
314 if (langcode == "ss") return "Siswati";
315 if (langcode == "st") return "Sesotho";
316 if (langcode == "su") return "Sudanese";
317 if (langcode == "sv") return "Swedish";
318 if (langcode == "sw") return "Swahili";
319
320 if (langcode == "ta") return "Tamil";
321 if (langcode == "te") return "Tegulu";
322 if (langcode == "tg") return "Tajik";
323 if (langcode == "th") return "Thai";
324 if (langcode == "ti") return "Tigrinya";
325 if (langcode == "tk") return "Turkmen";
326 if (langcode == "tl") return "Tagalog";
327 if (langcode == "tn") return "Setswana";
328 if (langcode == "to") return "Tonga";
329 if (langcode == "tr") return "Turkish";
330 if (langcode == "ts") return "Tsonga";
331 if (langcode == "tt") return "Tatar";
332 if (langcode == "tw") return "Twi";
333
334 if (langcode == "uk") return "Ukrainian";
335 if (langcode == "ur") return "Urdu";
336 if (langcode == "uz") return "Uzbek";
337
338 if (langcode == "vi") return "Vietnamese";
339 if (langcode == "vo") return "Volapuk";
340
341 if (langcode == "wo") return "Wolof";
342
343 if (langcode == "xh") return "Xhosa";
344
345 if (langcode == "yo") return "Yoruba";
346
347 if (langcode == "zh") return "Chinese";
348 if (langcode == "zu") return "Zulu";
349 return "";
350}
351
352text_t get_href (const text_t &link) {
353
354 text_t href;
355
356 text_t::const_iterator here = findchar(link.begin(), link.end(), '"');
357 text_t::const_iterator end = link.end();
358
359 here ++;
360 while (here != end) {
361 if (*here == '"') break;
362 href.push_back(*here);
363 here ++;
364 }
365
366 return href;
367}
368
369//this function gets the information associated with the relation
370//metadata for the document associated with 'docinfo'. This relation
371//metadata consists of a line of pairs containing 'collection, document OID'
372//(this is the OID of the document related to the current document, and
373//the collection the related document belongs to). For each of these pairs
374//the title metadata is obtained and then an html link between the title
375//of the related doc and the document's position (the document will be
376//found in "<a href=\"_httpdocument_&c=collection&cl=search&d=OID">
377//(where collection is the related documents collection, and OID is the
378//related documents OID). A list of these html links are made for as many
379//related documents as there are. This list is then returned. If there are
380//no related documents available for the current document then the string
381//'.. no related documents .. ' is returned.
382text_t get_related_docs(const text_t& collection, recptproto* collectproto,
383 ResultDocInfo_t &docinfo, ostream& logout){
384
385 text_tset metadata;
386
387 //insert the metadata we wish to collect
388 metadata.insert("relation");
389 metadata.insert("Title");
390 metadata.insert("Subject"); //for emails, where title data doesn't apply
391
392 FilterResponse_t response;
393 text_t relation = ""; //string for displaying relation metadata
394 text_t relationTitle = ""; //the related documents Title (or subject)
395 text_t relationOID = ""; //the related documents OID
396
397 //get the information associated with the metadata for current doc
398 if (get_info (docinfo.OID, collection, metadata,
399 false, collectproto, response, logout)) {
400
401 //if the relation metadata exists, store for displaying
402 if(!response.docInfo[0].metadata["relation"].values.empty()){
403 relationOID += response.docInfo[0].metadata["relation"].values[0];
404
405 //split relation data into pairs of collectionname,ID number
406 text_tarray relationpairs;
407 splitchar (relationOID.begin(), relationOID.end(), ' ', relationpairs);
408
409 text_tarray::const_iterator currDoc = relationpairs.begin();
410 text_tarray::const_iterator lastDoc = relationpairs.end();
411
412 //iterate through the pairs to split and display
413 while(currDoc != lastDoc){
414
415 //split pairs into collectionname and ID
416 text_tarray relationdata;
417 splitchar ((*currDoc).begin(), (*currDoc).end(), ',', relationdata);
418
419 //get first element in the array (collection)
420 text_tarray::const_iterator doc_data = relationdata.begin();
421 text_t document_collection = *doc_data;
422 doc_data++; //increment to get next item in array (oid)
423 text_t document_OID = *doc_data;
424
425 //create html link to related document
426 relation += "<a href=\"_httpdocument_&c=" + document_collection;
427 relation += "&cl=search&d=" + document_OID;
428
429 //get the information associated with the metadata for related doc
430 if (get_info (document_OID, document_collection, metadata,
431 false, collectproto, response, logout)) {
432
433 //if title metadata doesn't exist, collect subject metadata
434 //if that doesn't exist, just call it 'related document'
435 if (!response.docInfo[0].metadata["Title"].values[0].empty())
436 relationTitle = response.docInfo[0].metadata["Title"].values[0];
437 else if (!response.docInfo[0].metadata["Subject"].values.empty())
438 relationTitle = response.docInfo[0].metadata["Subject"].values[0];
439 else relationTitle = "RELATED DOCUMENT";
440
441 }
442
443 //link the related document's title to its page
444 relation += "\">" + relationTitle + "</a>";
445 relation += " (" + document_collection + ")<br>";
446
447 currDoc++;
448 }
449 }
450
451 }
452
453 if(relation.empty()) //no relation data for documnet
454 relation = ".. no related documents .. ";
455
456 return relation;
457}
458
459
460
461static void get_parent_options (text_t &instring, metadata_t &metaoption) {
462
463 assert (instring.size() > 7);
464 if (instring.size() <= 7) return;
465
466 text_t meta, com, op;
467 bool inbraces = false;
468 bool inquotes = false;
469 bool foundcolon = false;
470 text_t::const_iterator here = instring.begin()+6;
471 text_t::const_iterator end = instring.end();
472 while (here != end) {
473 if (*here == '(') inbraces = true;
474 else if (*here == ')') inbraces = false;
475 else if (*here == '\'' && !inquotes) inquotes = true;
476 else if (*here == '\'' && inquotes) inquotes = false;
477 else if (*here == ':' && !inbraces) foundcolon = true;
478 else if (foundcolon) meta.push_back (*here);
479 else if (inquotes) op.push_back (*here);
480 else com.push_back (*here);
481 here ++;
482 }
483 instring = meta;
484 if (com.empty())
485 metaoption.parentcommand = pImmediate;
486 else if (com == "Top")
487 metaoption.parentcommand = pTop;
488 else if (com == "All") {
489 metaoption.parentcommand = pAll;
490 metaoption.parentoptions = op;
491 }
492}
493
494static void parse_meta (text_t &meta, metadata_t &metaoption,
495 text_tset &metadata, bool &getParents) {
496
497 if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
498 metaoption.metacommand = mCgiSafe;
499 meta = substr (meta.begin()+8, meta.end());
500 }
501
502 if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
503 getParents = true;
504 get_parent_options (meta, metaoption);
505 }
506
507 metadata.insert (meta);
508 metaoption.metaname = meta;
509}
510
511static void parse_meta (text_t &meta, format_t *formatlistptr,
512 text_tset &metadata, bool &getParents) {
513
514 if (meta == "link")
515 formatlistptr->command = comLink;
516 else if (meta == "/link")
517 formatlistptr->command = comEndLink;
518
519 else if (meta == "href")
520 formatlistptr->command = comHref;
521
522 else if (meta == "num")
523 formatlistptr->command = comNum;
524
525 else if (meta == "icon")
526 formatlistptr->command = comIcon;
527
528 else if (meta == "Text")
529 formatlistptr->command = comDoc;
530
531 else if (meta == "RelatedDocuments")
532 formatlistptr->command = comRel;
533
534 else if (meta == "highlight")
535 formatlistptr->command = comHighlight;
536
537 else if (meta == "/highlight")
538 formatlistptr->command = comEndHighlight;
539
540 else if (meta == "Summary")
541 formatlistptr->command = comSummary;
542
543 else {
544 formatlistptr->command = comMeta;
545 parse_meta (meta, formatlistptr->meta, metadata, getParents);
546 }
547}
548
549static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
550 text_tset &metadata, bool &getParents) {
551
552 text_t text;
553 text_t::const_iterator here = formatstring.begin();
554 text_t::const_iterator end = formatstring.end();
555
556 while (here != end) {
557
558 if (*here == '\\') {
559 here ++;
560 if (here != end) text.push_back (*here);
561
562 } else if (*here == '{') {
563 if (!text.empty()) {
564 formatlistptr->command = comText;
565 formatlistptr->text = text;
566 formatlistptr->nextptr = new format_t();
567 formatlistptr = formatlistptr->nextptr;
568
569 text.clear();
570 }
571 if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
572
573 formatlistptr->nextptr = new format_t();
574 formatlistptr = formatlistptr->nextptr;
575 if (here == end) break;
576 }
577 } else if (*here == '[') {
578 if (!text.empty()) {
579 formatlistptr->command = comText;
580 formatlistptr->text = text;
581 formatlistptr->nextptr = new format_t();
582 formatlistptr = formatlistptr->nextptr;
583
584 text.clear();
585 }
586 text_t meta;
587 here ++;
588 while (*here != ']') {
589 if (here == end) return false;
590 meta.push_back (*here);
591 here ++;
592 }
593 parse_meta (meta, formatlistptr, metadata, getParents);
594 formatlistptr->nextptr = new format_t();
595 formatlistptr = formatlistptr->nextptr;
596
597 } else
598 text.push_back (*here);
599
600 if (here != end) here ++;
601 }
602 if (!text.empty()) {
603 formatlistptr->command = comText;
604 formatlistptr->text = text;
605 formatlistptr->nextptr = new format_t();
606 formatlistptr = formatlistptr->nextptr;
607
608 }
609 return true;
610}
611
612
613static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
614 format_t *formatlistptr, text_tset &metadata, bool &getParents) {
615
616 text_t::const_iterator it = findchar (here, end, '}');
617 if (it == end) return false;
618
619 text_t com = substr (here, it);
620 here = findchar (it, end, '{');
621 if (here == end) return false;
622 else here ++;
623
624 if (com == "If") formatlistptr->command = comIf;
625 else if (com == "Or") formatlistptr->command = comOr;
626 else return false;
627
628 int commacount = 0;
629 text_t text;
630 while (here != end) {
631
632 if (*here == '\\') {
633 here++;
634 if (here != end) text.push_back(*here);
635
636 }
637
638 else if (*here == ',' || *here == '}' || *here == '{') {
639
640 if (formatlistptr->command == comOr) {
641 // the {Or}{this, or this, or this, or this} statement
642 format_t *or_ptr;
643
644 // find the next unused orptr
645 if (formatlistptr->orptr == NULL) {
646 formatlistptr->orptr = new format_t();
647 or_ptr = formatlistptr->orptr;
648 } else {
649 or_ptr = formatlistptr->orptr;
650 while (or_ptr->nextptr != NULL)
651 or_ptr = or_ptr->nextptr;
652 or_ptr->nextptr = new format_t();
653 or_ptr = or_ptr->nextptr;
654 }
655
656 if (!text.empty())
657 {
658 if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
659 }
660
661 if (*here == '{')
662 {
663 // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
664 // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
665 // The latter can always be re-written:
666 // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
667
668 if (!text.empty()) // already used up allocated format_t
669 {
670 // => allocate new one for detected action
671 or_ptr->nextptr = new format_t();
672 or_ptr = or_ptr->nextptr;
673 }
674 if (!parse_action(++here, end, or_ptr, metadata, getParents))
675 {
676 return false;
677 }
678 }
679 else
680 {
681 if (*here == '}') break;
682 }
683 text.clear();
684
685 }
686
687 // Parse an {If}{decide,do,else} statement
688 else {
689
690 // Read the decision component.
691 if (commacount == 0) {
692 // Decsion can be a metadata element, or a piece of text.
693 // Originally Stefan's code, updated 25/10/2000 by Gordon.
694
695 text_t::const_iterator beginbracket = text.begin();
696 text_t::const_iterator endbracket = (text.end() - 1);
697
698 // Decision is based on a metadata element
699 if ((*beginbracket == '[') && (*endbracket == ']')) {
700 // Ignore the surrounding square brackets
701 text_t meta = substr (beginbracket+1, endbracket);
702 parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
703 commacount ++;
704 text.clear();
705 }
706
707 // Decision is a piece of text (probably a macro like _cgiargmode_).
708 else {
709 formatlistptr->decision.command = dText;
710 formatlistptr->decision.text = text;
711 commacount ++;
712 text.clear();
713 }
714 }
715
716 // Read the "then" and "else" components of the {If} statement.
717 else {
718 format_t** nextlistptr = NULL;
719 if (commacount == 1) {
720 nextlistptr = &formatlistptr->ifptr;
721 } else if (commacount == 2 ) {
722 nextlistptr = &formatlistptr->elseptr;
723 } else {
724 return false;
725 }
726
727 if (!text.empty()) {
728 if (*nextlistptr == NULL) {
729 *nextlistptr = new format_t();
730 } else {
731
732 // skip to the end of any format_t statements already added
733 while ((*nextlistptr)->nextptr != NULL)
734 {
735 nextlistptr = &(*nextlistptr)->nextptr;
736 }
737
738 (*nextlistptr)->nextptr = new format_t();
739 nextlistptr = &(*nextlistptr)->nextptr;
740 }
741
742 if (!parse_string (text, *nextlistptr, metadata, getParents))
743 {
744 return false;
745 }
746 text.clear();
747 }
748
749 if (*here == '{')
750 {
751 if (*nextlistptr == NULL) {
752 *nextlistptr = new format_t();
753 } else {
754 (*nextlistptr)->nextptr = new format_t();
755 nextlistptr = &(*nextlistptr)->nextptr;
756 }
757
758 if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
759 {
760 return false;
761 }
762 }
763 else
764 {
765 if (*here == '}') break;
766 commacount ++;
767 }
768 }
769 }
770
771 } else text.push_back(*here);
772
773 if (here != end) here ++;
774 }
775
776 return true;
777}
778
779
780bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
781 text_tset &metadata, bool &getParents) {
782
783 formatlistptr->clear();
784 getParents = false;
785
786 return (parse_string (formatstring, formatlistptr, metadata, getParents));
787}
788
789
790// note: all the format_date stuff is assuming that all Date metadata is going to
791// be of the form yyyymmdd, this is of course, crap ;)
792
793static text_t get_meta (ResultDocInfo_t &docinfo, const metadata_t &meta) {
794
795 // make sure we have the requested metadata
796 MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
797 if (it == docinfo.metadata.end()) return "";
798
799 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
800
801 switch (meta.parentcommand) {
802 case pNone:
803 {
804 text_t classifier_metaname = docinfo.classifier_metadata_type;
805 int metaname_index
806 = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
807 text_t metadata_item = docinfo.metadata[meta.metaname].values[metaname_index];
808
809 if (meta.metaname == "Date")
810 return format_date (metadata_item);
811 else if (meta.metaname == "Language")
812 return iso639(metadata_item);
813 if (meta.metacommand == mCgiSafe)
814 return cgi_safe (metadata_item);
815 else return metadata_item;
816 }
817
818 case pImmediate:
819 if (parent != NULL) {
820 if (meta.metaname == "Date")
821 return format_date (parent->values[0]);
822 if (meta.metacommand == mCgiSafe)
823 return cgi_safe (parent->values[0]);
824 else return parent->values[0];
825 }
826 break;
827
828 case pTop:
829 if (parent != NULL) {
830 while (parent->parent != NULL) parent = parent->parent;
831
832 if (meta.metaname == "Date")
833 return format_date (parent->values[0]);
834 if (meta.metacommand == mCgiSafe)
835 return cgi_safe (parent->values[0]);
836 else return parent->values[0];
837 }
838 break;
839
840 case pAll:
841 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
842 if (parent != NULL) {
843 text_tarray tmparray;
844 while (parent != NULL) {
845 tmparray.push_back (parent->values[0]);
846 parent = parent->parent;
847 }
848 bool first = true;
849 text_t tmp;
850 text_tarray::reverse_iterator here = tmparray.rbegin();
851 text_tarray::reverse_iterator end = tmparray.rend();
852 while (here != end) {
853 if (!first) tmp += meta.parentoptions;
854 if (meta.metaname == "Date") tmp += format_date (*here);
855 else tmp += *here;
856 first = false;
857 here ++;
858 }
859 if (meta.metacommand == mCgiSafe) return cgi_safe (tmp);
860 else return tmp;
861 }
862 }
863 return "";
864}
865
866static text_t get_or (const text_t& collection, recptproto* collectproto,
867 ResultDocInfo_t &docinfo, displayclass &disp,
868 format_t *orptr,
869 const text_t &link, const text_t &icon,
870 const text_t &text, bool highlight,
871 ostream& logout) {
872
873 text_t tmp;
874 while (orptr != NULL) {
875
876 tmp = format_string (collection,collectproto, docinfo, disp, orptr,
877 link, icon, text, highlight, logout);
878 if (!tmp.empty()) return tmp;
879
880 orptr = orptr->nextptr;
881 }
882 return "";
883}
884
885static text_t get_if (const text_t& collection, recptproto* collectproto,
886 ResultDocInfo_t &docinfo, displayclass &disp,
887 const decision_t &decision,
888 format_t *ifptr, format_t *elseptr, const text_t &link,
889 const text_t &icon, const text_t &text, bool highlight,
890 ostream& logout)
891{
892
893 // If the decision component is a metadata element, then evaluate it
894 // to see whether we output the "then" or the "else" clause
895 if (decision.command == dMeta) {
896 if (get_meta (docinfo, decision.meta) != "") {
897 if (ifptr != NULL)
898 return get_formatted_string (collection,collectproto, docinfo, disp, ifptr,
899 link, icon, text, highlight, logout);
900 }
901 else {
902 if (elseptr != NULL)
903 return get_formatted_string (collection,collectproto, docinfo, disp, elseptr,
904 link, icon, text, highlight, logout);
905 }
906 }
907
908 // If the decision component is text, then evaluate it (it is probably a
909 // macro like _cgiargmode_) to decide what to output.
910 else if (decision.command == dText) {
911
912 text_t outstring;
913 disp.expandstring (decision.text, outstring);
914
915 // This is a tad tricky. When we expand a string like _cgiargmode_, that is
916 // a cgi argument macro that has not been set, it evaluates to itself.
917 // Therefore, were have to say that a piece of text evalautes true if
918 // it is non-empty and if it is a cgi argument evaulating to itself.
919 if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
920 if (ifptr != NULL)
921 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
922 link, icon, text, highlight, logout);
923 } else {
924 if (elseptr != NULL)
925 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
926 link, icon, text, highlight, logout);
927 }
928 }
929
930 return "";
931}
932
933bool includes_metadata(const text_t& text)
934{
935 text_t::const_iterator here = text.begin();
936 text_t::const_iterator end = text.end();
937 while (here != end) {
938 if (*here == '[') return true;
939 here ++;
940 }
941
942 return false;
943}
944
945
946
947text_t format_string (const text_t& collection, recptproto* collectproto,
948 ResultDocInfo_t &docinfo, displayclass &disp,
949 format_t *formatlistptr,
950 const text_t &link, const text_t &icon,
951 const text_t &text, bool highlight,
952 ostream& logout) {
953
954 if (formatlistptr == NULL) return "";
955
956 switch (formatlistptr->command) {
957 case comText:
958 return formatlistptr->text;
959 case comLink:
960 return link;
961 case comEndLink:
962 if (link.empty()) return "";
963 else return "</a>";
964 case comHref:
965 return get_href(link);
966 case comIcon:
967 return icon;
968 case comNum:
969 return docinfo.result_num;
970 case comRel: //if [RelatedDocuments] appears in format string, collect relation data
971 return get_related_docs(collection, collectproto, docinfo, logout);
972 case comSummary:
973 return format_summary(collection,collectproto,docinfo,disp,text,highlight,logout);
974 case comMeta:
975
976 {
977 const text_t& metavalue = get_meta (docinfo, formatlistptr->meta);
978
979 if (includes_metadata(metavalue))
980 {
981 // text has embedded metadata in it => expand it
982 FilterRequest_t request;
983 FilterResponse_t response;
984
985 request.getParents = false;
986
987 format_t *expanded_formatlistptr = new format_t();
988 parse_formatstring (metavalue, expanded_formatlistptr,
989 request.fields, request.getParents);
990
991 // retrieve metadata
992 get_info(docinfo.OID, collection, request.fields, request.getParents,
993 collectproto, response, logout);
994
995 if (!response.docInfo.empty())
996 {
997 text_t expanded_metavalue
998 = get_formatted_string(collection, collectproto,
999 response.docInfo[0], disp, expanded_formatlistptr,
1000 link, icon, highlight, logout);
1001
1002 return expanded_metavalue;
1003 }
1004 else
1005 {
1006 return metavalue;
1007 }
1008 }
1009 else
1010 {
1011 return metavalue;
1012 }
1013 }
1014 case comDoc:
1015 return text;
1016 case comHighlight:
1017 if (highlight) return "<b>";
1018 break;
1019 case comEndHighlight:
1020 if (highlight) return "</b>";
1021 break;
1022 case comIf:
1023 return get_if (collection, collectproto, docinfo, disp,
1024 formatlistptr->decision, formatlistptr->ifptr,
1025 formatlistptr->elseptr, link, icon, text, highlight,
1026 logout);
1027 case comOr:
1028 return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
1029 link, icon, text, highlight, logout);
1030 }
1031 return "";
1032}
1033
1034
1035
1036
1037text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1038 ResultDocInfo_t& docinfo, displayclass &disp,
1039 format_t* formatlistptr,
1040 const text_t& link, const text_t& icon,
1041 const text_t& text, const bool highlight,
1042 ostream& logout) {
1043
1044 text_t ft;
1045 while (formatlistptr != NULL)
1046 {
1047 ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
1048 link, icon, text, highlight, logout);
1049 formatlistptr = formatlistptr->nextptr;
1050 }
1051
1052 return ft;
1053}
1054
1055text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1056 ResultDocInfo_t &docinfo, displayclass &disp,
1057 format_t *formatlistptr,
1058 const text_t &link, const text_t &icon,
1059 const bool highlight,
1060 ostream& logout) {
1061
1062 text_t text = "";
1063
1064 return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
1065 link, icon, text, highlight, logout);
1066}
1067
1068text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1069 ResultDocInfo_t &docinfo, displayclass &disp,
1070 format_t *formatlistptr,
1071 const text_t& text,
1072 ostream& logout) {
1073
1074 text_t link = "<a href=\"_httpdocument_&cl=search&d=" + docinfo.OID + "\">";
1075 text_t icon = "_icontext_";
1076 bool highlight = false;
1077
1078 return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
1079 link, icon, text, highlight, logout);
1080}
1081
1082text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1083 ResultDocInfo_t &docinfo, displayclass &disp,
1084 format_t *formatlistptr,
1085 ostream& logout) {
1086
1087 text_t text = "";
1088
1089 return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
1090 text, logout);
1091}
1092
1093
1094/* FUNCTION NAME: format_summary
1095 * DESC: this is invoked when a [Summary] special metadata is processed.
1096 * RETURNS: a query-biased summary for the document */
1097
1098text_t format_summary (const text_t& collection, recptproto* collectproto,
1099 ResultDocInfo_t &docinfo, displayclass &disp,
1100 const text_t &text, bool highlight,
1101 ostream& logout) {
1102
1103 // GRB: added code here to ensure that the cstr (and other collections)
1104 // uses the document metadata item Summary, rather than compressing
1105 // the text of the document, processed via the methods in
1106 // summarise.cpp
1107 if (docinfo.metadata.count("Summary") > 0 &&
1108 docinfo.metadata["Summary"].values.size() > 0) {
1109 return docinfo.metadata["Summary"].values[0];
1110 }
1111
1112 text_t textToSummarise, query;
1113 if(text.empty()) { // get document text
1114 DocumentRequest_t docrequest;
1115 DocumentResponse_t docresponse;
1116 comerror_t err;
1117 docrequest.OID = docinfo.OID;
1118 collectproto->get_document (collection, docrequest, docresponse, err, logout);
1119 textToSummarise = docresponse.doc;
1120 } else // in practice, this would not happen, because text is only
1121 // loaded with the [Text] command
1122 textToSummarise = text;
1123 disp.expandstring("_cgiargq_",query);
1124 return summarise(textToSummarise,query,80);
1125}
Note: See TracBrowser for help on using the repository browser.