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->formatPrefix());
|
---|
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::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 | if (metaItem == this->formatPrefix()) {
|
---|
70 | metaItem = substr(last+1, here->first.end()); // Get the rest of the metadata tag (it's name) but without the '.'
|
---|
71 | lc(metaItem); // Convert it to lowercase for putting in the xml tags
|
---|
72 |
|
---|
73 | if (doOutput) {
|
---|
74 | this->output_item(output, utf8convert, headerDone, metaItem, here->second.values);
|
---|
75 | }
|
---|
76 | else {
|
---|
77 | if (here->second.values.size() > 0) {
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | text_t mapTo = this->get_mapping(collection, here->first);
|
---|
84 | if (mapTo != "") {
|
---|
85 | // Do we actually want to do anything here? Doesn't getting here imply that this
|
---|
86 | // particular metadata is stuff we don't want?
|
---|
87 | if (doOutput) {
|
---|
88 | this->output_item(output, utf8convert, headerDone, mapTo, here->second.values);
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | if (here->second.values.size() > 0) {
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | ++here;
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (!doOutput) {
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (headerDone) {
|
---|
106 | this->output_metadata_footer(output);
|
---|
107 | }
|
---|
108 |
|
---|
109 | return headerDone;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | bool metaformat::is_available(const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
114 | {
|
---|
115 | ofstream o("dummy", ios::out);
|
---|
116 | return this->scan_metadata(o, collection, docInfo, false);
|
---|
117 | }
|
---|
118 |
|
---|
119 | bool metaformat::output_metadata(ostream &output, const text_t &collection, ResultDocInfo_t &docInfo)
|
---|
120 | {
|
---|
121 | return this->scan_metadata(output, collection, docInfo, true);
|
---|
122 | }
|
---|
123 |
|
---|
124 | bool metaformat::output_record(ostream &output, recptproto *protocol, const text_t &collection,
|
---|
125 | const text_t &OID)
|
---|
126 | {
|
---|
127 | FilterResponse_t response;
|
---|
128 | text_tset metadata;
|
---|
129 | ofstream logout("oai.log", ios::app);
|
---|
130 |
|
---|
131 | // get the document information
|
---|
132 | if (!get_info(OID, collection, "", metadata, false, protocol, response, logout)) {
|
---|
133 | // TODO: error, bad request
|
---|
134 | // cerr << "Bad identifier or protocol " << OID << endl;
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // check to see if it's a classifier
|
---|
139 | text_t childHead;
|
---|
140 | // int oaiVersion = this->oaiConfigure->getOAIVersion();
|
---|
141 | text_t::const_iterator start = OID.begin();
|
---|
142 | text_t::const_iterator here = OID.begin();
|
---|
143 | here += 2;
|
---|
144 | childHead = substr(start, here);
|
---|
145 |
|
---|
146 | // if it isn't a document, kill it now
|
---|
147 | if (childHead == "CL") {
|
---|
148 | // cerr << "Not a document" << endl;
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // output record header
|
---|
153 | output << "<record>\n";
|
---|
154 |
|
---|
155 | // output header part of oai response
|
---|
156 | output << "<header>" << endl;
|
---|
157 | output << " <identifier>" << OID << "</identifier>" << endl;
|
---|
158 | // TODO: add modified date
|
---|
159 |
|
---|
160 | output << "</header>" << endl;
|
---|
161 |
|
---|
162 | // output metadata part of oai response
|
---|
163 | this->output_metadata(output, collection, response.docInfo[0]);
|
---|
164 |
|
---|
165 | // output the description of the document
|
---|
166 | // output << "<about>\n";
|
---|
167 | // output << "</about>\n";
|
---|
168 |
|
---|
169 | // close record
|
---|
170 | output << "</record>\n";
|
---|
171 |
|
---|
172 | return true;
|
---|
173 | }
|
---|