source: main/tags/2.80/gsdl/src/oaiservr/metaformat.cpp@ 24527

Last change on this file since 24527 was 11311, checked in by kjdon, 18 years ago

anywhere where we determine document/classifier by checking for starting with HASH, I changed to check if starts with CL

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1#include "metaformat.h"
2#include "gsdlunicode.h"
3#include <fstream>
4
5#include "OIDtools.h"
6
7metaformat::metaformat()
8{
9}
10
11text_t metaformat::get_mapping(const text_t &collection, const text_t &collectionField)
12{
13 if (this->oaiConfigure == NULL) {
14 return "";
15 }
16
17 return this->oaiConfigure->getMapping(collection, collectionField, this->formatPrefix());
18}
19
20void metaformat::output_item(ostream &output, outconvertclass &outconvert,
21 bool &headerDone, const text_t &label,
22 const text_tarray &values)
23{
24
25 if (!headerDone && (values.size() > 0)) {
26 this->output_metadata_header(output);
27 headerDone = true;
28 }
29
30 for (int item = 0; item < values.size(); ++item) {
31 if (this->oaiConfigure->getOAIVersion() >= 200) { // TODO: GRB: This code may need to be subclassed by dc for 200 and later...
32 output << outconvert << " <" << this->formatPrefix() << ":" << label << ">" << values[item] << "</" << this->formatPrefix() << ":" << label << ">\n";
33 }
34 else {
35 output << outconvert << " <" << label << ">" << values[item] << "</" << label << ">\n";
36 }
37 }
38}
39
40bool 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
47 utf8outconvertclass utf8convert; // we want to output metadata in utf8
48
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();
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
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) {
72 this->output_item(output, utf8convert, headerDone, metaItem, here->second.values);
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) {
86 this->output_item(output, utf8convert, headerDone, mapTo, here->second.values);
87 }
88 else {
89 if (here->second.values.size() > 0) {
90 return true;
91 }
92 }
93 }
94 }
95
96 ++here;
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
111bool 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
117bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo)
118{
119 return this->scan_metadata(output, collection, docInfo, true);
120}
121
122bool 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;
127 ofstream logout("oai.log", ios::app);
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();
141 here += 2;
142 childHead = substr(start, here);
143
144 // if it isn't a document, kill it now
145 if (childHead == "CL") {
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}
Note: See TracBrowser for help on using the repository browser.