Changeset 26045


Ignore:
Timestamp:
2012-07-31T15:06:27+12:00 (12 years ago)
Author:
kjdon
Message:

added lots more functions to the BasicDocument classes. Code has come from Browse and Retrieve services

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util
Files:
3 edited

Legend:

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

    r26041 r26045  
    1919package org.greenstone.gsdl3.util;
    2020
     21import java.util.ArrayList;
     22
    2123import org.apache.log4j.*;
    2224import org.w3c.dom.Document;
     
    2426
    2527public abstract class AbstractBasicDocument {
     28
     29  /** info types */
     30    protected static final String INFO_NUM_SIBS = "numSiblings";
     31    protected static final String INFO_NUM_CHILDREN = "numChildren";
     32    protected static final String INFO_SIB_POS = "siblingPosition";
     33    protected static final String INFO_DOC_TYPE = "documentType";
    2634
    2735    /** XML element for describe requests - the container doc */
     
    5967    }
    6068
     69    /**
     70     * adds all the children of doc_id to the doc element, and if
     71     * recursive=true, adds all their children as well
     72     */
     73  abstract public void addDescendants(Element doc, String doc_id, boolean recursive);
     74 
     75
     76    /**
     77     * adds all the siblings of current_id to the parent element. returns the
     78     * new current element
     79     */
     80  abstract public Element addSiblings(Element parent_node, String parent_id, String current_id);
    6181
    6282    /** returns the node type of the specified node.
     
    97117    abstract public boolean hasChildren(String node_id);
    98118   
     119    /**
     120     * returns the structural information asked for. info_type may be one of
     121     * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE
     122     */
     123    abstract public String getStructureInfo(String doc_id, String info_type);
     124
     125  abstract public int getNumChildren(String node_id) ;
     126  /** returns a list of the child ids in order, null if no children
     127   */
     128  abstract public ArrayList<String> getChildrenIds(String node_id);
    99129    /** returns true if the node has a parent
    100130     */
    101131    abstract public boolean hasParent(String node_id);
     132
     133    /**
     134     * returns the node id of the parent node, null if no parent
     135     */
     136  abstract public String getParentId(String node_id);
     137
     138  /**
     139   * returns the node id of the root node of the document containing node_id
     140   */
     141  abstract public String getRootId(String node_id);
     142
     143  /** returns the list of sibling ids, including the specified node_id */
     144  abstract public ArrayList<String> getSiblingIds(String node_id);
    102145}
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/BasicDocument.java

    r26041 r26045  
    1818 */
    1919package org.greenstone.gsdl3.util;
     20
     21import java.util.ArrayList;
    2022
    2123import org.apache.log4j.*;
     
    3941    }
    4042
     43    /**
     44     * returns the structural information asked for. info_type may be one of
     45     * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE
     46     */
     47  public String getStructureInfo(String doc_id, String info_type) {
     48    if (info_type.equals(INFO_NUM_SIBS)) {
     49      return "0";
     50    }
     51    if (info_type.equals(INFO_NUM_CHILDREN)) {
     52      return "0";
     53    }
     54    if (info_type.equals(INFO_SIB_POS)) {
     55      return "-1";
     56    }
     57    if (info_type.equals(INFO_DOC_TYPE)) {
     58      return getDocType(doc_id);
     59    }
     60    return null;
     61  }
    4162    /** returns the document type of the doc that the specified node
    4263    belongs to. should be one of
     
    6283    return false;
    6384    }
    64 
     85  public int getNumChildren(String node_id) {
     86    return 0;
     87  }
     88  /** returns a list of the child ids in order, null if no children
     89   * default implementation: return null
     90   */
     91  public ArrayList<String> getChildrenIds(String node_id) {
     92    return null;
     93  }
    6594    /** returns true if the node has a parent
    6695     * default implementation returns false, over ride if documents can be
     
    6998    return false;
    7099    }
     100
     101    /**
     102     * returns the node id of the parent node, null if no parent
     103     * default implementation return null
     104     */
     105  public String getParentId(String node_id) {
     106    return null;
     107  }
     108
     109  /**
     110   * returns the node id of the root node of the document containing node_id
     111   * default implementation: return node_id
     112   */
     113  public String getRootId(String node_id) {
     114    return node_id;
     115  }
     116    /**
     117     * adds all the children of doc_id to the doc element, and if
     118     * recursive=true, adds all their children as well
     119     * default implementation: do nothing
     120     */
     121  public void addDescendants(Element doc, String doc_id, boolean recursive) {
     122    return;
     123  }
     124
     125    /**
     126     * adds all the siblings of current_id to the parent element. returns the
     127     * new current element
     128     */
     129  public Element addSiblings(Element parent_node, String parent_id, String current_id)
     130  {
     131    return null;
     132  }
     133
     134  /** returns the list of sibling ids, including the specified node_id */
     135  public ArrayList<String> getSiblingIds(String node_id) {
     136    return null;
     137  }
     138
     139 
    71140}
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/BasicDocumentDatabase.java

    r26041 r26045  
    3636import java.util.Map;
    3737import java.util.Set;
     38import java.util.StringTokenizer;
    3839import java.util.Iterator;
    3940import java.io.File;
    4041
     42import org.apache.commons.lang3.StringUtils;
    4143import org.apache.log4j.*;
    4244
     
    136138    }
    137139   
     140    /**
     141     * returns the structural information asked for. info_type may be one of
     142     * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE
     143     */
     144  public String getStructureInfo(String doc_id, String info_type){
     145        String value = "";
     146        if (info_type.equals(INFO_NUM_SIBS))
     147        {
     148            String parent_id = OID.getParent(doc_id);
     149            if (parent_id.equals(doc_id))
     150            {
     151                value = "0";
     152            }
     153            else
     154            {
     155                value = String.valueOf(getNumChildren(parent_id));
     156            }
     157            return value;
     158        }
     159
     160        if (info_type.equals(INFO_NUM_CHILDREN))
     161        {
     162            return String.valueOf(getNumChildren(doc_id));
     163        }
     164
     165        if (info_type.equals(INFO_SIB_POS))
     166        {
     167            String parent_id = OID.getParent(doc_id);
     168            if (parent_id.equals(doc_id))
     169            {
     170                return "-1";
     171            }
     172
     173            DBInfo info = this.coll_db.getInfo(parent_id);
     174            if (info == null)
     175            {
     176                return "-1";
     177            }
     178
     179            String contains = info.getInfo("contains");
     180            contains = StringUtils.replace(contains, "\"", parent_id);
     181            String[] children = contains.split(";");
     182            for (int i = 0; i < children.length; i++)
     183            {
     184                String child_id = children[i];
     185                if (child_id.equals(doc_id))
     186                {
     187                    return String.valueOf(i + 1); // make it from 1 to length
     188
     189                }
     190            }
     191
     192            return "-1";
     193        }
     194        if (info_type.equals(INFO_DOC_TYPE))
     195   
     196        {
     197          return getDocType(doc_id);
     198        }
     199        return null;
     200
     201  }
     202
    138203    /** returns true if the node has child nodes */
    139204    public boolean hasChildren(String node_id){
     
    149214    }
    150215   
     216  public int getNumChildren(String node_id) {
     217        DBInfo info = this.coll_db.getInfo(node_id);
     218        if (info == null)
     219        {
     220            return 0;
     221        }
     222        String contains = info.getInfo("contains");
     223        if (contains.equals(""))
     224        {
     225            return 0;
     226        }
     227        String[] children = contains.split(";");
     228        return children.length;
     229
     230
     231  }
     232  /** returns a list of the child ids in order, null if no children
     233   */
     234  public ArrayList<String> getChildrenIds(String node_id) {
     235        DBInfo info = this.coll_db.getInfo(node_id);
     236        if (info == null)
     237        {
     238            return null;
     239        }
     240
     241        String contains = info.getInfo("contains");
     242        if (contains.equals(""))
     243        {
     244            return null;
     245        }
     246        ArrayList<String> children = new ArrayList<String>();
     247        StringTokenizer st = new StringTokenizer(contains, ";");
     248        while (st.hasMoreTokens())
     249        {
     250            String child_id = StringUtils.replace(st.nextToken(), "\"", node_id);
     251            children.add(child_id);
     252        }
     253        return children;
     254
     255
     256  }
     257
    151258    /** returns true if the node has a parent */
    152259    public boolean hasParent(String node_id){
     
    157264    return true;
    158265    }
     266    /**
     267     * returns the node id of the parent node, null if no parent
     268     */
     269  public String getParentId(String node_id) {
     270        String parent = OID.getParent(node_id);
     271        if (parent.equals(node_id))
     272        {
     273            return null;
     274        }
     275        return parent;
     276
     277  }
    159278   
     279  /**
     280   * returns the node id of the root node of the document containing node_id
     281   */
     282  public String getRootId(String node_id) {
     283    return OID.getTop(node_id);
     284  }
     285
    160286   /** convert indexer internal id to Greenstone oid */
    161287    public String internalNum2OID(long docnum)
     
    169295    }
    170296
     297
     298    /**
     299     * adds all the children of doc_id to the doc element, and if
     300     * recursive=true, adds all their children as well
     301     */
     302    public void addDescendants(Element doc, String doc_id, boolean recursive)
     303    {
     304        ArrayList<String> child_ids = getChildrenIds(doc_id);
     305        if (child_ids == null)
     306            return;
     307        for (int i = 0; i < child_ids.size(); i++)
     308        {
     309            String child_id = child_ids.get(i);
     310            Element child_elem = createDocNode(child_id);
     311            doc.appendChild(child_elem);
     312            if (recursive && (!child_elem.getAttribute(GSXML.NODE_TYPE_ATT).equals(GSXML.NODE_TYPE_LEAF) || child_elem.getAttribute(GSXML.DOC_TYPE_ATT).equals(GSXML.DOC_TYPE_PAGED)))
     313            {
     314                addDescendants(child_elem, child_id, recursive);
     315            }
     316        }
     317    }
     318
     319    /**
     320     * adds all the siblings of current_id to the parent element. returns the
     321     * new current element
     322     */
     323    public Element addSiblings(Element parent_node, String parent_id, String current_id)
     324    {
     325        Element current_node = GSXML.getFirstElementChild(parent_node);//(Element)parent_node.getFirstChild();
     326        if (current_node == null)
     327        {
     328            // create a sensible error message
     329            logger.error(" there should be a first child.");
     330            return null;
     331        }
     332        // remove the current child,- will add it in later in its correct place
     333        parent_node.removeChild(current_node);
     334
     335        // add in all the siblings,
     336        addDescendants(parent_node, parent_id, false);
     337
     338        // find the node that is now the current node
     339        // this assumes that the new node that was created is the same as
     340        // the old one that was removed - we may want to replace the new one
     341        // with the old one.
     342        Element new_current = GSXML.getNamedElement(parent_node, current_node.getNodeName(), GSXML.NODE_ID_ATT, current_id);
     343        return new_current;
     344    }
     345
     346  /** returns the list of sibling ids, including the specified node_id */
     347  public ArrayList<String> getSiblingIds(String node_id){
     348        String parent_id = getParentId(node_id);
     349        if (parent_id == null)
     350        {
     351            return null;
     352        }
     353        return getChildrenIds(parent_id);
     354
     355  }
     356
    171357}
    172358
Note: See TracChangeset for help on using the changeset viewer.