Ignore:
Timestamp:
2010-06-28T17:28:42+12:00 (14 years ago)
Author:
ak19
Message:

Moved the portion of SimpleCollectionDatabase.translateOID() that is now common to FedoraServiceProxy into util's OID.java class which has a new interface that both SimpleCollectionDB and FedoraServiceProxy now implement in order to use the shared recursive function OID.translateOID and expand it with the interface's processOID() to add their custom code to it for further processing the OID.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/OID.java

    r18503 r22319  
    1919package org.greenstone.gsdl3.util;
    2020
     21import org.apache.log4j.*;
     22
    2123/** utility class to handle greenstone OIDs
    2224 *
     
    2426 */
    2527public class OID {
     28
     29    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.OID.class.getName());
     30
     31    public interface OIDTranslatable {
     32    public String processOID(String doc_id, String top, String suffix, int sibling_num);
     33    }
    2634   
    2735    /** returns everything up to the first dot
     
    102110      return child.startsWith(parent);
    103111    }
     112
     113   
     114  /** translates relative oids into proper oids:
     115   * .pr (parent), .rt (root) .fc (first child), .lc (last child),
     116   * .ns (next sibling), .ps (previous sibling)
     117   * .np (next page), .pp (previous page) : links sections in the order that you'd read the document
     118   * a suffix is expected to be present so test before using
     119   * Other classes can implement the method processOID of interface OIDTranslatable
     120   * to process the oid further to handle siblings.
     121   */
     122    public static String translateOID(OIDTranslatable translator, String oid) {
     123    int p = oid.lastIndexOf('.');
     124    if (p != oid.length()-3) {
     125      logger.info("translateoid error: '.' is not the third to last char!!");
     126      return oid;
     127    }
     128   
     129    String top = oid.substring(0, p);
     130    String suff = oid.substring(p+1);
     131
     132    // just in case we have multiple extensions, we must translate
     133    // we process inner ones first
     134    if (OID.needsTranslating(top)) {
     135    top = OID.translateOID(translator, top);
     136    }
     137    if (suff.equals("pr")) {
     138      return OID.getParent(top);
     139    }
     140    if (suff.equals("rt")) {
     141      return OID.getTop(top);
     142    }
     143    if (suff.equals("np")) {
     144      // try first child
     145
     146      String node_id = OID.translateOID(translator, top+".fc");
     147      if (!node_id.equals(top)) {
     148      return node_id;
     149      }
     150
     151      // try next sibling
     152      node_id = OID.translateOID(translator, top+".ns");
     153      if (!node_id.equals(top)) {
     154      return node_id;
     155      }
     156      // otherwise we keep trying parents sibling
     157      String child_id = top;
     158      String parent_id = OID.getParent(child_id);
     159      while(!parent_id.equals(child_id)) {
     160    node_id = OID.translateOID(translator, parent_id+".ns");
     161    if (!node_id.equals(parent_id)) {
     162      return node_id;
     163    }
     164    child_id = parent_id;
     165    parent_id = OID.getParent(child_id);
     166      }
     167      return top; // we couldn't get a next page, so just return the original
     168    }
     169    if (suff.equals("pp")) {
     170      String prev_sib = OID.translateOID(translator, top+".ps");
     171      if (prev_sib.equals(top)) {
     172    // no previous sibling, so return the parent
     173    return OID.getParent(top);
     174      }
     175      // there is a previous sibling, so it's either this section, or the last child of the last child
     176      String last_child = OID.translateOID(translator, prev_sib+".lc");
     177      while (!last_child.equals(prev_sib)) {
     178    prev_sib = last_child;
     179    last_child = OID.translateOID(translator, prev_sib+".lc");
     180      }
     181      return last_child;
     182    }
     183   
     184    int sibling_num = 0;
     185    if (suff.equals("ss")) {
     186      // we have to remove the sib num before we get top
     187      p = top.lastIndexOf('.');
     188      sibling_num = Integer.parseInt(top.substring(p+1));
     189      top = top.substring(0, p);
     190    }
     191   
     192    // need to get info out of Fedora
     193    String doc_id = top;
     194    if (suff.endsWith("s")) {
     195      doc_id = OID.getParent(top);
     196      if (doc_id.equals(top)) {
     197    // i.e. we are already at the top
     198    return top;
     199      }
     200    }
     201   
     202    return translator.processOID(doc_id, top, suff, sibling_num);
     203
     204  }
    104205}
Note: See TracChangeset for help on using the changeset viewer.