source: main/trunk/greenstone2/runtime-src/src/oaiservr/oaitools.cpp@ 28762

Last change on this file since 28762 was 24096, checked in by ak19, 13 years ago

Fixed a server crashing bug in the oaiserver. Off by one error.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/**********************************************************************
2 *
3 * oaitools.cpp --
4 *
5 * Copyright (C) 2004-2010 The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27#include "oaitools.h"
28
29#include "recptprototools.h"
30
31
32void oaiclassifier::swapColonsAndPeriods(text_t &classifier)
33{
34 for (int i = 0; i < classifier.size(); ++i) {
35 if (classifier[i] == '.') {
36 classifier[i] = ':';
37 }
38 else if (classifier[i] == ':') {
39 classifier[i] = '.';
40 }
41 }
42
43}
44
45text_t oaiclassifier::getGSDL_OID(const text_t &collection, const text_t& oai_id,
46 recptproto *protocol, ostream &logout)
47{
48 FilterResponse_t response;
49 text_tset metadata;
50
51 // metadata.insert("gsdl_id");
52
53 bool status_ok = get_children(oai_id, collection, "", metadata, false, protocol, response, logout);
54 text_t gsdl_id = "";
55 if (status_ok) {
56 if(response.docInfo.size() > 0){
57 gsdl_id = response.docInfo[0].OID;
58 }
59 else {
60 // Response.docInfo is empty, meaning the doc in question didn't have an oai_id number.
61 return gsdl_id; // Return the empty string to indicate this.
62 }
63 }
64
65 return gsdl_id;
66}
67
68/**
69 * Called to convert GS classifier/document identifiers that use
70 * a '.' to separate levels in the hierarchy into OAI identifiers
71 * that use ':' instead.
72 */
73void oaiclassifier::toOAI(const text_t repos_id, const text_t &collection, text_t &classifier)
74{
75
76 text_t tmp = "oai:";
77 tmp.append(repos_id);
78 tmp.append(":");
79 tmp.append(collection);
80 toOAI(tmp, classifier);
81
82}
83
84/**
85 * Called to convert GS classifier/document identifiers that use
86 * a '.' to separate levels in the hierarchy into OAI identifiers
87 * that use ':' instead.
88 */
89void oaiclassifier::toOAI(const text_t &collection, text_t &classifier)
90{
91 oaiclassifier::swapColonsAndPeriods(classifier);
92
93 // prepend the collection identifier to the beginning of the
94 // OAI identifier.
95 text_t tmp = collection;
96 tmp.append(":");
97 tmp.append(classifier);
98
99 classifier = tmp;
100}
101
102void oaiclassifier::getCollectionFromOAIID(const text_t oai_id, text_t &collection_id) {
103
104 // id is like oai:repos-id:collection:OID, we just want the collection bit
105
106 text_t::const_iterator begin = oai_id.begin();
107 text_t::const_iterator end = oai_id.end();
108 text_t::const_iterator colon = findchar(begin, end, ':'); // 1
109 if (colon != end) {
110 colon = findchar(colon+1, end, ':'); // 2
111 }
112 text_t::const_iterator third_colon;
113 if (colon !=end) {
114 third_colon = findchar(colon+1, end, ':');
115 }
116 if (third_colon != end) {
117 collection_id = substr(colon+1, third_colon);
118 }
119}
120
121/**
122 * Called to convert OAI classifier/document identifiers that use
123 * a ':' to separate levels in the hierarchy into GS identifiers
124 * that use '.' instead.
125 */
126void oaiclassifier::toGSDL(text_t &collection, text_t &classifier)
127{
128 oaiclassifier::swapColonsAndPeriods(classifier);
129
130 // separate out the collection identifier that should be
131 // found at the beginning of the OAI identifier from the
132 // item identifier within the collection
133 text_t::iterator colon = find(classifier.begin(), classifier.end(), '.');
134 if (colon != classifier.end()) {
135 collection = substr(classifier.begin(), colon);
136 classifier = substr(colon + 1, classifier.end());
137 }
138 else{
139 collection = classifier;
140 classifier = "";
141 }
142}
Note: See TracBrowser for help on using the repository browser.