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

Last change on this file since 7067 was 6710, checked in by kjdon, 20 years ago

added a [DocRank] format statement option that returns the rank of the document in the results list

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