[347] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * formattools.cpp --
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
[533] | 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.
|
---|
[347] | 9 | *
|
---|
[533] | 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 | *
|
---|
[347] | 24 | *********************************************************************/
|
---|
| 25 |
|
---|
| 26 | #include "formattools.h"
|
---|
[462] | 27 | #include "cgiutils.h"
|
---|
[22650] | 28 | #include "gsdltools.h"
|
---|
[15418] | 29 | #include "recptprototools.h"
|
---|
[1443] | 30 | #include "OIDtools.h"
|
---|
[2967] | 31 | #include "summarise.h"
|
---|
[1443] | 32 |
|
---|
[1257] | 33 | #include <assert.h>
|
---|
[347] | 34 |
|
---|
[23515] | 35 | static bool metadata_wrap = false;
|
---|
| 36 | static text_t metadata_wrap_type = "";
|
---|
[19298] | 37 |
|
---|
[354] | 38 | // a few function prototypes
|
---|
[5788] | 39 |
|
---|
[1443] | 40 | static text_t format_string (const text_t& collection, recptproto* collectproto,
|
---|
[1610] | 41 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
[5788] | 42 | format_t *formatlistptr, text_tmap &options,
|
---|
| 43 | ostream& logout);
|
---|
[354] | 44 |
|
---|
| 45 | static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
|
---|
[649] | 46 | format_t *formatlistptr, text_tset &metadata, bool &getParents);
|
---|
[354] | 47 |
|
---|
[5788] | 48 | static text_t format_summary (const text_t& collection, recptproto* collectproto,
|
---|
| 49 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
| 50 | text_tmap &options, ostream& logout);
|
---|
[9852] | 51 | static text_t format_text (const text_t& collection, recptproto* collectproto,
|
---|
| 52 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
| 53 | text_tmap &options, ostream& logout);
|
---|
[2967] | 54 |
|
---|
[9401] | 55 | static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
|
---|
| 56 | recptproto* collectproto, ResultDocInfo_t &docinfo,
|
---|
| 57 | displayclass &disp, text_tmap &options,
|
---|
| 58 | ostream &logout);
|
---|
[2967] | 59 |
|
---|
[9401] | 60 |
|
---|
[347] | 61 | void metadata_t::clear() {
|
---|
[410] | 62 | metaname.clear();
|
---|
[462] | 63 | metacommand = mNone;
|
---|
[9401] | 64 | mqualifier.parent = pNone;
|
---|
| 65 | mqualifier.sibling = sNone;
|
---|
| 66 | mqualifier.child = cNone;
|
---|
[19046] | 67 | pre_tree_traverse.clear();
|
---|
[10415] | 68 | parentoptions.clear();
|
---|
| 69 | siblingoptions.clear();
|
---|
| 70 | childoptions.clear();
|
---|
[749] | 71 | }
|
---|
[347] | 72 |
|
---|
| 73 | void decision_t::clear() {
|
---|
| 74 | command = dMeta;
|
---|
| 75 | meta.clear();
|
---|
[1610] | 76 | text.clear();
|
---|
[749] | 77 | }
|
---|
[347] | 78 |
|
---|
[21808] | 79 | format_t::~format_t()
|
---|
| 80 | {
|
---|
| 81 | if (nextptr != NULL) delete nextptr;
|
---|
| 82 | if (ifptr != NULL) delete ifptr;
|
---|
| 83 | if (elseptr != NULL) delete elseptr;
|
---|
| 84 | if (orptr != NULL) delete orptr;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[347] | 87 | void format_t::clear() {
|
---|
| 88 | command = comText;
|
---|
| 89 | decision.clear();
|
---|
| 90 | text.clear();
|
---|
| 91 | meta.clear();
|
---|
| 92 | nextptr = NULL;
|
---|
| 93 | ifptr = NULL;
|
---|
| 94 | elseptr = NULL;
|
---|
| 95 | orptr = NULL;
|
---|
[749] | 96 | }
|
---|
[347] | 97 |
|
---|
[442] | 98 | void formatinfo_t::clear() {
|
---|
[1079] | 99 | DocumentImages = false;
|
---|
| 100 | DocumentTitles = true;
|
---|
| 101 | DocumentHeading = "{Or}{[parent(Top):Title],[Title],untitled}<br>";
|
---|
| 102 | DocumentContents = true;
|
---|
[1941] | 103 | DocumentArrowsBottom = true;
|
---|
[5788] | 104 | DocumentArrowsTop = false;
|
---|
[13365] | 105 | DocumentSearchResultLinks = false;
|
---|
[442] | 106 | DocumentButtons.erase (DocumentButtons.begin(), DocumentButtons.end());
|
---|
[1496] | 107 | // DocumentButtons.push_back ("Expand Text");
|
---|
| 108 | // DocumentButtons.push_back ("Expand Contents");
|
---|
[442] | 109 | DocumentButtons.push_back ("Detach");
|
---|
| 110 | DocumentButtons.push_back ("Highlight");
|
---|
[1941] | 111 | RelatedDocuments = "";
|
---|
[11146] | 112 | DocumentText = "[Text]";
|
---|
[649] | 113 | formatstrings.erase (formatstrings.begin(), formatstrings.end());
|
---|
[868] | 114 | DocumentUseHTML = false;
|
---|
[5788] | 115 | AllowExtendedOptions = false;
|
---|
[442] | 116 | }
|
---|
| 117 |
|
---|
[749] | 118 | // simply checks to see if formatstring begins with a <td> tag
|
---|
| 119 | bool is_table_content (const text_t &formatstring) {
|
---|
| 120 | text_t::const_iterator here = formatstring.begin();
|
---|
| 121 | text_t::const_iterator end = formatstring.end();
|
---|
| 122 |
|
---|
| 123 | while (here != end) {
|
---|
| 124 | if (*here != ' ') {
|
---|
[1257] | 125 | if ((*here == '<') && ((here+3) < end)) {
|
---|
[749] | 126 | if ((*(here+1) == 't' || *(here+1) == 'T') &&
|
---|
| 127 | (*(here+2) == 'd' || *(here+2) == 'D') &&
|
---|
| 128 | (*(here+3) == '>' || *(here+3) == ' '))
|
---|
| 129 | return true;
|
---|
| 130 | } else return false;
|
---|
| 131 | }
|
---|
[9620] | 132 | ++here;
|
---|
[749] | 133 | }
|
---|
| 134 | return false;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | bool is_table_content (const format_t *formatlistptr) {
|
---|
| 138 |
|
---|
| 139 | if (formatlistptr == NULL) return false;
|
---|
| 140 |
|
---|
| 141 | if (formatlistptr->command == comText)
|
---|
| 142 | return is_table_content (formatlistptr->text);
|
---|
| 143 |
|
---|
| 144 | return false;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[649] | 147 | // returns false if key isn't in formatstringmap
|
---|
| 148 | bool get_formatstring (const text_t &key, const text_tmap &formatstringmap,
|
---|
| 149 | text_t &formatstring) {
|
---|
[442] | 150 |
|
---|
[649] | 151 | formatstring.clear();
|
---|
| 152 | text_tmap::const_iterator it = formatstringmap.find(key);
|
---|
| 153 | if (it == formatstringmap.end()) return false;
|
---|
| 154 | formatstring = (*it).second;
|
---|
| 155 | return true;
|
---|
| 156 | }
|
---|
[749] | 157 |
|
---|
[649] | 158 | // tries to find "key1key2" then "key1" then "key2"
|
---|
| 159 | bool get_formatstring (const text_t &key1, const text_t &key2,
|
---|
| 160 | const text_tmap &formatstringmap,
|
---|
| 161 | text_t &formatstring) {
|
---|
| 162 |
|
---|
| 163 | formatstring.clear();
|
---|
| 164 | text_tmap::const_iterator it = formatstringmap.find(key1 + key2);
|
---|
| 165 | if (it != formatstringmap.end()) {
|
---|
| 166 | formatstring = (*it).second;
|
---|
| 167 | return true;
|
---|
| 168 | }
|
---|
| 169 | it = formatstringmap.find(key1);
|
---|
| 170 | if (it != formatstringmap.end()) {
|
---|
| 171 | formatstring = (*it).second;
|
---|
| 172 | return true;
|
---|
| 173 | }
|
---|
| 174 | it = formatstringmap.find(key2);
|
---|
| 175 | if (it != formatstringmap.end()) {
|
---|
| 176 | formatstring = (*it).second;
|
---|
| 177 | return true;
|
---|
| 178 | }
|
---|
| 179 | return false;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
[6645] | 183 | text_t remove_namespace(const text_t &meta_name) {
|
---|
| 184 | text_t::const_iterator end = meta_name.end();
|
---|
| 185 | text_t::const_iterator it = findchar(meta_name.begin(), end, '.');
|
---|
| 186 | if (it != end) {
|
---|
| 187 | return substr(it+1, end);
|
---|
| 188 | }
|
---|
[12567] | 189 |
|
---|
[6645] | 190 | return meta_name;
|
---|
| 191 |
|
---|
| 192 | }
|
---|
[12567] | 193 | // returns a date of form _format:date_(year, month, day)
|
---|
| 194 | // input is date of type yyyy-?mm-?dd
|
---|
[410] | 195 | // at least the year must be present in date
|
---|
[422] | 196 | text_t format_date (const text_t &date) {
|
---|
[347] | 197 |
|
---|
[410] | 198 | if (date.size() < 4) return "";
|
---|
[347] | 199 |
|
---|
[410] | 200 | text_t::const_iterator datebegin = date.begin();
|
---|
[354] | 201 |
|
---|
[410] | 202 | text_t year = substr (datebegin, datebegin+4);
|
---|
[12567] | 203 | int chars_seen_so_far = 4;
|
---|
[20756] | 204 | if (chars_seen_so_far == date.size()) return "_format:date_("+year+")";
|
---|
[410] | 205 |
|
---|
[12567] | 206 | if (date[chars_seen_so_far] == '-') ++chars_seen_so_far ;
|
---|
| 207 | if (date.size() < chars_seen_so_far+2) return "_format:date_("+year+")";
|
---|
| 208 |
|
---|
| 209 | text_t month = substr (datebegin+chars_seen_so_far, datebegin+chars_seen_so_far+2);
|
---|
[410] | 210 | int imonth = month.getint();
|
---|
[12567] | 211 | if (imonth <= 0 || imonth > 12) return "_format:date_("+year+")";
|
---|
| 212 |
|
---|
| 213 | chars_seen_so_far += 2;
|
---|
[20756] | 214 | if (chars_seen_so_far == date.size()) return "_format:date_("+year+","+month+")";
|
---|
| 215 |
|
---|
[12567] | 216 | if (date[chars_seen_so_far] == '-') ++chars_seen_so_far ;
|
---|
| 217 | if (date.size() < chars_seen_so_far+2) return "_format:date_("+year+","+month+")";
|
---|
[410] | 218 |
|
---|
[12567] | 219 | text_t day = substr (datebegin+chars_seen_so_far, datebegin+chars_seen_so_far+2);
|
---|
[410] | 220 | if (day[0] == '0') day = substr (day.begin()+1, day.end());
|
---|
| 221 | int iday = day.getint();
|
---|
[12567] | 222 | if (iday <= 0 || iday > 31) return "_format:date_("+year+","+month+")";
|
---|
[410] | 223 |
|
---|
[12567] | 224 | return "_format:date_("+year+","+month+","+day+")";
|
---|
[410] | 225 | }
|
---|
| 226 |
|
---|
[2001] | 227 | // converts an iso639 language code to its English equivalent
|
---|
[12567] | 228 | // should we be checking that the macro exists??
|
---|
[2001] | 229 | text_t iso639 (const text_t &langcode) {
|
---|
[12567] | 230 | if (langcode.empty()) return "";
|
---|
| 231 | return "_iso639:iso639"+langcode+"_";
|
---|
[2001] | 232 | }
|
---|
| 233 |
|
---|
[12567] | 234 |
|
---|
[2706] | 235 | text_t get_href (const text_t &link) {
|
---|
| 236 |
|
---|
| 237 | text_t href;
|
---|
| 238 |
|
---|
| 239 | text_t::const_iterator here = findchar(link.begin(), link.end(), '"');
|
---|
| 240 | text_t::const_iterator end = link.end();
|
---|
[13117] | 241 | if (here == end) return g_EmptyText;
|
---|
[2706] | 242 |
|
---|
[9620] | 243 | ++here;
|
---|
[2706] | 244 | while (here != end) {
|
---|
| 245 | if (*here == '"') break;
|
---|
| 246 | href.push_back(*here);
|
---|
[9620] | 247 | ++here;
|
---|
[2706] | 248 | }
|
---|
| 249 |
|
---|
| 250 | return href;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[1941] | 253 | //this function gets the information associated with the relation
|
---|
| 254 | //metadata for the document associated with 'docinfo'. This relation
|
---|
| 255 | //metadata consists of a line of pairs containing 'collection, document OID'
|
---|
| 256 | //(this is the OID of the document related to the current document, and
|
---|
| 257 | //the collection the related document belongs to). For each of these pairs
|
---|
| 258 | //the title metadata is obtained and then an html link between the title
|
---|
| 259 | //of the related doc and the document's position (the document will be
|
---|
| 260 | //found in "<a href=\"_httpdocument_&c=collection&cl=search&d=OID">
|
---|
| 261 | //(where collection is the related documents collection, and OID is the
|
---|
| 262 | //related documents OID). A list of these html links are made for as many
|
---|
| 263 | //related documents as there are. This list is then returned. If there are
|
---|
| 264 | //no related documents available for the current document then the string
|
---|
| 265 | //'.. no related documents .. ' is returned.
|
---|
| 266 | text_t get_related_docs(const text_t& collection, recptproto* collectproto,
|
---|
[9948] | 267 | ResultDocInfo_t &docinfo, ostream& logout){
|
---|
[1941] | 268 |
|
---|
| 269 | text_tset metadata;
|
---|
| 270 |
|
---|
| 271 | //insert the metadata we wish to collect
|
---|
[11324] | 272 | metadata.insert("dc.Relation");
|
---|
[1941] | 273 | metadata.insert("Title");
|
---|
| 274 | metadata.insert("Subject"); //for emails, where title data doesn't apply
|
---|
| 275 |
|
---|
| 276 | FilterResponse_t response;
|
---|
| 277 | text_t relation = ""; //string for displaying relation metadata
|
---|
| 278 | text_t relationTitle = ""; //the related documents Title (or subject)
|
---|
[1963] | 279 | text_t relationOID = ""; //the related documents OID
|
---|
[1941] | 280 |
|
---|
| 281 | //get the information associated with the metadata for current doc
|
---|
[7432] | 282 | if (get_info (docinfo.OID, collection, "", metadata,
|
---|
[1941] | 283 | false, collectproto, response, logout)) {
|
---|
| 284 |
|
---|
| 285 | //if the relation metadata exists, store for displaying
|
---|
[11324] | 286 | if(!response.docInfo[0].metadata["dc.Relation"].values.empty()){
|
---|
| 287 | relationOID += response.docInfo[0].metadata["dc.Relation"].values[0];
|
---|
[1941] | 288 |
|
---|
| 289 | //split relation data into pairs of collectionname,ID number
|
---|
| 290 | text_tarray relationpairs;
|
---|
| 291 | splitchar (relationOID.begin(), relationOID.end(), ' ', relationpairs);
|
---|
| 292 |
|
---|
| 293 | text_tarray::const_iterator currDoc = relationpairs.begin();
|
---|
| 294 | text_tarray::const_iterator lastDoc = relationpairs.end();
|
---|
| 295 |
|
---|
| 296 | //iterate through the pairs to split and display
|
---|
| 297 | while(currDoc != lastDoc){
|
---|
| 298 |
|
---|
| 299 | //split pairs into collectionname and ID
|
---|
| 300 | text_tarray relationdata;
|
---|
| 301 | splitchar ((*currDoc).begin(), (*currDoc).end(), ',', relationdata);
|
---|
| 302 |
|
---|
| 303 | //get first element in the array (collection)
|
---|
| 304 | text_tarray::const_iterator doc_data = relationdata.begin();
|
---|
| 305 | text_t document_collection = *doc_data;
|
---|
[9620] | 306 | ++doc_data; //increment to get next item in array (oid)
|
---|
[1941] | 307 | text_t document_OID = *doc_data;
|
---|
| 308 |
|
---|
| 309 | //create html link to related document
|
---|
[22810] | 310 | relation += "<a href=\"_httpdocument_&c=" + document_collection;
|
---|
| 311 | relation += "&cl=search&d=" + document_OID;
|
---|
[1941] | 312 |
|
---|
| 313 | //get the information associated with the metadata for related doc
|
---|
[7432] | 314 | if (get_info (document_OID, document_collection, "", metadata,
|
---|
[1941] | 315 | false, collectproto, response, logout)) {
|
---|
| 316 |
|
---|
| 317 | //if title metadata doesn't exist, collect subject metadata
|
---|
| 318 | //if that doesn't exist, just call it 'related document'
|
---|
| 319 | if (!response.docInfo[0].metadata["Title"].values[0].empty())
|
---|
| 320 | relationTitle = response.docInfo[0].metadata["Title"].values[0];
|
---|
| 321 | else if (!response.docInfo[0].metadata["Subject"].values.empty())
|
---|
| 322 | relationTitle = response.docInfo[0].metadata["Subject"].values[0];
|
---|
| 323 | else relationTitle = "RELATED DOCUMENT";
|
---|
| 324 |
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | //link the related document's title to its page
|
---|
| 328 | relation += "\">" + relationTitle + "</a>";
|
---|
| 329 | relation += " (" + document_collection + ")<br>";
|
---|
| 330 |
|
---|
[9620] | 331 | ++currDoc;
|
---|
[1941] | 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | if(relation.empty()) //no relation data for documnet
|
---|
| 338 | relation = ".. no related documents .. ";
|
---|
| 339 |
|
---|
| 340 | return relation;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 |
|
---|
| 344 |
|
---|
[354] | 345 | static void get_parent_options (text_t &instring, metadata_t &metaoption) {
|
---|
| 346 |
|
---|
[1257] | 347 | assert (instring.size() > 7);
|
---|
| 348 | if (instring.size() <= 7) return;
|
---|
| 349 |
|
---|
[354] | 350 | text_t meta, com, op;
|
---|
| 351 | bool inbraces = false;
|
---|
| 352 | bool inquotes = false;
|
---|
| 353 | bool foundcolon = false;
|
---|
| 354 | text_t::const_iterator here = instring.begin()+6;
|
---|
| 355 | text_t::const_iterator end = instring.end();
|
---|
| 356 | while (here != end) {
|
---|
[10415] | 357 | if (foundcolon) meta.push_back (*here);
|
---|
| 358 | else if (*here == '(') inbraces = true;
|
---|
[354] | 359 | else if (*here == ')') inbraces = false;
|
---|
| 360 | else if (*here == '\'' && !inquotes) inquotes = true;
|
---|
| 361 | else if (*here == '\'' && inquotes) inquotes = false;
|
---|
| 362 | else if (*here == ':' && !inbraces) foundcolon = true;
|
---|
| 363 | else if (inquotes) op.push_back (*here);
|
---|
| 364 | else com.push_back (*here);
|
---|
[9620] | 365 | ++here;
|
---|
[354] | 366 | }
|
---|
[9401] | 367 |
|
---|
[354] | 368 | instring = meta;
|
---|
| 369 | if (com.empty())
|
---|
[9401] | 370 | metaoption.mqualifier.parent = pImmediate;
|
---|
[354] | 371 | else if (com == "Top")
|
---|
[9401] | 372 | metaoption.mqualifier.parent = pTop;
|
---|
[649] | 373 | else if (com == "All") {
|
---|
[9401] | 374 | metaoption.mqualifier.parent = pAll;
|
---|
[10415] | 375 | metaoption.parentoptions = op;
|
---|
[354] | 376 | }
|
---|
| 377 | }
|
---|
| 378 |
|
---|
[5787] | 379 |
|
---|
| 380 | static void get_sibling_options (text_t &instring, metadata_t &metaoption) {
|
---|
| 381 |
|
---|
| 382 | assert (instring.size() > 8);
|
---|
| 383 | if (instring.size() <= 8) return;
|
---|
| 384 | text_t meta, com, op;
|
---|
| 385 | bool inbraces = false;
|
---|
| 386 | bool inquotes = false;
|
---|
| 387 | bool foundcolon = false;
|
---|
| 388 | text_t::const_iterator here = instring.begin()+7;
|
---|
| 389 | text_t::const_iterator end = instring.end();
|
---|
| 390 | while (here != end) {
|
---|
[10415] | 391 | if (foundcolon) meta.push_back (*here);
|
---|
| 392 | else if (*here == '(') inbraces = true;
|
---|
[5787] | 393 | else if (*here == ')') inbraces = false;
|
---|
| 394 | else if (*here == '\'' && !inquotes) inquotes = true;
|
---|
| 395 | else if (*here == '\'' && inquotes) inquotes = false;
|
---|
[10415] | 396 | else if (*here == ':' && !inbraces) foundcolon = true;
|
---|
[5787] | 397 | else if (inquotes) op.push_back (*here);
|
---|
| 398 | else com.push_back (*here);
|
---|
[9620] | 399 | ++here;
|
---|
[5787] | 400 | }
|
---|
| 401 |
|
---|
| 402 | instring = meta;
|
---|
[10415] | 403 | metaoption.siblingoptions.clear();
|
---|
[5787] | 404 |
|
---|
| 405 | if (com.empty()) {
|
---|
[9401] | 406 | metaoption.mqualifier.sibling = sAll;
|
---|
[10415] | 407 | metaoption.siblingoptions = " ";
|
---|
[5787] | 408 | }
|
---|
[10415] | 409 | else if (com == "first") {
|
---|
| 410 | metaoption.mqualifier.sibling = sNum;
|
---|
| 411 | metaoption.siblingoptions = "0";
|
---|
| 412 | }
|
---|
| 413 | else if (com == "last") {
|
---|
| 414 | metaoption.mqualifier.sibling = sNum;
|
---|
| 415 | metaoption.siblingoptions = "-2"; // == last
|
---|
| 416 | }
|
---|
| 417 | else if (com.getint()>0) {
|
---|
| 418 | metaoption.mqualifier.sibling = sNum;
|
---|
| 419 | int pos = com.getint()-1;
|
---|
| 420 | metaoption.siblingoptions +=pos;
|
---|
| 421 | }
|
---|
[5787] | 422 | else {
|
---|
[9401] | 423 | metaoption.mqualifier.sibling = sAll;
|
---|
[10415] | 424 | metaoption.siblingoptions = op;
|
---|
[5787] | 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[9401] | 428 | static void get_child_options (text_t &instring, metadata_t &metaoption) {
|
---|
[5788] | 429 |
|
---|
[9401] | 430 | assert (instring.size() > 6);
|
---|
| 431 | if (instring.size() <= 6) return;
|
---|
| 432 | text_t meta, com, op;
|
---|
| 433 | bool inbraces = false;
|
---|
| 434 | bool inquotes = false;
|
---|
| 435 | bool foundcolon = false;
|
---|
| 436 | text_t::const_iterator here = instring.begin()+5;
|
---|
| 437 | text_t::const_iterator end = instring.end();
|
---|
| 438 | while (here != end) {
|
---|
[10415] | 439 | if (foundcolon) meta.push_back (*here);
|
---|
| 440 | else if (*here == '(') inbraces = true;
|
---|
[9401] | 441 | else if (*here == ')') inbraces = false;
|
---|
| 442 | else if (*here == '\'' && !inquotes) inquotes = true;
|
---|
| 443 | else if (*here == '\'' && inquotes) inquotes = false;
|
---|
| 444 | else if (*here == ':' && !inbraces) foundcolon = true;
|
---|
| 445 | else if (inquotes) op.push_back (*here);
|
---|
| 446 | else com.push_back (*here);
|
---|
[9620] | 447 | ++here;
|
---|
[9401] | 448 | }
|
---|
| 449 |
|
---|
| 450 | instring = meta;
|
---|
| 451 | if (com.empty()) {
|
---|
| 452 | metaoption.mqualifier.child = cAll;
|
---|
[10415] | 453 | metaoption.childoptions = " ";
|
---|
[9401] | 454 | }
|
---|
| 455 | else if (com == "first") {
|
---|
| 456 | metaoption.mqualifier.child = cNum;
|
---|
[10415] | 457 | metaoption.childoptions = ".fc";
|
---|
[9401] | 458 | }
|
---|
| 459 | else if (com == "last") {
|
---|
| 460 | metaoption.mqualifier.child = cNum;
|
---|
[10415] | 461 | metaoption.childoptions = ".lc";
|
---|
[9401] | 462 | }
|
---|
| 463 | else if (com.getint()>0) {
|
---|
| 464 | metaoption.mqualifier.child = cNum;
|
---|
[10415] | 465 | metaoption.childoptions = "."+com;
|
---|
[9401] | 466 | }
|
---|
| 467 | else {
|
---|
| 468 | metaoption.mqualifier.child = cAll;
|
---|
[10415] | 469 | metaoption.childoptions = op;
|
---|
[9401] | 470 | }
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 |
|
---|
[21752] | 474 | static void get_truncate_options (text_t &instring, metadata_t &metaoption)
|
---|
| 475 | {
|
---|
| 476 | assert (instring.size() > ((text_t) "truncate").size());
|
---|
| 477 | if (instring.size() <= ((text_t) "truncate").size()) return;
|
---|
| 478 | text_t meta, com;
|
---|
| 479 | bool inbraces = false;
|
---|
| 480 | bool foundcolon = false;
|
---|
| 481 | text_t::const_iterator here = instring.begin() + ((text_t) "truncate").size();
|
---|
| 482 | text_t::const_iterator end = instring.end();
|
---|
| 483 | while (here != end) {
|
---|
| 484 | if (foundcolon) meta.push_back (*here);
|
---|
| 485 | else if (*here == '(') inbraces = true;
|
---|
| 486 | else if (*here == ')') inbraces = false;
|
---|
| 487 | else if (*here == ':' && !inbraces) foundcolon = true;
|
---|
| 488 | else com.push_back (*here);
|
---|
| 489 | ++here;
|
---|
| 490 | }
|
---|
[9401] | 491 |
|
---|
[21752] | 492 | instring = meta;
|
---|
| 493 |
|
---|
| 494 | if (!com.empty())
|
---|
| 495 | {
|
---|
| 496 | metaoption.siblingoptions = com;
|
---|
| 497 | }
|
---|
| 498 | else
|
---|
| 499 | {
|
---|
| 500 | // Default is 100 characters if not specified
|
---|
| 501 | metaoption.siblingoptions = "100";
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 |
|
---|
| 506 |
|
---|
[649] | 507 | static void parse_meta (text_t &meta, metadata_t &metaoption,
|
---|
| 508 | text_tset &metadata, bool &getParents) {
|
---|
[354] | 509 |
|
---|
[21752] | 510 | // Look for the various format statement modifiers
|
---|
| 511 | // This needs to be done in a loop otherwise not all combinations will be accepted, but actually the order
|
---|
| 512 | // is irrelevant because this is not stored in metaoption.metacommand anyway
|
---|
| 513 | bool keep_trying = true;
|
---|
| 514 | while (keep_trying)
|
---|
| 515 | {
|
---|
| 516 | keep_trying = false;
|
---|
| 517 |
|
---|
| 518 | if (meta.size() > ((text_t) "cgisafe:").size() && starts_with(meta, "cgisafe:"))
|
---|
| 519 | {
|
---|
| 520 | metaoption.metacommand |= mCgiSafe;
|
---|
| 521 | meta = substr(meta.begin() + ((text_t) "cgisafe:").size(), meta.end());
|
---|
| 522 | keep_trying = true;
|
---|
| 523 | }
|
---|
| 524 | if (meta.size() > ((text_t) "format:").size() && starts_with(meta, "format:"))
|
---|
| 525 | {
|
---|
| 526 | metaoption.metacommand |= mSpecial;
|
---|
| 527 | meta = substr(meta.begin() + ((text_t) "format:").size(), meta.end());
|
---|
| 528 | keep_trying = true;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | // New "truncate" special formatting option
|
---|
| 532 | if (meta.size() > ((text_t) "truncate").size() && starts_with(meta, "truncate")) // No colons due to truncate(X)
|
---|
| 533 | {
|
---|
| 534 | metaoption.metacommand |= mTruncate;
|
---|
| 535 | get_truncate_options (meta, metaoption);
|
---|
| 536 | keep_trying = true;
|
---|
| 537 | }
|
---|
| 538 | // New "htmlsafe" special formatting option
|
---|
| 539 | if (meta.size() > ((text_t) "htmlsafe:").size() && starts_with(meta, "htmlsafe:"))
|
---|
| 540 | {
|
---|
| 541 | metaoption.metacommand |= mHTMLSafe;
|
---|
| 542 | meta = substr(meta.begin() + ((text_t) "htmlsafe:").size(), meta.end());
|
---|
| 543 | keep_trying = true;
|
---|
| 544 | }
|
---|
| 545 | // New "xmlsafe" special formatting option
|
---|
| 546 | if (meta.size() > ((text_t) "xmlsafe:").size() && starts_with(meta, "xmlsafe:"))
|
---|
| 547 | {
|
---|
| 548 | metaoption.metacommand |= mXMLSafe;
|
---|
| 549 | meta = substr(meta.begin() + ((text_t) "xmlsafe:").size(), meta.end());
|
---|
| 550 | keep_trying = true;
|
---|
| 551 | }
|
---|
[22650] | 552 | // New "dmsafe" special formatting option
|
---|
| 553 | if (meta.size() > ((text_t) "dmsafe:").size() && starts_with(meta, "dmsafe:"))
|
---|
| 554 | {
|
---|
| 555 | metaoption.metacommand |= mDMSafe;
|
---|
| 556 | meta = substr(meta.begin() + ((text_t) "dmsafe:").size(), meta.end());
|
---|
| 557 | keep_trying = true;
|
---|
| 558 | }
|
---|
[649] | 559 | }
|
---|
| 560 |
|
---|
[19046] | 561 | bool had_parent_or_child = true;
|
---|
| 562 | bool prev_was_parent = false;
|
---|
| 563 | bool prev_was_child = false;
|
---|
| 564 |
|
---|
| 565 | while (had_parent_or_child) {
|
---|
| 566 | if (meta.size() > 7
|
---|
| 567 | && (substr (meta.begin(), meta.begin()+6) == "parent")) {
|
---|
| 568 |
|
---|
| 569 | // clear out sibling and child (cmd and options)
|
---|
| 570 | metaoption.metacommand &= ~(mChild|mSibling);
|
---|
| 571 | metaoption.childoptions.clear();
|
---|
| 572 | metaoption.siblingoptions.clear();
|
---|
| 573 |
|
---|
| 574 | getParents = true;
|
---|
| 575 | metaoption.metacommand |= mParent;
|
---|
| 576 | get_parent_options (meta, metaoption);
|
---|
| 577 |
|
---|
| 578 | if (prev_was_parent) {
|
---|
| 579 | metaoption.pre_tree_traverse += ".pr";
|
---|
| 580 | }
|
---|
| 581 | else if (prev_was_child) {
|
---|
| 582 | metaoption.pre_tree_traverse += ".fc";
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | prev_was_parent = true;
|
---|
| 586 | prev_was_child = false;
|
---|
| 587 | }
|
---|
| 588 | else if (meta.size() > 6
|
---|
| 589 | && (substr (meta.begin(), meta.begin()+5) == "child")) {
|
---|
| 590 |
|
---|
| 591 | // clear out sibling and parent (cmd and options)
|
---|
| 592 | metaoption.metacommand &= ~(mParent|mSibling);
|
---|
| 593 | metaoption.parentoptions.clear();
|
---|
| 594 | metaoption.siblingoptions.clear();
|
---|
| 595 |
|
---|
| 596 | metaoption.metacommand |= mChild;
|
---|
| 597 | get_child_options (meta, metaoption);
|
---|
| 598 | metadata.insert("contains");
|
---|
| 599 |
|
---|
| 600 | if (prev_was_parent) {
|
---|
| 601 | metaoption.pre_tree_traverse += ".pr";
|
---|
| 602 | }
|
---|
| 603 | else if (prev_was_child) {
|
---|
| 604 | metaoption.pre_tree_traverse += ".fc";
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | prev_was_child = true;
|
---|
| 608 | prev_was_parent = false;
|
---|
| 609 | }
|
---|
| 610 | else {
|
---|
| 611 | prev_was_child = false;
|
---|
| 612 | prev_was_parent = false;
|
---|
| 613 | had_parent_or_child = false;
|
---|
| 614 | }
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | // parent/child can have sibling tacked on end also
|
---|
[10415] | 618 | if (meta.size() > 8 && (substr (meta.begin(), meta.begin()+7) == "sibling")) {
|
---|
| 619 | metaoption.metacommand |= mSibling;
|
---|
| 620 | get_sibling_options (meta, metaoption);
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[7599] | 623 | // check for ex. which may occur in format statements
|
---|
| 624 | if (meta.size()>3 && (substr(meta.begin(), meta.begin()+3) == "ex.")) {
|
---|
| 625 | meta = substr (meta.begin()+3, meta.end());
|
---|
| 626 | }
|
---|
[649] | 627 | metadata.insert (meta);
|
---|
| 628 | metaoption.metaname = meta;
|
---|
[354] | 629 | }
|
---|
| 630 |
|
---|
[9948] | 631 | static void parse_coll_meta(text_t &meta, metadata_t &metaoption) {
|
---|
| 632 | if (meta == "collection") {
|
---|
| 633 | // no qualifiers
|
---|
| 634 | metaoption.metaname = g_EmptyText;
|
---|
| 635 | return;
|
---|
| 636 | }
|
---|
| 637 | meta = substr (meta.begin()+11, meta.end());
|
---|
| 638 | metaoption.metaname = meta;
|
---|
| 639 |
|
---|
| 640 | }
|
---|
| 641 |
|
---|
[649] | 642 | static void parse_meta (text_t &meta, format_t *formatlistptr,
|
---|
| 643 | text_tset &metadata, bool &getParents) {
|
---|
[354] | 644 |
|
---|
[22437] | 645 | // check for ex. which may occur in format statements
|
---|
| 646 | if (meta.size()>3 && (substr(meta.begin(), meta.begin()+3) == "ex.")) {
|
---|
| 647 | meta = substr (meta.begin()+3, meta.end());
|
---|
| 648 | }
|
---|
[354] | 649 | if (meta == "link")
|
---|
| 650 | formatlistptr->command = comLink;
|
---|
| 651 | else if (meta == "/link")
|
---|
| 652 | formatlistptr->command = comEndLink;
|
---|
| 653 |
|
---|
[24226] | 654 | // the metaname "srclink_file" is deprecated, use "srclinkFile"
|
---|
[21758] | 655 | else if (meta == "srclink") {
|
---|
| 656 | formatlistptr->command = comAssocLink;
|
---|
[24226] | 657 | formatlistptr->meta.metaname = "srclinkFile";
|
---|
| 658 | metadata.insert("srclinkFile");
|
---|
[21758] | 659 | }
|
---|
| 660 | else if (meta == "srchref") {
|
---|
| 661 | formatlistptr->command = comAssocLink;
|
---|
| 662 | formatlistptr->text = "href";
|
---|
[24226] | 663 | formatlistptr->meta.metaname = "srclinkFile";
|
---|
| 664 | metadata.insert("srclinkFile");
|
---|
[21758] | 665 | }
|
---|
| 666 | else if (meta == "/srclink") {
|
---|
| 667 | formatlistptr->command = comEndAssocLink;
|
---|
[24226] | 668 | formatlistptr->meta.metaname = "srclinkFile";
|
---|
[21758] | 669 | }
|
---|
| 670 | // and weblink etc
|
---|
[2706] | 671 | else if (meta == "href")
|
---|
| 672 | formatlistptr->command = comHref;
|
---|
| 673 |
|
---|
[354] | 674 | else if (meta == "num")
|
---|
| 675 | formatlistptr->command = comNum;
|
---|
| 676 |
|
---|
[407] | 677 | else if (meta == "icon")
|
---|
| 678 | formatlistptr->command = comIcon;
|
---|
| 679 |
|
---|
[442] | 680 | else if (meta == "Text")
|
---|
| 681 | formatlistptr->command = comDoc;
|
---|
[1941] | 682 |
|
---|
| 683 | else if (meta == "RelatedDocuments")
|
---|
| 684 | formatlistptr->command = comRel;
|
---|
[442] | 685 |
|
---|
[670] | 686 | else if (meta == "highlight")
|
---|
| 687 | formatlistptr->command = comHighlight;
|
---|
| 688 |
|
---|
| 689 | else if (meta == "/highlight")
|
---|
| 690 | formatlistptr->command = comEndHighlight;
|
---|
| 691 |
|
---|
[19302] | 692 | else if (meta == "metadata-spanwrap")
|
---|
| 693 | formatlistptr->command = comMetadataSpanWrap;
|
---|
| 694 |
|
---|
| 695 | else if (meta == "/metadata-spanwrap")
|
---|
| 696 | formatlistptr->command = comEndMetadataSpanWrap;
|
---|
| 697 |
|
---|
[23515] | 698 | else if (meta == "metadata-divwrap")
|
---|
| 699 | formatlistptr->command = comMetadataDivWrap;
|
---|
| 700 |
|
---|
| 701 | else if (meta == "/metadata-divwrap")
|
---|
| 702 | formatlistptr->command = comEndMetadataDivWrap;
|
---|
| 703 |
|
---|
[2967] | 704 | else if (meta == "Summary")
|
---|
| 705 | formatlistptr->command = comSummary;
|
---|
| 706 |
|
---|
[5788] | 707 | else if (meta == "DocImage")
|
---|
| 708 | formatlistptr->command = comImage;
|
---|
| 709 |
|
---|
| 710 | else if (meta == "DocTOC")
|
---|
| 711 | formatlistptr->command = comTOC;
|
---|
| 712 |
|
---|
| 713 | else if (meta == "DocumentButtonDetach")
|
---|
| 714 | formatlistptr->command = comDocumentButtonDetach;
|
---|
| 715 |
|
---|
| 716 | else if (meta == "DocumentButtonHighlight")
|
---|
| 717 | formatlistptr->command = comDocumentButtonHighlight;
|
---|
| 718 |
|
---|
| 719 | else if (meta == "DocumentButtonExpandContents")
|
---|
| 720 | formatlistptr->command = comDocumentButtonExpandContents;
|
---|
| 721 |
|
---|
| 722 | else if (meta == "DocumentButtonExpandText")
|
---|
| 723 | formatlistptr->command = comDocumentButtonExpandText;
|
---|
[6020] | 724 |
|
---|
| 725 | else if (meta == "DocOID")
|
---|
| 726 | formatlistptr->command = comOID;
|
---|
[13118] | 727 | else if (meta == "DocTopOID")
|
---|
| 728 | formatlistptr->command = comTopOID;
|
---|
[6710] | 729 | else if (meta == "DocRank")
|
---|
| 730 | formatlistptr->command = comRank;
|
---|
[16915] | 731 | else if (meta == "DocTermsFreqTotal")
|
---|
| 732 | formatlistptr->command = comDocTermsFreqTotal;
|
---|
[9948] | 733 | else if (meta.size() >= 10 && (substr(meta.begin(), meta.begin()+10) == "collection")) {
|
---|
| 734 | formatlistptr->command = comCollection;
|
---|
| 735 | parse_coll_meta(meta, formatlistptr->meta);
|
---|
| 736 | }
|
---|
[354] | 737 | else {
|
---|
| 738 | formatlistptr->command = comMeta;
|
---|
[649] | 739 | parse_meta (meta, formatlistptr->meta, metadata, getParents);
|
---|
[354] | 740 | }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
[9948] | 743 |
|
---|
[354] | 744 | static bool parse_string (const text_t &formatstring, format_t *formatlistptr,
|
---|
[649] | 745 | text_tset &metadata, bool &getParents) {
|
---|
[354] | 746 |
|
---|
[347] | 747 | text_t text;
|
---|
| 748 | text_t::const_iterator here = formatstring.begin();
|
---|
| 749 | text_t::const_iterator end = formatstring.end();
|
---|
| 750 |
|
---|
| 751 | while (here != end) {
|
---|
| 752 |
|
---|
[1257] | 753 | if (*here == '\\') {
|
---|
[9620] | 754 | ++here;
|
---|
[1257] | 755 | if (here != end) text.push_back (*here);
|
---|
[347] | 756 |
|
---|
[1257] | 757 | } else if (*here == '{') {
|
---|
[347] | 758 | if (!text.empty()) {
|
---|
| 759 | formatlistptr->command = comText;
|
---|
| 760 | formatlistptr->text = text;
|
---|
| 761 | formatlistptr->nextptr = new format_t();
|
---|
| 762 | formatlistptr = formatlistptr->nextptr;
|
---|
| 763 |
|
---|
| 764 | text.clear();
|
---|
| 765 | }
|
---|
[649] | 766 | if (parse_action (++here, end, formatlistptr, metadata, getParents)) {
|
---|
[1443] | 767 |
|
---|
[347] | 768 | formatlistptr->nextptr = new format_t();
|
---|
| 769 | formatlistptr = formatlistptr->nextptr;
|
---|
| 770 | if (here == end) break;
|
---|
| 771 | }
|
---|
| 772 | } else if (*here == '[') {
|
---|
| 773 | if (!text.empty()) {
|
---|
| 774 | formatlistptr->command = comText;
|
---|
| 775 | formatlistptr->text = text;
|
---|
| 776 | formatlistptr->nextptr = new format_t();
|
---|
| 777 | formatlistptr = formatlistptr->nextptr;
|
---|
| 778 |
|
---|
| 779 | text.clear();
|
---|
| 780 | }
|
---|
| 781 | text_t meta;
|
---|
[9620] | 782 | ++here;
|
---|
[347] | 783 | while (*here != ']') {
|
---|
| 784 | if (here == end) return false;
|
---|
| 785 | meta.push_back (*here);
|
---|
[9620] | 786 | ++here;
|
---|
[347] | 787 | }
|
---|
[649] | 788 | parse_meta (meta, formatlistptr, metadata, getParents);
|
---|
| 789 | formatlistptr->nextptr = new format_t();
|
---|
| 790 | formatlistptr = formatlistptr->nextptr;
|
---|
[347] | 791 |
|
---|
| 792 | } else
|
---|
| 793 | text.push_back (*here);
|
---|
| 794 |
|
---|
[9620] | 795 | if (here != end) ++here;
|
---|
[347] | 796 | }
|
---|
| 797 | if (!text.empty()) {
|
---|
| 798 | formatlistptr->command = comText;
|
---|
| 799 | formatlistptr->text = text;
|
---|
| 800 | formatlistptr->nextptr = new format_t();
|
---|
| 801 | formatlistptr = formatlistptr->nextptr;
|
---|
| 802 |
|
---|
| 803 | }
|
---|
| 804 | return true;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 |
|
---|
[354] | 808 | static bool parse_action (text_t::const_iterator &here, const text_t::const_iterator &end,
|
---|
[649] | 809 | format_t *formatlistptr, text_tset &metadata, bool &getParents) {
|
---|
[354] | 810 |
|
---|
[347] | 811 | text_t::const_iterator it = findchar (here, end, '}');
|
---|
| 812 | if (it == end) return false;
|
---|
| 813 |
|
---|
| 814 | text_t com = substr (here, it);
|
---|
| 815 | here = findchar (it, end, '{');
|
---|
| 816 | if (here == end) return false;
|
---|
[9620] | 817 | else ++here;
|
---|
[347] | 818 |
|
---|
[7266] | 819 | if (com == "If" || com == "if" || com == "IF") formatlistptr->command = comIf;
|
---|
| 820 | else if (com == "Or" || com == "or" || com == "OR") formatlistptr->command = comOr;
|
---|
[347] | 821 | else return false;
|
---|
| 822 |
|
---|
| 823 | int commacount = 0;
|
---|
| 824 | text_t text;
|
---|
| 825 | while (here != end) {
|
---|
[636] | 826 |
|
---|
| 827 | if (*here == '\\') {
|
---|
[9745] | 828 | ++here;
|
---|
[636] | 829 | if (here != end) text.push_back(*here);
|
---|
| 830 |
|
---|
[1443] | 831 | }
|
---|
| 832 |
|
---|
| 833 | else if (*here == ',' || *here == '}' || *here == '{') {
|
---|
[347] | 834 |
|
---|
| 835 | if (formatlistptr->command == comOr) {
|
---|
| 836 | // the {Or}{this, or this, or this, or this} statement
|
---|
| 837 | format_t *or_ptr;
|
---|
| 838 |
|
---|
| 839 | // find the next unused orptr
|
---|
| 840 | if (formatlistptr->orptr == NULL) {
|
---|
| 841 | formatlistptr->orptr = new format_t();
|
---|
| 842 | or_ptr = formatlistptr->orptr;
|
---|
| 843 | } else {
|
---|
| 844 | or_ptr = formatlistptr->orptr;
|
---|
| 845 | while (or_ptr->nextptr != NULL)
|
---|
| 846 | or_ptr = or_ptr->nextptr;
|
---|
| 847 | or_ptr->nextptr = new format_t();
|
---|
| 848 | or_ptr = or_ptr->nextptr;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
[1443] | 851 | if (!text.empty())
|
---|
| 852 | {
|
---|
| 853 | if (!parse_string(text, or_ptr, metadata, getParents)) { return false; }
|
---|
| 854 | }
|
---|
[347] | 855 |
|
---|
[1443] | 856 | if (*here == '{')
|
---|
| 857 | {
|
---|
| 858 | // Supports: {Or}{[Booktitle],[Title],{If}{[XXXX],aaa,bbb}}
|
---|
| 859 | // but not : {Or}{[Booktitle],[Title]{If}{[XXXX],aaa,bbb}}
|
---|
| 860 | // The latter can always be re-written:
|
---|
| 861 | // {Or}{[Booktitle],{If}{[Title],[Title]{If}{[XXXX],aaa,bbb}}}
|
---|
| 862 |
|
---|
| 863 | if (!text.empty()) // already used up allocated format_t
|
---|
| 864 | {
|
---|
| 865 | // => allocate new one for detected action
|
---|
| 866 | or_ptr->nextptr = new format_t();
|
---|
| 867 | or_ptr = or_ptr->nextptr;
|
---|
| 868 | }
|
---|
| 869 | if (!parse_action(++here, end, or_ptr, metadata, getParents))
|
---|
| 870 | {
|
---|
| 871 | return false;
|
---|
| 872 | }
|
---|
| 873 | }
|
---|
| 874 | else
|
---|
| 875 | {
|
---|
| 876 | if (*here == '}') break;
|
---|
| 877 | }
|
---|
[347] | 878 | text.clear();
|
---|
| 879 |
|
---|
[1610] | 880 | }
|
---|
| 881 |
|
---|
| 882 | // Parse an {If}{decide,do,else} statement
|
---|
| 883 | else {
|
---|
| 884 |
|
---|
| 885 | // Read the decision component.
|
---|
[347] | 886 | if (commacount == 0) {
|
---|
[1610] | 887 | // Decsion can be a metadata element, or a piece of text.
|
---|
| 888 | // Originally Stefan's code, updated 25/10/2000 by Gordon.
|
---|
[1443] | 889 |
|
---|
[347] | 890 | text_t::const_iterator beginbracket = text.begin();
|
---|
| 891 | text_t::const_iterator endbracket = (text.end() - 1);
|
---|
[1610] | 892 |
|
---|
| 893 | // Decision is based on a metadata element
|
---|
[347] | 894 | if ((*beginbracket == '[') && (*endbracket == ']')) {
|
---|
[1610] | 895 | // Ignore the surrounding square brackets
|
---|
[347] | 896 | text_t meta = substr (beginbracket+1, endbracket);
|
---|
[649] | 897 | parse_meta (meta, formatlistptr->decision.meta, metadata, getParents);
|
---|
[9620] | 898 | ++commacount;
|
---|
[347] | 899 | text.clear();
|
---|
| 900 | }
|
---|
[1610] | 901 |
|
---|
| 902 | // Decision is a piece of text (probably a macro like _cgiargmode_).
|
---|
| 903 | else {
|
---|
[7389] | 904 |
|
---|
| 905 | // hunt for any metadata in string, which might be uses in
|
---|
| 906 | // to test a condition, e.g. [Format] eq 'PDF'
|
---|
| 907 | format_t* dummyformat = new format_t();
|
---|
| 908 | // update which metadata fields needed
|
---|
| 909 | // (not interested in updatng formatlistptr)
|
---|
| 910 | parse_string (text, dummyformat, metadata, getParents);
|
---|
| 911 | delete dummyformat;
|
---|
| 912 |
|
---|
[1610] | 913 | formatlistptr->decision.command = dText;
|
---|
| 914 | formatlistptr->decision.text = text;
|
---|
[9620] | 915 | ++commacount;
|
---|
[1610] | 916 | text.clear();
|
---|
| 917 | }
|
---|
| 918 | }
|
---|
| 919 |
|
---|
| 920 | // Read the "then" and "else" components of the {If} statement.
|
---|
| 921 | else {
|
---|
[1443] | 922 | format_t** nextlistptr = NULL;
|
---|
| 923 | if (commacount == 1) {
|
---|
[1610] | 924 | nextlistptr = &formatlistptr->ifptr;
|
---|
[1443] | 925 | } else if (commacount == 2 ) {
|
---|
| 926 | nextlistptr = &formatlistptr->elseptr;
|
---|
| 927 | } else {
|
---|
| 928 | return false;
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 | if (!text.empty()) {
|
---|
| 932 | if (*nextlistptr == NULL) {
|
---|
| 933 | *nextlistptr = new format_t();
|
---|
| 934 | } else {
|
---|
| 935 |
|
---|
| 936 | // skip to the end of any format_t statements already added
|
---|
| 937 | while ((*nextlistptr)->nextptr != NULL)
|
---|
| 938 | {
|
---|
| 939 | nextlistptr = &(*nextlistptr)->nextptr;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | (*nextlistptr)->nextptr = new format_t();
|
---|
| 943 | nextlistptr = &(*nextlistptr)->nextptr;
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | if (!parse_string (text, *nextlistptr, metadata, getParents))
|
---|
| 947 | {
|
---|
| 948 | return false;
|
---|
| 949 | }
|
---|
| 950 | text.clear();
|
---|
| 951 | }
|
---|
[347] | 952 |
|
---|
[1443] | 953 | if (*here == '{')
|
---|
| 954 | {
|
---|
| 955 | if (*nextlistptr == NULL) {
|
---|
| 956 | *nextlistptr = new format_t();
|
---|
| 957 | } else {
|
---|
[7474] | 958 | // skip to the end of any format_t statements already added
|
---|
| 959 | while ((*nextlistptr)->nextptr != NULL)
|
---|
| 960 | {
|
---|
| 961 | nextlistptr = &(*nextlistptr)->nextptr;
|
---|
| 962 | }
|
---|
| 963 |
|
---|
[1443] | 964 | (*nextlistptr)->nextptr = new format_t();
|
---|
| 965 | nextlistptr = &(*nextlistptr)->nextptr;
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | if (!parse_action(++here, end, *nextlistptr, metadata, getParents))
|
---|
| 969 | {
|
---|
| 970 | return false;
|
---|
| 971 | }
|
---|
| 972 | }
|
---|
| 973 | else
|
---|
| 974 | {
|
---|
| 975 | if (*here == '}') break;
|
---|
[9620] | 976 | ++commacount;
|
---|
[1443] | 977 | }
|
---|
[347] | 978 | }
|
---|
| 979 | }
|
---|
[636] | 980 |
|
---|
| 981 | } else text.push_back(*here);
|
---|
[347] | 982 |
|
---|
[9620] | 983 | if (here != end) ++here;
|
---|
[347] | 984 | }
|
---|
| 985 |
|
---|
| 986 | return true;
|
---|
| 987 | }
|
---|
| 988 |
|
---|
[354] | 989 |
|
---|
[23515] | 990 | static text_t wrap_metatext(const text_t& metatext, const text_t& OID,
|
---|
[19312] | 991 | const text_t metaname, int metapos=-1)
|
---|
| 992 | {
|
---|
| 993 |
|
---|
[23515] | 994 | text_t tag_type = metadata_wrap_type;
|
---|
[19312] | 995 | text_t editable_type = (metaname == "Text") ? "text" : "metadata";
|
---|
| 996 |
|
---|
| 997 | text_t wrapped_metatext = "<" + tag_type + " ";
|
---|
| 998 | wrapped_metatext += "class=\"editable-" + editable_type + "\" ";
|
---|
| 999 |
|
---|
| 1000 | wrapped_metatext += "docoid=\"" + OID + "\" ";
|
---|
| 1001 | wrapped_metatext += "metaname=\"" + metaname + "\"";
|
---|
| 1002 |
|
---|
| 1003 | if (metapos>=0) {
|
---|
| 1004 | text_t metapos_str = metapos;
|
---|
| 1005 | wrapped_metatext += " metapos=\"" + metapos_str + "\"";
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | wrapped_metatext += ">" + metatext + "</" + tag_type + ">";
|
---|
| 1009 |
|
---|
| 1010 | return wrapped_metatext;
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 |
|
---|
| 1014 |
|
---|
[347] | 1015 | bool parse_formatstring (const text_t &formatstring, format_t *formatlistptr,
|
---|
[649] | 1016 | text_tset &metadata, bool &getParents) {
|
---|
[347] | 1017 |
|
---|
| 1018 | formatlistptr->clear();
|
---|
| 1019 | getParents = false;
|
---|
| 1020 |
|
---|
[649] | 1021 | return (parse_string (formatstring, formatlistptr, metadata, getParents));
|
---|
[347] | 1022 | }
|
---|
| 1023 |
|
---|
[10415] | 1024 | // position -1 for all, -2 for the last, 0 for the first, or x for a particular piece
|
---|
| 1025 | // metainfo has all the values for the metadata, meta has the request metadata name and options, position tells which values to get
|
---|
[19312] | 1026 |
|
---|
| 1027 | static text_t get_formatted_meta_text(const text_t& OID, MetadataInfo_t &metainfo, const metadata_t &meta, int position, bool no_cgisafe = false)
|
---|
[9401] | 1028 | {
|
---|
| 1029 | text_t no_ns_metaname = remove_namespace(meta.metaname);
|
---|
[19298] | 1030 | text_t formatted_metatext;
|
---|
[9401] | 1031 | bool first = true;
|
---|
[649] | 1032 |
|
---|
[9401] | 1033 | const int start_i=0;
|
---|
| 1034 | const int end_i = metainfo.values.size()-1;
|
---|
[10415] | 1035 |
|
---|
| 1036 | if (position == -1) { // all
|
---|
| 1037 | for (int i=start_i; i<=end_i; ++i) {
|
---|
[19298] | 1038 | if (!first) formatted_metatext += meta.siblingoptions;
|
---|
| 1039 |
|
---|
| 1040 | text_t fresh_metatext;
|
---|
| 1041 |
|
---|
[12567] | 1042 | if (meta.metacommand & mSpecial) {
|
---|
| 1043 | // special formatting
|
---|
[19298] | 1044 | if (no_ns_metaname == "Date") fresh_metatext = format_date (metainfo.values[i]);
|
---|
| 1045 | else if (no_ns_metaname == "Language") fresh_metatext = iso639(metainfo.values[i]);
|
---|
| 1046 | else fresh_metatext = "_format:"+meta.metaname+"_("+metainfo.values[i]+")";
|
---|
[12567] | 1047 | }
|
---|
[19298] | 1048 | else fresh_metatext = metainfo.values[i];
|
---|
| 1049 |
|
---|
[21752] | 1050 | // New "truncate" special formatting option
|
---|
| 1051 | if (meta.metacommand & mTruncate)
|
---|
| 1052 | {
|
---|
| 1053 | int truncate_length = meta.siblingoptions.getint();
|
---|
| 1054 | text_t truncated_value = fresh_metatext;
|
---|
| 1055 | if (truncated_value.size() > truncate_length)
|
---|
| 1056 | {
|
---|
| 1057 | truncated_value = substr(truncated_value.begin(), truncated_value.begin() + truncate_length) + "... _texttruncated_";
|
---|
| 1058 | }
|
---|
| 1059 | fresh_metatext = truncated_value;
|
---|
| 1060 | }
|
---|
| 1061 | // New "xmlsafe" special formatting option
|
---|
| 1062 | if (meta.metacommand & mXMLSafe)
|
---|
| 1063 | {
|
---|
| 1064 | // Make it XML-safe
|
---|
| 1065 | text_t text_xml_safe = "";
|
---|
| 1066 | text_t::const_iterator text_iterator = fresh_metatext.begin();
|
---|
| 1067 | while (text_iterator != fresh_metatext.end())
|
---|
| 1068 | {
|
---|
| 1069 | if (*text_iterator == '&') text_xml_safe += "&";
|
---|
| 1070 | else if (*text_iterator == '<') text_xml_safe += "<";
|
---|
| 1071 | else if (*text_iterator == '>') text_xml_safe += ">";
|
---|
| 1072 | else text_xml_safe.push_back(*text_iterator);
|
---|
| 1073 | text_iterator++;
|
---|
| 1074 | }
|
---|
| 1075 | fresh_metatext = text_xml_safe;
|
---|
| 1076 | }
|
---|
| 1077 | // New "htmlsafe" special formatting option
|
---|
| 1078 | if (meta.metacommand & mHTMLSafe)
|
---|
| 1079 | {
|
---|
| 1080 | // Make it HTML-safe
|
---|
| 1081 | text_t text_html_safe = "";
|
---|
| 1082 | text_t::const_iterator text_iterator = fresh_metatext.begin();
|
---|
| 1083 | while (text_iterator != fresh_metatext.end())
|
---|
| 1084 | {
|
---|
| 1085 | if (*text_iterator == '&') text_html_safe += "&";
|
---|
| 1086 | else if (*text_iterator == '<') text_html_safe += "<";
|
---|
| 1087 | else if (*text_iterator == '>') text_html_safe += ">";
|
---|
| 1088 | else if (*text_iterator == '"') text_html_safe += """;
|
---|
| 1089 | else text_html_safe.push_back(*text_iterator);
|
---|
| 1090 | text_iterator++;
|
---|
| 1091 | }
|
---|
| 1092 | fresh_metatext = text_html_safe;
|
---|
| 1093 | }
|
---|
[24226] | 1094 | // New "dmsafe" special formatting option (always apply to "srclinkFile" metadata)
|
---|
| 1095 | // (The metaname "srclink_file" is deprecated, use "srclinkFile")
|
---|
| 1096 | if (meta.metacommand & mDMSafe || meta.metaname == "srclinkFile")
|
---|
[22650] | 1097 | {
|
---|
| 1098 | // Make it macro-safe
|
---|
| 1099 | text_t text_dm_safe = dm_safe(fresh_metatext);
|
---|
| 1100 | fresh_metatext = text_dm_safe;
|
---|
| 1101 | }
|
---|
[21752] | 1102 |
|
---|
[23515] | 1103 | if (metadata_wrap) {
|
---|
| 1104 | fresh_metatext = wrap_metatext(fresh_metatext,OID,meta.metaname,i);
|
---|
[19298] | 1105 | }
|
---|
| 1106 | formatted_metatext += fresh_metatext;
|
---|
| 1107 |
|
---|
[10415] | 1108 | first = false;
|
---|
[9401] | 1109 |
|
---|
[10415] | 1110 | }
|
---|
| 1111 | } else {
|
---|
| 1112 | if (position == -2) { // end
|
---|
| 1113 | position = end_i;
|
---|
| 1114 | } else if (position < start_i || position > end_i) {
|
---|
| 1115 | return "";
|
---|
| 1116 | }
|
---|
[19298] | 1117 |
|
---|
| 1118 | text_t fresh_metatext;
|
---|
[12567] | 1119 | if (meta.metacommand & mSpecial) {
|
---|
[19298] | 1120 |
|
---|
[12567] | 1121 | // special formatting
|
---|
[19298] | 1122 | if (no_ns_metaname == "Date") fresh_metatext = format_date (metainfo.values[position]);
|
---|
| 1123 | else if (no_ns_metaname == "Language") fresh_metatext = iso639(metainfo.values[position]);
|
---|
| 1124 | else fresh_metatext = "_format:"+meta.metaname+"_("+metainfo.values[position]+")";
|
---|
[12567] | 1125 | }
|
---|
[19298] | 1126 | else fresh_metatext = metainfo.values[position];
|
---|
| 1127 |
|
---|
[21752] | 1128 | // New "truncate" special formatting option
|
---|
| 1129 | if (meta.metacommand & mTruncate)
|
---|
| 1130 | {
|
---|
| 1131 | int truncate_length = meta.siblingoptions.getint();
|
---|
| 1132 | text_t truncated_value = fresh_metatext;
|
---|
| 1133 | if (truncated_value.size() > truncate_length)
|
---|
| 1134 | {
|
---|
| 1135 | truncated_value = substr(truncated_value.begin(), truncated_value.begin() + truncate_length) + "... _texttruncated_";
|
---|
| 1136 | }
|
---|
| 1137 | fresh_metatext = truncated_value;
|
---|
| 1138 | }
|
---|
| 1139 | // New "xmlsafe" special formatting option
|
---|
| 1140 | if (meta.metacommand & mXMLSafe)
|
---|
| 1141 | {
|
---|
| 1142 | // Make it XML-safe
|
---|
| 1143 | text_t text_xml_safe = "";
|
---|
| 1144 | text_t::const_iterator text_iterator = fresh_metatext.begin();
|
---|
| 1145 | while (text_iterator != fresh_metatext.end())
|
---|
| 1146 | {
|
---|
| 1147 | if (*text_iterator == '&') text_xml_safe += "&";
|
---|
| 1148 | else if (*text_iterator == '<') text_xml_safe += "<";
|
---|
| 1149 | else if (*text_iterator == '>') text_xml_safe += ">";
|
---|
| 1150 | else text_xml_safe.push_back(*text_iterator);
|
---|
| 1151 | text_iterator++;
|
---|
| 1152 | }
|
---|
| 1153 | fresh_metatext = text_xml_safe;
|
---|
| 1154 | }
|
---|
| 1155 | // New "htmlsafe" special formatting option
|
---|
| 1156 | if (meta.metacommand & mHTMLSafe)
|
---|
| 1157 | {
|
---|
| 1158 | // Make it HTML-safe
|
---|
| 1159 | text_t text_html_safe = "";
|
---|
| 1160 | text_t::const_iterator text_iterator = fresh_metatext.begin();
|
---|
| 1161 | while (text_iterator != fresh_metatext.end())
|
---|
| 1162 | {
|
---|
| 1163 | if (*text_iterator == '&') text_html_safe += "&";
|
---|
| 1164 | else if (*text_iterator == '<') text_html_safe += "<";
|
---|
| 1165 | else if (*text_iterator == '>') text_html_safe += ">";
|
---|
| 1166 | else if (*text_iterator == '"') text_html_safe += """;
|
---|
[23305] | 1167 | else if (*text_iterator == '\'') text_html_safe += "'";
|
---|
| 1168 | else if (*text_iterator == ',') text_html_safe += ",";
|
---|
[21752] | 1169 | else text_html_safe.push_back(*text_iterator);
|
---|
| 1170 | text_iterator++;
|
---|
| 1171 | }
|
---|
| 1172 | fresh_metatext = text_html_safe;
|
---|
| 1173 | }
|
---|
[24226] | 1174 | // New "dmsafe" special formatting option (always apply to "srclinkFile" metadata)
|
---|
| 1175 | // (The metaname "srclink_file" is deprecated, use "srclinkFile")
|
---|
| 1176 | if (meta.metacommand & mDMSafe || meta.metaname == "srclinkFile")
|
---|
[22650] | 1177 | {
|
---|
| 1178 | // Make it macro-safe
|
---|
| 1179 | text_t text_dm_safe = dm_safe(fresh_metatext);
|
---|
| 1180 | fresh_metatext = text_dm_safe;
|
---|
| 1181 | }
|
---|
[21752] | 1182 |
|
---|
[23515] | 1183 | if (metadata_wrap) {
|
---|
| 1184 | fresh_metatext = wrap_metatext(fresh_metatext,OID,meta.metaname,position);
|
---|
[19298] | 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | formatted_metatext += fresh_metatext;
|
---|
[9401] | 1188 | }
|
---|
[19298] | 1189 |
|
---|
| 1190 | if (meta.metacommand & mCgiSafe && !no_cgisafe) return cgi_safe_unicode (formatted_metatext);
|
---|
| 1191 | else return formatted_metatext;
|
---|
[9401] | 1192 | }
|
---|
[347] | 1193 |
|
---|
[10415] | 1194 | static text_t get_parent_meta (ResultDocInfo_t &docinfo, const metadata_t &meta, int siblings_values)
|
---|
[9401] | 1195 | {
|
---|
| 1196 |
|
---|
[649] | 1197 | MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
|
---|
[19312] | 1198 |
|
---|
[9401] | 1199 | switch (meta.mqualifier.parent) {
|
---|
[347] | 1200 | case pNone:
|
---|
[9401] | 1201 | return "Nothing!!";
|
---|
| 1202 | break;
|
---|
[5787] | 1203 |
|
---|
[347] | 1204 | case pImmediate:
|
---|
[649] | 1205 | if (parent != NULL) {
|
---|
[19312] | 1206 | text_t parent_oid = get_parent(docinfo.OID);
|
---|
| 1207 | return get_formatted_meta_text(parent_oid,*parent, meta, siblings_values);
|
---|
[410] | 1208 | }
|
---|
[347] | 1209 | break;
|
---|
| 1210 |
|
---|
| 1211 | case pTop:
|
---|
[649] | 1212 | if (parent != NULL) {
|
---|
[19312] | 1213 | text_t parent_oid = get_parent(docinfo.OID);
|
---|
| 1214 |
|
---|
| 1215 | while (parent->parent != NULL) {
|
---|
| 1216 | parent = parent->parent;
|
---|
| 1217 | parent_oid = get_parent(parent_oid);
|
---|
| 1218 | }
|
---|
| 1219 | return get_formatted_meta_text(parent_oid,*parent, meta, siblings_values);
|
---|
[410] | 1220 | }
|
---|
[347] | 1221 | break;
|
---|
| 1222 |
|
---|
| 1223 | case pAll:
|
---|
[649] | 1224 | MetadataInfo_t *parent = docinfo.metadata[meta.metaname].parent;
|
---|
| 1225 | if (parent != NULL) {
|
---|
[19312] | 1226 | text_t parent_oid = get_parent(docinfo.OID);
|
---|
| 1227 |
|
---|
[649] | 1228 | text_tarray tmparray;
|
---|
| 1229 | while (parent != NULL) {
|
---|
[19312] | 1230 | tmparray.push_back (get_formatted_meta_text(parent_oid,*parent, meta, siblings_values, true)); // set no_cgisafe to true, as we'll do it once we have all the metadata
|
---|
[649] | 1231 | parent = parent->parent;
|
---|
[19312] | 1232 | parent_oid = get_parent(parent_oid);
|
---|
| 1233 |
|
---|
[649] | 1234 | }
|
---|
[10415] | 1235 | // now join them up - use teh parent separator
|
---|
[649] | 1236 | bool first = true;
|
---|
| 1237 | text_t tmp;
|
---|
| 1238 | text_tarray::reverse_iterator here = tmparray.rbegin();
|
---|
| 1239 | text_tarray::reverse_iterator end = tmparray.rend();
|
---|
[359] | 1240 | while (here != end) {
|
---|
[10415] | 1241 | if (!first) tmp += meta.parentoptions;
|
---|
| 1242 | tmp += *here;
|
---|
[359] | 1243 | first = false;
|
---|
[9620] | 1244 | ++here;
|
---|
[359] | 1245 | }
|
---|
[13457] | 1246 | if (meta.metacommand & mCgiSafe) return cgi_safe_unicode (tmp);
|
---|
[649] | 1247 | else return tmp;
|
---|
[347] | 1248 | }
|
---|
| 1249 | }
|
---|
| 1250 | return "";
|
---|
[9401] | 1251 |
|
---|
[347] | 1252 | }
|
---|
| 1253 |
|
---|
[9948] | 1254 | static text_t get_child_meta (const text_t& collection,
|
---|
| 1255 | recptproto* collectproto,
|
---|
[9401] | 1256 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
| 1257 | const metadata_t &meta, text_tmap &options,
|
---|
[10415] | 1258 | ostream& logout, int siblings_values)
|
---|
[9401] | 1259 | {
|
---|
[10415] | 1260 | if (docinfo.metadata["contains"].values[0].size()==0) return ""; // no children
|
---|
| 1261 |
|
---|
[19046] | 1262 | const text_t& pre_tree_trav = meta.pre_tree_traverse;
|
---|
[10415] | 1263 | const text_t& child_metaname = meta.metaname;
|
---|
| 1264 | const text_t& child_field = meta.childoptions;
|
---|
| 1265 | text_tset child_metadata;
|
---|
| 1266 | child_metadata.insert(child_metaname);
|
---|
[9401] | 1267 |
|
---|
[10415] | 1268 | FilterResponse_t child_response;
|
---|
| 1269 | if (meta.mqualifier.child == cNum) {
|
---|
| 1270 | // just one child
|
---|
| 1271 | //get the information associated with the metadata for child doc
|
---|
[19046] | 1272 | if (!get_info (docinfo.OID+pre_tree_trav+child_field, collection, "",
|
---|
| 1273 | child_metadata, false, collectproto, child_response,
|
---|
| 1274 | logout)) return ""; // invalid child number
|
---|
[9401] | 1275 |
|
---|
[10415] | 1276 | if (child_response.docInfo.empty()) return false; // no info for the child
|
---|
| 1277 |
|
---|
| 1278 | ResultDocInfo_t& child_docinfo = child_response.docInfo[0];
|
---|
| 1279 | MetadataInfo_t& metaname_rec = child_docinfo.metadata[child_metaname];
|
---|
| 1280 |
|
---|
[19312] | 1281 | text_t child_metavalue
|
---|
| 1282 | = get_formatted_meta_text(child_docinfo.OID,metaname_rec,meta,siblings_values);
|
---|
[10415] | 1283 | return expand_metadata(child_metavalue,collection,collectproto,
|
---|
| 1284 | child_docinfo,disp,options,logout);
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
[9401] | 1287 |
|
---|
[10415] | 1288 | if (meta.mqualifier.child != cAll) return false; // invalid qualifier
|
---|
[19046] | 1289 |
|
---|
| 1290 |
|
---|
| 1291 | if (!pre_tree_trav.empty()) {
|
---|
| 1292 | // need to get relevant "contains" metadata for new (e.g. pre tree trav) node
|
---|
| 1293 | FilterResponse_t trav_response;
|
---|
| 1294 |
|
---|
| 1295 | text_tset trav_metadata;
|
---|
| 1296 | trav_metadata.insert("contains");
|
---|
| 1297 |
|
---|
| 1298 | if (!get_info (docinfo.OID+pre_tree_trav, collection, "",
|
---|
| 1299 | trav_metadata, false, collectproto, trav_response,
|
---|
| 1300 | logout)) return ""; // invalid pre_tree_trav
|
---|
| 1301 |
|
---|
| 1302 | if (trav_response.docInfo.empty()) return false; // no info for the pre_tree_trav OID
|
---|
[10415] | 1303 |
|
---|
[19046] | 1304 | ResultDocInfo_t& trav_docinfo = trav_response.docInfo[0];
|
---|
[22046] | 1305 |
|
---|
[19046] | 1306 | // use this for rest of routine
|
---|
| 1307 | docinfo = trav_docinfo;
|
---|
| 1308 | }
|
---|
| 1309 |
|
---|
[10415] | 1310 | // we need to get all children
|
---|
| 1311 | text_t result = "";
|
---|
| 1312 | text_tarray children;
|
---|
| 1313 | text_t contains = docinfo.metadata["contains"].values[0];
|
---|
| 1314 | splitchar (contains.begin(), contains.end(), ';', children);
|
---|
| 1315 | text_tarray::const_iterator here = children.begin();
|
---|
| 1316 | text_tarray::const_iterator end = children.end();
|
---|
| 1317 | bool first = true;
|
---|
| 1318 | while (here !=end) {
|
---|
| 1319 | text_t oid = *here;
|
---|
| 1320 | here++;
|
---|
| 1321 | if (*(oid.begin()) == '"') translate_parent (oid, docinfo.OID);
|
---|
[22046] | 1322 |
|
---|
[9401] | 1323 | //get the information associated with the metadata for child doc
|
---|
[10415] | 1324 | if (!get_info (oid, collection, "", child_metadata,
|
---|
| 1325 | false, collectproto, child_response, logout) ||
|
---|
| 1326 | child_response.docInfo.empty()) {
|
---|
| 1327 | first = false;
|
---|
| 1328 | continue;
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 |
|
---|
| 1332 | ResultDocInfo_t& child_docinfo = child_response.docInfo[0];
|
---|
| 1333 | MetadataInfo_t& metaname_rec = child_docinfo.metadata[child_metaname];
|
---|
| 1334 |
|
---|
[19312] | 1335 | text_t child_metavalue
|
---|
| 1336 | = get_formatted_meta_text(child_docinfo.OID,metaname_rec,meta,siblings_values);
|
---|
[22046] | 1337 |
|
---|
| 1338 |
|
---|
[10415] | 1339 | if (!first) result += child_field;
|
---|
| 1340 | first = false;
|
---|
| 1341 | // need to do this here cos otherwise we are in the wrong document
|
---|
[22046] | 1342 | text_t em = expand_metadata(child_metavalue,collection,collectproto,
|
---|
| 1343 | child_docinfo,disp,options,logout);
|
---|
| 1344 |
|
---|
| 1345 | result += em;
|
---|
[9401] | 1346 | }
|
---|
[10415] | 1347 | return result;
|
---|
| 1348 |
|
---|
[9401] | 1349 | }
|
---|
| 1350 |
|
---|
| 1351 | static text_t get_meta (const text_t& collection, recptproto* collectproto,
|
---|
| 1352 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
| 1353 | const metadata_t &meta, text_tmap &options,
|
---|
| 1354 | ostream& logout) {
|
---|
| 1355 |
|
---|
| 1356 | // make sure we have the requested metadata
|
---|
| 1357 | MetadataInfo_tmap::iterator it = docinfo.metadata.find (meta.metaname);
|
---|
| 1358 | if (it == docinfo.metadata.end()) return "";
|
---|
| 1359 |
|
---|
[10415] | 1360 | int siblings_values = 0; // default is no siblings, just the first metadata available
|
---|
| 1361 | if (meta.metacommand & mSibling) {
|
---|
| 1362 | if (meta.mqualifier.sibling == sAll) {
|
---|
| 1363 | siblings_values = -1; //all
|
---|
| 1364 | } else if (meta.mqualifier.sibling == sNum) {
|
---|
| 1365 | siblings_values = meta.siblingoptions.getint();
|
---|
| 1366 | }
|
---|
| 1367 | }
|
---|
[9401] | 1368 | if (meta.metacommand & mParent) {
|
---|
[10415] | 1369 | return get_parent_meta(docinfo,meta,siblings_values);
|
---|
[9401] | 1370 | }
|
---|
[10415] | 1371 |
|
---|
[9401] | 1372 | else if (meta.metacommand & mChild) {
|
---|
| 1373 | return get_child_meta(collection,collectproto,docinfo,disp,meta,
|
---|
[10415] | 1374 | options,logout, siblings_values);
|
---|
[9401] | 1375 | }
|
---|
[10415] | 1376 | else if (meta.metacommand & mSibling) { // only siblings
|
---|
[9401] | 1377 | MetadataInfo_t& metaname_rec = docinfo.metadata[meta.metaname];
|
---|
[19312] | 1378 | return get_formatted_meta_text(docinfo.OID,docinfo.metadata[meta.metaname],meta, siblings_values);
|
---|
[9401] | 1379 | }
|
---|
| 1380 | else {
|
---|
| 1381 |
|
---|
| 1382 | // straightforward metadata request (nothing fancy)
|
---|
| 1383 |
|
---|
| 1384 | text_t classifier_metaname = docinfo.classifier_metadata_type;
|
---|
| 1385 | int metaname_index
|
---|
| 1386 | = (classifier_metaname == meta.metaname) ? docinfo.classifier_metadata_offset : 0;
|
---|
[19312] | 1387 | return get_formatted_meta_text(docinfo.OID,docinfo.metadata[meta.metaname], meta, metaname_index);
|
---|
[9401] | 1388 | }
|
---|
[10415] | 1389 |
|
---|
[9401] | 1390 | return "";
|
---|
| 1391 | }
|
---|
| 1392 |
|
---|
[1443] | 1393 | static text_t get_or (const text_t& collection, recptproto* collectproto,
|
---|
[1610] | 1394 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
[5788] | 1395 | format_t *orptr, text_tmap &options,
|
---|
[1443] | 1396 | ostream& logout) {
|
---|
[347] | 1397 |
|
---|
[354] | 1398 | while (orptr != NULL) {
|
---|
[347] | 1399 |
|
---|
[23515] | 1400 | if (metadata_wrap) {
|
---|
[19302] | 1401 | // need to be a bit more careful about this
|
---|
[23515] | 1402 | // => test for it *without* spanwrap or divwrap, and if defined, then
|
---|
| 1403 | // got back and generate it again, this time with spanwrap/divwrap on
|
---|
[354] | 1404 |
|
---|
[23515] | 1405 | metadata_wrap = false;
|
---|
[19302] | 1406 | text_t test_tmp = format_string (collection,collectproto,docinfo, disp, orptr,
|
---|
| 1407 | options, logout);
|
---|
[23515] | 1408 | metadata_wrap = true;
|
---|
[19302] | 1409 | if (!test_tmp.empty()) {
|
---|
| 1410 |
|
---|
| 1411 | return format_string (collection,collectproto,docinfo, disp, orptr,
|
---|
| 1412 | options, logout);
|
---|
| 1413 | }
|
---|
| 1414 | }
|
---|
| 1415 | else {
|
---|
| 1416 | text_t tmp = format_string (collection,collectproto,docinfo, disp, orptr,
|
---|
| 1417 | options, logout);
|
---|
| 1418 | if (!tmp.empty()) return tmp;
|
---|
| 1419 | }
|
---|
| 1420 |
|
---|
[354] | 1421 | orptr = orptr->nextptr;
|
---|
[347] | 1422 | }
|
---|
[354] | 1423 | return "";
|
---|
[347] | 1424 | }
|
---|
| 1425 |
|
---|
[7389] | 1426 | static bool char_is_whitespace(const char c)
|
---|
| 1427 | {
|
---|
| 1428 | return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
|
---|
| 1429 |
|
---|
| 1430 | }
|
---|
| 1431 |
|
---|
| 1432 | static int scan_over_whitespace(const text_t& outstring, const int start_pos)
|
---|
| 1433 | {
|
---|
| 1434 | int pos = start_pos;
|
---|
| 1435 | while (pos<outstring.size()) {
|
---|
| 1436 | if (!char_is_whitespace(outstring[pos])) {
|
---|
| 1437 | break;
|
---|
| 1438 | }
|
---|
[9620] | 1439 | ++pos;
|
---|
[7389] | 1440 | }
|
---|
| 1441 |
|
---|
| 1442 | return pos;
|
---|
| 1443 | }
|
---|
| 1444 |
|
---|
| 1445 | static int rscan_over_whitespace(const text_t& outstring, const int start_pos)
|
---|
| 1446 | {
|
---|
| 1447 | int pos = start_pos;
|
---|
| 1448 | while (pos>=0) {
|
---|
| 1449 | if (!char_is_whitespace(outstring[pos])) {
|
---|
| 1450 | break;
|
---|
| 1451 | }
|
---|
[9620] | 1452 | --pos;
|
---|
[7389] | 1453 | }
|
---|
| 1454 |
|
---|
| 1455 | return pos;
|
---|
| 1456 | }
|
---|
| 1457 |
|
---|
| 1458 | static int rscan_for_whitespace(const text_t& outstring, const int start_pos)
|
---|
| 1459 | {
|
---|
| 1460 | int pos = start_pos;
|
---|
| 1461 | while (pos>=0) {
|
---|
| 1462 | if (char_is_whitespace(outstring[pos])) {
|
---|
| 1463 | break;
|
---|
| 1464 | }
|
---|
[9620] | 1465 | --pos;
|
---|
[7389] | 1466 | }
|
---|
| 1467 |
|
---|
| 1468 | return pos;
|
---|
| 1469 | }
|
---|
| 1470 |
|
---|
| 1471 |
|
---|
| 1472 | static int rscan_for(const text_t& outstring, const int start_pos,
|
---|
| 1473 | const char find_c)
|
---|
| 1474 | {
|
---|
| 1475 | int pos = start_pos;
|
---|
| 1476 | while (pos>=0) {
|
---|
| 1477 | char c = outstring[pos];
|
---|
| 1478 | if (outstring[pos] == find_c) {
|
---|
| 1479 | break;
|
---|
| 1480 | }
|
---|
[9620] | 1481 | --pos;
|
---|
[7389] | 1482 | }
|
---|
| 1483 |
|
---|
| 1484 | return pos;
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
| 1487 | text_t extract_substr(const text_t& outstring, const int start_pos,
|
---|
| 1488 | const int end_pos)
|
---|
| 1489 | {
|
---|
| 1490 | text_t extracted_str;
|
---|
| 1491 | extracted_str.clear();
|
---|
| 1492 |
|
---|
[9620] | 1493 | for (int pos=start_pos; pos<=end_pos; ++pos) {
|
---|
[7389] | 1494 | extracted_str.push_back(outstring[pos]);
|
---|
| 1495 | }
|
---|
| 1496 |
|
---|
| 1497 | return extracted_str;
|
---|
| 1498 | }
|
---|
| 1499 |
|
---|
| 1500 |
|
---|
[9401] | 1501 | static text_t expand_potential_metadata(const text_t& collection,
|
---|
| 1502 | recptproto* collectproto,
|
---|
| 1503 | ResultDocInfo_t &docinfo,
|
---|
| 1504 | displayclass &disp,
|
---|
| 1505 | const text_t& intext,
|
---|
| 1506 | text_tmap &options,
|
---|
| 1507 | ostream& logout)
|
---|
[7389] | 1508 | {
|
---|
| 1509 | text_t outtext;
|
---|
| 1510 |
|
---|
| 1511 | // decide if dealing with metadata or text
|
---|
| 1512 |
|
---|
| 1513 | text_t::const_iterator beginbracket = intext.begin();
|
---|
| 1514 | text_t::const_iterator endbracket = (intext.end() - 1);
|
---|
| 1515 |
|
---|
| 1516 | // Decision is based on a metadata element
|
---|
| 1517 | if ((*beginbracket == '[') && (*endbracket == ']')) {
|
---|
| 1518 | // Ignore the surrounding square brackets
|
---|
| 1519 | text_t meta_text = substr (beginbracket+1, endbracket);
|
---|
| 1520 |
|
---|
[10614] | 1521 | if (meta_text == "Text") {
|
---|
| 1522 | outtext = format_text(collection, collectproto, docinfo, disp, options, logout);
|
---|
[19311] | 1523 | }
|
---|
| 1524 | else {
|
---|
[7389] | 1525 |
|
---|
[10614] | 1526 | text_tset metadata;
|
---|
| 1527 | bool getParents =false;
|
---|
| 1528 | metadata_t meta;
|
---|
| 1529 |
|
---|
| 1530 | parse_meta (meta_text, meta, metadata, getParents);
|
---|
| 1531 | outtext
|
---|
| 1532 | = get_meta (collection,collectproto,docinfo,disp,meta,options,logout);
|
---|
| 1533 | }
|
---|
| 1534 |
|
---|
[7389] | 1535 | }
|
---|
| 1536 | else {
|
---|
| 1537 | outtext = intext;
|
---|
| 1538 | }
|
---|
| 1539 |
|
---|
| 1540 | return outtext;
|
---|
| 1541 | }
|
---|
| 1542 |
|
---|
| 1543 |
|
---|
| 1544 |
|
---|
| 1545 |
|
---|
[9401] | 1546 | static bool uses_expression(const text_t& collection, recptproto* collectproto,
|
---|
| 1547 | ResultDocInfo_t &docinfo,
|
---|
| 1548 | displayclass &disp,
|
---|
[7389] | 1549 | const text_t& outstring, text_t& lhs_expr,
|
---|
[9401] | 1550 | text_t& op_expr, text_t& rhs_expr,
|
---|
| 1551 | text_tmap &options,
|
---|
| 1552 | ostream& logout)
|
---|
[7389] | 1553 | {
|
---|
| 1554 | // Note: the string may not be of the form: str1 op str2, however
|
---|
| 1555 | // to deterine this we have to process it on the assumption it is,
|
---|
| 1556 | // and if at any point an 'erroneous' value is encountered, return
|
---|
| 1557 | // false and let something else have a go at evaluating it
|
---|
| 1558 |
|
---|
| 1559 | // Starting at the end of the string and working backwards ..
|
---|
| 1560 |
|
---|
| 1561 | const int outstring_len = outstring.size();
|
---|
| 1562 |
|
---|
| 1563 | // skip over white space
|
---|
| 1564 | int rhs_end = rscan_over_whitespace(outstring,outstring_len-1);
|
---|
| 1565 |
|
---|
| 1566 | if (rhs_end<=0) {
|
---|
| 1567 | // no meaningful text or (rhs_end==0) no room for operator
|
---|
| 1568 | return false;
|
---|
| 1569 | }
|
---|
| 1570 |
|
---|
| 1571 | // check for ' or " and then scan over token
|
---|
| 1572 | const char potential_quote = outstring[rhs_end];
|
---|
| 1573 | int rhs_start=rhs_end;
|
---|
| 1574 | bool quoted = false;
|
---|
| 1575 |
|
---|
| 1576 | if ((potential_quote == '\'') || (potential_quote == '\"')) {
|
---|
[9620] | 1577 | --rhs_end;
|
---|
[7389] | 1578 | rhs_start = rscan_for(outstring,rhs_end-1,potential_quote) +1;
|
---|
| 1579 | quoted = true;
|
---|
| 1580 | }
|
---|
| 1581 | else {
|
---|
| 1582 | rhs_start = rscan_for_whitespace(outstring,rhs_end-1) +1;
|
---|
| 1583 | }
|
---|
| 1584 |
|
---|
[7617] | 1585 | if ((rhs_end-rhs_start)<0) {
|
---|
[7389] | 1586 | // no meaningful rhs expression
|
---|
| 1587 | return false;
|
---|
| 1588 | }
|
---|
| 1589 |
|
---|
| 1590 | // form rhs_expr
|
---|
| 1591 | rhs_expr = extract_substr(outstring,rhs_start,rhs_end);
|
---|
| 1592 |
|
---|
| 1593 | // skip over white space
|
---|
| 1594 | const int to_whitespace = (quoted) ? 2 : 1;
|
---|
| 1595 |
|
---|
| 1596 | int op_end = rscan_over_whitespace(outstring,rhs_start-to_whitespace);
|
---|
| 1597 | int op_start = rscan_for_whitespace(outstring,op_end-1)+1;
|
---|
| 1598 |
|
---|
[19058] | 1599 | if ((op_end<0) && (op_start<0)) {
|
---|
| 1600 | // no meaningful expression operator
|
---|
| 1601 | return false;
|
---|
| 1602 | }
|
---|
[7389] | 1603 |
|
---|
[7617] | 1604 | if (op_end-op_start<0) {
|
---|
[7389] | 1605 | // no meaningful expression operator
|
---|
| 1606 | return false;
|
---|
| 1607 | }
|
---|
| 1608 |
|
---|
| 1609 | op_expr = extract_substr(outstring,op_start,op_end);
|
---|
| 1610 |
|
---|
| 1611 |
|
---|
| 1612 | // check for operator
|
---|
[10142] | 1613 | if ((op_expr != "eq") && (op_expr != "ne") && (op_expr != "gt") &&
|
---|
| 1614 | (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")) {
|
---|
[10145] | 1615 |
|
---|
[7389] | 1616 | // not a valid operator
|
---|
| 1617 | return false;
|
---|
| 1618 | }
|
---|
| 1619 |
|
---|
| 1620 | int lhs_end = rscan_over_whitespace(outstring,op_start-1);
|
---|
[7617] | 1621 | if (lhs_end<0) {
|
---|
[7389] | 1622 | // no meaningful lhs expression
|
---|
| 1623 | return false;
|
---|
| 1624 | }
|
---|
| 1625 |
|
---|
| 1626 | int lhs_start = scan_over_whitespace(outstring,0);
|
---|
| 1627 |
|
---|
| 1628 | // form lhs_expr from remainder of string
|
---|
| 1629 | lhs_expr = extract_substr(outstring,lhs_start,lhs_end);
|
---|
| 1630 |
|
---|
| 1631 | // Now we know we have a valid expression, look up any
|
---|
| 1632 | // metadata terms
|
---|
| 1633 |
|
---|
[9401] | 1634 | rhs_expr = expand_potential_metadata(collection,collectproto,docinfo,
|
---|
| 1635 | disp,rhs_expr,options,logout);
|
---|
| 1636 | lhs_expr = expand_potential_metadata(collection,collectproto,docinfo,
|
---|
| 1637 | disp,lhs_expr,options,logout);
|
---|
[7389] | 1638 |
|
---|
| 1639 | return true;
|
---|
| 1640 | }
|
---|
| 1641 |
|
---|
| 1642 | static bool eval_expression_true(const text_t& lhs_expr,const text_t& op_expr,
|
---|
| 1643 | const text_t& rhs_expr, ostream& logout)
|
---|
| 1644 | {
|
---|
[10142] | 1645 | if (op_expr == "eq") return (lhs_expr == rhs_expr);
|
---|
| 1646 | else if (op_expr == "ne" ) return (lhs_expr != rhs_expr);
|
---|
| 1647 | else if (op_expr == "gt") return (lhs_expr > rhs_expr);
|
---|
| 1648 | else if (op_expr == "ge") return (lhs_expr >= rhs_expr);
|
---|
| 1649 | else if (op_expr == "lt") return (lhs_expr < rhs_expr);
|
---|
| 1650 | else if (op_expr == "le") return (lhs_expr <= rhs_expr);
|
---|
| 1651 | else if (op_expr == "==") return (lhs_expr.getint() == rhs_expr.getint());
|
---|
| 1652 | else if (op_expr == "!=") return (lhs_expr.getint() != rhs_expr.getint());
|
---|
| 1653 | else if (op_expr == ">") return (lhs_expr.getint() > rhs_expr.getint());
|
---|
| 1654 | else if (op_expr == ">=") return (lhs_expr.getint() >= rhs_expr.getint());
|
---|
| 1655 | else if (op_expr == "<") return (lhs_expr.getint() < rhs_expr.getint());
|
---|
| 1656 | else if (op_expr == "<=") return (lhs_expr.getint() <= rhs_expr.getint());
|
---|
| 1657 | else if (op_expr == "sw") return (starts_with(lhs_expr,rhs_expr));
|
---|
| 1658 | else if (op_expr == "ew") return (ends_with(lhs_expr,rhs_expr));
|
---|
[7389] | 1659 | else {
|
---|
| 1660 | logout << "Error: '" << op_expr << "' is not a recognised operator." << endl;
|
---|
| 1661 | }
|
---|
[10142] | 1662 |
|
---|
[7389] | 1663 | return false;
|
---|
| 1664 | }
|
---|
| 1665 |
|
---|
| 1666 |
|
---|
[1443] | 1667 | static text_t get_if (const text_t& collection, recptproto* collectproto,
|
---|
[1610] | 1668 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
| 1669 | const decision_t &decision,
|
---|
[5788] | 1670 | format_t *ifptr, format_t *elseptr,
|
---|
| 1671 | text_tmap &options, ostream& logout)
|
---|
[1443] | 1672 | {
|
---|
[1610] | 1673 | // If the decision component is a metadata element, then evaluate it
|
---|
| 1674 | // to see whether we output the "then" or the "else" clause
|
---|
[354] | 1675 | if (decision.command == dMeta) {
|
---|
[19298] | 1676 |
|
---|
[23515] | 1677 | bool store_metadata_wrap = metadata_wrap;
|
---|
| 1678 | metadata_wrap = 0;
|
---|
[19298] | 1679 |
|
---|
[23515] | 1680 | // temporarily suspend metadata-XXXwrap (if on) so can test if metadata item really exits or not
|
---|
[19298] | 1681 | bool metadata_exists
|
---|
| 1682 | = (get_meta (collection,collectproto,docinfo,disp,decision.meta,options,
|
---|
| 1683 | logout) != "");
|
---|
| 1684 |
|
---|
[23515] | 1685 | metadata_wrap = store_metadata_wrap;
|
---|
[19298] | 1686 |
|
---|
| 1687 | if (metadata_exists) {
|
---|
[354] | 1688 | if (ifptr != NULL)
|
---|
[9948] | 1689 | return get_formatted_string (collection,collectproto,docinfo, disp, ifptr,
|
---|
[5788] | 1690 | options, logout);
|
---|
[354] | 1691 | }
|
---|
| 1692 | else {
|
---|
| 1693 | if (elseptr != NULL)
|
---|
[9948] | 1694 | return get_formatted_string (collection,collectproto,docinfo, disp, elseptr,
|
---|
[5788] | 1695 | options, logout);
|
---|
[354] | 1696 | }
|
---|
| 1697 | }
|
---|
[1610] | 1698 |
|
---|
| 1699 | // If the decision component is text, then evaluate it (it is probably a
|
---|
| 1700 | // macro like _cgiargmode_) to decide what to output.
|
---|
| 1701 | else if (decision.command == dText) {
|
---|
| 1702 |
|
---|
| 1703 | text_t outstring;
|
---|
| 1704 | disp.expandstring (decision.text, outstring);
|
---|
| 1705 |
|
---|
[7389] | 1706 | // Check for if expression in form: str1 op str2
|
---|
| 1707 | // (such as [x] eq "y")
|
---|
| 1708 | text_t lhs_expr, op_expr, rhs_expr;
|
---|
[9401] | 1709 | if (uses_expression(collection,collectproto,docinfo, disp, outstring,lhs_expr,op_expr,rhs_expr, options,logout)) {
|
---|
[7389] | 1710 | if (eval_expression_true(lhs_expr,op_expr,rhs_expr,logout)) {
|
---|
| 1711 | if (ifptr != NULL) {
|
---|
| 1712 | return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
|
---|
| 1713 | options, logout);
|
---|
| 1714 | }
|
---|
| 1715 | else {
|
---|
| 1716 | return "";
|
---|
| 1717 | }
|
---|
| 1718 | } else {
|
---|
| 1719 | if (elseptr != NULL) {
|
---|
| 1720 | return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
|
---|
| 1721 | options, logout);
|
---|
| 1722 | }
|
---|
| 1723 | else {
|
---|
| 1724 | return "";
|
---|
| 1725 | }
|
---|
| 1726 | }
|
---|
| 1727 | }
|
---|
| 1728 |
|
---|
| 1729 |
|
---|
[1610] | 1730 | // This is a tad tricky. When we expand a string like _cgiargmode_, that is
|
---|
| 1731 | // a cgi argument macro that has not been set, it evaluates to itself.
|
---|
| 1732 | // Therefore, were have to say that a piece of text evalautes true if
|
---|
| 1733 | // it is non-empty and if it is a cgi argument evaulating to itself.
|
---|
[7389] | 1734 |
|
---|
[1610] | 1735 | if ((outstring != "") && !((outstring == decision.text) && (outstring[0] == '_'))) {
|
---|
| 1736 | if (ifptr != NULL)
|
---|
| 1737 | return get_formatted_string (collection, collectproto, docinfo, disp, ifptr,
|
---|
[5788] | 1738 | options, logout);
|
---|
[1610] | 1739 | } else {
|
---|
| 1740 | if (elseptr != NULL)
|
---|
| 1741 | return get_formatted_string (collection, collectproto, docinfo, disp, elseptr,
|
---|
[5788] | 1742 | options, logout);
|
---|
[1610] | 1743 | }
|
---|
| 1744 | }
|
---|
| 1745 |
|
---|
[354] | 1746 | return "";
|
---|
| 1747 | }
|
---|
| 1748 |
|
---|
[1443] | 1749 | bool includes_metadata(const text_t& text)
|
---|
| 1750 | {
|
---|
| 1751 | text_t::const_iterator here = text.begin();
|
---|
| 1752 | text_t::const_iterator end = text.end();
|
---|
[23707] | 1753 |
|
---|
| 1754 | char startbracket = '[';
|
---|
| 1755 | char endbracket = ']';
|
---|
| 1756 |
|
---|
| 1757 | char bracket = startbracket;
|
---|
[1443] | 1758 | while (here != end) {
|
---|
[23707] | 1759 | if (*here == bracket) {
|
---|
| 1760 | if(bracket == startbracket) {
|
---|
| 1761 | // seen a [, next look for a ] to confirm it's metadata
|
---|
| 1762 | bracket = endbracket;
|
---|
| 1763 | } else if(bracket == endbracket) {
|
---|
| 1764 | // found [ ... ] in text, so we think it includes metadata
|
---|
| 1765 | return true;
|
---|
| 1766 | }
|
---|
| 1767 | }
|
---|
[9620] | 1768 | ++here;
|
---|
[1443] | 1769 | }
|
---|
| 1770 |
|
---|
| 1771 | return false;
|
---|
| 1772 | }
|
---|
| 1773 |
|
---|
[5788] | 1774 | static text_t expand_metadata(const text_t &metavalue, const text_t& collection,
|
---|
[9948] | 1775 | recptproto* collectproto,
|
---|
| 1776 | ResultDocInfo_t &docinfo,
|
---|
[5788] | 1777 | displayclass &disp, text_tmap &options,
|
---|
| 1778 | ostream &logout) {
|
---|
| 1779 |
|
---|
[10415] | 1780 | if (includes_metadata(metavalue)) {
|
---|
| 1781 |
|
---|
| 1782 | // text has embedded metadata in it => expand it
|
---|
| 1783 | FilterRequest_t request;
|
---|
| 1784 | FilterResponse_t response;
|
---|
| 1785 |
|
---|
| 1786 | request.getParents = false;
|
---|
| 1787 |
|
---|
| 1788 | format_t *expanded_formatlistptr = new format_t();
|
---|
| 1789 | parse_formatstring (metavalue, expanded_formatlistptr,
|
---|
| 1790 | request.fields, request.getParents);
|
---|
| 1791 |
|
---|
| 1792 | // retrieve metadata
|
---|
| 1793 | get_info(docinfo.OID, collection, "", request.fields, request.getParents,
|
---|
| 1794 | collectproto, response, logout);
|
---|
| 1795 |
|
---|
| 1796 | if (!response.docInfo.empty()) {
|
---|
| 1797 |
|
---|
| 1798 | text_t expanded_metavalue
|
---|
| 1799 | = get_formatted_string(collection, collectproto,
|
---|
| 1800 | response.docInfo[0], disp, expanded_formatlistptr,
|
---|
| 1801 | options, logout);
|
---|
| 1802 |
|
---|
| 1803 | return expanded_metavalue;
|
---|
| 1804 | }
|
---|
| 1805 | else {
|
---|
| 1806 | return metavalue;
|
---|
| 1807 | }
|
---|
| 1808 | }
|
---|
| 1809 | else {
|
---|
| 1810 |
|
---|
| 1811 | return metavalue;
|
---|
| 1812 | }
|
---|
[5788] | 1813 | }
|
---|
[1941] | 1814 |
|
---|
[9948] | 1815 | text_t get_collection_meta(const text_t& collection, recptproto* collectproto,
|
---|
| 1816 | displayclass &disp,
|
---|
| 1817 | text_t meta_name, ostream& logout) {
|
---|
| 1818 |
|
---|
| 1819 | ColInfoResponse_t collectinfo;
|
---|
| 1820 | comerror_t err;
|
---|
| 1821 | collectproto->get_collectinfo (collection, collectinfo,err,logout);
|
---|
| 1822 | text_t meta_value = "";
|
---|
| 1823 | text_t lang;
|
---|
| 1824 | disp.expandstring("_cgiargl_",lang);
|
---|
| 1825 | if (lang.empty()) {
|
---|
| 1826 | lang = "en";
|
---|
| 1827 | }
|
---|
| 1828 |
|
---|
| 1829 | if (err == noError) {
|
---|
| 1830 | meta_value = collectinfo.get_collectionmeta(meta_name, lang);
|
---|
| 1831 | }
|
---|
| 1832 | return meta_value;
|
---|
| 1833 |
|
---|
| 1834 |
|
---|
| 1835 | }
|
---|
[1443] | 1836 | text_t format_string (const text_t& collection, recptproto* collectproto,
|
---|
[1610] | 1837 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
[5788] | 1838 | format_t *formatlistptr, text_tmap &options,
|
---|
[1443] | 1839 | ostream& logout) {
|
---|
[354] | 1840 |
|
---|
[347] | 1841 | if (formatlistptr == NULL) return "";
|
---|
| 1842 |
|
---|
| 1843 | switch (formatlistptr->command) {
|
---|
[6020] | 1844 | case comOID:
|
---|
| 1845 | return docinfo.OID;
|
---|
[13118] | 1846 | case comTopOID:
|
---|
| 1847 | {
|
---|
| 1848 | text_t top_id;
|
---|
| 1849 | get_top(docinfo.OID, top_id);
|
---|
| 1850 | return top_id;
|
---|
| 1851 | }
|
---|
[6710] | 1852 | case comRank:
|
---|
| 1853 | return text_t(docinfo.ranking);
|
---|
[5788] | 1854 | case comText:
|
---|
| 1855 | return formatlistptr->text;
|
---|
| 1856 | case comLink:
|
---|
| 1857 | return options["link"];
|
---|
| 1858 | case comEndLink:
|
---|
[21758] | 1859 | {
|
---|
| 1860 | if (options["link"].empty()) return "";
|
---|
[5788] | 1861 | else return "</a>";
|
---|
[21758] | 1862 | }
|
---|
[5788] | 1863 | case comHref:
|
---|
| 1864 | return get_href(options["link"]);
|
---|
| 1865 | case comIcon:
|
---|
| 1866 | return options["icon"];
|
---|
| 1867 | case comNum:
|
---|
| 1868 | return docinfo.result_num;
|
---|
| 1869 | case comRel: //if [RelatedDocuments] appears in format string, collect relation data
|
---|
| 1870 | return get_related_docs(collection, collectproto, docinfo, logout);
|
---|
[19311] | 1871 |
|
---|
[5788] | 1872 | case comSummary:
|
---|
[19312] | 1873 | return format_summary(collection, collectproto, docinfo, disp, options, logout);
|
---|
[21758] | 1874 | case comAssocLink:
|
---|
| 1875 | {
|
---|
| 1876 | text_t link_filename = get_meta(collection, collectproto, docinfo, disp, formatlistptr->meta, options, logout);
|
---|
| 1877 | if (!link_filename.empty()) {
|
---|
[22670] | 1878 | text_t href= expand_metadata(options["assocfilepath"], collection, collectproto, docinfo, disp, options, logout) + link_filename;
|
---|
[21758] | 1879 | if (formatlistptr->text == "href") {
|
---|
| 1880 | return href;
|
---|
| 1881 | }
|
---|
| 1882 | return "<a href=\""+ href + "\">";
|
---|
| 1883 | }
|
---|
| 1884 | return "";
|
---|
| 1885 | }
|
---|
| 1886 | case comEndAssocLink:
|
---|
| 1887 | {
|
---|
| 1888 | text_t link_filename = get_meta(collection, collectproto, docinfo, disp, formatlistptr->meta, options, logout);
|
---|
| 1889 | if (!link_filename.empty()) {
|
---|
| 1890 | return "</a>";
|
---|
| 1891 | }
|
---|
| 1892 | return "";
|
---|
| 1893 | }
|
---|
[5788] | 1894 | case comMeta:
|
---|
[1443] | 1895 | {
|
---|
[9948] | 1896 | const text_t& metavalue = get_meta (collection,collectproto, docinfo, disp,formatlistptr->meta,options,logout);
|
---|
[5788] | 1897 | return expand_metadata(metavalue, collection, collectproto, docinfo, disp, options, logout);
|
---|
[1443] | 1898 | }
|
---|
[19311] | 1899 |
|
---|
[5788] | 1900 | case comDoc:
|
---|
[19311] | 1901 | return format_text(collection, collectproto, docinfo, disp, options, logout);
|
---|
| 1902 |
|
---|
[5788] | 1903 | case comImage:
|
---|
| 1904 | return expand_metadata(options["DocImage"], collection, collectproto, docinfo, disp, options, logout);
|
---|
| 1905 | case comTOC:
|
---|
| 1906 | return options["DocTOC"];
|
---|
| 1907 | case comDocumentButtonDetach:
|
---|
| 1908 | return options["DocumentButtonDetach"];
|
---|
| 1909 | case comDocumentButtonHighlight:
|
---|
| 1910 | return options["DocumentButtonHighlight"];
|
---|
| 1911 | case comDocumentButtonExpandContents:
|
---|
| 1912 | return options["DocumentButtonExpandContents"];
|
---|
| 1913 | case comDocumentButtonExpandText:
|
---|
| 1914 | return options["DocumentButtonExpandText"];
|
---|
| 1915 | case comHighlight:
|
---|
| 1916 | if (options["highlight"] == "1") return "<b>";
|
---|
| 1917 | break;
|
---|
| 1918 | case comEndHighlight:
|
---|
| 1919 | if (options["highlight"] == "1") return "</b>";
|
---|
| 1920 | break;
|
---|
[19302] | 1921 | case comMetadataSpanWrap:
|
---|
[23515] | 1922 | metadata_wrap=true; metadata_wrap_type="span"; return "";
|
---|
[19302] | 1923 | break;
|
---|
| 1924 | case comEndMetadataSpanWrap:
|
---|
[23515] | 1925 | metadata_wrap=false; metadata_wrap_type=""; return "";
|
---|
[19302] | 1926 | break;
|
---|
[23515] | 1927 | case comMetadataDivWrap:
|
---|
| 1928 | metadata_wrap=true; metadata_wrap_type="div"; return "";
|
---|
| 1929 | break;
|
---|
| 1930 | case comEndMetadataDivWrap:
|
---|
| 1931 | metadata_wrap=false; metadata_wrap_type=""; return "";
|
---|
| 1932 | break;
|
---|
[5788] | 1933 | case comIf:
|
---|
| 1934 | return get_if (collection, collectproto, docinfo, disp,
|
---|
| 1935 | formatlistptr->decision, formatlistptr->ifptr,
|
---|
| 1936 | formatlistptr->elseptr, options, logout);
|
---|
| 1937 | case comOr:
|
---|
| 1938 | return get_or (collection,collectproto, docinfo, disp, formatlistptr->orptr,
|
---|
| 1939 | options, logout);
|
---|
[16915] | 1940 | case comDocTermsFreqTotal:
|
---|
| 1941 | return docinfo.num_terms_matched;
|
---|
[9948] | 1942 | case comCollection:
|
---|
| 1943 | if (formatlistptr->meta.metaname == g_EmptyText) {
|
---|
| 1944 | return collection;
|
---|
| 1945 | }
|
---|
| 1946 | return get_collection_meta(collection, collectproto, disp, formatlistptr->meta.metaname, logout);
|
---|
| 1947 |
|
---|
[347] | 1948 | }
|
---|
| 1949 | return "";
|
---|
| 1950 | }
|
---|
| 1951 |
|
---|
[1443] | 1952 | text_t get_formatted_string (const text_t& collection, recptproto* collectproto,
|
---|
[1610] | 1953 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
[5788] | 1954 | format_t *formatlistptr, text_tmap &options,
|
---|
[1443] | 1955 | ostream& logout) {
|
---|
[407] | 1956 |
|
---|
[5788] | 1957 | text_t ft;
|
---|
| 1958 | while (formatlistptr != NULL)
|
---|
| 1959 | {
|
---|
| 1960 | ft += format_string (collection, collectproto, docinfo, disp, formatlistptr,
|
---|
| 1961 | options, logout);
|
---|
| 1962 | formatlistptr = formatlistptr->nextptr;
|
---|
| 1963 | }
|
---|
| 1964 |
|
---|
| 1965 | return ft;
|
---|
[347] | 1966 | }
|
---|
| 1967 |
|
---|
| 1968 |
|
---|
[22924] | 1969 | // we have only preloaded the text in DocumentAction. But you may want
|
---|
| 1970 | // to get the text in query, so copy what we have done with
|
---|
| 1971 | // format_summary and get the text here. Probably is quite expensive?
|
---|
[9852] | 1972 | text_t format_text (const text_t& collection, recptproto* collectproto,
|
---|
[9948] | 1973 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
[19311] | 1974 | text_tmap &options, ostream& logout)
|
---|
| 1975 | {
|
---|
| 1976 | text_t text;
|
---|
| 1977 |
|
---|
[22924] | 1978 | if (!options["text"].empty()) {
|
---|
[19311] | 1979 | text = options["text"];
|
---|
[9852] | 1980 | }
|
---|
[19311] | 1981 | else {
|
---|
| 1982 | // get document text here
|
---|
| 1983 | DocumentRequest_t docrequest;
|
---|
| 1984 | DocumentResponse_t docresponse;
|
---|
| 1985 | comerror_t err;
|
---|
| 1986 | docrequest.OID = docinfo.OID;
|
---|
| 1987 | collectproto->get_document (collection, docrequest, docresponse, err, logout);
|
---|
| 1988 | text = docresponse.doc;
|
---|
| 1989 | }
|
---|
[9852] | 1990 |
|
---|
[23515] | 1991 | if (metadata_wrap) {
|
---|
| 1992 | text = wrap_metatext(text,docinfo.OID,"Text");
|
---|
[19311] | 1993 | }
|
---|
| 1994 |
|
---|
| 1995 | return text;
|
---|
[9852] | 1996 | }
|
---|
| 1997 |
|
---|
[2967] | 1998 | /* FUNCTION NAME: format_summary
|
---|
| 1999 | * DESC: this is invoked when a [Summary] special metadata is processed.
|
---|
| 2000 | * RETURNS: a query-biased summary for the document */
|
---|
| 2001 |
|
---|
| 2002 | text_t format_summary (const text_t& collection, recptproto* collectproto,
|
---|
[5788] | 2003 | ResultDocInfo_t &docinfo, displayclass &disp,
|
---|
| 2004 | text_tmap &options, ostream& logout) {
|
---|
[3673] | 2005 |
|
---|
| 2006 | // GRB: added code here to ensure that the cstr (and other collections)
|
---|
| 2007 | // uses the document metadata item Summary, rather than compressing
|
---|
| 2008 | // the text of the document, processed via the methods in
|
---|
| 2009 | // summarise.cpp
|
---|
[19312] | 2010 |
|
---|
| 2011 | text_t summary;
|
---|
| 2012 |
|
---|
[3673] | 2013 | if (docinfo.metadata.count("Summary") > 0 &&
|
---|
| 2014 | docinfo.metadata["Summary"].values.size() > 0) {
|
---|
[19312] | 2015 | summary = docinfo.metadata["Summary"].values[0];
|
---|
[3673] | 2016 | }
|
---|
[19312] | 2017 | else {
|
---|
| 2018 |
|
---|
| 2019 | text_t textToSummarise, query;
|
---|
[3673] | 2020 |
|
---|
[19312] | 2021 | if(options["text"].empty()) { // get document text
|
---|
| 2022 | DocumentRequest_t docrequest;
|
---|
| 2023 | DocumentResponse_t docresponse;
|
---|
| 2024 | comerror_t err;
|
---|
| 2025 | docrequest.OID = docinfo.OID;
|
---|
| 2026 | collectproto->get_document (collection, docrequest, docresponse, err, logout);
|
---|
| 2027 | textToSummarise = docresponse.doc;
|
---|
| 2028 | }
|
---|
| 2029 | else {
|
---|
| 2030 | // in practice, this would not happen, because text is only
|
---|
| 2031 | // loaded with the [Text] command
|
---|
[22924] | 2032 | textToSummarise = options["text"];
|
---|
[19312] | 2033 | }
|
---|
| 2034 |
|
---|
| 2035 | disp.expandstring("_cgiargq_",query);
|
---|
| 2036 | summary = summarise(textToSummarise,query,80);
|
---|
[22924] | 2037 | //summary = substr(textToSummarise.begin(),textToSummarise.begin()+80);
|
---|
[19311] | 2038 | }
|
---|
| 2039 |
|
---|
[22924] | 2040 | summary.replace("'","'");
|
---|
| 2041 | summary.replace("\n","
");
|
---|
| 2042 |
|
---|
[23515] | 2043 | if (metadata_wrap) {
|
---|
| 2044 | summary = wrap_metatext(summary,docinfo.OID,"Summary");
|
---|
[19312] | 2045 | }
|
---|
| 2046 |
|
---|
| 2047 | return summary;
|
---|
[2967] | 2048 | }
|
---|