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

Last change on this file since 5787 was 5787, checked in by davidb, 20 years ago

Added functionality to format statements. In addition to 'parent(...):Metadata'
syntax, now supports 'sibling(...):Metadata' which retrieve multile values
of the given metadata value at that level in the doc.

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