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