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)
|
---|
31 | void 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)
|
---|
44 | bool 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)
|
---|
59 | void 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 |
|
---|
77 | bool get_info (const text_t &OID, const text_t &collection, const text_t &lang,
|
---|
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 = noError;
|
---|
85 | FilterRequest_t request;
|
---|
86 | request.clear();
|
---|
87 |
|
---|
88 | request.filterName = "NullFilter";
|
---|
89 | request.filterLang = lang;
|
---|
90 | request.filterResultOptions = FRmetadata;
|
---|
91 | request.getParents = getParents;
|
---|
92 | request.fields = metadata;
|
---|
93 | request.docSet.push_back (OID);
|
---|
94 | assert (collectproto != NULL);
|
---|
95 |
|
---|
96 | collectproto->filter (collection, request, response, err, logout);
|
---|
97 | if (err != noError) {
|
---|
98 | outconvertclass text_t2ascii;
|
---|
99 | logout << text_t2ascii
|
---|
100 | << "Error: call to filter failed for " << OID
|
---|
101 | << " in OIDtools::get_info ("
|
---|
102 | << get_comerror_string (err) << ")\n";
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | // overloaded, to allow "custom" filter options.
|
---|
110 | bool get_info (const text_t &OID, const text_t &collection, const text_t &lang,
|
---|
111 | const text_tset &metadata, const OptionValue_tarray &options,
|
---|
112 | bool getParents,
|
---|
113 | recptproto *collectproto, FilterResponse_t &response,
|
---|
114 | ostream &logout) {
|
---|
115 |
|
---|
116 | response.clear();
|
---|
117 |
|
---|
118 | comerror_t err = noError;
|
---|
119 | FilterRequest_t request;
|
---|
120 |
|
---|
121 | request.filterName = "NullFilter";
|
---|
122 | request.filterLang = lang;
|
---|
123 | request.filterResultOptions = FRmetadata;
|
---|
124 | request.getParents = getParents;
|
---|
125 | request.filterOptions = options;
|
---|
126 | request.fields = metadata;
|
---|
127 | request.docSet.push_back (OID);
|
---|
128 |
|
---|
129 | assert (collectproto != NULL);
|
---|
130 | collectproto->filter (collection, request, response, err, logout);
|
---|
131 | if (err != noError) {
|
---|
132 | outconvertclass text_t2ascii;
|
---|
133 | logout << text_t2ascii
|
---|
134 | << "Error: call to filter failed for " << OID
|
---|
135 | << " in OIDtools::get_info ("
|
---|
136 | << get_comerror_string (err) << ")\n";
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool get_info (const text_tarray &OIDs, const text_t &collection, const text_t &lang,
|
---|
144 | const text_tset &metadata, bool getParents,
|
---|
145 | recptproto *collectproto, FilterResponse_t &response,
|
---|
146 | ostream &logout) {
|
---|
147 |
|
---|
148 | response.clear();
|
---|
149 | if (OIDs.empty()) return true;
|
---|
150 |
|
---|
151 | comerror_t err = noError;
|
---|
152 | FilterRequest_t request;
|
---|
153 |
|
---|
154 | request.filterName = "NullFilter";
|
---|
155 | request.filterLang = lang;
|
---|
156 | request.filterResultOptions = FRmetadata;
|
---|
157 | request.getParents = getParents;
|
---|
158 | request.fields = metadata;
|
---|
159 |
|
---|
160 | request.docSet = OIDs;
|
---|
161 |
|
---|
162 | collectproto->filter (collection, request, response, err, logout);
|
---|
163 | if (err != noError) {
|
---|
164 | outconvertclass text_t2ascii;
|
---|
165 | logout << text_t2ascii
|
---|
166 | << "Error: call to filter failed in OIDtools::get_info ("
|
---|
167 | << get_comerror_string (err) << ")\n";
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return true;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 | // has_children returns true if OID has children
|
---|
177 | bool has_children (const text_t &OID, const text_t &collection, const text_t &lang,
|
---|
178 | recptproto *collectproto, ostream &logout) {
|
---|
179 |
|
---|
180 | FilterResponse_t response;
|
---|
181 | text_tset metadata;
|
---|
182 | metadata.insert ("haschildren");
|
---|
183 |
|
---|
184 | if (get_info (OID, collection, lang, metadata, false, collectproto, response, logout)) {
|
---|
185 | if (response.docInfo[0].metadata["haschildren"].values[0] == "1")
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | // get_children does a protocol call and returns (in response) the OIDs and
|
---|
193 | // metadata of all the children of OID. The metadata set should be loaded
|
---|
194 | // with whatever metadata fields are to be requested.
|
---|
195 |
|
---|
196 | bool get_children (const text_t &OID, const text_t &collection, const text_t &lang,
|
---|
197 | const text_tset &metadata, bool getParents,
|
---|
198 | recptproto *collectproto, FilterResponse_t &response,
|
---|
199 | ostream &logout) {
|
---|
200 |
|
---|
201 | response.clear();
|
---|
202 |
|
---|
203 | comerror_t err = noError;
|
---|
204 | FilterRequest_t request;
|
---|
205 | OptionValue_t option;
|
---|
206 |
|
---|
207 | option.name = "ParentNode";
|
---|
208 | option.value = OID;
|
---|
209 | request.filterOptions.push_back (option);
|
---|
210 | request.filterName = "BrowseFilter";
|
---|
211 | request.filterLang = lang;
|
---|
212 | request.filterResultOptions = FROID | FRmetadata;
|
---|
213 | request.fields = metadata;
|
---|
214 | request.getParents = getParents;
|
---|
215 |
|
---|
216 | collectproto->filter (collection, request, response, err, logout);
|
---|
217 |
|
---|
218 | if (err != noError) {
|
---|
219 | outconvertclass text_t2ascii;
|
---|
220 | logout << text_t2ascii
|
---|
221 | << "Error: call to filter failed for " << OID
|
---|
222 | << " in OIDtools::get_children ("
|
---|
223 | << get_comerror_string (err) << ")\n";
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // get_parent returns the parent of the document or classification
|
---|
230 | // specified by OID
|
---|
231 | text_t get_parent (text_t OID) {
|
---|
232 |
|
---|
233 | if (OID.empty() || is_top (OID)) return "";
|
---|
234 |
|
---|
235 | text_t::const_iterator begin = OID.begin();
|
---|
236 | text_t::const_iterator here = (OID.end() - 1);
|
---|
237 |
|
---|
238 | while (here >= begin) {
|
---|
239 | OID.pop_back();
|
---|
240 | if (*here == '.') break;
|
---|
241 | here --;
|
---|
242 | }
|
---|
243 | return OID;
|
---|
244 | }
|
---|
245 |
|
---|
246 | // takes an OID like ".2 and replaces the " with parent
|
---|
247 | void translate_parent (text_t &OID, const text_t &parent) {
|
---|
248 |
|
---|
249 | text_t::const_iterator here = OID.begin();
|
---|
250 | text_t::const_iterator end = OID.end();
|
---|
251 | text_t temp;
|
---|
252 |
|
---|
253 | while (here != end) {
|
---|
254 | if (*here == '"') temp += parent;
|
---|
255 | else temp.push_back (*here);
|
---|
256 | ++here;
|
---|
257 | }
|
---|
258 | OID = temp;
|
---|
259 | }
|
---|
260 |
|
---|
261 | // shrink_parent does the opposite to translate_parent
|
---|
262 | void shrink_parent (text_t &OID) {
|
---|
263 |
|
---|
264 | text_tarray tmp;
|
---|
265 | splitchar (OID.begin(), OID.end(), '.', tmp);
|
---|
266 | OID = "\"." + tmp.back();
|
---|
267 | }
|
---|
268 |
|
---|
269 | // checks if OID uses ".fc", ".lc", ".pr", ".ns",
|
---|
270 | // or ".ps" syntax (first child, last child, parent,
|
---|
271 | // next sibling, previous sibling)
|
---|
272 | bool needs_translating (const text_t &OID) {
|
---|
273 |
|
---|
274 | if (OID.size() < 4) return false;
|
---|
275 |
|
---|
276 | text_t tail = substr (OID.end()-3, OID.end());
|
---|
277 | if (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
|
---|
278 | tail == ".ns" || tail == ".ps") return true;
|
---|
279 |
|
---|
280 | return false;
|
---|
281 | }
|
---|
282 |
|
---|
283 | // strips the ".fc", ".lc", ".pr", ".ns",
|
---|
284 | // or ".ps" suffix from the end of OID
|
---|
285 | void strip_suffix (text_t &OID) {
|
---|
286 |
|
---|
287 | text_t tail = substr (OID.end()-3, OID.end());
|
---|
288 | while (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
|
---|
289 | tail == ".ns" || tail == ".ps") {
|
---|
290 | OID.erase (OID.end()-3, OID.end());
|
---|
291 | tail = substr (OID.end()-3, OID.end());
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | static void recurse_contents (ResultDocInfo_t section, const bool &is_classify,
|
---|
296 | const text_t &collection, const text_t &lang,
|
---|
297 | const text_tset &metadata,
|
---|
298 | recptproto *collectproto, FilterResponse_t &response,
|
---|
299 | ostream &logout) {
|
---|
300 |
|
---|
301 | int haschildren = section.metadata["haschildren"].values[0].getint();
|
---|
302 | const text_t &doctype = section.metadata["doctype"].values[0];
|
---|
303 |
|
---|
304 | if ((haschildren == 1) && ((!is_classify) || (doctype == "classify"))) {
|
---|
305 | FilterResponse_t tmp;
|
---|
306 | bool getParents = false;
|
---|
307 | get_children (section.OID, collection, lang, metadata, getParents, collectproto, tmp, logout);
|
---|
308 | ResultDocInfo_tarray::iterator thisdoc = tmp.docInfo.begin();
|
---|
309 | ResultDocInfo_tarray::iterator lastdoc = tmp.docInfo.end();
|
---|
310 | while (thisdoc != lastdoc) {
|
---|
311 | response.docInfo.push_back (*thisdoc);
|
---|
312 | recurse_contents (*thisdoc, is_classify, collection, lang, metadata,
|
---|
313 | collectproto, response, logout);
|
---|
314 | ++thisdoc;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | // get_contents returns OIDs and metadata of all contents
|
---|
320 | // below (and including) OID.
|
---|
321 | void get_contents (const text_t &topOID, const bool &is_classify,
|
---|
322 | text_tset &metadata, const text_t &collection, const text_t &lang,
|
---|
323 | recptproto *collectproto, FilterResponse_t &response,
|
---|
324 | ostream &logout) {
|
---|
325 |
|
---|
326 | if (topOID.empty()) return;
|
---|
327 | response.clear();
|
---|
328 |
|
---|
329 | metadata.insert ("haschildren");
|
---|
330 | metadata.insert ("doctype");
|
---|
331 |
|
---|
332 | // get topOIDs info
|
---|
333 | if (get_info (topOID, collection, lang, metadata, false, collectproto, response, logout)) {
|
---|
334 | recurse_contents (response.docInfo[0], is_classify, collection, lang,
|
---|
335 | metadata, collectproto, response, logout);
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | // is_child_of returns true if OID2 is a child of OID1
|
---|
340 | bool is_child_of(const text_t &OID1, const text_t &OID2) {
|
---|
341 |
|
---|
342 | text_t parent = get_parent(OID2);
|
---|
343 |
|
---|
344 | while (!parent.empty()) {
|
---|
345 | if (parent == OID1) return true;
|
---|
346 | parent = get_parent(parent);
|
---|
347 | }
|
---|
348 | return false;
|
---|
349 | }
|
---|