source: main/tags/2.41/gsdl/src/recpt/formattools.cpp@ 32780

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

Added a [DocOID] format statement option that returns the OID of the
section being displayed.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 35.2 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
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.functionoptions = op;
491 }
492}
493
494
495static void get_sibling_options (text_t &instring, metadata_t &metaoption) {
496
497 assert (instring.size() > 8);
498 if (instring.size() <= 8) return;
499
500 text_t meta, com, op;
501 bool inbraces = false;
502 bool inquotes = false;
503 bool foundcolon = false;
504 text_t::const_iterator here = instring.begin()+7;
505 text_t::const_iterator end = instring.end();
506 while (here != end) {
507 if (*here == '(') inbraces = true;
508 else if (*here == ')') inbraces = false;
509 else if (*here == '\'' && !inquotes) inquotes = true;
510 else if (*here == '\'' && inquotes) inquotes = false;
511 else if (*here == ':' && !inbraces) foundcolon = true;
512 else if (foundcolon) meta.push_back (*here);
513 else if (inquotes) op.push_back (*here);
514 else com.push_back (*here);
515 here ++;
516 }
517
518 instring = meta;
519
520 if (com.empty()) {
521 metaoption.functionoptions = " ";
522 }
523 else {
524 metaoption.functionoptions = op;
525 }
526}
527
528
529static void parse_meta (text_t &meta, metadata_t &metaoption,
530 text_tset &metadata, bool &getParents) {
531
532 if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
533 metaoption.metacommand |= mCgiSafe;
534 meta = substr (meta.begin()+8, meta.end());
535 }
536
537 if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
538 getParents = true;
539 get_parent_options (meta, metaoption);
540 }
541 else if (meta.size() > 8 && (substr (meta.begin(), meta.begin()+7) == "sibling")) {
542 metaoption.metacommand |= mSibling;
543 get_sibling_options (meta, metaoption);
544 }
545
546 metadata.insert (meta);
547 metaoption.metaname = meta;
548}
549
550static void parse_meta (text_t &meta, format_t *formatlistptr,
551 text_tset &metadata, bool &getParents) {
552
553 if (meta == "link")
554 formatlistptr->command = comLink;
555 else if (meta == "/link")
556 formatlistptr->command = comEndLink;
557
558 else if (meta == "href")
559 formatlistptr->command = comHref;
560
561 else if (meta == "num")
562 formatlistptr->command = comNum;
563
564 else if (meta == "icon")
565 formatlistptr->command = comIcon;
566
567 else if (meta == "Text")
568 formatlistptr->command = comDoc;
569
570 else if (meta == "RelatedDocuments")
571 formatlistptr->command = comRel;
572
573 else if (meta == "highlight")
574 formatlistptr->command = comHighlight;
575
576 else if (meta == "/highlight")
577 formatlistptr->command = comEndHighlight;
578
579 else if (meta == "Summary")
580 formatlistptr->command = comSummary;
581
582 else if (meta == "DocImage")
583 formatlistptr->command = comImage;
584
585 else if (meta == "DocTOC")
586 formatlistptr->command = comTOC;
587
588 else if (meta == "DocumentButtonDetach")
589 formatlistptr->command = comDocumentButtonDetach;
590
591 else if (meta == "DocumentButtonHighlight")
592 formatlistptr->command = comDocumentButtonHighlight;
593
594 else if (meta == "DocumentButtonExpandContents")
595 formatlistptr->command = comDocumentButtonExpandContents;
596
597 else if (meta == "DocumentButtonExpandText")
598 formatlistptr->command = comDocumentButtonExpandText;
599
600 else if (meta == "DocOID")
601 formatlistptr->command = comOID;
602
603 else {
604 formatlistptr->command = comMeta;
605 parse_meta (meta, formatlistptr->meta, metadata, getParents);
606 }
607}
608
609static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
610 text_tset &metadata, bool &getParents) {
611
612 text_t text;
613 text_t::const_iterator here = formatstring.begin();
614 text_t::const_iterator end = formatstring.end();
615
616 while (here != end) {
617
618 if (*here == '\\') {
619 here ++;
620 if (here != end) text.push_back (*here);
621
622 } else if (*here == '{') {
623 if (!text.empty()) {
624 formatlistptr->command = comText;
625 formatlistptr->text = text;
626 formatlistptr->nextptr = new format_t();
627 formatlistptr = formatlistptr->nextptr;
628
629 text.clear();
630 }
631 if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
632
633 formatlistptr->nextptr = new format_t();
634 formatlistptr = formatlistptr->nextptr;
635 if (here == end) break;
636 }
637 } else if (*here == '[') {
638 if (!text.empty()) {
639 formatlistptr->command = comText;
640 formatlistptr->text = text;
641 formatlistptr->nextptr = new format_t();
642 formatlistptr = formatlistptr->nextptr;
643
644 text.clear();
645 }
646 text_t meta;
647 here ++;
648 while (*here != ']') {
649 if (here == end) return false;
650 meta.push_back (*here);
651 here ++;
652 }
653 parse_meta (meta, formatlistptr, metadata, getParents);
654 formatlistptr->nextptr = new format_t();
655 formatlistptr = formatlistptr->nextptr;
656
657 } else
658 text.push_back (*here);
659
660 if (here != end) here ++;
661 }
662 if (!text.empty()) {
663 formatlistptr->command = comText;
664 formatlistptr->text = text;
665 formatlistptr->nextptr = new format_t();
666 formatlistptr = formatlistptr->nextptr;
667
668 }
669 return true;
670}
671
672
673static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
674 format_t *formatlistptr, text_tset &metadata, bool &getParents) {
675
676 text_t::const_iterator it = findchar (here, end, '}');
677 if (it == end) return false;
678
679 text_t com = substr (here, it);
680 here = findchar (it, end, '{');
681 if (here == end) return false;
682 else here ++;
683
684 if (com == "If") formatlistptr->command = comIf;
685 else if (com == "Or") formatlistptr->command = comOr;
686 else return false;
687
688 int commacount = 0;
689 text_t text;
690 while (here != end) {
691
692 if (*here == '\\') {
693 here++;
694 if (here != end) text.push_back(*here);
695
696 }
697
698 else if (*here == ',' || *here == '}' || *here == '{') {
699
700 if (formatlistptr->command == comOr) {
701 // the {Or}{this, or this, or this, or this} statement
702 format_t *or_ptr;
703
704 // find the next unused orptr
705 if (formatlistptr->orptr == NULL) {
706 formatlistptr->orptr = new format_t();
707 or_ptr = formatlistptr->orptr;
708 } else {
709 or_ptr = formatlistptr->orptr;
710 while (or_ptr->nextptr != NULL)
711 or_ptr = or_ptr->nextptr;
712 or_ptr->nextptr = new format_t();
713 or_ptr = or_ptr->nextptr;
714 }
715
716 if (!text.empty())
717 {
718 if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
719 }
720
721 if (*here == '{')
722 {
723 // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
724 // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
725 // The latter can always be re-written:
726 // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
727
728 if (!text.empty()) // already used up allocated format_t
729 {
730 // => allocate new one for detected action
731 or_ptr->nextptr = new format_t();
732 or_ptr = or_ptr->nextptr;
733 }
734 if (!parse_action(++here, end, or_ptr, metadata, getParents))
735 {
736 return false;
737 }
738 }
739 else
740 {
741 if (*here == '}') break;
742 }
743 text.clear();
744
745 }
746
747 // Parse an {If}{decide,do,else} statement
748 else {
749
750 // Read the decision component.
751 if (commacount == 0) {
752 // Decsion can be a metadata element, or a piece of text.
753 // Originally Stefan's code, updated 25/10/2000 by Gordon.
754
755 text_t::const_iterator beginbracket = text.begin();
756 text_t::const_iterator endbracket = (text.end() - 1);
757
758 // Decision is based on a metadata element
759 if ((*beginbracket == '[') && (*endbracket == ']')) {
760 // Ignore the surrounding square brackets
761 text_t meta = substr (beginbracket+1, endbracket);
762 parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
763 commacount ++;
764 text.clear();
765 }
766
767 // Decision is a piece of text (probably a macro like _cgiargmode_).
768 else {
769 formatlistptr->decision.command = dText;
770 formatlistptr->decision.text = text;
771 commacount ++;
772 text.clear();
773 }
774 }
775
776 // Read the "then" and "else" components of the {If} statement.
777 else {
778 format_t** nextlistptr = NULL;
779 if (commacount == 1) {
780 nextlistptr = &formatlistptr->ifptr;
781 } else if (commacount == 2 ) {
782 nextlistptr = &formatlistptr->elseptr;
783 } else {
784 return false;
785 }
786
787 if (!text.empty()) {
788 if (*nextlistptr == NULL) {
789 *nextlistptr = new format_t();
790 } else {
791
792 // skip to the end of any format_t statements already added
793 while ((*nextlistptr)->nextptr != NULL)
794 {
795 nextlistptr = &(*nextlistptr)->nextptr;
796 }
797
798 (*nextlistptr)->nextptr = new format_t();
799 nextlistptr = &(*nextlistptr)->nextptr;
800 }
801
802 if (!parse_string (text, *nextlistptr, metadata, getParents))
803 {
804 return false;
805 }
806 text.clear();
807 }
808
809 if (*here == '{')
810 {
811 if (*nextlistptr == NULL) {
812 *nextlistptr = new format_t();
813 } else {
814 (*nextlistptr)->nextptr = new format_t();
815 nextlistptr = &(*nextlistptr)->nextptr;
816 }
817
818 if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
819 {
820 return false;
821 }
822 }
823 else
824 {
825 if (*here == '}') break;
826 commacount ++;
827 }
828 }
829 }
830
831 } else text.push_back(*here);
832
833 if (here != end) here ++;
834 }
835
836 return true;
837}
838
839
840bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
841 text_tset &metadata, bool &getParents) {
842
843 formatlistptr->clear();
844 getParents = false;
845
846 return (parse_string (formatstring, formatlistptr, metadata, getParents));
847}
848
849
850// note: all the format_date stuff is assuming that all Date metadata is going to
851// be of the form yyyymmdd, this is of course, crap ;)
852
853static text_t get_meta (ResultDocInfo_t &docinfo, const metadata_t &meta) {
854
855 // make sure we have the requested metadata
856 MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
857 if (it == docinfo.metadata.end()) return "";
858
859 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
860
861 switch (meta.parentcommand) {
862 case pNone:
863 {
864 if (meta.metacommand & mSibling) {
865 text_t tmp;
866 bool first = true;
867
868 MetadataInfo_t& metaname_rec = docinfo.metadata[meta.metaname];
869
870 const int start_i=0;
871 const int end_i = metaname_rec.values.size()-1;
872
873 for (int i=start_i; i<=end_i; i++) {
874 if (!first) tmp += meta.functionoptions;
875 if (meta.metaname == "Date") tmp += format_date (metaname_rec.values[i]);
876 else if (meta.metaname == "Language") tmp += iso639(metaname_rec.values[i]);
877 else tmp += metaname_rec.values[i];
878 first = false;
879 }
880
881 if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
882 else return tmp;
883
884 }
885 else {
886
887 text_t classifier_metaname = docinfo.classifier_metadata_type;
888 int metaname_index
889 = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
890 text_t metadata_item = docinfo.metadata[meta.metaname].values[metaname_index];
891
892 if (meta.metaname == "Date")
893 return format_date (metadata_item);
894 else if (meta.metaname == "Language")
895 return iso639(metadata_item);
896 if (meta.metacommand & mCgiSafe)
897 return cgi_safe (metadata_item);
898 else return metadata_item;
899 }
900 }
901
902 case pImmediate:
903 if (parent != NULL) {
904 if (meta.metaname == "Date")
905 return format_date (parent->values[0]);
906 if (meta.metacommand & mCgiSafe)
907 return cgi_safe (parent->values[0]);
908 else return parent->values[0];
909 }
910 break;
911
912 case pTop:
913 if (parent != NULL) {
914 while (parent->parent != NULL) parent = parent->parent;
915
916 if (meta.metaname == "Date")
917 return format_date (parent->values[0]);
918 if (meta.metacommand & mCgiSafe)
919 return cgi_safe (parent->values[0]);
920 else return parent->values[0];
921 }
922 break;
923
924 case pAll:
925 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
926 if (parent != NULL) {
927 text_tarray tmparray;
928 while (parent != NULL) {
929 tmparray.push_back (parent->values[0]);
930 parent = parent->parent;
931 }
932 bool first = true;
933 text_t tmp;
934 text_tarray::reverse_iterator here = tmparray.rbegin();
935 text_tarray::reverse_iterator end = tmparray.rend();
936 while (here != end) {
937 if (!first) tmp += meta.functionoptions;
938 if (meta.metaname == "Date") tmp += format_date (*here);
939 else tmp += *here;
940 first = false;
941 here ++;
942 }
943 if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
944 else return tmp;
945 }
946 }
947 return "";
948}
949
950static text_t get_or (const text_t& collection, recptproto* collectproto,
951 ResultDocInfo_t &docinfo, displayclass &disp,
952 format_t *orptr, text_tmap &options,
953 ostream& logout) {
954
955 text_t tmp;
956 while (orptr != NULL) {
957
958 tmp = format_string (collection,collectproto, docinfo, disp, orptr,
959 options, logout);
960 if (!tmp.empty()) return tmp;
961
962 orptr = orptr->nextptr;
963 }
964 return "";
965}
966
967static text_t get_if (const text_t& collection, recptproto* collectproto,
968 ResultDocInfo_t &docinfo, displayclass &disp,
969 const decision_t &decision,
970 format_t *ifptr, format_t *elseptr,
971 text_tmap &options, ostream& logout)
972{
973
974 // If the decision component is a metadata element, then evaluate it
975 // to see whether we output the "then" or the "else" clause
976 if (decision.command == dMeta) {
977 if (get_meta (docinfo, decision.meta) != "") {
978 if (ifptr != NULL)
979 return get_formatted_string (collection,collectproto, docinfo, disp, ifptr,
980 options, logout);
981 }
982 else {
983 if (elseptr != NULL)
984 return get_formatted_string (collection,collectproto, docinfo, disp, elseptr,
985 options, logout);
986 }
987 }
988
989 // If the decision component is text, then evaluate it (it is probably a
990 // macro like _cgiargmode_) to decide what to output.
991 else if (decision.command == dText) {
992
993 text_t outstring;
994 disp.expandstring (decision.text, outstring);
995
996 // This is a tad tricky. When we expand a string like _cgiargmode_, that is
997 // a cgi argument macro that has not been set, it evaluates to itself.
998 // Therefore, were have to say that a piece of text evalautes true if
999 // it is non-empty and if it is a cgi argument evaulating to itself.
1000 if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
1001 if (ifptr != NULL)
1002 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1003 options, logout);
1004 } else {
1005 if (elseptr != NULL)
1006 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1007 options, logout);
1008 }
1009 }
1010
1011 return "";
1012}
1013
1014bool includes_metadata(const text_t& text)
1015{
1016 text_t::const_iterator here = text.begin();
1017 text_t::const_iterator end = text.end();
1018 while (here != end) {
1019 if (*here == '[') return true;
1020 here ++;
1021 }
1022
1023 return false;
1024}
1025
1026static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
1027 recptproto* collectproto, ResultDocInfo_t &docinfo,
1028 displayclass &disp, text_tmap &options,
1029 ostream &logout) {
1030
1031 if (includes_metadata(metavalue))
1032 {
1033 // text has embedded metadata in it => expand it
1034 FilterRequest_t request;
1035 FilterResponse_t response;
1036
1037 request.getParents = false;
1038
1039 format_t *expanded_formatlistptr = new format_t();
1040 parse_formatstring (metavalue, expanded_formatlistptr,
1041 request.fields, request.getParents);
1042
1043 // retrieve metadata
1044 get_info(docinfo.OID, collection, request.fields, request.getParents,
1045 collectproto, response, logout);
1046
1047 if (!response.docInfo.empty())
1048 {
1049 text_t expanded_metavalue
1050 = get_formatted_string(collection, collectproto,
1051 response.docInfo[0], disp, expanded_formatlistptr,
1052 options, logout);
1053
1054 return expanded_metavalue;
1055 }
1056 else
1057 {
1058 return metavalue;
1059 }
1060 }
1061 else
1062 {
1063 return metavalue;
1064 }
1065}
1066
1067text_t format_string (const text_t& collection, recptproto* collectproto,
1068 ResultDocInfo_t &docinfo, displayclass &disp,
1069 format_t *formatlistptr, text_tmap &options,
1070 ostream& logout) {
1071
1072 if (formatlistptr == NULL) return "";
1073
1074 switch (formatlistptr->command) {
1075 case comOID:
1076 return docinfo.OID;
1077 case comText:
1078 return formatlistptr->text;
1079 case comLink:
1080 return options["link"];
1081 case comEndLink:
1082 if (options["link"].empty()) return "";
1083 else return "</a>";
1084 case comHref:
1085 return get_href(options["link"]);
1086 case comIcon:
1087 return options["icon"];
1088 case comNum:
1089 return docinfo.result_num;
1090 case comRel: //if [RelatedDocuments] appears in format string, collect relation data
1091 return get_related_docs(collection, collectproto, docinfo, logout);
1092 case comSummary:
1093 return format_summary(collection, collectproto, docinfo, disp, options, logout);
1094 case comMeta:
1095 {
1096 const text_t& metavalue = get_meta (docinfo, formatlistptr->meta);
1097 return expand_metadata(metavalue, collection, collectproto, docinfo, disp, options, logout);
1098 }
1099 case comDoc:
1100 return options["text"];
1101 case comImage:
1102 return expand_metadata(options["DocImage"], collection, collectproto, docinfo, disp, options, logout);
1103 case comTOC:
1104 return options["DocTOC"];
1105 case comDocumentButtonDetach:
1106 return options["DocumentButtonDetach"];
1107 case comDocumentButtonHighlight:
1108 return options["DocumentButtonHighlight"];
1109 case comDocumentButtonExpandContents:
1110 return options["DocumentButtonExpandContents"];
1111 case comDocumentButtonExpandText:
1112 return options["DocumentButtonExpandText"];
1113 case comHighlight:
1114 if (options["highlight"] == "1") return "<b>";
1115 break;
1116 case comEndHighlight:
1117 if (options["highlight"] == "1") return "</b>";
1118 break;
1119 case comIf:
1120 return get_if (collection, collectproto, docinfo, disp,
1121 formatlistptr->decision, formatlistptr->ifptr,
1122 formatlistptr->elseptr, options, logout);
1123 case comOr:
1124 return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
1125 options, logout);
1126 }
1127 return "";
1128}
1129
1130text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1131 ResultDocInfo_t &docinfo, displayclass &disp,
1132 format_t *formatlistptr, text_tmap &options,
1133 ostream& logout) {
1134
1135 text_t ft;
1136 while (formatlistptr != NULL)
1137 {
1138 ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
1139 options, logout);
1140 formatlistptr = formatlistptr->nextptr;
1141 }
1142
1143 return ft;
1144}
1145
1146
1147/* FUNCTION NAME: format_summary
1148 * DESC: this is invoked when a [Summary] special metadata is processed.
1149 * RETURNS: a query-biased summary for the document */
1150
1151text_t format_summary (const text_t& collection, recptproto* collectproto,
1152 ResultDocInfo_t &docinfo, displayclass &disp,
1153 text_tmap &options, ostream& logout) {
1154
1155 // GRB: added code here to ensure that the cstr (and other collections)
1156 // uses the document metadata item Summary, rather than compressing
1157 // the text of the document, processed via the methods in
1158 // summarise.cpp
1159 if (docinfo.metadata.count("Summary") > 0 &&
1160 docinfo.metadata["Summary"].values.size() > 0) {
1161 return docinfo.metadata["Summary"].values[0];
1162 }
1163
1164 text_t textToSummarise, query;
1165 if(options["text"].empty()) { // get document text
1166 DocumentRequest_t docrequest;
1167 DocumentResponse_t docresponse;
1168 comerror_t err;
1169 docrequest.OID = docinfo.OID;
1170 collectproto->get_document (collection, docrequest, docresponse, err, logout);
1171 textToSummarise = docresponse.doc;
1172 } else // in practice, this would not happen, because text is only
1173 // loaded with the [Text] command
1174 textToSummarise = options["text"];
1175 disp.expandstring("_cgiargq_",query);
1176 return summarise(textToSummarise,query,80);
1177}
Note: See TracBrowser for help on using the repository browser.