1 | #include <fstream>
|
---|
2 | #include "metaformat.h"
|
---|
3 | #include "gsdltools.h"
|
---|
4 | #include "gsdlunicode.h"
|
---|
5 | #include "recptprototools.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | metaformat::metaformat()
|
---|
9 | {
|
---|
10 | }
|
---|
11 |
|
---|
12 | text_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 |
|
---|
21 | void 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 |
|
---|
41 | bool metaformat::output_custom_metadata(ostream &output, outconvertclass &outconvert, bool &headerDone, const text_t &collection, ResultDocInfo_t &docInfo) {
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 | bool metaformat::scan_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo,
|
---|
45 | bool doOutput)
|
---|
46 | {
|
---|
47 | bool headerDone = false;
|
---|
48 | MetadataInfo_tmap::iterator here = docInfo.metadata.begin();
|
---|
49 | MetadataInfo_tmap::iterator end = docInfo.metadata.end();
|
---|
50 |
|
---|
51 | utf8outconvertclass utf8convert; // we want to output metadata in utf8
|
---|
52 |
|
---|
53 | // metaItem is used initially to identify the rfc1807 (etc) metadata items. It is
|
---|
54 | // then used to hold the name of the metadata item, such as "title" or "subject".
|
---|
55 | text_t metaItem;
|
---|
56 | text_t::const_iterator start, last; // Use two iterators to go through metaItem
|
---|
57 |
|
---|
58 | while (here != end) {
|
---|
59 | start = last = here->first.begin();
|
---|
60 |
|
---|
61 | if (here->first.size() < this->formatPrefix().size() ||
|
---|
62 | here->first[this->formatPrefix().size()] != '.') {
|
---|
63 | metaItem == "";
|
---|
64 | }
|
---|
65 | else {
|
---|
66 | last += this->formatPrefix().size(); // Move last so that it is at the
|
---|
67 | // '.'
|
---|
68 | metaItem = substr(start, last); // Gets the substring starting at start and going up to (but
|
---|
69 | // not including) last. This should be "dc" (for example)
|
---|
70 | }
|
---|
71 |
|
---|
72 | // Map the element using the "oaimapping" specification from the oai.cfg/collect.cfg files, if defined
|
---|
73 | text_t mapTo = this->get_mapping(collection, here->first);
|
---|
74 | if (mapTo != "") {
|
---|
75 | if (doOutput) {
|
---|
76 | if (this->is_valid_element(mapTo)) {
|
---|
77 | this->output_item(output, utf8convert, headerDone, mapTo, here->second.values);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | if (here->second.values.size() > 0) {
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | // Otherwise try to map the element automatically
|
---|
88 | // For example, dc.X is mapped to oai_dc.X
|
---|
89 | else if (metaItem == this->formatPrefix()) {
|
---|
90 | metaItem = substr(last+1, here->first.end()); // Get the rest of the metadata tag (it's name) but without the '.'
|
---|
91 | // remove xxx^ eg Coverage^Spatial becomes spatial
|
---|
92 | // this is for qualified dublin core. May affect other sets later if they
|
---|
93 | // validly have ^ in them.
|
---|
94 | text_t::iterator hat = findchar(metaItem.begin(), metaItem.end(), '^');
|
---|
95 | if (hat != metaItem.end()) {
|
---|
96 | metaItem = substr(hat+1, metaItem.end());
|
---|
97 | }
|
---|
98 | 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
|
---|
99 | if (doOutput) {
|
---|
100 | if (this->is_valid_element(metaItem)) {
|
---|
101 |
|
---|
102 | this->output_item(output, utf8convert, headerDone, metaItem, here->second.values);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | else {
|
---|
106 | if (here->second.values.size() > 0) {
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | }
|
---|
113 |
|
---|
114 | ++here;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (!doOutput) {
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 | // specific metadata formats might need to do some custom metadata that is not just a standard mapping. eg oai_dc outputting an identifier that is a link
|
---|
121 | this->output_custom_metadata(output, utf8convert, headerDone, collection, docInfo);
|
---|
122 | if (headerDone) {
|
---|
123 |
|
---|
124 | this->output_metadata_footer(output);
|
---|
125 | }
|
---|
126 |
|
---|
127 | return headerDone;
|
---|
128 | }
|
---|
129 |
|
---|
130 | text_t metaformat::get_metadata_value(ResultDocInfo_t &docInfo, const text_t &meta_name) {
|
---|
131 | MetadataInfo_tmap::iterator here = docInfo.metadata.find(meta_name);
|
---|
132 | if (here == docInfo.metadata.end()) {
|
---|
133 | return "";
|
---|
134 | }
|
---|
135 | return here->second.values[0];
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | void metaformat::get_metadata_values(ResultDocInfo_t &docInfo, const text_t &meta_name, text_tarray &values) {
|
---|
140 | MetadataInfo_tmap::iterator here = docInfo.metadata.find(meta_name);
|
---|
141 | if (here != docInfo.metadata.end()) {
|
---|
142 | values = here->second.values;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | bool metaformat::is_available(const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
147 | {
|
---|
148 | ofstream o("dummy", ios::out);
|
---|
149 | return this->scan_metadata(o, collection, docInfo, false);
|
---|
150 | }
|
---|
151 |
|
---|
152 | bool metaformat::is_valid_element(text_t &meta_name)
|
---|
153 | {
|
---|
154 | if (elementSet.count(meta_name)==1) return true;
|
---|
155 | return false;
|
---|
156 |
|
---|
157 | }
|
---|
158 |
|
---|
159 | bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
160 | {
|
---|
161 | return this->scan_metadata(output, collection, docInfo, true);
|
---|
162 | }
|
---|
163 |
|
---|
164 | bool metaformat::output_record(ostream &output, recptproto *protocol, const text_t &collection,
|
---|
165 | const text_t &OID)
|
---|
166 | {
|
---|
167 | FilterResponse_t response;
|
---|
168 | text_tset metadata;
|
---|
169 | ofstream logout("oai.log", ios::app);
|
---|
170 |
|
---|
171 | // get the document information
|
---|
172 | if (!get_info(OID, collection, "", metadata, false, protocol, response, logout)) {
|
---|
173 | // TODO: error, bad request
|
---|
174 | // cerr << "Bad identifier or protocol " << OID << endl;
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // check to see if it's a classifier
|
---|
179 | text_t childHead;
|
---|
180 | // int oaiVersion = this->oaiConfigure->getOAIVersion();
|
---|
181 | text_t::const_iterator start = OID.begin();
|
---|
182 | text_t::const_iterator here = OID.begin();
|
---|
183 | here += 2;
|
---|
184 | childHead = substr(start, here);
|
---|
185 |
|
---|
186 | // if it isn't a document, kill it now
|
---|
187 | if (childHead == "CL") {
|
---|
188 | // cerr << "Not a document" << endl;
|
---|
189 | return false;
|
---|
190 | }
|
---|
191 |
|
---|
192 | // output record header
|
---|
193 | output << "<record>\n";
|
---|
194 |
|
---|
195 | // output header part of oai response
|
---|
196 | output << "<header>" << endl;
|
---|
197 | output << " <identifier>" << OID << "</identifier>" << endl;
|
---|
198 | // TODO: add modified date
|
---|
199 |
|
---|
200 | output << "</header>" << endl;
|
---|
201 |
|
---|
202 | // output metadata part of oai response
|
---|
203 | this->output_metadata(output, collection, response.docInfo[0]);
|
---|
204 |
|
---|
205 | // output the description of the document
|
---|
206 | // output << "<about>\n";
|
---|
207 | // output << "</about>\n";
|
---|
208 |
|
---|
209 | // close record
|
---|
210 | output << "</record>\n";
|
---|
211 |
|
---|
212 | return true;
|
---|
213 | }
|
---|