[17546] | 1 | #include <fstream>
|
---|
[8182] | 2 | #include "metaformat.h"
|
---|
[17546] | 3 | #include "gsdltools.h"
|
---|
[10738] | 4 | #include "gsdlunicode.h"
|
---|
[15428] | 5 | #include "recptprototools.h"
|
---|
[8182] | 6 |
|
---|
[17546] | 7 |
|
---|
[8182] | 8 | metaformat::metaformat()
|
---|
| 9 | {
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | text_t metaformat::get_mapping(const text_t &collection, const text_t &collectionField)
|
---|
| 13 | {
|
---|
| 14 | if (this->oaiConfigure == NULL) {
|
---|
| 15 | return "";
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[18884] | 18 | return this->oaiConfigure->getMapping(collection, collectionField, this->formatName());
|
---|
[8182] | 19 | }
|
---|
| 20 |
|
---|
[10738] | 21 | void metaformat::output_item(ostream &output, outconvertclass &outconvert,
|
---|
| 22 | bool &headerDone, const text_t &label,
|
---|
[8182] | 23 | const text_tarray &values)
|
---|
| 24 | {
|
---|
[10738] | 25 |
|
---|
[8182] | 26 | if (!headerDone && (values.size() > 0)) {
|
---|
| 27 | this->output_metadata_header(output);
|
---|
| 28 | headerDone = true;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[9608] | 31 | for (int item = 0; item < values.size(); ++item) {
|
---|
[8219] | 32 | if (this->oaiConfigure->getOAIVersion() >= 200) { // TODO: GRB: This code may need to be subclassed by dc for 200 and later...
|
---|
[17546] | 33 | output << outconvert << " <" << this->formatPrefix() << ":" << label << ">" << xml_safe(values[item]) << "</" << this->formatPrefix() << ":" << label << ">\n";
|
---|
[8182] | 34 | }
|
---|
| 35 | else {
|
---|
[17546] | 36 | output << outconvert << " <" << label << ">" << xml_safe(values[item]) << "</" << label << ">\n";
|
---|
[8182] | 37 | }
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | bool metaformat::scan_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo,
|
---|
| 42 | bool doOutput)
|
---|
| 43 | {
|
---|
| 44 | bool headerDone = false;
|
---|
| 45 | MetadataInfo_tmap::iterator here = docInfo.metadata.begin();
|
---|
| 46 | MetadataInfo_tmap::iterator end = docInfo.metadata.end();
|
---|
| 47 |
|
---|
[10738] | 48 | utf8outconvertclass utf8convert; // we want to output metadata in utf8
|
---|
| 49 |
|
---|
[8182] | 50 | // metaItem is used initially to identify the rfc1807 (etc) metadata items. It is
|
---|
| 51 | // then used to hold the name of the metadata item, such as "title" or "subject".
|
---|
| 52 | text_t metaItem;
|
---|
| 53 | text_t::const_iterator start, last; // Use two iterators to go through metaItem
|
---|
[18581] | 54 |
|
---|
[8182] | 55 | while (here != end) {
|
---|
| 56 | start = last = here->first.begin();
|
---|
[8219] | 57 |
|
---|
| 58 | if (here->first.size() < this->formatPrefix().size() ||
|
---|
| 59 | here->first[this->formatPrefix().size()] != '.') {
|
---|
| 60 | metaItem == "";
|
---|
| 61 | }
|
---|
| 62 | else {
|
---|
[18581] | 63 | last += this->formatPrefix().size(); // Move last so that it is at the
|
---|
| 64 | // '.'
|
---|
[8219] | 65 | metaItem = substr(start, last); // Gets the substring starting at start and going up to (but
|
---|
[18581] | 66 | // not including) last. This should be "dc" (for example)
|
---|
[8219] | 67 | }
|
---|
| 68 |
|
---|
[8182] | 69 | if (metaItem == this->formatPrefix()) {
|
---|
[18581] | 70 | metaItem = substr(last+1, here->first.end()); // Get the rest of the metadata tag (it's name) but without the '.'
|
---|
[18896] | 71 | // remove xxx^ eg Coverage^Spatial becomes spatial
|
---|
| 72 | // this is for qualified dublin core. May affect other sets later if they
|
---|
| 73 | // validly have ^ in them.
|
---|
| 74 | text_t::iterator hat = findchar(metaItem.begin(), metaItem.end(), '^');
|
---|
| 75 | if (hat != metaItem.end()) {
|
---|
| 76 | metaItem = substr(hat+1, metaItem.end());
|
---|
| 77 | }
|
---|
[18895] | 78 | lc(metaItem.begin(),metaItem.begin()+1); // We want lowercase, but some of the fields in qualified dublin core have internal upper case, eg instructionalMethod. So we assume that lowercasing the first letter is enough
|
---|
[8182] | 79 | if (doOutput) {
|
---|
[18895] | 80 | if (this->is_valid_element(metaItem)) {
|
---|
| 81 |
|
---|
| 82 | this->output_item(output, utf8convert, headerDone, metaItem, here->second.values);
|
---|
| 83 | }
|
---|
[8182] | 84 | }
|
---|
| 85 | else {
|
---|
| 86 | if (here->second.values.size() > 0) {
|
---|
| 87 | return true;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | else {
|
---|
| 92 | text_t mapTo = this->get_mapping(collection, here->first);
|
---|
| 93 | if (mapTo != "") {
|
---|
| 94 | // Do we actually want to do anything here? Doesn't getting here imply that this
|
---|
| 95 | // particular metadata is stuff we don't want?
|
---|
| 96 | if (doOutput) {
|
---|
[18895] | 97 | if (this->is_valid_element(mapTo)) {
|
---|
| 98 | this->output_item(output, utf8convert, headerDone, mapTo, here->second.values);
|
---|
| 99 | }
|
---|
[8182] | 100 | }
|
---|
| 101 | else {
|
---|
| 102 | if (here->second.values.size() > 0) {
|
---|
| 103 | return true;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[9608] | 109 | ++here;
|
---|
[8182] | 110 | }
|
---|
| 111 |
|
---|
| 112 | if (!doOutput) {
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if (headerDone) {
|
---|
| 117 | this->output_metadata_footer(output);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return headerDone;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | bool metaformat::is_available(const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
| 125 | {
|
---|
| 126 | ofstream o("dummy", ios::out);
|
---|
| 127 | return this->scan_metadata(o, collection, docInfo, false);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[18895] | 130 | bool metaformat::is_valid_element(text_t &meta_name)
|
---|
| 131 | {
|
---|
| 132 | if (elementSet.count(meta_name)==1) return true;
|
---|
| 133 | return false;
|
---|
| 134 |
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[8182] | 137 | bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
| 138 | {
|
---|
| 139 | return this->scan_metadata(output, collection, docInfo, true);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | bool metaformat::output_record(ostream &output, recptproto *protocol, const text_t &collection,
|
---|
| 143 | const text_t &OID)
|
---|
| 144 | {
|
---|
| 145 | FilterResponse_t response;
|
---|
| 146 | text_tset metadata;
|
---|
[8303] | 147 | ofstream logout("oai.log", ios::app);
|
---|
[8182] | 148 |
|
---|
| 149 | // get the document information
|
---|
| 150 | if (!get_info(OID, collection, "", metadata, false, protocol, response, logout)) {
|
---|
| 151 | // TODO: error, bad request
|
---|
| 152 | // cerr << "Bad identifier or protocol " << OID << endl;
|
---|
| 153 | return false;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // check to see if it's a classifier
|
---|
| 157 | text_t childHead;
|
---|
| 158 | // int oaiVersion = this->oaiConfigure->getOAIVersion();
|
---|
| 159 | text_t::const_iterator start = OID.begin();
|
---|
| 160 | text_t::const_iterator here = OID.begin();
|
---|
[11311] | 161 | here += 2;
|
---|
[8182] | 162 | childHead = substr(start, here);
|
---|
| 163 |
|
---|
| 164 | // if it isn't a document, kill it now
|
---|
[11311] | 165 | if (childHead == "CL") {
|
---|
[8182] | 166 | // cerr << "Not a document" << endl;
|
---|
| 167 | return false;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | // output record header
|
---|
| 171 | output << "<record>\n";
|
---|
| 172 |
|
---|
| 173 | // output header part of oai response
|
---|
| 174 | output << "<header>" << endl;
|
---|
| 175 | output << " <identifier>" << OID << "</identifier>" << endl;
|
---|
| 176 | // TODO: add modified date
|
---|
| 177 |
|
---|
| 178 | output << "</header>" << endl;
|
---|
| 179 |
|
---|
| 180 | // output metadata part of oai response
|
---|
| 181 | this->output_metadata(output, collection, response.docInfo[0]);
|
---|
| 182 |
|
---|
| 183 | // output the description of the document
|
---|
| 184 | // output << "<about>\n";
|
---|
| 185 | // output << "</about>\n";
|
---|
| 186 |
|
---|
| 187 | // close record
|
---|
| 188 | output << "</record>\n";
|
---|
| 189 |
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|