[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 |
|
---|
[21761] | 41 | bool metaformat::output_custom_metadata(ostream &output, outconvertclass &outconvert, bool &headerDone, const text_t &collection, ResultDocInfo_t &docInfo) {
|
---|
| 42 | return false;
|
---|
| 43 | }
|
---|
[8182] | 44 | bool metaformat::scan_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo,
|
---|
| 45 | bool doOutput)
|
---|
| 46 | {
|
---|
| 47 | bool headerDone = false;
|
---|
| 48 | MetadataInfo_tmap::iterator here = docInfo.metadata.begin();
|
---|
| 49 | MetadataInfo_tmap::iterator end = docInfo.metadata.end();
|
---|
| 50 |
|
---|
[10738] | 51 | utf8outconvertclass utf8convert; // we want to output metadata in utf8
|
---|
| 52 |
|
---|
[8182] | 53 | // metaItem is used initially to identify the rfc1807 (etc) metadata items. It is
|
---|
| 54 | // then used to hold the name of the metadata item, such as "title" or "subject".
|
---|
| 55 | text_t metaItem;
|
---|
| 56 | text_t::const_iterator start, last; // Use two iterators to go through metaItem
|
---|
[18581] | 57 |
|
---|
[8182] | 58 | while (here != end) {
|
---|
| 59 | start = last = here->first.begin();
|
---|
[8219] | 60 |
|
---|
| 61 | if (here->first.size() < this->formatPrefix().size() ||
|
---|
| 62 | here->first[this->formatPrefix().size()] != '.') {
|
---|
| 63 | metaItem == "";
|
---|
| 64 | }
|
---|
| 65 | else {
|
---|
[18581] | 66 | last += this->formatPrefix().size(); // Move last so that it is at the
|
---|
| 67 | // '.'
|
---|
[8219] | 68 | metaItem = substr(start, last); // Gets the substring starting at start and going up to (but
|
---|
[18581] | 69 | // not including) last. This should be "dc" (for example)
|
---|
[8219] | 70 | }
|
---|
| 71 |
|
---|
[21605] | 72 | // Map the element using the "oaimapping" specification from the oai.cfg/collect.cfg files, if defined
|
---|
| 73 | text_t mapTo = this->get_mapping(collection, here->first);
|
---|
| 74 | if (mapTo != "") {
|
---|
| 75 | if (doOutput) {
|
---|
| 76 | if (this->is_valid_element(mapTo)) {
|
---|
| 77 | this->output_item(output, utf8convert, headerDone, mapTo, here->second.values);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | else {
|
---|
| 81 | if (here->second.values.size() > 0) {
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | // Otherwise try to map the element automatically
|
---|
[21761] | 88 | // For example, dc.X is mapped to oai_dc.X
|
---|
[21605] | 89 | else if (metaItem == this->formatPrefix()) {
|
---|
[18581] | 90 | metaItem = substr(last+1, here->first.end()); // Get the rest of the metadata tag (it's name) but without the '.'
|
---|
[18896] | 91 | // remove xxx^ eg Coverage^Spatial becomes spatial
|
---|
| 92 | // this is for qualified dublin core. May affect other sets later if they
|
---|
| 93 | // validly have ^ in them.
|
---|
| 94 | text_t::iterator hat = findchar(metaItem.begin(), metaItem.end(), '^');
|
---|
| 95 | if (hat != metaItem.end()) {
|
---|
| 96 | metaItem = substr(hat+1, metaItem.end());
|
---|
| 97 | }
|
---|
[18895] | 98 | 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] | 99 | if (doOutput) {
|
---|
[18895] | 100 | if (this->is_valid_element(metaItem)) {
|
---|
| 101 |
|
---|
| 102 | this->output_item(output, utf8convert, headerDone, metaItem, here->second.values);
|
---|
| 103 | }
|
---|
[8182] | 104 | }
|
---|
| 105 | else {
|
---|
| 106 | if (here->second.values.size() > 0) {
|
---|
| 107 | return true;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | else {
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[9608] | 114 | ++here;
|
---|
[8182] | 115 | }
|
---|
| 116 |
|
---|
| 117 | if (!doOutput) {
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
[21761] | 120 | // specific metadata formats might need to do some custom metadata that is not just a standard mapping. eg oai_dc outputting an identifier that is a link
|
---|
| 121 | this->output_custom_metadata(output, utf8convert, headerDone, collection, docInfo);
|
---|
[8182] | 122 | if (headerDone) {
|
---|
[21761] | 123 |
|
---|
[8182] | 124 | this->output_metadata_footer(output);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return headerDone;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[21761] | 130 | text_t metaformat::get_metadata_value(ResultDocInfo_t &docInfo, const text_t &meta_name) {
|
---|
| 131 | MetadataInfo_tmap::iterator here = docInfo.metadata.find(meta_name);
|
---|
| 132 | if (here == docInfo.metadata.end()) {
|
---|
| 133 | return "";
|
---|
| 134 | }
|
---|
| 135 | return here->second.values[0];
|
---|
| 136 |
|
---|
| 137 | }
|
---|
[8182] | 138 |
|
---|
[21761] | 139 | void metaformat::get_metadata_values(ResultDocInfo_t &docInfo, const text_t &meta_name, text_tarray &values) {
|
---|
| 140 | MetadataInfo_tmap::iterator here = docInfo.metadata.find(meta_name);
|
---|
| 141 | if (here != docInfo.metadata.end()) {
|
---|
| 142 | values = here->second.values;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[8182] | 146 | bool metaformat::is_available(const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
| 147 | {
|
---|
| 148 | ofstream o("dummy", ios::out);
|
---|
| 149 | return this->scan_metadata(o, collection, docInfo, false);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[18895] | 152 | bool metaformat::is_valid_element(text_t &meta_name)
|
---|
| 153 | {
|
---|
| 154 | if (elementSet.count(meta_name)==1) return true;
|
---|
| 155 | return false;
|
---|
| 156 |
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[8182] | 159 | bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
| 160 | {
|
---|
| 161 | return this->scan_metadata(output, collection, docInfo, true);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | bool metaformat::output_record(ostream &output, recptproto *protocol, const text_t &collection,
|
---|
| 165 | const text_t &OID)
|
---|
| 166 | {
|
---|
| 167 | FilterResponse_t response;
|
---|
| 168 | text_tset metadata;
|
---|
[8303] | 169 | ofstream logout("oai.log", ios::app);
|
---|
[8182] | 170 |
|
---|
| 171 | // get the document information
|
---|
| 172 | if (!get_info(OID, collection, "", metadata, false, protocol, response, logout)) {
|
---|
| 173 | // TODO: error, bad request
|
---|
| 174 | // cerr << "Bad identifier or protocol " << OID << endl;
|
---|
| 175 | return false;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | // check to see if it's a classifier
|
---|
| 179 | text_t childHead;
|
---|
| 180 | // int oaiVersion = this->oaiConfigure->getOAIVersion();
|
---|
| 181 | text_t::const_iterator start = OID.begin();
|
---|
| 182 | text_t::const_iterator here = OID.begin();
|
---|
[11311] | 183 | here += 2;
|
---|
[8182] | 184 | childHead = substr(start, here);
|
---|
| 185 |
|
---|
| 186 | // if it isn't a document, kill it now
|
---|
[11311] | 187 | if (childHead == "CL") {
|
---|
[8182] | 188 | // cerr << "Not a document" << endl;
|
---|
| 189 | return false;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | // output record header
|
---|
| 193 | output << "<record>\n";
|
---|
| 194 |
|
---|
| 195 | // output header part of oai response
|
---|
| 196 | output << "<header>" << endl;
|
---|
| 197 | output << " <identifier>" << OID << "</identifier>" << endl;
|
---|
| 198 | // TODO: add modified date
|
---|
| 199 |
|
---|
| 200 | output << "</header>" << endl;
|
---|
| 201 |
|
---|
| 202 | // output metadata part of oai response
|
---|
| 203 | this->output_metadata(output, collection, response.docInfo[0]);
|
---|
| 204 |
|
---|
| 205 | // output the description of the document
|
---|
| 206 | // output << "<about>\n";
|
---|
| 207 | // output << "</about>\n";
|
---|
| 208 |
|
---|
| 209 | // close record
|
---|
| 210 | output << "</record>\n";
|
---|
| 211 |
|
---|
| 212 | return true;
|
---|
| 213 | }
|
---|