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