source: gsdl/trunk/runtime-src/src/oaiservr/oaiconfig.cpp@ 20607

Last change on this file since 20607 was 20607, checked in by kjdon, 15 years ago

find should be findchar

  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1#include "oaiconfig.h"
2#include <iostream>
3#include "fileutil.h"
4
5
6/**
7 * The mapping works as follows in the oai.cfg or collect.cfg file.
8 *
9 * A line is in the format oaimapping <collection field> <oai field>
10 *
11 * The map here is used to look up the "Greenstone" name which is mapped from
12 * a given OAI field name, the reverse direction to that given in the
13 * Greenstone collect.cfg file. The oairecordaction class instance which
14 * produces output for an OAI record information request thus uses the map
15 * to work from the field in the collection it has on hand which OAI
16 * record name it should use instead.
17 *
18 * An extension is to be used for this in which the OAI field name in the
19 * collect.cfg file can be made specific for a particular record format.
20 * This is done using the OAI field name in the format of
21 * <OAI format>.<OAI field name>
22 * Thus, an rfc1807 Title field would be referred to as rfc1807.Title
23 *
24 * A collection-level mapping can be provided in oai.cfg by prepending
25 * collname to collection field:
26 * <collname:field> <oai field>
27
28 * In the absence of a particular format name, the mapping is taken to be
29 * universal.
30 */
31
32oaiconfig::oaiconfig() : configurable () {
33 this->resumptionSize = -1; // Default = do not use resumption tokens
34}
35
36oaiconfig::oaiconfig(text_t &gsdlhome, text_t &gsdlcollect)
37{
38 // read main configuration file (oai.cfg) to get oai collections
39 text_t mainconfig = filename_cat(gsdlhome, "etc", "oai.cfg");
40 this->collection = "";
41 this->resumptionSize = -1;
42 this->read_configfile(mainconfig);
43
44 // then if we've not got a specified collection in the gsdlcollect
45 // parameter, read in all the collection's individual configurations
46 if (gsdlcollect == "") {
47 text_tarray::iterator start = this->collectList.begin();
48 text_tarray::iterator here = this->collectList.end()-1;
49 while (here != start) {
50 if (!this->configureCollection(gsdlhome, *here)) {
51 this->collectList.erase(here);
52 }
53 --here;
54 }
55 // and do the first one
56 if (!this->configureCollection(gsdlhome, *here)) {
57 this->collectList.erase(here);
58 }
59
60 }
61 else {
62 // what do we do if this fails?
63 this->configureCollection(gsdlhome, gsdlcollect);
64 }
65}
66
67oaiconfig::~oaiconfig()
68{
69 oaicollectmap::iterator here = this->collectMap.begin();
70 oaicollectmap::iterator end = this->collectMap.end();
71 while (here != end) {
72 delete here->second;
73 ++here;
74 }
75}
76
77int oaiconfig::resumeAfter()
78{ return this->resumptionSize;
79}
80
81int oaiconfig::getOAIVersion()
82{
83 if (this->oaiVersion == "1.1") {
84 return 110;
85 }
86 return 200;
87}
88
89bool oaiconfig::configureCollection(const text_t &gsdlhome, const text_t &gsdlcollect)
90{
91 text_t cnfgfile = filename_cat(gsdlhome, "collect", gsdlcollect, "etc", "collect.cfg");
92 if (!file_exists(cnfgfile)) {
93 return false;
94 }
95 this->collection = gsdlcollect;
96 this->read_configfile(cnfgfile);
97
98 return true;
99}
100void oaiconfig::configure (const text_t &key, const text_tarray &cfgline)
101{
102 // we've got an oai mapping item, and at least two fields
103 if (key == "oaimapping" && cfgline.size() > 1) {
104 text_t::const_iterator colonAt;
105 text_t index, name, configCollection;
106
107 // Take a default collection as being whatever the collection being configured is...
108 configCollection = this->collection;
109
110 // get the name of the (collection) field to map; this may actually
111 // be in a colon separated format of the type
112 // <collection name>:<field name>
113 index = cfgline[0];
114 if ((colonAt = findchar(index.begin(), index.end(), ':')) != index.end()) {
115 configCollection = substr(index.begin(), colonAt);
116
117 if (this->collection != "" && configCollection != this->collection) {
118 cerr << "Attempt to configure OAI mappings for " << configCollection << " in " << this->collection << endl;
119 }
120
121 colonAt += 1;
122 index = substr(colonAt, index.end());
123 }
124
125 // the second parameter is the metadata field to map the collection
126 // field onto. It may be provided with a metadata protocol (which
127 // will be given first and separated by a period or full stop). In
128 // the case of format.field name, the splitting is done here.
129 if ((colonAt = findchar(cfgline[1].begin(), cfgline[1].end(), '.')) != cfgline[1].end()) {
130 text_t stub = substr(cfgline[1].begin(), colonAt);
131 colonAt += 1;
132 name = substr(colonAt, cfgline[1].end());
133 index.append(":");
134 index.append(stub);
135 }
136 else {
137 name = cfgline[1];
138 }
139
140 // now 'index' is in the form <collectionfield>:(formatname)
141 // 'name' is simply the fieldname within the format
142 // 'configCollection' is the collection to be configured
143
144 // now simply map the field name (index) onto the collection name (name)
145 if (this->collectMap[configCollection] == NULL) {
146 this->collectMap[configCollection] = new oaicollectconfig(configCollection);
147 }
148 this->collectMap[configCollection]->fieldMap[index] = name;
149
150 // cerr << "Mapping " << index << " to " << name << " in " << configCollection << endl;
151
152 // TODO: check that the mapped field is actually in use
153 }
154 else if (key == "oaicollection" && cfgline.size() >= 1) {
155 // Configure a collection to be used as part of the OAI archive.
156 // This line should read:
157 //
158 // oaicollection <collectionname>
159 //
160 // Where <collectionname> is the name of the directory inside the
161 // gsdl/collect folder which contains the collection.
162 //
163 // To configure several collections, merely repeat this line,
164 // or alternatively use additional collection names after the
165 // first one.
166 //
167 // This configuration should only appear in oai.cfg
168 //
169 if (this->collection != "") {
170 cerr << "Attempt to configure an oai collection outside of oai.cfg" << endl;
171 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
172 exit(1);
173 }
174 for (int c = 0; c < cfgline.size(); ++c) {
175 this->collectList.push_back(cfgline[c]);
176 }
177 }
178 else if (key == "oaimetadata" && cfgline.size() >= 1) {
179 // List of metadata prefixes to suuport
180 // This line should read:
181 //
182 // oaicollection <metadataname> <metadataname>...
183 //
184 //
185 // This configuration should only appear in oai.cfg
186 //
187 if (this->collection != "") {
188 cerr << "Attempt to configure oai metadata outside of oai.cfg" << endl;
189 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
190 exit(1);
191 }
192 for (int c = 0; c < cfgline.size(); ++c) {
193 // todo: check that the set name is valid
194 this->metadataSet.insert(cfgline[c]);
195 }
196 }
197 else if (key == "oaiinfo" && cfgline.size() >= 1) {
198 // Get a piece of information for the oai repository information
199 // request. The line should read:
200 //
201 // oaiinfo <information field name> <value>
202 //
203 // This configuration should only be attempted in oai.cfg
204 //
205 if (this->collection != "") {
206 cerr << "Attempt to set oai information outside of oai.cfg" << endl;
207 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
208 exit(1);
209 }
210
211 // if no second parameter is given, then the first parameter
212 if (cfgline.size() == 1) {
213 this->infoMap[cfgline[0]] = cfgline[0];
214 }
215 else {
216 this->infoMap[cfgline[0]] = cfgline[1];
217 }
218 }
219 else if (key == "oaiversion" && cfgline.size() >= 1) {
220 this->oaiVersion = cfgline[0];
221 }
222 else if (key == "resumeafter" && cfgline.size() >= 1) {
223 this->resumptionSize = cfgline[0].getint();
224 }
225 // get and note a maintainer item to support the Identify Verb of OAI
226 else if (key == "maintainer" && cfgline.size() >= 1) {
227 int line = 0;
228
229 // TODO: exhaustive checks for empty or default values of maintainer
230 while (line < cfgline.size()) {
231 if (cfgline[line] != "NULL" &&
232 cfgline[line] != "") {
233 // do something
234 break;
235 }
236 else {
237 ++line;
238 }
239 }
240
241 // Only try to set the configuration if we have a legitimate value ...
242 if (line < cfgline.size()) {
243 // ensure we have a map to write to
244 if (this->collectMap[this->collection] == NULL) {
245 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
246 }
247 this->collectMap[this->collection]->maintainer = cfgline[line];
248 }
249 }
250 else if (key == "repositoryName" && cfgline.size() >= 1) {
251 int line = 0;
252
253 // TODO: exhaustive checks for empty or default values of repositoryName
254 while (line < cfgline.size()) {
255 if (cfgline[line] != "NULL" &&
256 cfgline[line] != "") {
257 // do something
258 break;
259 }
260 else {
261 ++line;
262 }
263 }
264
265 // Only try to set the configuration if we have a legitimate value ...
266 if (line < cfgline.size()) {
267 // ensure we have a map to write to
268 if (this->collectMap[this->collection] == NULL) {
269 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
270 }
271 this->collectMap[this->collection]->repositoryName = cfgline[line];
272 }
273 }
274 else if (key == "baseURL" && cfgline.size() >= 1) {
275 int line = 0;
276
277 while (line < cfgline.size()) {
278 if (cfgline[line] != "NULL" &&
279 cfgline[line] != "") {
280 // do something
281 break;
282 }
283 else {
284 ++line;
285 }
286 }
287
288 // Only try to set the configuration if we have a legitimate value ...
289 if (line < cfgline.size()) {
290 // ensure we have a map to write to
291 if (this->collectMap[this->collection] == NULL) {
292 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
293 }
294 this->collectMap[this->collection]->baseURL = cfgline[line];
295 }
296 }
297 else if (key == "baseDocRoot" && cfgline.size() >= 1) {
298 int line = 0;
299
300 while (line < cfgline.size()) {
301 if (cfgline[line] != "NULL" &&
302 cfgline[line] != "") {
303 // do something
304 break;
305 }
306 else {
307 ++line;
308 }
309 }
310
311 // Only try to set the configuration if we have a legitimate value ...
312 if (line < cfgline.size()) {
313 // ensure we have a map to write to
314 if (this->collectMap[this->collection] == NULL) {
315 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
316 }
317 this->collectMap[this->collection]->baseDocRoot = cfgline[line];
318 }
319 }
320}
321
322/**
323 * TODO: store all field values in a map per collection
324 */
325text_t oaiconfig::getCollectionConfig(const text_t &collection, const text_t &field)
326{
327 if (this->collectMap[collection] == NULL) {
328 return "";
329 }
330 if (field == "maintainer") {
331 return this->collectMap[collection]->maintainer;
332 }
333
334 if (field == "repositoryName") {
335 return this->collectMap[collection]->repositoryName;
336 }
337
338 if (field == "baseURL") {
339 return this->collectMap[collection]->baseURL;
340 }
341
342 if (field == "baseDocRoot") {
343 return this->collectMap[collection]->baseDocRoot;
344 }
345
346 return "";
347}
348
349text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield)
350{
351 if (this->collectMap[collection] == NULL) {
352 return "";
353 }
354 return this->collectMap[collection]->fieldMap[collectfield];
355}
356
357/**
358 * Get the mapping for a field in a given collection; if no mapping
359 * exists, the result will be a blank string.
360 */
361text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield, const text_t &formatname)
362{
363 text_t fullName = collectfield;
364 fullName.append(":");
365 fullName.append(formatname);
366
367 // try the collection-specific options first
368 if (this->collectMap[collection] != NULL) {
369 // first try the most specific item - this collection, and given that protocol
370 if (this->collectMap[collection]->fieldMap.count(fullName) >= 1) {
371 return this->collectMap[collection]->fieldMap[fullName];
372 }
373 // otherwise, fall back to this collection, and all protocols
374 else if (this->collectMap[collection]->fieldMap.count(collectfield) >= 1) {
375 return this->collectMap[collection]->fieldMap[collectfield];
376 }
377 }
378
379 // if no mappings exist, return an empty item
380 if (this->collectMap[""] == NULL) {
381 return "";
382 }
383
384 // then try generic rules
385 if (this->collectMap[""]->fieldMap.count(fullName) >= 1) {
386 return this->collectMap[""]->fieldMap[fullName];
387 }
388 else {
389 return this->collectMap[""]->fieldMap[collectfield];
390 }
391}
Note: See TracBrowser for help on using the repository browser.