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

Last change on this file since 3036 was 2967, checked in by paradis, 22 years ago

new metadata command [Summary]

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