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