/********************************************************************** * * OIDtools.cpp -- * Copyright (C) 1999 The New Zealand Digital Library Project * * PUT COPYRIGHT NOTICE HERE * * $Id: OIDtools.cpp 406 1999-07-20 03:01:15Z sjboddie $ * *********************************************************************/ /* $Log$ Revision 1.11 1999/07/20 02:59:03 sjboddie get_children now takes a getParents argument Revision 1.10 1999/07/07 05:47:41 sjboddie changed around the way browsetools works Revision 1.9 1999/06/16 23:51:53 sjboddie added a strip_suffix function Revision 1.8 1999/06/16 03:11:25 sjboddie get_info() now takes a getParents argument Revision 1.7 1999/06/16 02:05:23 sjboddie just changed a comment that was confusing me Revision 1.6 1999/05/10 03:40:25 sjboddie lots of changes - slowly getting document action sorted out Revision 1.5 1999/04/30 01:59:37 sjboddie lots of stuff - getting documentaction working (documentaction replaces old browseaction) Revision 1.4 1999/03/29 02:14:25 sjboddie More changes to browseaction Revision 1.3 1999/03/25 03:13:42 sjboddie More library functions for dealing with OIDs. Many of them just return dummy data at present Revision 1.2 1999/03/05 03:53:53 sjboddie fixed some bugs Revision 1.1 1999/03/04 22:38:20 sjboddie Added subjectbrowseaction. - Doesn't do anything yet. */ #include "OIDtools.h" // returns (in top) the top level of OID (i.e. everything // up until the first dot) void get_top (const text_t &OID, text_t &top) { top.clear(); if (OID.empty()) return; text_t::const_iterator begin = OID.begin(); text_t::const_iterator end = OID.end(); top.appendrange (begin, findchar(begin, end, '.')); } // checks if OID is top level (i.e. contains no dots) bool is_top (const text_t &OID) { if (OID.empty()) return true; text_t::const_iterator here = OID.begin(); text_t::const_iterator end = OID.end(); here = findchar (here, end, '.'); if (here == end) return true; return false; } // get_parents_array loads the parents array with all the parents of the // document or classification specified by OID (not including OID itself) void get_parents_array (const text_t &OID, text_tarray &parents) { parents.erase (parents.begin(), parents.end()); text_t::const_iterator here = OID.begin (); text_t::const_iterator end = OID.end (); text_t thisparent; while (here != end) { if (*here == '.') parents.push_back(thisparent); thisparent.push_back(*here); here ++; } } // get_info does a protocol call and returns (in response) the metadata // associated with OID. The metadata array should be loaded with whatever // metadata fields are to be requested. bool get_info (const text_t &OID, const text_t &collection, const text_tarray &metadata, bool getParents, recptproto *collectproto,FilterResponse_t &response, ostream &logout) { response.clear(); comerror_t err; FilterRequest_t request; request.filterName = "NullFilter"; request.filterResultOptions = FRmetadata; request.getParents = getParents; request.fields = metadata; request.docSet.insert (OID); collectproto->filter (collection, request, response, err, logout); if (err != noError) { outconvertclass text_t2ascii; logout << text_t2ascii << "Error: call to filter failed for " << OID << " in OIDtools::get_info (" << get_comerror_string (err) << ")\n"; return false; } return true; } // has_children returns true if OID has children bool has_children (const text_t &OID, const text_t &collection, recptproto *collectproto, ostream &logout) { FilterResponse_t response; text_tarray metadata; metadata.push_back ("haschildren"); if (get_info (OID, collection, metadata, false, collectproto, response, logout)) { if (response.docInfo[0].metadata[0].values[0] == "1") return true; } return false; } // get_children does a protocol call and returns (in response) the OIDs and // metadata of all the children of OID. The metadata array should be loaded // with whatever metadata fields are to be requested. bool get_children (const text_t &OID, const text_t &collection, const text_tarray &metadata, bool getParents, recptproto *collectproto, FilterResponse_t &response, ostream &logout) { response.clear(); comerror_t err; FilterRequest_t request; OptionValue_t option; option.name = "ParentNode"; option.value = OID; request.filterOptions.push_back (option); request.filterName = "BrowseFilter"; request.filterResultOptions = FROID | FRmetadata; request.fields = metadata; request.getParents = getParents; collectproto->filter (collection, request, response, err, logout); if (err != noError) { outconvertclass text_t2ascii; logout << text_t2ascii << "Error: call to filter failed for " << OID << " in OIDtools::get_children (" << get_comerror_string (err) << ")\n"; return false; } return true; } // get_parent returns the parent of the document or classification // specified by OID text_t get_parent (text_t OID) { if (OID.empty() || is_top (OID)) return ""; text_t::const_iterator begin = OID.begin(); text_t::const_iterator here = (OID.end() - 1); while (here >= begin) { OID.pop_back(); if (*here == '.') break; here --; } return OID; } // takes an OID like ".2 and replaces the " with parent void translate_parent (text_t &OID, const text_t &parent) { text_t::const_iterator here = OID.begin(); text_t::const_iterator end = OID.end(); text_t temp; while (here != end) { if (*here == '"') temp += parent; else temp.push_back (*here); here ++; } OID = temp; } // shrink_parent does the opposite to translate_parent void shrink_parent (text_t &OID) { text_tarray tmp; splitchar (OID.begin(), OID.end(), '.', tmp); OID = "\"." + tmp.back(); } // checks if OID uses ".fc", ".lc", ".pr", ".ns", // or ".ps" syntax (first child, last child, parent, // next sibling, previous sibling) bool needs_translating (const text_t &OID) { if (OID.size() < 4) return false; text_t tail = substr (OID.end()-3, OID.end()); if (tail == ".fc" || tail == ".lc" || tail == ".pr" || tail == ".ns" || tail == ".ps") return true; return false; } // strips the ".fc", ".lc", ".pr", ".ns", // or ".ps" suffix from the end of OID void strip_suffix (text_t &OID) { text_t tail = substr (OID.end()-3, OID.end()); while (tail == ".fc" || tail == ".lc" || tail == ".pr" || tail == ".ns" || tail == ".ps") { OID.erase (OID.end()-3, OID.end()); tail = substr (OID.end()-3, OID.end()); } } void recurse_contents (const ResultDocInfo_t section, const bool &classify, int &totalcols, const text_t &collection, const text_tarray &metadata, recptproto *collectproto, FilterResponse_t &response, ostream &logout) { int haschildren = section.metadata[1].values[0].getint(); const text_t &doctype = section.metadata[2].values[0]; int cols; if ((haschildren == 1) && ((!classify) || (doctype == "classify"))) { text_t parent = response.docInfo.back().OID; int parentcols = countchar (parent.begin(), parent.end(), '.'); FilterResponse_t tmp; bool getParents = false; get_children (section.OID, collection, metadata, getParents, collectproto, tmp, logout); ResultDocInfo_tarray::const_iterator thisdoc = tmp.docInfo.begin(); ResultDocInfo_tarray::const_iterator lastdoc = tmp.docInfo.end(); while (thisdoc != lastdoc) { if (((*thisdoc).metadata[2].values[0] != "classify") && (classify)) cols = parentcols + 1; else cols = countchar ((*thisdoc).OID.begin(), (*thisdoc).OID.end(), '.'); if (cols > totalcols) totalcols = cols; response.docInfo.push_back (*thisdoc); recurse_contents (*thisdoc, classify, totalcols, collection, metadata, collectproto, response, logout); thisdoc ++; } } } // get_contents returns OIDs and metadata of all contents // below (and including) OID. // metadata being returned for each is Title, haschildren, // doctype, and hastxt void get_contents (const text_t &topOID, const text_t &classifytype, int &totalcols, const text_t &collection, recptproto *collectproto, FilterResponse_t &response, ostream &logout) { bool classify = false; response.clear(); text_tarray metadata; metadata.push_back ("Title"); metadata.push_back ("haschildren"); metadata.push_back ("doctype"); metadata.push_back ("hastxt"); // we don't want to recurse all the way down through each document // if we're expanding top level contents if (classifytype == "classify") classify = true; // update totalcols totalcols = countchar (topOID.begin(), topOID.end(), '.'); // get topOIDs info if (get_info (topOID, collection, metadata, false, collectproto, response, logout)) recurse_contents (response.docInfo[0], classify, totalcols, collection, metadata, collectproto, response, logout); } // is_child_of returns true if OID2 is a child of OID1 bool is_child_of(const text_t &OID1, const text_t &OID2) { text_t parent = get_parent(OID2); while (!parent.empty()) { if (parent == OID1) return true; parent = get_parent(parent); } return false; }