source: main/tags/2.32/gsdl/src/recpt/formattools.cpp@ 25600

Last change on this file since 25600 was 2001, checked in by sjboddie, 23 years ago

Added a hack that mysteriously converts iso639 language codes appearing
in formatstrings to the English name of the language (i.e. Language
metadata is now treated as a special case much like Date is).

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