source: trunk/gsdl/src/recpt/OIDtools.cpp@ 1861

Last change on this file since 1861 was 1860, checked in by cs025, 23 years ago

Included CORBA branch for first time

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/**********************************************************************
2 *
3 * OIDtools.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "OIDtools.h"
27#include <assert.h>
28
29// returns (in top) the top level of OID (i.e. everything
30// up until the first dot)
31void get_top (const text_t &OID, text_t &top) {
32
33 top.clear();
34 if (OID.empty()) return;
35
36 text_t::const_iterator begin = OID.begin();
37 text_t::const_iterator end = OID.end();
38
39 top.appendrange (begin, findchar(begin, end, '.'));
40}
41
42
43// checks if OID is top level (i.e. contains no dots)
44bool is_top (const text_t &OID) {
45
46 if (OID.empty()) return true;
47
48 text_t::const_iterator here = OID.begin();
49 text_t::const_iterator end = OID.end();
50 here = findchar (here, end, '.');
51
52 if (here == end) return true;
53 return false;
54}
55
56
57// get_parents_array loads the parents array with all the parents of the
58// document or classification specified by OID (not including OID itself)
59void get_parents_array (const text_t &OID, text_tarray &parents) {
60
61 text_t::const_iterator here = OID.begin ();
62 text_t::const_iterator end = OID.end ();
63 text_t thisparent;
64
65 while (here != end) {
66 if (*here == '.') parents.push_back(thisparent);
67 thisparent.push_back(*here);
68 here ++;
69 }
70}
71
72
73// get_info does a protocol call and returns (in response) the metadata
74// associated with OID. Metadata should be loaded with whatever
75// metadata fields are to be requested.
76
77bool get_info (const text_t &OID, const text_t &collection,
78 const text_tset &metadata, bool getParents,
79 recptproto *collectproto, FilterResponse_t &response,
80 ostream &logout) {
81
82 response.clear();
83
84 comerror_t err;
85 FilterRequest_t request;
86 request.clear();
87
88 request.filterName = "NullFilter";
89 request.filterResultOptions = FRmetadata;
90 request.getParents = getParents;
91 request.fields = metadata;
92 request.docSet.push_back (OID);
93 assert (collectproto != NULL);
94
95 collectproto->filter (collection, request, response, err, logout);
96 if (err != noError) {
97 outconvertclass text_t2ascii;
98 logout << text_t2ascii
99 << "Error: call to filter failed for " << OID
100 << " in OIDtools::get_info ("
101 << get_comerror_string (err) << ")\n";
102 return false;
103 }
104
105 return true;
106}
107
108// overloaded, to allow "custom" filter options.
109bool get_info (const text_t &OID, const text_t &collection,
110 const text_tset &metadata, const OptionValue_tarray &options,
111 bool getParents,
112 recptproto *collectproto, FilterResponse_t &response,
113 ostream &logout) {
114
115 response.clear();
116
117 comerror_t err;
118 FilterRequest_t request;
119
120 request.filterName = "NullFilter";
121 request.filterResultOptions = FRmetadata;
122 request.getParents = getParents;
123 request.filterOptions = options;
124 request.fields = metadata;
125 request.docSet.push_back (OID);
126
127 assert (collectproto != NULL);
128 collectproto->filter (collection, request, response, err, logout);
129 if (err != noError) {
130 outconvertclass text_t2ascii;
131 logout << text_t2ascii
132 << "Error: call to filter failed for " << OID
133 << " in OIDtools::get_info ("
134 << get_comerror_string (err) << ")\n";
135 return false;
136 }
137
138 return true;
139}
140
141bool get_info (const text_tarray &OIDs, const text_t &collection,
142 const text_tset &metadata, bool getParents,
143 recptproto *collectproto, FilterResponse_t &response,
144 ostream &logout) {
145
146 response.clear();
147 if (OIDs.empty()) return true;
148
149 comerror_t err;
150 FilterRequest_t request;
151
152 request.filterName = "NullFilter";
153 request.filterResultOptions = FRmetadata;
154 request.getParents = getParents;
155 request.fields = metadata;
156
157 request.docSet = OIDs;
158
159 collectproto->filter (collection, request, response, err, logout);
160 if (err != noError) {
161 outconvertclass text_t2ascii;
162 logout << text_t2ascii
163 << "Error: call to filter failed in OIDtools::get_info ("
164 << get_comerror_string (err) << ")\n";
165 return false;
166 }
167
168 return true;
169}
170
171
172
173// has_children returns true if OID has children
174bool has_children (const text_t &OID, const text_t &collection,
175 recptproto *collectproto, ostream &logout) {
176
177 FilterResponse_t response;
178 text_tset metadata;
179 metadata.insert ("haschildren");
180
181 if (get_info (OID, collection, metadata, false, collectproto, response, logout)) {
182 if (response.docInfo[0].metadata["haschildren"].values[0] == "1")
183 return true;
184 }
185 return false;
186}
187
188
189// get_children does a protocol call and returns (in response) the OIDs and
190// metadata of all the children of OID. The metadata set should be loaded
191// with whatever metadata fields are to be requested.
192
193bool get_children (const text_t &OID, const text_t &collection,
194 const text_tset &metadata, bool getParents,
195 recptproto *collectproto, FilterResponse_t &response,
196 ostream &logout) {
197
198 response.clear();
199
200 comerror_t err;
201 FilterRequest_t request;
202 OptionValue_t option;
203
204 option.name = "ParentNode";
205 option.value = OID;
206 request.filterOptions.push_back (option);
207 request.filterName = "BrowseFilter";
208 request.filterResultOptions = FROID | FRmetadata;
209 request.fields = metadata;
210 request.getParents = getParents;
211
212 collectproto->filter (collection, request, response, err, logout);
213
214 if (err != noError) {
215 outconvertclass text_t2ascii;
216 logout << text_t2ascii
217 << "Error: call to filter failed for " << OID
218 << " in OIDtools::get_children ("
219 << get_comerror_string (err) << ")\n";
220 return false;
221 }
222 return true;
223}
224
225// get_parent returns the parent of the document or classification
226// specified by OID
227text_t get_parent (text_t OID) {
228
229 if (OID.empty() || is_top (OID)) return "";
230
231 text_t::const_iterator begin = OID.begin();
232 text_t::const_iterator here = (OID.end() - 1);
233
234 while (here >= begin) {
235 OID.pop_back();
236 if (*here == '.') break;
237 here --;
238 }
239 return OID;
240}
241
242// takes an OID like ".2 and replaces the " with parent
243void translate_parent (text_t &OID, const text_t &parent) {
244
245 text_t::const_iterator here = OID.begin();
246 text_t::const_iterator end = OID.end();
247 text_t temp;
248
249 while (here != end) {
250 if (*here == '"') temp += parent;
251 else temp.push_back (*here);
252 here ++;
253 }
254 OID = temp;
255}
256
257// shrink_parent does the opposite to translate_parent
258void shrink_parent (text_t &OID) {
259
260 text_tarray tmp;
261 splitchar (OID.begin(), OID.end(), '.', tmp);
262 OID = "\"." + tmp.back();
263}
264
265// checks if OID uses ".fc", ".lc", ".pr", ".ns",
266// or ".ps" syntax (first child, last child, parent,
267// next sibling, previous sibling)
268bool needs_translating (const text_t &OID) {
269
270 if (OID.size() < 4) return false;
271
272 text_t tail = substr (OID.end()-3, OID.end());
273 if (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
274 tail == ".ns" || tail == ".ps") return true;
275
276 return false;
277}
278
279// strips the ".fc", ".lc", ".pr", ".ns",
280// or ".ps" suffix from the end of OID
281void strip_suffix (text_t &OID) {
282
283 text_t tail = substr (OID.end()-3, OID.end());
284 while (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
285 tail == ".ns" || tail == ".ps") {
286 OID.erase (OID.end()-3, OID.end());
287 tail = substr (OID.end()-3, OID.end());
288 }
289}
290
291static void recurse_contents (ResultDocInfo_t section, const bool &is_classify,
292 const text_t &collection, const text_tset &metadata,
293 recptproto *collectproto, FilterResponse_t &response,
294 ostream &logout) {
295
296 int haschildren = section.metadata["haschildren"].values[0].getint();
297 const text_t &doctype = section.metadata["doctype"].values[0];
298
299 if ((haschildren == 1) && ((!is_classify) || (doctype == "classify"))) {
300 FilterResponse_t tmp;
301 bool getParents = false;
302 get_children (section.OID, collection, metadata, getParents, collectproto, tmp, logout);
303 ResultDocInfo_tarray::iterator thisdoc = tmp.docInfo.begin();
304 ResultDocInfo_tarray::iterator lastdoc = tmp.docInfo.end();
305 while (thisdoc != lastdoc) {
306 response.docInfo.push_back (*thisdoc);
307 recurse_contents (*thisdoc, is_classify, collection, metadata,
308 collectproto, response, logout);
309 thisdoc ++;
310 }
311 }
312}
313
314// get_contents returns OIDs and metadata of all contents
315// below (and including) OID.
316void get_contents (const text_t &topOID, const bool &is_classify,
317 text_tset &metadata, const text_t &collection,
318 recptproto *collectproto, FilterResponse_t &response,
319 ostream &logout) {
320
321 if (topOID.empty()) return;
322 response.clear();
323
324 metadata.insert ("haschildren");
325 metadata.insert ("doctype");
326
327 // get topOIDs info
328 if (get_info (topOID, collection, metadata, false, collectproto, response, logout))
329 recurse_contents (response.docInfo[0], is_classify, collection,
330 metadata, collectproto, response, logout);
331}
332
333// is_child_of returns true if OID2 is a child of OID1
334bool is_child_of(const text_t &OID1, const text_t &OID2) {
335
336 text_t parent = get_parent(OID2);
337
338 while (!parent.empty()) {
339 if (parent == OID1) return true;
340 parent = get_parent(parent);
341 }
342 return false;
343}
Note: See TracBrowser for help on using the repository browser.