#include #include "metaformat.h" #include "gsdltools.h" #include "gsdlunicode.h" #include "recptprototools.h" metaformat::metaformat() { } text_t metaformat::get_mapping(const text_t &collection, const text_t &collectionField) { if (this->oaiConfigure == NULL) { return ""; } return this->oaiConfigure->getMapping(collection, collectionField, this->formatPrefix()); } void metaformat::output_item(ostream &output, outconvertclass &outconvert, bool &headerDone, const text_t &label, const text_tarray &values) { if (!headerDone && (values.size() > 0)) { this->output_metadata_header(output); headerDone = true; } for (int item = 0; item < values.size(); ++item) { if (this->oaiConfigure->getOAIVersion() >= 200) { // TODO: GRB: This code may need to be subclassed by dc for 200 and later... output << outconvert << " <" << this->formatPrefix() << ":" << label << ">" << xml_safe(values[item]) << "formatPrefix() << ":" << label << ">\n"; } else { output << outconvert << " <" << label << ">" << xml_safe(values[item]) << "\n"; } } } bool metaformat::scan_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo, bool doOutput) { bool headerDone = false; MetadataInfo_tmap::iterator here = docInfo.metadata.begin(); MetadataInfo_tmap::iterator end = docInfo.metadata.end(); utf8outconvertclass utf8convert; // we want to output metadata in utf8 // metaItem is used initially to identify the rfc1807 (etc) metadata items. It is // then used to hold the name of the metadata item, such as "title" or "subject". text_t metaItem; text_t::const_iterator start, last; // Use two iterators to go through metaItem while (here != end) { start = last = here->first.begin(); if (here->first.size() < this->formatPrefix().size() || here->first[this->formatPrefix().size()] != '.') { metaItem == ""; } else { last += this->formatPrefix().size(); // Move last so that it is at the // '.' metaItem = substr(start, last); // Gets the substring starting at start and going up to (but // not including) last. This should be "dc" (for example) } if (metaItem == this->formatPrefix()) { metaItem = substr(last+1, here->first.end()); // Get the rest of the metadata tag (it's name) but without the '.' lc(metaItem); // Convert it to lowercase for putting in the xml tags if (doOutput) { this->output_item(output, utf8convert, headerDone, metaItem, here->second.values); } else { if (here->second.values.size() > 0) { return true; } } } else { text_t mapTo = this->get_mapping(collection, here->first); if (mapTo != "") { // Do we actually want to do anything here? Doesn't getting here imply that this // particular metadata is stuff we don't want? if (doOutput) { this->output_item(output, utf8convert, headerDone, mapTo, here->second.values); } else { if (here->second.values.size() > 0) { return true; } } } } ++here; } if (!doOutput) { return false; } if (headerDone) { this->output_metadata_footer(output); } return headerDone; } bool metaformat::is_available(const text_t &collection, ResultDocInfo_t &docInfo) { ofstream o("dummy", ios::out); return this->scan_metadata(o, collection, docInfo, false); } bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo) { return this->scan_metadata(output, collection, docInfo, true); } bool metaformat::output_record(ostream &output, recptproto *protocol, const text_t &collection, const text_t &OID) { FilterResponse_t response; text_tset metadata; ofstream logout("oai.log", ios::app); // get the document information if (!get_info(OID, collection, "", metadata, false, protocol, response, logout)) { // TODO: error, bad request // cerr << "Bad identifier or protocol " << OID << endl; return false; } // check to see if it's a classifier text_t childHead; // int oaiVersion = this->oaiConfigure->getOAIVersion(); text_t::const_iterator start = OID.begin(); text_t::const_iterator here = OID.begin(); here += 2; childHead = substr(start, here); // if it isn't a document, kill it now if (childHead == "CL") { // cerr << "Not a document" << endl; return false; } // output record header output << "\n"; // output header part of oai response output << "
" << endl; output << " " << OID << "" << endl; // TODO: add modified date output << "
" << endl; // output metadata part of oai response this->output_metadata(output, collection, response.docInfo[0]); // output the description of the document // output << "\n"; // output << "\n"; // close record output << "
\n"; return true; }