Changeset 3284


Ignore:
Timestamp:
2002-07-30T09:54:24+12:00 (22 years ago)
Author:
kjdon
Message:

more functionality added along with Test classes

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/util
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSFile.java

    r3235 r3284  
    2020
    2121import java.io.File;
     22import org.apache.soap.encoding.soapenc.Base64;
     23import java.io.BufferedInputStream;
     24import java.io.BufferedOutputStream;
     25import java.io.FileInputStream;
     26import java.io.FileOutputStream;
     27import java.io.InputStream;
     28import java.io.IOException;
    2229
    2330/**
     
    3946
    4047    /** creates a File for the collection directory  */
    41     static public File collectionDirFile(String site_home) {
     48    static public File collectDirFile(String site_home) {
    4249    return new File(site_home+File.separatorChar+"collect");
    4350    }
     51   
    4452   
    4553    /** creates a File for the collection config file */
     
    6674    return gsdl_home+File.separatorChar+"transform";
    6775    }
     76
     77    /** returns the base directory name for a collection */ 
     78    static public String collectionBaseDir(String site_home,
     79                       String collection_name) {
     80    return site_home+File.separatorChar+"collect"+
     81        File.separatorChar+collection_name;
     82    }
     83
     84    /** returns the text path (for doc retrieval) relative to collectionBaseDir */
     85    static public String collectionTextPath(String collection_name) {
     86    return "index"+File.separatorChar+"text"+File.separatorChar+
     87        collection_name;
     88    }
     89
     90    /** returns the index path (for querying) relative to collectionBaseDir */
     91    static public String collectionIndexPath(String collection_name,
     92                         String index_name) {
     93    return "index"+File.separatorChar+index_name+File.separatorChar+
     94        collection_name;
     95    }
     96
     97    /** returns an absolute path for an associated file */
     98    static public String assocFileAbsolutePath(String site_home,
     99                           String collection_name,
     100                           String assoc_file_path,
     101                           String filename) {
     102    return collectionBaseDir(site_home, collection_name)+
     103        File.separatorChar+"index"+File.separatorChar+
     104        "assoc"+File.separatorChar+assoc_file_path+
     105        File.separatorChar+filename;
     106    }
     107                         
     108    static public String base64EncodeFromFile(String in_filename) {
     109    byte [] data=null;
     110    try {
     111        data = readFile(in_filename);
     112    } catch (Exception e) {
     113        System.out.println("couldn't read the file");
     114    }
     115    String encodedString = Base64.encode(data);
     116    return encodedString;
     117
     118    }
     119    static public boolean base64DecodeToFile(String data, String out_filename) {
     120    try {
     121        byte[] buffer=Base64.decode(data);
     122        writeFile(buffer, out_filename);
    68123       
     124    } catch (Exception e) {
     125        System.err.println("file opening/closing errors"+e.getMessage());
     126        return false;
     127    }
     128    return true;
     129   
     130    }
     131
     132    public static byte[] readFile(String filename) throws IOException {
     133    File file = new File(filename);
     134    BufferedInputStream bis = new BufferedInputStream(new
     135        FileInputStream(file));
     136    int bytes = (int) file.length();
     137    byte[] buffer = new byte[bytes];
     138    int readBytes = bis.read(buffer);
     139    bis.close();
     140    return buffer;
     141    }
     142    public static void writeFile(byte [] buffer, String filename) throws IOException {
     143    File file = new File(filename);
     144    BufferedOutputStream bos = new BufferedOutputStream(new
     145        FileOutputStream(file));
     146    bos.write(buffer);
     147    bos.close();
     148    }
    69149
    70150}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/OID.java

    r3257 r3284  
    88   
    99    /** returns everything up to the first dot
    10     if no dot, returns OID */
    11     public static String getTop(String OID) {
    12     int pos = OID.indexOf('.');
     10    if no dot, returns oid */
     11    public static String getTop(String oid) {
     12    int pos = oid.indexOf('.');
    1313    if (pos == -1) {
    14         return OID;
     14        return oid;
    1515    }
    16     return OID.substring(0, pos);
     16    return oid.substring(0, pos);
     17   
    1718    }
    18     /** returns true is OID is top level (ie has no dots) */
    19     public static boolean isTop(String OID) {
    20     return (OID.indexOf('.')==-1);
     19    /** returns true is oid is top level (ie has no dots)
     20     returns false for an empty oid */
     21    public static boolean isTop(String oid) {
     22    if (oid.equals("")) {
     23        return false;
     24    }
     25    return (oid.indexOf('.')==-1);
    2126    }
    2227   
    23     /** returns the parent of OID (everything up to last dot)
    24      returns "" if OID has no parent */
    25     public static String getParent(String OID) {
    26     int pos = OID.lastIndexOf('.');
     28    /** returns the parent of oid (everything up to last dot)
     29     returns "" if oid has no parent */
     30    public static String getParent(String oid) {
     31    int pos = oid.lastIndexOf('.');
    2732    if (pos == -1) {
    2833        return "";
    2934    }
    30     return OID.substring(0, pos);
     35    return oid.substring(0, pos);
    3136    }
    3237
    3338    /** returns the full name - replaces " with parent */
    34     public static String translateParent(String OID, String parent) {
     39    public static String translateParent(String oid, String parent) {
    3540    // this is the behaviour of the original - should only replace first
    3641    //perhaps?
    37     return OID.replaceAll("\"", parent);
     42    return oid.replaceAll("\"", parent);
    3843    }
    3944    /** does the opposite to translate_parent */
    40     public static String shrinkParent(String OID) {
    41     int pos = OID.lastIndexOf('.');
    42     if (pos==-1) return OID;
    43     return "\""+OID.substring(pos);
     45    public static String shrinkParent(String oid) {
     46    int pos = oid.lastIndexOf('.');
     47    if (pos==-1) return oid;
     48    return "\""+oid.substring(pos);
    4449    }
    45     /** returns true if OID uses .fc, .lc, .pr, .ns, .ps */
    46     public static boolean needsTranslating(String OID) {
    47     String tail = OID.substring(OID.length()-3);
     50    /** returns true if oid uses .fc, .lc, .pr, .ns, .ps */
     51    public static boolean needsTranslating(String oid) {
     52    String tail = oid.substring(oid.length()-3);
    4853    return (tail.equals(".fc") || tail.equals(".lc") || tail.equals(".pr")
    4954        || tail.equals(".ns") || tail.equals(".ps"));
    5055    }
    5156    /** strips suffix from end */
    52     public static String stripSuffix(String OID) {
    53     String tail = OID.substring(OID.length()-3);
     57    public static String stripSuffix(String oid) {
     58    String tail = oid.substring(oid.length()-3);
    5459    while (tail.equals(".fc") || tail.equals(".lc") || tail.equals(".pr")
    5560           || tail.equals(".ns") || tail.equals(".ps")) {
    56         OID = OID.substring(0, OID.length()-3);
    57         tail = OID.substring(OID.length()-3);
     61        oid = oid.substring(0, oid.length()-3);
     62        tail = oid.substring(oid.length()-3);
    5863    }
    59     return OID;
     64    return oid;
    6065    }
    61     /** returns true if child is a child of parent */
     66    /** returns true if child is a child of parent
     67     an oid is not a child of itself */
    6268    public static boolean isChildOf(String parent, String child) {
     69    if (parent.equals(child)) {
     70        return false;
     71    }
    6372    return child.startsWith(parent);
    6473    }
Note: See TracChangeset for help on using the changeset viewer.