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

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

Added new format options for [DocTOC], [DocImage], [DocumentButtonDetach],
[DocumentButtonHighlight], [DocumentButtonExpandText], and
[DocumentButtonExpandContents]. These can all be inserted in a
DocumentText format string to allow more flexible arrangement of table of
contents, document buttons, etc.

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