source: main/tags/2.53/gsdl/src/recpt/formattools.cpp@ 33212

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

Metadata in [x] eq '1' updated so can support [parent(Top):x] syntax.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 41.5 KB
Line 
1/**********************************************************************
2 *
3 * formattools.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "formattools.h"
27#include "cgiutils.h"
28#include "OIDtools.h"
29#include "summarise.h"
30
31#include <assert.h>
32
33// a few function prototypes
34
35static text_t format_string (const text_t& collection, recptproto* collectproto,
36 ResultDocInfo_t &docinfo, displayclass &disp,
37 format_t *formatlistptr, text_tmap &options,
38 ostream& logout);
39
40static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
41 format_t *formatlistptr, text_tset &metadata, bool &getParents);
42
43static text_t format_summary (const text_t& collection, recptproto* collectproto,
44 ResultDocInfo_t &docinfo, displayclass &disp,
45 text_tmap &options, ostream& logout);
46
47
48void metadata_t::clear() {
49 metaname.clear();
50 metacommand = mNone;
51 parentcommand = pNone;
52 functionoptions.clear();
53}
54
55void decision_t::clear() {
56 command = dMeta;
57 meta.clear();
58 text.clear();
59}
60
61void format_t::clear() {
62 command = comText;
63 decision.clear();
64 text.clear();
65 meta.clear();
66 nextptr = NULL;
67 ifptr = NULL;
68 elseptr = NULL;
69 orptr = NULL;
70}
71
72void formatinfo_t::clear() {
73 DocumentImages = false;
74 DocumentTitles = true;
75 DocumentHeading = "{Or}{[parent(Top):Title],[Title],untitled}<br>";
76 DocumentContents = true;
77 DocumentArrowsBottom = true;
78 DocumentArrowsTop = false;
79 DocumentButtons.erase (DocumentButtons.begin(), DocumentButtons.end());
80 // DocumentButtons.push_back ("Expand Text");
81 // DocumentButtons.push_back ("Expand Contents");
82 DocumentButtons.push_back ("Detach");
83 DocumentButtons.push_back ("Highlight");
84 RelatedDocuments = "";
85 DocumentText = "<center><table width=_pagewidth_><tr><td>[Text]</td></tr></table></center>";
86 formatstrings.erase (formatstrings.begin(), formatstrings.end());
87 DocumentUseHTML = false;
88 AllowExtendedOptions = false;
89}
90
91// simply checks to see if formatstring begins with a <td> tag
92bool is_table_content (const text_t &formatstring) {
93 text_t::const_iterator here = formatstring.begin();
94 text_t::const_iterator end = formatstring.end();
95
96 while (here != end) {
97 if (*here != ' ') {
98 if ((*here == '<') && ((here+3) < end)) {
99 if ((*(here+1) == 't' || *(here+1) == 'T') &&
100 (*(here+2) == 'd' || *(here+2) == 'D') &&
101 (*(here+3) == '>' || *(here+3) == ' '))
102 return true;
103 } else return false;
104 }
105 here ++;
106 }
107 return false;
108}
109
110bool is_table_content (const format_t *formatlistptr) {
111
112 if (formatlistptr == NULL) return false;
113
114 if (formatlistptr->command == comText)
115 return is_table_content (formatlistptr->text);
116
117 return false;
118}
119
120// returns false if key isn't in formatstringmap
121bool get_formatstring (const text_t &key, const text_tmap &formatstringmap,
122 text_t &formatstring) {
123
124 formatstring.clear();
125 text_tmap::const_iterator it = formatstringmap.find(key);
126 if (it == formatstringmap.end()) return false;
127 formatstring = (*it).second;
128 return true;
129}
130
131// tries to find "key1key2" then "key1" then "key2"
132bool get_formatstring (const text_t &key1, const text_t &key2,
133 const text_tmap &formatstringmap,
134 text_t &formatstring) {
135
136 formatstring.clear();
137 text_tmap::const_iterator it = formatstringmap.find(key1 + key2);
138 if (it != formatstringmap.end()) {
139 formatstring = (*it).second;
140 return true;
141 }
142 it = formatstringmap.find(key1);
143 if (it != formatstringmap.end()) {
144 formatstring = (*it).second;
145 return true;
146 }
147 it = formatstringmap.find(key2);
148 if (it != formatstringmap.end()) {
149 formatstring = (*it).second;
150 return true;
151 }
152 return false;
153}
154
155
156text_t remove_namespace(const text_t &meta_name) {
157 text_t::const_iterator end = meta_name.end();
158 text_t::const_iterator it = findchar(meta_name.begin(), end, '.');
159 if (it != end) {
160 return substr(it+1, end);
161 }
162 return meta_name;
163
164}
165// returns a date of form 31 _textmonthnn_ 1999
166// input is date of type 19991231
167// at least the year must be present in date
168text_t format_date (const text_t &date) {
169
170 if (date.size() < 4) return "";
171
172 text_t::const_iterator datebegin = date.begin();
173
174 text_t year = substr (datebegin, datebegin+4);
175
176 if (date.size() < 6) return year;
177
178 text_t month = "_textmonth" + substr (datebegin+4, datebegin+6) + "_";
179 int imonth = month.getint();
180 if (imonth < 0 || imonth > 12) return year;
181
182 if (date.size() < 8) return month + " " + year;
183
184 text_t day = substr (datebegin+6, datebegin+8);
185 if (day[0] == '0') day = substr (day.begin()+1, day.end());
186 int iday = day.getint();
187 if (iday < 0 || iday > 31) return month + " " + year;
188
189 return day + " " + month + " " + year;
190}
191
192// converts an iso639 language code to its English equivalent
193// I realize that this isn't the pretiest or most efficient implementation,
194// hopefully this ugly Language (and Date too) formatting won't survive to
195// see gsdl-3.0
196text_t iso639 (const text_t &langcode) {
197
198 if (langcode == "aa") return "Afar";
199 if (langcode == "ab") return "Abkhazian";
200 if (langcode == "af") return "Afrikaans";
201 if (langcode == "am") return "Amharic";
202 if (langcode == "ar") return "Arabic";
203 if (langcode == "as") return "Assamese";
204 if (langcode == "ay") return "Aymara";
205 if (langcode == "az") return "Azerbaijani";
206
207 if (langcode == "ba") return "Bashkir";
208 if (langcode == "be") return "Byelorussian";
209 if (langcode == "bg") return "Bulgarian";
210 if (langcode == "bh") return "Bihari";
211 if (langcode == "bi") return "Bislama";
212 if (langcode == "bn") return "Bengali; Bangla";
213 if (langcode == "bo") return "Tibetan";
214 if (langcode == "br") return "Breton";
215
216 if (langcode == "ca") return "Catalan";
217 if (langcode == "co") return "Corsican";
218 if (langcode == "cs") return "Czech";
219 if (langcode == "cy") return "Welsh";
220
221 if (langcode == "da") return "Danish";
222 if (langcode == "de") return "German";
223 if (langcode == "dz") return "Bhutani";
224
225 if (langcode == "el") return "Greek";
226 if (langcode == "en") return "English";
227 if (langcode == "eo") return "Esperanto";
228 if (langcode == "es") return "Spanish";
229 if (langcode == "et") return "Estonian";
230 if (langcode == "eu") return "Basque";
231
232 if (langcode == "fa") return "Persian";
233 if (langcode == "fi") return "Finnish";
234 if (langcode == "fj") return "Fiji";
235 if (langcode == "fo") return "Faroese";
236 if (langcode == "fr") return "French";
237 if (langcode == "fy") return "Frisian";
238
239 if (langcode == "ga") return "Irish";
240 if (langcode == "gd") return "Scots Gaelic";
241 if (langcode == "gl") return "Galician";
242 if (langcode == "gn") return "Guarani";
243 if (langcode == "gu") return "Gujarati";
244
245 if (langcode == "ha") return "Hausa";
246 if (langcode == "hi") return "Hindi";
247 if (langcode == "hr") return "Croatian";
248 if (langcode == "hu") return "Hungarian";
249 if (langcode == "hy") return "Armenian";
250
251 if (langcode == "ia") return "Interlingua";
252 if (langcode == "ie") return "Interlingue";
253 if (langcode == "ik") return "Inupiak";
254 if (langcode == "in") return "Indonesian";
255 if (langcode == "is") return "Icelandic";
256 if (langcode == "it") return "Italian";
257 if (langcode == "iw") return "Hebrew";
258
259 if (langcode == "ja") return "Japanese";
260 if (langcode == "ji") return "Yiddish";
261 if (langcode == "jw") return "Javanese";
262
263 if (langcode == "ka") return "Georgian";
264 if (langcode == "kk") return "Kazakh";
265 if (langcode == "kl") return "Greenlandic";
266 if (langcode == "km") return "Cambodian";
267 if (langcode == "kn") return "Kannada";
268 if (langcode == "ko") return "Korean";
269 if (langcode == "ks") return "Kashmiri";
270 if (langcode == "ku") return "Kurdish";
271 if (langcode == "ky") return "Kirghiz";
272
273 if (langcode == "la") return "Latin";
274 if (langcode == "ln") return "Lingala";
275 if (langcode == "lo") return "Laothian";
276 if (langcode == "lt") return "Lithuanian";
277 if (langcode == "lv") return "Latvian, Lettish";
278
279 if (langcode == "mg") return "Malagasy";
280 if (langcode == "mi") return "Maori";
281 if (langcode == "mk") return "Macedonian";
282 if (langcode == "ml") return "Malayalam";
283 if (langcode == "mn") return "Mongolian";
284 if (langcode == "mo") return "Moldavian";
285 if (langcode == "mr") return "Marathi";
286 if (langcode == "ms") return "Malay";
287 if (langcode == "mt") return "Maltese";
288 if (langcode == "my") return "Burmese";
289
290 if (langcode == "na") return "Nauru";
291 if (langcode == "ne") return "Nepali";
292 if (langcode == "nl") return "Dutch";
293 if (langcode == "no") return "Norwegian";
294
295 if (langcode == "oc") return "Occitan";
296 if (langcode == "om") return "(Afan) Oromo";
297 if (langcode == "or") return "Oriya";
298
299 if (langcode == "pa") return "Punjabi";
300 if (langcode == "pl") return "Polish";
301 if (langcode == "ps") return "Pashto, Pushto";
302 if (langcode == "pt") return "Portuguese";
303
304 if (langcode == "qu") return "Quechua";
305 if (langcode == "rm") return "Rhaeto-Romance";
306 if (langcode == "rn") return "Kirundi";
307 if (langcode == "ro") return "Romanian";
308 if (langcode == "ru") return "Russian";
309 if (langcode == "rw") return "Kinyarwanda";
310
311 if (langcode == "sa") return "Sanskrit";
312 if (langcode == "sd") return "Sindhi";
313 if (langcode == "sg") return "Sangro";
314 if (langcode == "sh") return "Serbo-Croatian";
315 if (langcode == "si") return "Singhalese";
316 if (langcode == "sk") return "Slovak";
317 if (langcode == "sl") return "Slovenian";
318 if (langcode == "sm") return "Samoan";
319 if (langcode == "sn") return "Shona";
320 if (langcode == "so") return "Somali";
321 if (langcode == "sq") return "Albanian";
322 if (langcode == "sr") return "Serbian";
323 if (langcode == "ss") return "Siswati";
324 if (langcode == "st") return "Sesotho";
325 if (langcode == "su") return "Sudanese";
326 if (langcode == "sv") return "Swedish";
327 if (langcode == "sw") return "Swahili";
328
329 if (langcode == "ta") return "Tamil";
330 if (langcode == "te") return "Tegulu";
331 if (langcode == "tg") return "Tajik";
332 if (langcode == "th") return "Thai";
333 if (langcode == "ti") return "Tigrinya";
334 if (langcode == "tk") return "Turkmen";
335 if (langcode == "tl") return "Tagalog";
336 if (langcode == "tn") return "Setswana";
337 if (langcode == "to") return "Tonga";
338 if (langcode == "tr") return "Turkish";
339 if (langcode == "ts") return "Tsonga";
340 if (langcode == "tt") return "Tatar";
341 if (langcode == "tw") return "Twi";
342
343 if (langcode == "uk") return "Ukrainian";
344 if (langcode == "ur") return "Urdu";
345 if (langcode == "uz") return "Uzbek";
346
347 if (langcode == "vi") return "Vietnamese";
348 if (langcode == "vo") return "Volapuk";
349
350 if (langcode == "wo") return "Wolof";
351
352 if (langcode == "xh") return "Xhosa";
353
354 if (langcode == "yo") return "Yoruba";
355
356 if (langcode == "zh") return "Chinese";
357 if (langcode == "zu") return "Zulu";
358 return "";
359}
360
361text_t get_href (const text_t &link) {
362
363 text_t href;
364
365 text_t::const_iterator here = findchar(link.begin(), link.end(), '"');
366 text_t::const_iterator end = link.end();
367
368 here ++;
369 while (here != end) {
370 if (*here == '"') break;
371 href.push_back(*here);
372 here ++;
373 }
374
375 return href;
376}
377
378//this function gets the information associated with the relation
379//metadata for the document associated with 'docinfo'. This relation
380//metadata consists of a line of pairs containing 'collection, document OID'
381//(this is the OID of the document related to the current document, and
382//the collection the related document belongs to). For each of these pairs
383//the title metadata is obtained and then an html link between the title
384//of the related doc and the document's position (the document will be
385//found in "<a href=\"_httpdocument_&c=collection&cl=search&d=OID">
386//(where collection is the related documents collection, and OID is the
387//related documents OID). A list of these html links are made for as many
388//related documents as there are. This list is then returned. If there are
389//no related documents available for the current document then the string
390//'.. no related documents .. ' is returned.
391text_t get_related_docs(const text_t& collection, recptproto* collectproto,
392 ResultDocInfo_t &docinfo, ostream& logout){
393
394 text_tset metadata;
395
396 //insert the metadata we wish to collect
397 metadata.insert("relation");
398 metadata.insert("Title");
399 metadata.insert("Subject"); //for emails, where title data doesn't apply
400
401 FilterResponse_t response;
402 text_t relation = ""; //string for displaying relation metadata
403 text_t relationTitle = ""; //the related documents Title (or subject)
404 text_t relationOID = ""; //the related documents OID
405
406 //get the information associated with the metadata for current doc
407 if (get_info (docinfo.OID, collection, "", metadata,
408 false, collectproto, response, logout)) {
409
410 //if the relation metadata exists, store for displaying
411 if(!response.docInfo[0].metadata["relation"].values.empty()){
412 relationOID += response.docInfo[0].metadata["relation"].values[0];
413
414 //split relation data into pairs of collectionname,ID number
415 text_tarray relationpairs;
416 splitchar (relationOID.begin(), relationOID.end(), ' ', relationpairs);
417
418 text_tarray::const_iterator currDoc = relationpairs.begin();
419 text_tarray::const_iterator lastDoc = relationpairs.end();
420
421 //iterate through the pairs to split and display
422 while(currDoc != lastDoc){
423
424 //split pairs into collectionname and ID
425 text_tarray relationdata;
426 splitchar ((*currDoc).begin(), (*currDoc).end(), ',', relationdata);
427
428 //get first element in the array (collection)
429 text_tarray::const_iterator doc_data = relationdata.begin();
430 text_t document_collection = *doc_data;
431 doc_data++; //increment to get next item in array (oid)
432 text_t document_OID = *doc_data;
433
434 //create html link to related document
435 relation += "<a href=\"_httpdocument_&c=" + document_collection;
436 relation += "&cl=search&d=" + document_OID;
437
438 //get the information associated with the metadata for related doc
439 if (get_info (document_OID, document_collection, "", metadata,
440 false, collectproto, response, logout)) {
441
442 //if title metadata doesn't exist, collect subject metadata
443 //if that doesn't exist, just call it 'related document'
444 if (!response.docInfo[0].metadata["Title"].values[0].empty())
445 relationTitle = response.docInfo[0].metadata["Title"].values[0];
446 else if (!response.docInfo[0].metadata["Subject"].values.empty())
447 relationTitle = response.docInfo[0].metadata["Subject"].values[0];
448 else relationTitle = "RELATED DOCUMENT";
449
450 }
451
452 //link the related document's title to its page
453 relation += "\">" + relationTitle + "</a>";
454 relation += " (" + document_collection + ")<br>";
455
456 currDoc++;
457 }
458 }
459
460 }
461
462 if(relation.empty()) //no relation data for documnet
463 relation = ".. no related documents .. ";
464
465 return relation;
466}
467
468
469
470static void get_parent_options (text_t &instring, metadata_t &metaoption) {
471
472 assert (instring.size() > 7);
473 if (instring.size() <= 7) return;
474
475 text_t meta, com, op;
476 bool inbraces = false;
477 bool inquotes = false;
478 bool foundcolon = false;
479 text_t::const_iterator here = instring.begin()+6;
480 text_t::const_iterator end = instring.end();
481 while (here != end) {
482 if (*here == '(') inbraces = true;
483 else if (*here == ')') inbraces = false;
484 else if (*here == '\'' && !inquotes) inquotes = true;
485 else if (*here == '\'' && inquotes) inquotes = false;
486 else if (*here == ':' && !inbraces) foundcolon = true;
487 else if (foundcolon) meta.push_back (*here);
488 else if (inquotes) op.push_back (*here);
489 else com.push_back (*here);
490 here ++;
491 }
492 instring = meta;
493 if (com.empty())
494 metaoption.parentcommand = pImmediate;
495 else if (com == "Top")
496 metaoption.parentcommand = pTop;
497 else if (com == "All") {
498 metaoption.parentcommand = pAll;
499 metaoption.functionoptions = op;
500 }
501}
502
503
504static void get_sibling_options (text_t &instring, metadata_t &metaoption) {
505
506 assert (instring.size() > 8);
507 if (instring.size() <= 8) return;
508
509 text_t meta, com, op;
510 bool inbraces = false;
511 bool inquotes = false;
512 bool foundcolon = false;
513 text_t::const_iterator here = instring.begin()+7;
514 text_t::const_iterator end = instring.end();
515 while (here != end) {
516 if (*here == '(') inbraces = true;
517 else if (*here == ')') inbraces = false;
518 else if (*here == '\'' && !inquotes) inquotes = true;
519 else if (*here == '\'' && inquotes) inquotes = false;
520 else if (*here == ':' && !inbraces) foundcolon = true;
521 else if (foundcolon) meta.push_back (*here);
522 else if (inquotes) op.push_back (*here);
523 else com.push_back (*here);
524 here ++;
525 }
526
527 instring = meta;
528
529 if (com.empty()) {
530 metaoption.functionoptions = " ";
531 }
532 else {
533 metaoption.functionoptions = op;
534 }
535}
536
537
538static void parse_meta (text_t &meta, metadata_t &metaoption,
539 text_tset &metadata, bool &getParents) {
540
541 if (meta.size() > 8 && (substr(meta.begin(), meta.begin()+8) == "cgisafe:")) {
542 metaoption.metacommand |= mCgiSafe;
543 meta = substr (meta.begin()+8, meta.end());
544 }
545
546 if (meta.size() > 7 && (substr (meta.begin(), meta.begin()+6) == "parent")) {
547 getParents = true;
548 get_parent_options (meta, metaoption);
549 }
550 else if (meta.size() > 8 && (substr (meta.begin(), meta.begin()+7) == "sibling")) {
551 metaoption.metacommand |= mSibling;
552 get_sibling_options (meta, metaoption);
553 }
554
555 // check for ex. which may occur in format statements
556 if (meta.size()>3 && (substr(meta.begin(), meta.begin()+3) == "ex.")) {
557 meta = substr (meta.begin()+3, meta.end());
558 }
559 metadata.insert (meta);
560 metaoption.metaname = meta;
561}
562
563static void parse_meta (text_t &meta, format_t *formatlistptr,
564 text_tset &metadata, bool &getParents) {
565
566 if (meta == "link")
567 formatlistptr->command = comLink;
568 else if (meta == "/link")
569 formatlistptr->command = comEndLink;
570
571 else if (meta == "href")
572 formatlistptr->command = comHref;
573
574 else if (meta == "num")
575 formatlistptr->command = comNum;
576
577 else if (meta == "icon")
578 formatlistptr->command = comIcon;
579
580 else if (meta == "Text")
581 formatlistptr->command = comDoc;
582
583 else if (meta == "RelatedDocuments")
584 formatlistptr->command = comRel;
585
586 else if (meta == "highlight")
587 formatlistptr->command = comHighlight;
588
589 else if (meta == "/highlight")
590 formatlistptr->command = comEndHighlight;
591
592 else if (meta == "Summary")
593 formatlistptr->command = comSummary;
594
595 else if (meta == "DocImage")
596 formatlistptr->command = comImage;
597
598 else if (meta == "DocTOC")
599 formatlistptr->command = comTOC;
600
601 else if (meta == "DocumentButtonDetach")
602 formatlistptr->command = comDocumentButtonDetach;
603
604 else if (meta == "DocumentButtonHighlight")
605 formatlistptr->command = comDocumentButtonHighlight;
606
607 else if (meta == "DocumentButtonExpandContents")
608 formatlistptr->command = comDocumentButtonExpandContents;
609
610 else if (meta == "DocumentButtonExpandText")
611 formatlistptr->command = comDocumentButtonExpandText;
612
613 else if (meta == "DocOID")
614 formatlistptr->command = comOID;
615 else if (meta == "DocRank")
616 formatlistptr->command = comRank;
617 else {
618 formatlistptr->command = comMeta;
619 parse_meta (meta, formatlistptr->meta, metadata, getParents);
620 }
621}
622
623static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
624 text_tset &metadata, bool &getParents) {
625
626 text_t text;
627 text_t::const_iterator here = formatstring.begin();
628 text_t::const_iterator end = formatstring.end();
629
630 while (here != end) {
631
632 if (*here == '\\') {
633 here ++;
634 if (here != end) text.push_back (*here);
635
636 } else if (*here == '{') {
637 if (!text.empty()) {
638 formatlistptr->command = comText;
639 formatlistptr->text = text;
640 formatlistptr->nextptr = new format_t();
641 formatlistptr = formatlistptr->nextptr;
642
643 text.clear();
644 }
645 if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
646
647 formatlistptr->nextptr = new format_t();
648 formatlistptr = formatlistptr->nextptr;
649 if (here == end) break;
650 }
651 } else if (*here == '[') {
652 if (!text.empty()) {
653 formatlistptr->command = comText;
654 formatlistptr->text = text;
655 formatlistptr->nextptr = new format_t();
656 formatlistptr = formatlistptr->nextptr;
657
658 text.clear();
659 }
660 text_t meta;
661 here ++;
662 while (*here != ']') {
663 if (here == end) return false;
664 meta.push_back (*here);
665 here ++;
666 }
667 parse_meta (meta, formatlistptr, metadata, getParents);
668 formatlistptr->nextptr = new format_t();
669 formatlistptr = formatlistptr->nextptr;
670
671 } else
672 text.push_back (*here);
673
674 if (here != end) here ++;
675 }
676 if (!text.empty()) {
677 formatlistptr->command = comText;
678 formatlistptr->text = text;
679 formatlistptr->nextptr = new format_t();
680 formatlistptr = formatlistptr->nextptr;
681
682 }
683 return true;
684}
685
686
687static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
688 format_t *formatlistptr, text_tset &metadata, bool &getParents) {
689
690 text_t::const_iterator it = findchar (here, end, '}');
691 if (it == end) return false;
692
693 text_t com = substr (here, it);
694 here = findchar (it, end, '{');
695 if (here == end) return false;
696 else here ++;
697
698 if (com == "If" || com == "if" || com == "IF") formatlistptr->command = comIf;
699 else if (com == "Or" || com == "or" || com == "OR") formatlistptr->command = comOr;
700 else return false;
701
702 int commacount = 0;
703 text_t text;
704 while (here != end) {
705
706 if (*here == '\\') {
707 here++;
708 if (here != end) text.push_back(*here);
709
710 }
711
712 else if (*here == ',' || *here == '}' || *here == '{') {
713
714 if (formatlistptr->command == comOr) {
715 // the {Or}{this, or this, or this, or this} statement
716 format_t *or_ptr;
717
718 // find the next unused orptr
719 if (formatlistptr->orptr == NULL) {
720 formatlistptr->orptr = new format_t();
721 or_ptr = formatlistptr->orptr;
722 } else {
723 or_ptr = formatlistptr->orptr;
724 while (or_ptr->nextptr != NULL)
725 or_ptr = or_ptr->nextptr;
726 or_ptr->nextptr = new format_t();
727 or_ptr = or_ptr->nextptr;
728 }
729
730 if (!text.empty())
731 {
732 if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
733 }
734
735 if (*here == '{')
736 {
737 // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
738 // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
739 // The latter can always be re-written:
740 // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
741
742 if (!text.empty()) // already used up allocated format_t
743 {
744 // => allocate new one for detected action
745 or_ptr->nextptr = new format_t();
746 or_ptr = or_ptr->nextptr;
747 }
748 if (!parse_action(++here, end, or_ptr, metadata, getParents))
749 {
750 return false;
751 }
752 }
753 else
754 {
755 if (*here == '}') break;
756 }
757 text.clear();
758
759 }
760
761 // Parse an {If}{decide,do,else} statement
762 else {
763
764 // Read the decision component.
765 if (commacount == 0) {
766 // Decsion can be a metadata element, or a piece of text.
767 // Originally Stefan's code, updated 25/10/2000 by Gordon.
768
769 text_t::const_iterator beginbracket = text.begin();
770 text_t::const_iterator endbracket = (text.end() - 1);
771
772 // Decision is based on a metadata element
773 if ((*beginbracket == '[') && (*endbracket == ']')) {
774 // Ignore the surrounding square brackets
775 text_t meta = substr (beginbracket+1, endbracket);
776 parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
777 commacount ++;
778 text.clear();
779 }
780
781 // Decision is a piece of text (probably a macro like _cgiargmode_).
782 else {
783
784 // hunt for any metadata in string, which might be uses in
785 // to test a condition, e.g. [Format] eq 'PDF'
786 format_t* dummyformat = new format_t();
787 // update which metadata fields needed
788 // (not interested in updatng formatlistptr)
789 parse_string (text, dummyformat, metadata, getParents);
790 delete dummyformat;
791
792 formatlistptr->decision.command = dText;
793 formatlistptr->decision.text = text;
794 commacount ++;
795 text.clear();
796 }
797 }
798
799 // Read the "then" and "else" components of the {If} statement.
800 else {
801 format_t** nextlistptr = NULL;
802 if (commacount == 1) {
803 nextlistptr = &formatlistptr->ifptr;
804 } else if (commacount == 2 ) {
805 nextlistptr = &formatlistptr->elseptr;
806 } else {
807 return false;
808 }
809
810 if (!text.empty()) {
811 if (*nextlistptr == NULL) {
812 *nextlistptr = new format_t();
813 } else {
814
815 // skip to the end of any format_t statements already added
816 while ((*nextlistptr)->nextptr != NULL)
817 {
818 nextlistptr = &(*nextlistptr)->nextptr;
819 }
820
821 (*nextlistptr)->nextptr = new format_t();
822 nextlistptr = &(*nextlistptr)->nextptr;
823 }
824
825 if (!parse_string (text, *nextlistptr, metadata, getParents))
826 {
827 return false;
828 }
829 text.clear();
830 }
831
832 if (*here == '{')
833 {
834 if (*nextlistptr == NULL) {
835 *nextlistptr = new format_t();
836 } else {
837 // skip to the end of any format_t statements already added
838 while ((*nextlistptr)->nextptr != NULL)
839 {
840 nextlistptr = &(*nextlistptr)->nextptr;
841 }
842
843 (*nextlistptr)->nextptr = new format_t();
844 nextlistptr = &(*nextlistptr)->nextptr;
845 }
846
847 if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
848 {
849 return false;
850 }
851 }
852 else
853 {
854 if (*here == '}') break;
855 commacount ++;
856 }
857 }
858 }
859
860 } else text.push_back(*here);
861
862 if (here != end) here ++;
863 }
864
865 return true;
866}
867
868
869bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
870 text_tset &metadata, bool &getParents) {
871
872 formatlistptr->clear();
873 getParents = false;
874
875 return (parse_string (formatstring, formatlistptr, metadata, getParents));
876}
877
878
879// note: all the format_date stuff is assuming that all Date metadata is going to
880// be of the form yyyymmdd, this is of course, crap ;)
881
882static text_t get_meta (ResultDocInfo_t &docinfo, const metadata_t &meta) {
883
884 // make sure we have the requested metadata
885 MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
886 if (it == docinfo.metadata.end()) return "";
887
888 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
889 text_t no_ns_metaname = remove_namespace(meta.metaname);
890 switch (meta.parentcommand) {
891 case pNone:
892 {
893 if (meta.metacommand & mSibling) {
894 text_t tmp;
895 bool first = true;
896
897 MetadataInfo_t& metaname_rec = docinfo.metadata[meta.metaname];
898
899 const int start_i=0;
900 const int end_i = metaname_rec.values.size()-1;
901
902 for (int i=start_i; i<=end_i; i++) {
903 if (!first) tmp += meta.functionoptions;
904
905 if (no_ns_metaname == "Date") tmp += format_date (metaname_rec.values[i]);
906 else if (no_ns_metaname == "Language") tmp += iso639(metaname_rec.values[i]);
907 else tmp += metaname_rec.values[i];
908 first = false;
909 }
910
911 if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
912 else return tmp;
913
914 }
915 else {
916
917 text_t classifier_metaname = docinfo.classifier_metadata_type;
918 int metaname_index
919 = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
920 text_t metadata_item = docinfo.metadata[meta.metaname].values[metaname_index];
921
922 if (no_ns_metaname == "Date")
923 return format_date (metadata_item);
924 else if (no_ns_metaname == "Language")
925 return iso639(metadata_item);
926 if (meta.metacommand & mCgiSafe)
927 return cgi_safe (metadata_item);
928 else return metadata_item;
929 }
930 }
931
932 case pImmediate:
933 if (parent != NULL) {
934 if (no_ns_metaname == "Date")
935 return format_date (parent->values[0]);
936 if (meta.metacommand & mCgiSafe)
937 return cgi_safe (parent->values[0]);
938 else return parent->values[0];
939 }
940 break;
941
942 case pTop:
943 if (parent != NULL) {
944 while (parent->parent != NULL) parent = parent->parent;
945
946 if (no_ns_metaname == "Date")
947 return format_date (parent->values[0]);
948 if (meta.metacommand & mCgiSafe)
949 return cgi_safe (parent->values[0]);
950 else return parent->values[0];
951 }
952 break;
953
954 case pAll:
955 MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
956 if (parent != NULL) {
957 text_tarray tmparray;
958 while (parent != NULL) {
959 tmparray.push_back (parent->values[0]);
960 parent = parent->parent;
961 }
962 bool first = true;
963 text_t tmp;
964 text_tarray::reverse_iterator here = tmparray.rbegin();
965 text_tarray::reverse_iterator end = tmparray.rend();
966 while (here != end) {
967 if (!first) tmp += meta.functionoptions;
968 if (no_ns_metaname == "Date") tmp += format_date (*here);
969 else tmp += *here;
970 first = false;
971 here ++;
972 }
973 if (meta.metacommand & mCgiSafe) return cgi_safe (tmp);
974 else return tmp;
975 }
976 }
977 return "";
978}
979
980static text_t get_or (const text_t& collection, recptproto* collectproto,
981 ResultDocInfo_t &docinfo, displayclass &disp,
982 format_t *orptr, text_tmap &options,
983 ostream& logout) {
984
985 text_t tmp;
986 while (orptr != NULL) {
987
988 tmp = format_string (collection,collectproto, docinfo, disp, orptr,
989 options, logout);
990 if (!tmp.empty()) return tmp;
991
992 orptr = orptr->nextptr;
993 }
994 return "";
995}
996
997static bool char_is_whitespace(const char c)
998{
999 return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
1000
1001}
1002
1003static int scan_over_whitespace(const text_t& outstring, const int start_pos)
1004{
1005 int pos = start_pos;
1006 while (pos<outstring.size()) {
1007 if (!char_is_whitespace(outstring[pos])) {
1008 break;
1009 }
1010 pos++;
1011 }
1012
1013 return pos;
1014}
1015
1016static int rscan_over_whitespace(const text_t& outstring, const int start_pos)
1017{
1018 int pos = start_pos;
1019 while (pos>=0) {
1020 if (!char_is_whitespace(outstring[pos])) {
1021 break;
1022 }
1023 pos--;
1024 }
1025
1026 return pos;
1027}
1028
1029static int rscan_for_whitespace(const text_t& outstring, const int start_pos)
1030{
1031 int pos = start_pos;
1032 while (pos>=0) {
1033 if (char_is_whitespace(outstring[pos])) {
1034 break;
1035 }
1036 pos--;
1037 }
1038
1039 return pos;
1040}
1041
1042
1043static int rscan_for(const text_t& outstring, const int start_pos,
1044 const char find_c)
1045{
1046 int pos = start_pos;
1047 while (pos>=0) {
1048 char c = outstring[pos];
1049 if (outstring[pos] == find_c) {
1050 break;
1051 }
1052 pos--;
1053 }
1054
1055 return pos;
1056}
1057
1058text_t extract_substr(const text_t& outstring, const int start_pos,
1059 const int end_pos)
1060{
1061 text_t extracted_str;
1062 extracted_str.clear();
1063
1064 for (int pos=start_pos; pos<=end_pos; pos++) {
1065 extracted_str.push_back(outstring[pos]);
1066 }
1067
1068 return extracted_str;
1069}
1070
1071
1072static text_t expand_potential_metadata(ResultDocInfo_t &docinfo,
1073 const text_t& intext)
1074{
1075 text_t outtext;
1076
1077 // decide if dealing with metadata or text
1078
1079 text_t::const_iterator beginbracket = intext.begin();
1080 text_t::const_iterator endbracket = (intext.end() - 1);
1081
1082 // Decision is based on a metadata element
1083 if ((*beginbracket == '[') && (*endbracket == ']')) {
1084 // Ignore the surrounding square brackets
1085 text_t meta_text = substr (beginbracket+1, endbracket);
1086
1087 text_tset metadata;
1088 bool getParents =false;
1089 metadata_t meta;
1090
1091 parse_meta (meta_text, meta, metadata, getParents);
1092 outtext = get_meta (docinfo,meta);
1093 }
1094 else {
1095 outtext = intext;
1096 }
1097
1098 return outtext;
1099}
1100
1101
1102
1103
1104static bool uses_expression(ResultDocInfo_t &docinfo,
1105 const text_t& outstring, text_t& lhs_expr,
1106 text_t& op_expr, text_t& rhs_expr)
1107{
1108 // Note: the string may not be of the form: str1 op str2, however
1109 // to deterine this we have to process it on the assumption it is,
1110 // and if at any point an 'erroneous' value is encountered, return
1111 // false and let something else have a go at evaluating it
1112
1113 // Starting at the end of the string and working backwards ..
1114
1115 const int outstring_len = outstring.size();
1116
1117 // skip over white space
1118 int rhs_end = rscan_over_whitespace(outstring,outstring_len-1);
1119
1120 if (rhs_end<=0) {
1121 // no meaningful text or (rhs_end==0) no room for operator
1122 return false;
1123 }
1124
1125 // check for ' or " and then scan over token
1126 const char potential_quote = outstring[rhs_end];
1127 int rhs_start=rhs_end;
1128 bool quoted = false;
1129
1130 if ((potential_quote == '\'') || (potential_quote == '\"')) {
1131 rhs_end--;
1132 rhs_start = rscan_for(outstring,rhs_end-1,potential_quote) +1;
1133 quoted = true;
1134 }
1135 else {
1136 rhs_start = rscan_for_whitespace(outstring,rhs_end-1) +1;
1137 }
1138
1139 if ((rhs_end-rhs_start)<0) {
1140 // no meaningful rhs expression
1141 return false;
1142 }
1143
1144 // form rhs_expr
1145 rhs_expr = extract_substr(outstring,rhs_start,rhs_end);
1146
1147 // skip over white space
1148 const int to_whitespace = (quoted) ? 2 : 1;
1149
1150 int op_end = rscan_over_whitespace(outstring,rhs_start-to_whitespace);
1151 int op_start = rscan_for_whitespace(outstring,op_end-1)+1;
1152
1153
1154 if (op_end-op_start<0) {
1155 // no meaningful expression operator
1156 return false;
1157 }
1158
1159 op_expr = extract_substr(outstring,op_start,op_end);
1160
1161
1162 // check for operator
1163 if ((op_expr != "eq") && (op_expr != "ne")) {
1164 // not a valid operator
1165 return false;
1166 }
1167
1168 int lhs_end = rscan_over_whitespace(outstring,op_start-1);
1169 if (lhs_end<0) {
1170 // no meaningful lhs expression
1171 return false;
1172 }
1173
1174 int lhs_start = scan_over_whitespace(outstring,0);
1175
1176 // form lhs_expr from remainder of string
1177 lhs_expr = extract_substr(outstring,lhs_start,lhs_end);
1178
1179 // Now we know we have a valid expression, look up any
1180 // metadata terms
1181
1182 rhs_expr = expand_potential_metadata(docinfo,rhs_expr);
1183 lhs_expr = expand_potential_metadata(docinfo,lhs_expr);
1184
1185 return true;
1186}
1187
1188static bool eval_expression_true(const text_t& lhs_expr,const text_t& op_expr,
1189 const text_t& rhs_expr, ostream& logout)
1190{
1191 if (op_expr == "eq") {
1192 return (lhs_expr == rhs_expr);
1193 }
1194 else if (op_expr == "ne" ) {
1195 return (lhs_expr != rhs_expr);
1196 }
1197 else {
1198 logout << "Error: '" << op_expr << "' is not a recognised operator." << endl;
1199 }
1200
1201 return false;
1202}
1203
1204
1205static text_t get_if (const text_t& collection, recptproto* collectproto,
1206 ResultDocInfo_t &docinfo, displayclass &disp,
1207 const decision_t &decision,
1208 format_t *ifptr, format_t *elseptr,
1209 text_tmap &options, ostream& logout)
1210{
1211 // If the decision component is a metadata element, then evaluate it
1212 // to see whether we output the "then" or the "else" clause
1213 if (decision.command == dMeta) {
1214 if (get_meta (docinfo, decision.meta) != "") {
1215 if (ifptr != NULL)
1216 return get_formatted_string (collection,collectproto, docinfo, disp, ifptr,
1217 options, logout);
1218 }
1219 else {
1220 if (elseptr != NULL)
1221 return get_formatted_string (collection,collectproto, docinfo, disp, elseptr,
1222 options, logout);
1223 }
1224 }
1225
1226 // If the decision component is text, then evaluate it (it is probably a
1227 // macro like _cgiargmode_) to decide what to output.
1228 else if (decision.command == dText) {
1229
1230 text_t outstring;
1231 disp.expandstring (decision.text, outstring);
1232
1233 // Check for if expression in form: str1 op str2
1234 // (such as [x] eq "y")
1235 text_t lhs_expr, op_expr, rhs_expr;
1236 if (uses_expression(docinfo, outstring,lhs_expr,op_expr,rhs_expr)) {
1237 if (eval_expression_true(lhs_expr,op_expr,rhs_expr,logout)) {
1238 if (ifptr != NULL) {
1239 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1240 options, logout);
1241 }
1242 else {
1243 return "";
1244 }
1245 } else {
1246 if (elseptr != NULL) {
1247 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1248 options, logout);
1249 }
1250 else {
1251 return "";
1252 }
1253 }
1254 }
1255
1256
1257 // This is a tad tricky. When we expand a string like _cgiargmode_, that is
1258 // a cgi argument macro that has not been set, it evaluates to itself.
1259 // Therefore, were have to say that a piece of text evalautes true if
1260 // it is non-empty and if it is a cgi argument evaulating to itself.
1261
1262 if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
1263 if (ifptr != NULL)
1264 return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
1265 options, logout);
1266 } else {
1267 if (elseptr != NULL)
1268 return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
1269 options, logout);
1270 }
1271 }
1272
1273 return "";
1274}
1275
1276bool includes_metadata(const text_t& text)
1277{
1278 text_t::const_iterator here = text.begin();
1279 text_t::const_iterator end = text.end();
1280 while (here != end) {
1281 if (*here == '[') return true;
1282 here ++;
1283 }
1284
1285 return false;
1286}
1287
1288static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
1289 recptproto* collectproto, ResultDocInfo_t &docinfo,
1290 displayclass &disp, text_tmap &options,
1291 ostream &logout) {
1292
1293 if (includes_metadata(metavalue))
1294 {
1295 // text has embedded metadata in it => expand it
1296 FilterRequest_t request;
1297 FilterResponse_t response;
1298
1299 request.getParents = false;
1300
1301 format_t *expanded_formatlistptr = new format_t();
1302 parse_formatstring (metavalue, expanded_formatlistptr,
1303 request.fields, request.getParents);
1304
1305 // retrieve metadata
1306 get_info(docinfo.OID, collection, "", request.fields, request.getParents,
1307 collectproto, response, logout);
1308
1309 if (!response.docInfo.empty())
1310 {
1311 text_t expanded_metavalue
1312 = get_formatted_string(collection, collectproto,
1313 response.docInfo[0], disp, expanded_formatlistptr,
1314 options, logout);
1315
1316 return expanded_metavalue;
1317 }
1318 else
1319 {
1320 return metavalue;
1321 }
1322 }
1323 else
1324 {
1325 return metavalue;
1326 }
1327}
1328
1329text_t format_string (const text_t& collection, recptproto* collectproto,
1330 ResultDocInfo_t &docinfo, displayclass &disp,
1331 format_t *formatlistptr, text_tmap &options,
1332 ostream& logout) {
1333
1334 if (formatlistptr == NULL) return "";
1335
1336 switch (formatlistptr->command) {
1337 case comOID:
1338 return docinfo.OID;
1339 case comRank:
1340 return text_t(docinfo.ranking);
1341 case comText:
1342 return formatlistptr->text;
1343 case comLink:
1344 return options["link"];
1345 case comEndLink:
1346 if (options["link"].empty()) return "";
1347 else return "</a>";
1348 case comHref:
1349 return get_href(options["link"]);
1350 case comIcon:
1351 return options["icon"];
1352 case comNum:
1353 return docinfo.result_num;
1354 case comRel: //if [RelatedDocuments] appears in format string, collect relation data
1355 return get_related_docs(collection, collectproto, docinfo, logout);
1356 case comSummary:
1357 return format_summary(collection, collectproto, docinfo, disp, options, logout);
1358 case comMeta:
1359 {
1360 const text_t& metavalue = get_meta (docinfo, formatlistptr->meta);
1361 return expand_metadata(metavalue, collection, collectproto, docinfo, disp, options, logout);
1362 }
1363 case comDoc:
1364 return options["text"];
1365 case comImage:
1366 return expand_metadata(options["DocImage"], collection, collectproto, docinfo, disp, options, logout);
1367 case comTOC:
1368 return options["DocTOC"];
1369 case comDocumentButtonDetach:
1370 return options["DocumentButtonDetach"];
1371 case comDocumentButtonHighlight:
1372 return options["DocumentButtonHighlight"];
1373 case comDocumentButtonExpandContents:
1374 return options["DocumentButtonExpandContents"];
1375 case comDocumentButtonExpandText:
1376 return options["DocumentButtonExpandText"];
1377 case comHighlight:
1378 if (options["highlight"] == "1") return "<b>";
1379 break;
1380 case comEndHighlight:
1381 if (options["highlight"] == "1") return "</b>";
1382 break;
1383 case comIf:
1384 return get_if (collection, collectproto, docinfo, disp,
1385 formatlistptr->decision, formatlistptr->ifptr,
1386 formatlistptr->elseptr, options, logout);
1387 case comOr:
1388 return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
1389 options, logout);
1390 }
1391 return "";
1392}
1393
1394text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
1395 ResultDocInfo_t &docinfo, displayclass &disp,
1396 format_t *formatlistptr, text_tmap &options,
1397 ostream& logout) {
1398
1399 text_t ft;
1400 while (formatlistptr != NULL)
1401 {
1402 ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
1403 options, logout);
1404 formatlistptr = formatlistptr->nextptr;
1405 }
1406
1407 return ft;
1408}
1409
1410
1411/* FUNCTION NAME: format_summary
1412 * DESC: this is invoked when a [Summary] special metadata is processed.
1413 * RETURNS: a query-biased summary for the document */
1414
1415text_t format_summary (const text_t& collection, recptproto* collectproto,
1416 ResultDocInfo_t &docinfo, displayclass &disp,
1417 text_tmap &options, ostream& logout) {
1418
1419 // GRB: added code here to ensure that the cstr (and other collections)
1420 // uses the document metadata item Summary, rather than compressing
1421 // the text of the document, processed via the methods in
1422 // summarise.cpp
1423 if (docinfo.metadata.count("Summary") > 0 &&
1424 docinfo.metadata["Summary"].values.size() > 0) {
1425 return docinfo.metadata["Summary"].values[0];
1426 }
1427
1428 text_t textToSummarise, query;
1429 if(options["text"].empty()) { // get document text
1430 DocumentRequest_t docrequest;
1431 DocumentResponse_t docresponse;
1432 comerror_t err;
1433 docrequest.OID = docinfo.OID;
1434 collectproto->get_document (collection, docrequest, docresponse, err, logout);
1435 textToSummarise = docresponse.doc;
1436 } else // in practice, this would not happen, because text is only
1437 // loaded with the [Text] command
1438 textToSummarise = options["text"];
1439 disp.expandstring("_cgiargq_",query);
1440 return summarise(textToSummarise,query,80);
1441}
Note: See TracBrowser for help on using the repository browser.