source: trunk/gsdl/src/oaiservr/metaformat.cpp@ 9608

Last change on this file since 9608 was 9608, checked in by kjdon, 19 years ago

added in x++ -> ++x changes submitted by Emanuel Dejanu

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