source: main/trunk/greenstone2/runtime-src/src/oaiservr/metaformat.cpp@ 21605

Last change on this file since 21605 was 21605, checked in by mdewsnip, 14 years ago

Swapped an "if" block and an "else" block in metaformat::scan_metadata() so the oaimapping (specified in the oai.cfg file) is used in preference to the automatic code that looks at a value like "dc.Provenance" and maps it automatically to oai_dc.provenance. This means the user always has full control if they want it, instead of being unable to override the automatic mapping if they need to.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1#include <fstream>
2#include "metaformat.h"
3#include "gsdltools.h"
4#include "gsdlunicode.h"
5#include "recptprototools.h"
6
7
8metaformat::metaformat()
9{
10}
11
12text_t metaformat::get_mapping(const text_t &collection, const text_t &collectionField)
13{
14 if (this->oaiConfigure == NULL) {
15 return "";
16 }
17
18 return this->oaiConfigure->getMapping(collection, collectionField, this->formatName());
19}
20
21void metaformat::output_item(ostream &output, outconvertclass &outconvert,
22 bool &headerDone, const text_t &label,
23 const text_tarray &values)
24{
25
26 if (!headerDone && (values.size() > 0)) {
27 this->output_metadata_header(output);
28 headerDone = true;
29 }
30
31 for (int item = 0; item < values.size(); ++item) {
32 if (this->oaiConfigure->getOAIVersion() >= 200) { // TODO: GRB: This code may need to be subclassed by dc for 200 and later...
33 output << outconvert << " <" << this->formatPrefix() << ":" << label << ">" << xml_safe(values[item]) << "</" << this->formatPrefix() << ":" << label << ">\n";
34 }
35 else {
36 output << outconvert << " <" << label << ">" << xml_safe(values[item]) << "</" << label << ">\n";
37 }
38 }
39}
40
41bool metaformat::scan_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo,
42 bool doOutput)
43{
44 bool headerDone = false;
45 MetadataInfo_tmap::iterator here = docInfo.metadata.begin();
46 MetadataInfo_tmap::iterator end = docInfo.metadata.end();
47
48 utf8outconvertclass utf8convert; // we want to output metadata in utf8
49
50 // metaItem is used initially to identify the rfc1807 (etc) metadata items. It is
51 // then used to hold the name of the metadata item, such as "title" or "subject".
52 text_t metaItem;
53 text_t::const_iterator start, last; // Use two iterators to go through metaItem
54
55 while (here != end) {
56 start = last = here->first.begin();
57
58 if (here->first.size() < this->formatPrefix().size() ||
59 here->first[this->formatPrefix().size()] != '.') {
60 metaItem == "";
61 }
62 else {
63 last += this->formatPrefix().size(); // Move last so that it is at the
64 // '.'
65 metaItem = substr(start, last); // Gets the substring starting at start and going up to (but
66 // not including) last. This should be "dc" (for example)
67 }
68
69 // Map the element using the "oaimapping" specification from the oai.cfg/collect.cfg files, if defined
70 text_t mapTo = this->get_mapping(collection, here->first);
71 if (mapTo != "") {
72 // Do we actually want to do anything here? Doesn't getting here imply that this
73 // particular metadata is stuff we don't want?
74 if (doOutput) {
75 if (this->is_valid_element(mapTo)) {
76 this->output_item(output, utf8convert, headerDone, mapTo, here->second.values);
77 }
78 }
79 else {
80 if (here->second.values.size() > 0) {
81 return true;
82 }
83 }
84 }
85
86 // Otherwise try to map the element automatically
87 // For example, dc.X is mapped to oai_dc.X (doesn't check X is a valid Dublin Core element though)
88 else if (metaItem == this->formatPrefix()) {
89 metaItem = substr(last+1, here->first.end()); // Get the rest of the metadata tag (it's name) but without the '.'
90 // remove xxx^ eg Coverage^Spatial becomes spatial
91 // this is for qualified dublin core. May affect other sets later if they
92 // validly have ^ in them.
93 text_t::iterator hat = findchar(metaItem.begin(), metaItem.end(), '^');
94 if (hat != metaItem.end()) {
95 metaItem = substr(hat+1, metaItem.end());
96 }
97 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
98 if (doOutput) {
99 if (this->is_valid_element(metaItem)) {
100
101 this->output_item(output, utf8convert, headerDone, metaItem, here->second.values);
102 }
103 }
104 else {
105 if (here->second.values.size() > 0) {
106 return true;
107 }
108 }
109 }
110 else {
111 }
112
113 ++here;
114 }
115
116 if (!doOutput) {
117 return false;
118 }
119
120 if (headerDone) {
121 this->output_metadata_footer(output);
122 }
123
124 return headerDone;
125}
126
127
128bool metaformat::is_available(const text_t &collection, ResultDocInfo_t &docInfo)
129{
130 ofstream o("dummy", ios::out);
131 return this->scan_metadata(o, collection, docInfo, false);
132}
133
134bool metaformat::is_valid_element(text_t &meta_name)
135{
136 if (elementSet.count(meta_name)==1) return true;
137 return false;
138
139}
140
141bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo)
142{
143 return this->scan_metadata(output, collection, docInfo, true);
144}
145
146bool metaformat::output_record(ostream &output, recptproto *protocol, const text_t &collection,
147 const text_t &OID)
148{
149 FilterResponse_t response;
150 text_tset metadata;
151 ofstream logout("oai.log", ios::app);
152
153 // get the document information
154 if (!get_info(OID, collection, "", metadata, false, protocol, response, logout)) {
155 // TODO: error, bad request
156 // cerr << "Bad identifier or protocol " << OID << endl;
157 return false;
158 }
159
160 // check to see if it's a classifier
161 text_t childHead;
162 // int oaiVersion = this->oaiConfigure->getOAIVersion();
163 text_t::const_iterator start = OID.begin();
164 text_t::const_iterator here = OID.begin();
165 here += 2;
166 childHead = substr(start, here);
167
168 // if it isn't a document, kill it now
169 if (childHead == "CL") {
170 // cerr << "Not a document" << endl;
171 return false;
172 }
173
174 // output record header
175 output << "<record>\n";
176
177 // output header part of oai response
178 output << "<header>" << endl;
179 output << " <identifier>" << OID << "</identifier>" << endl;
180 // TODO: add modified date
181
182 output << "</header>" << endl;
183
184 // output metadata part of oai response
185 this->output_metadata(output, collection, response.docInfo[0]);
186
187 // output the description of the document
188 // output << "<about>\n";
189 // output << "</about>\n";
190
191 // close record
192 output << "</record>\n";
193
194 return true;
195}
Note: See TracBrowser for help on using the repository browser.