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

Last change on this file since 2710 was 2706, checked in by sjboddie, 23 years ago

added [href] format string entity

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 32.0 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
344text_t get_href (const text_t &link) {
345
346 text_t href;
347
348 text_t::const_iterator here = findchar(link.begin(), link.end(), '"');
349 text_t::const_iterator end = link.end();
350
351 here ++;
352 while (here != end) {
353 if (*here == '"') break;
354 href.push_back(*here);
355 here ++;
356 }
357
358 return href;
359}
360
361//this function gets the information associated with the relation
362//metadata for the document associated with 'docinfo'. This relation
363//metadata consists of a line of pairs containing 'collection, document OID'
364//(this is the OID of the document related to the current document, and
365//the collection the related document belongs to). For each of these pairs
366//the title metadata is obtained and then an html link between the title
367//of the related doc and the document's position (the document will be
368//found in "<a href=\"_httpdocument_&c=collection&cl=search&d=OID">
369//(where collection is the related documents collection, and OID is the
370//related documents OID). A list of these html links are made for as many
371//related documents as there are. This list is then returned. If there are
372//no related documents available for the current document then the string
373//'.. no related documents .. ' is returned.
374text_t get_related_docs(const text_t& collection, recptproto* collectproto,
375 ResultDocInfo_t &docinfo, ostream& logout){
376
377 text_tset metadata;
378
379 //insert the metadata we wish to collect
380 metadata.insert("relation");
381 metadata.insert("Title");
382 metadata.insert("Subject"); //for emails, where title data doesn't apply
383
384 FilterResponse_t response;
385 text_t relation = ""; //string for displaying relation metadata
386 text_t relationTitle = ""; //the related documents Title (or subject)
387 text_t relationOID = ""; //the related documents OID
388
389 //get the information associated with the metadata for current doc
390 if (get_info (docinfo.OID, collection, metadata,
391 false, collectproto, response, logout)) {
392
393 //if the relation metadata exists, store for displaying
394 if(!response.docInfo[0].metadata["relation"].values.empty()){
395 relationOID += response.docInfo[0].metadata["relation"].values[0];
396
397 //split relation data into pairs of collectionname,ID number
398 text_tarray relationpairs;
399 splitchar (relationOID.begin(), relationOID.end(), ' ', relationpairs);
400
401 text_tarray::const_iterator currDoc = relationpairs.begin();
402 text_tarray::const_iterator lastDoc = relationpairs.end();
403
404 //iterate through the pairs to split and display
405 while(currDoc != lastDoc){
406
407 //split pairs into collectionname and ID
408 text_tarray relationdata;
409 splitchar ((*currDoc).begin(), (*currDoc).end(), ',', relationdata);
410
411 //get first element in the array (collection)
412 text_tarray::const_iterator doc_data = relationdata.begin();
413 text_t document_collection = *doc_data;
414 doc_data++; //increment to get next item in array (oid)
415 text_t document_OID = *doc_data;
416
417 //create html link to related document
418 relation += "<a href=\"_httpdocument_&c=" + document_collection;
419 relation += "&cl=search&d=" + document_OID;
420
421 //get the information associated with the metadata for related doc
422 if (get_info (document_OID, document_collection, metadata,
423 false, collectproto, response, logout)) {
424
425 //if title metadata doesn't exist, collect subject metadata
426 //if that doesn't exist, just call it 'related document'
427 if (!response.docInfo[0].metadata["Title"].values[0].empty())
428 relationTitle = response.docInfo[0].metadata["Title"].values[0];
429 else if (!response.docInfo[0].metadata["Subject"].values.empty())
430 relationTitle = response.docInfo[0].metadata["Subject"].values[0];
431 else relationTitle = "RELATED DOCUMENT";
432
433 }
434
435 //link the related document's title to its page
436 relation += "\">" + relationTitle + "</a>";
437 relation += " (" + document_collection + ")<br>";
438
439 currDoc++;
440 }
441 }
442
443 }
444
445 if(relation.empty()) //no relation data for documnet
446 relation = ".. no related documents .. ";
447
448 return relation;
449}
450
451
452
453static void get_parent_options (text_t &instring, metadata_t &metaoption) {
454
455 assert (instring.size() > 7);
456 if (instring.size() <= 7) return;
457
458 text_t meta, com, op;
459 bool inbraces = false;
460 bool inquotes = false;
461 bool foundcolon = false;
462 text_t::const_iterator here = instring.begin()+6;
463 text_t::const_iterator end = instring.end();
464 while (here != end) {
465 if (*here == '(') inbraces = true;
466 else if (*here == ')') inbraces = false;
467 else if (*here == '\'' && !inquotes) inquotes = true;
468 else if (*here == '\'' && inquotes) inquotes = false;
469 else if (*here == ':' && !inbraces) foundcolon = true;
470 else if (foundcolon) meta.push_back (*here);
471 else if (inquotes) op.push_back (*here);
472 else com.push_back (*here);
473 here ++;
474 }
475 instring = meta;
476 if (com.empty())
477 metaoption.parentcommand = pImmediate;
478 else if (com == "Top")
479 metaoption.parentcommand = pTop;
480 else if (com == "All") {
481 metaoption.parentcommand = pAll;
482 metaoption.parentoptions = op;
483 }
484}
485
486static void parse_meta (text_t &meta, metadata_t &metaoption,
487 text_tset &metadata, bool &getParents) {
488
489 if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
490 metaoption.metacommand = mCgiSafe;
491 meta = substr (meta.begin()+8, meta.end());
492 }
493
494 if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
495 getParents = true;
496 get_parent_options (meta, metaoption);
497 }
498
499 metadata.insert (meta);
500 metaoption.metaname = meta;
501}
502
503static void parse_meta (text_t &meta, format_t *formatlistptr,
504 text_tset &metadata, bool &getParents) {
505
506 if (meta == "link")
507 formatlistptr->command = comLink;
508 else if (meta == "/link")
509 formatlistptr->command = comEndLink;
510
511 else if (meta == "href")
512 formatlistptr->command = comHref;
513
514 else if (meta == "num")
515 formatlistptr->command = comNum;
516
517 else if (meta == "icon")
518 formatlistptr->command = comIcon;
519
520 else if (meta == "Text")
521 formatlistptr->command = comDoc;
522
523 else if (meta == "RelatedDocuments")
524 formatlistptr->command = comRel;
525
526 else if (meta == "highlight")
527 formatlistptr->command = comHighlight;
528
529 else if (meta == "/highlight")
530 formatlistptr->command = comEndHighlight;
531
532 else {
533 formatlistptr->command = comMeta;
534 parse_meta (meta, formatlistptr->meta, metadata, getParents);
535 }
536}
537
538static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
539 text_tset &metadata, bool &getParents) {
540
541 text_t text;
542 text_t::const_iterator here = formatstring.begin();
543 text_t::const_iterator end = formatstring.end();
544
545 while (here != end) {
546
547 if (*here == '\\') {
548 here ++;
549 if (here != end) text.push_back (*here);
550
551 } else if (*here == '{') {
552 if (!text.empty()) {
553 formatlistptr->command = comText;
554 formatlistptr->text = text;
555 formatlistptr->nextptr = new format_t();
556 formatlistptr = formatlistptr->nextptr;
557
558 text.clear();
559 }
560 if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
561
562 formatlistptr->nextptr = new format_t();
563 formatlistptr = formatlistptr->nextptr;
564 if (here == end) break;
565 }
566 } else if (*here == '[') {
567 if (!text.empty()) {
568 formatlistptr->command = comText;
569 formatlistptr->text = text;
570 formatlistptr->nextptr = new format_t();
571 formatlistptr = formatlistptr->nextptr;
572
573 text.clear();
574 }
575 text_t meta;
576 here ++;
577 while (*here != ']') {
578 if (here == end) return false;
579 meta.push_back (*here);
580 here ++;
581 }
582 parse_meta (meta, formatlistptr, metadata, getParents);
583 formatlistptr->nextptr = new format_t();
584 formatlistptr = formatlistptr->nextptr;
585
586 } else
587 text.push_back (*here);
588
589 if (here != end) here ++;
590 }
591 if (!text.empty()) {
592 formatlistptr->command = comText;
593 formatlistptr->text = text;
594 formatlistptr->nextptr = new format_t();
595 formatlistptr = formatlistptr->nextptr;
596
597 }
598 return true;
599}
600
601
602static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
603 format_t *formatlistptr, text_tset &metadata, bool &getParents) {
604
605 text_t::const_iterator it = findchar (here, end, '}');
606 if (it == end) return false;
607
608 text_t com = substr (here, it);
609 here = findchar (it, end, '{');
610 if (here == end) return false;
611 else here ++;
612
613 if (com == "If") formatlistptr->command = comIf;
614 else if (com == "Or") formatlistptr->command = comOr;
615 else return false;
616
617 int commacount = 0;
618 text_t text;
619 while (here != end) {
620
621 if (*here == '\\') {
622 here++;
623 if (here != end) text.push_back(*here);
624
625 }
626
627 else if (*here == ',' || *here == '}' || *here == '{') {
628
629 if (formatlistptr->command == comOr) {
630 // the {Or}{this, or this, or this, or this} statement
631 format_t *or_ptr;
632
633 // find the next unused orptr
634 if (formatlistptr->orptr == NULL) {
635 formatlistptr->orptr = new format_t();
636 or_ptr = formatlistptr->orptr;
637 } else {
638 or_ptr = formatlistptr->orptr;
639 while (or_ptr->nextptr != NULL)
640 or_ptr = or_ptr->nextptr;
641 or_ptr->nextptr = new format_t();
642 or_ptr = or_ptr->nextptr;
643 }
644
645 if (!text.empty())
646 {
647 if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
648 }
649
650 if (*here == '{')
651 {
652 // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
653 // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
654 // The latter can always be re-written:
655 // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
656
657 if (!text.empty()) // already used up allocated format_t
658 {
659 // => allocate new one for detected action
660 or_ptr->nextptr = new format_t();
661 or_ptr = or_ptr->nextptr;
662 }
663 if (!parse_action(++here, end, or_ptr, metadata, getParents))
664 {
665 return false;
666 }
667 }
668 else
669 {
670 if (*here == '}') break;
671 }
672 text.clear();
673
674 }
675
676 // Parse an {If}{decide,do,else} statement
677 else {
678
679 // Read the decision component.
680 if (commacount == 0) {
681 // Decsion can be a metadata element, or a piece of text.
682 // Originally Stefan's code, updated 25/10/2000 by Gordon.
683
684 text_t::const_iterator beginbracket = text.begin();
685 text_t::const_iterator endbracket = (text.end() - 1);
686
687 // Decision is based on a metadata element
688 if ((*beginbracket == '[') && (*endbracket == ']')) {
689 // Ignore the surrounding square brackets
690 text_t meta = substr (beginbracket+1, endbracket);
691 parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
692 commacount ++;
693 text.clear();
694 }
695
696 // Decision is a piece of text (probably a macro like _cgiargmode_).
697 else {
698 formatlistptr->decision.command = dText;
699 formatlistptr->decision.text = text;
700 commacount ++;
701 text.clear();
702 }
703 }
704
705 // Read the "then" and "else" components of the {If} statement.
706 else {
707 format_t** nextlistptr = NULL;
708 if (commacount == 1) {
709 nextlistptr = &formatlistptr->ifptr;
710 } else if (commacount == 2 ) {
711 nextlistptr = &formatlistptr->elseptr;
712 } else {
713 return false;
714 }
715
716 if (!text.empty()) {
717 if (*nextlistptr == NULL) {
718 *nextlistptr = new format_t();
719 } else {
720
721 // skip to the end of any format_t statements already added
722 while ((*nextlistptr)->nextptr != NULL)
723 {
724 nextlistptr = &(*nextlistptr)->nextptr;
725 }
726
727 (*nextlistptr)->nextptr = new format_t();
728 nextlistptr = &(*nextlistptr)->nextptr;
729 }
730
731 if (!parse_string (text, *nextlistptr, metadata, getParents))
732 {
733 return false;
734 }
735 text.clear();
736 }
737
738 if (*here == '{')
739 {
740 if (*nextlistptr == NULL) {
741 *nextlistptr = new format_t();
742 } else {
743 (*nextlistptr)->nextptr = new format_t();
744 nextlistptr = &(*nextlistptr)->nextptr;
745 }
746
747 if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
748 {
749 return false;
750 }
751 }
752 else
753 {
754 if (*here == '}') break;
755 commacount ++;
756 }
757 }
758 }
759
760 } else text.push_back(*here);
761
762 if (here != end) here ++;
763 }
764
765 return true;
766}
767
768
769bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
770 text_tset &metadata, bool &getParents) {
771
772 formatlistptr->clear();
773 getParents = false;
774
775 return (parse_string (formatstring, formatlistptr, metadata, getParents));
776}
777
778
779// note: all the format_date stuff is assuming that all Date metadata is going to
780// be of the form yyyymmdd, this is of course, crap ;)
781
782static text_t get_meta (ResultDocInfo_t &docinfo, const metadata_t &meta) {
783
784 // make sure we have the requested metadata
785 MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
786 if (it == docinfo.metadata.end()) return "";
787
788 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
789
790 switch (meta.parentcommand) {
791 case pNone:
792 {
793 text_t classifier_metaname = docinfo.classifier_metadata_type;
794 int metaname_index
795 = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
796 text_t metadata_item = docinfo.metadata[meta.metaname].values[metaname_index];
797
798 if (meta.metaname == "Date")
799 return format_date (metadata_item);
800 else if (meta.metaname == "Language")
801 return iso639(metadata_item);
802 if (meta.metacommand == mCgiSafe)
803 return cgi_safe (metadata_item);
804 else return metadata_item;
805 }
806
807 case pImmediate:
808 if (parent != NULL) {
809 if (meta.metaname == "Date")
810 return format_date (parent->values[0]);
811 if (meta.metacommand == mCgiSafe)
812 return cgi_safe (parent->values[0]);
813 else return parent->values[0];
814 }
815 break;
816
817 case pTop:
818 if (parent != NULL) {
819 while (parent->parent != NULL) parent = parent->parent;
820
821 if (meta.metaname == "Date")
822 return format_date (parent->values[0]);
823 if (meta.metacommand == mCgiSafe)
824 return cgi_safe (parent->values[0]);
825 else return parent->values[0];
826 }
827 break;
828
829 case pAll:
830 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
831 if (parent != NULL) {
832 text_tarray tmparray;
833 while (parent != NULL) {
834 tmparray.push_back (parent->values[0]);
835 parent = parent->parent;
836 }
837 bool first = true;
838 text_t tmp;
839 text_tarray::reverse_iterator here = tmparray.rbegin();
840 text_tarray::reverse_iterator end = tmparray.rend();
841 while (here != end) {
842 if (!first) tmp += meta.parentoptions;
843 if (meta.metaname == "Date") tmp += format_date (*here);
844 else tmp += *here;
845 first = false;
846 here ++;
847 }
848 if (meta.metacommand == mCgiSafe) return cgi_safe (tmp);
849 else return tmp;
850 }
851 }
852 return "";
853}
854
855static text_t get_or (const text_t& collection, recptproto* collectproto,
856 ResultDocInfo_t &docinfo, displayclass &disp,
857 format_t *orptr,
858 const text_t &link, const text_t &icon,
859 const text_t &text, bool highlight,
860 ostream& logout) {
861
862 text_t tmp;
863 while (orptr != NULL) {
864
865 tmp = format_string (collection,collectproto, docinfo, disp, orptr,
866 link, icon, text, highlight, logout);
867 if (!tmp.empty()) return tmp;
868
869 orptr = orptr->nextptr;
870 }
871 return "";
872}
873
874static text_t get_if (const text_t& collection, recptproto* collectproto,
875 ResultDocInfo_t &docinfo, displayclass &disp,
876 const decision_t &decision,
877 format_t *ifptr, format_t *elseptr, const text_t &link,
878 const text_t &icon, const text_t &text, bool highlight,
879 ostream& logout)
880{
881
882 // If the decision component is a metadata element, then evaluate it
883 // to see whether we output the "then" or the "else" clause
884 if (decision.command == dMeta) {
885 if (get_meta (docinfo, decision.meta) != "") {
886 if (ifptr != NULL)
887 return get_formatted_string (collection,collectproto, docinfo, disp, ifptr,
888 link, icon, text, highlight, logout);
889 }
890 else {
891 if (elseptr != NULL)
892 return get_formatted_string (collection,collectproto, docinfo, disp, elseptr,
893 link, icon, text, highlight, logout);
894 }
895 }
896
897 // If the decision component is text, then evaluate it (it is probably a
898 // macro like _cgiargmode_) to decide what to output.
899 else if (decision.command == dText) {
900
901 text_t outstring;
902 disp.expandstring (decision.text, outstring);
903
904 // This is a tad tricky. When we expand a string like _cgiargmode_, that is
905 // a cgi argument macro that has not been set, it evaluates to itself.
906 // Therefore, were have to say that a piece of text evalautes true if
907 // it is non-empty and if it is a cgi argument evaulating to itself.
908 if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
909 if (ifptr != NULL)
910 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
911 link, icon, text, highlight, logout);
912 } else {
913 if (elseptr != NULL)
914 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
915 link, icon, text, highlight, logout);
916 }
917 }
918
919 return "";
920}
921
922bool includes_metadata(const text_t& text)
923{
924 text_t::const_iterator here = text.begin();
925 text_t::const_iterator end = text.end();
926 while (here != end) {
927 if (*here == '[') return true;
928 here ++;
929 }
930
931 return false;
932}
933
934
935
936text_t format_string (const text_t& collection, recptproto* collectproto,
937 ResultDocInfo_t &docinfo, displayclass &disp,
938 format_t *formatlistptr,
939 const text_t &link, const text_t &icon,
940 const text_t &text, bool highlight,
941 ostream& logout) {
942
943 if (formatlistptr == NULL) return "";
944
945 switch (formatlistptr->command) {
946 case comText:
947 return formatlistptr->text;
948 case comLink:
949 return link;
950 case comEndLink:
951 if (link.empty()) return "";
952 else return "</a>";
953 case comHref:
954 return get_href(link);
955 case comIcon:
956 return icon;
957 case comNum:
958 return docinfo.result_num;
959 case comRel: //if [RelatedDocuments] appears in format string, collect relation data
960 return get_related_docs(collection, collectproto, docinfo, logout);
961 case comMeta:
962
963 {
964 const text_t& metavalue = get_meta (docinfo, formatlistptr->meta);
965
966 if (includes_metadata(metavalue))
967 {
968 // text has embedded metadata in it => expand it
969 FilterRequest_t request;
970 FilterResponse_t response;
971
972 request.getParents = false;
973
974 format_t *expanded_formatlistptr = new format_t();
975 parse_formatstring (metavalue, expanded_formatlistptr,
976 request.fields, request.getParents);
977
978 // retrieve metadata
979 get_info(docinfo.OID, collection, request.fields, request.getParents,
980 collectproto, response, logout);
981
982 if (!response.docInfo.empty())
983 {
984 text_t expanded_metavalue
985 = get_formatted_string(collection, collectproto,
986 response.docInfo[0], disp, expanded_formatlistptr,
987 link, icon, highlight, logout);
988
989 return expanded_metavalue;
990 }
991 else
992 {
993 return metavalue;
994 }
995 }
996 else
997 {
998 return metavalue;
999 }
1000 }
1001 case comDoc:
1002 return text;
1003 case comHighlight:
1004 if (highlight) return "<b>";
1005 break;
1006 case comEndHighlight:
1007 if (highlight) return "</b>";
1008 break;
1009 case comIf:
1010 return get_if (collection, collectproto, docinfo, disp,
1011 formatlistptr->decision, formatlistptr->ifptr,
1012 formatlistptr->elseptr, link, icon, text, highlight,
1013 logout);
1014 case comOr:
1015 return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
1016 link, icon, text, highlight, logout);
1017 }
1018 return "";
1019}
1020
1021
1022
1023
1024text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1025 ResultDocInfo_t& docinfo, displayclass &disp,
1026 format_t* formatlistptr,
1027 const text_t& link, const text_t& icon,
1028 const text_t& text, const bool highlight,
1029 ostream& logout) {
1030
1031 text_t ft;
1032 while (formatlistptr != NULL)
1033 {
1034 ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
1035 link, icon, text, highlight, logout);
1036 formatlistptr = formatlistptr->nextptr;
1037 }
1038
1039 return ft;
1040}
1041
1042text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1043 ResultDocInfo_t &docinfo, displayclass &disp,
1044 format_t *formatlistptr,
1045 const text_t &link, const text_t &icon,
1046 const bool highlight,
1047 ostream& logout) {
1048
1049 text_t text = "";
1050
1051 return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
1052 link, icon, text, highlight, logout);
1053}
1054
1055text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1056 ResultDocInfo_t &docinfo, displayclass &disp,
1057 format_t *formatlistptr,
1058 const text_t& text,
1059 ostream& logout) {
1060
1061 text_t link = "<a href=\"_httpdocument_&cl=search&d=" + docinfo.OID + "\">";
1062 text_t icon = "_icontext_";
1063 bool highlight = false;
1064
1065 return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
1066 link, icon, text, highlight, logout);
1067}
1068
1069text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1070 ResultDocInfo_t &docinfo, displayclass &disp,
1071 format_t *formatlistptr,
1072 ostream& logout) {
1073
1074 text_t text = "";
1075
1076 return get_formatted_string(collection, collectproto, docinfo, disp, formatlistptr,
1077 text, logout);
1078}
Note: See TracBrowser for help on using the repository browser.