Ignore:
Timestamp:
2011-08-29T10:33:36+12:00 (13 years ago)
Author:
sjm84
Message:

Adding in the DocumentBasket functionality and also the new cascading way of handling xsl files which will look in the collect/site/interface/base interface places in that order

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java

    r24458 r24509  
    1919import java.io.FileReader;
    2020import java.io.FileNotFoundException;
     21import java.util.ArrayList;
    2122import java.util.HashMap;
    2223import java.util.Enumeration;
     
    458459        }
    459460
    460         String xslt_file = getXSLTFileName(action, subaction, collection);
    461         if (xslt_file == null)
    462         {
    463             // returning file not found error page to indicate which file is missing
    464             return fileNotFoundErrorPage(xslt_file);
    465         }
    466 
    467         Document style_doc = this.converter.getDOM(new File(xslt_file), "UTF-8");
    468         String errorPage = this.converter.getParseErrorMessage();
    469         if (errorPage != null)
    470         {
    471             return XMLTransformer.constructErrorXHTMLPage("Cannot parse the xslt file: " + xslt_file + "\n" + errorPage);
    472         }
     461        Document style_doc = getXSLTDocument(action, subaction, collection);
    473462        if (style_doc == null)
    474463        {
    475             logger.error(" cant parse the xslt file needed, so returning the original page!");
     464            String errorPage = this.converter.getParseErrorMessage();
     465            if (errorPage != null)
     466            {
     467                return XMLTransformer.constructErrorXHTMLPage("Cannot parse the xslt file\n" + errorPage);
     468            }
    476469            return page;
    477470        }
     
    824817    }
    825818
    826     protected String getXSLTFileName(String action, String subaction, String collection)
    827     {
    828 
     819    protected Document getXSLTDocument(String action, String subaction, String collection)
     820    {
    829821        String name = null;
    830822        if (!subaction.equals(""))
     
    839831        }
    840832        // now find the absolute path
    841         String stylesheet = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, name);
    842         if (stylesheet == null)
     833        ArrayList<File> stylesheets = GSFile.getStylesheetFiles(GlobalProperties.getGSDL3Home(), (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, name);
     834
     835        if (stylesheets.size() == 0)
    843836        {
    844837            logger.info(" Can't find stylesheet for " + name);
    845838        }
    846         logger.debug("Stylesheet: " + stylesheet);
    847         return stylesheet;
     839        logger.debug("Stylesheet: " + name);
     840       
     841        Document finalDoc = this.converter.getDOM(stylesheets.get(stylesheets.size() -1), "UTF-8");
     842        if(finalDoc == null)
     843        {
     844            return null;
     845        }
     846       
     847        for(int i = stylesheets.size() - 2; i >= 0; i--)
     848        {
     849            Document currentDoc = this.converter.getDOM(stylesheets.get(i), "UTF-8");
     850            if(currentDoc == null)
     851            {
     852                return null;
     853            }
     854           
     855            GSXSLT.mergeStylesheets(finalDoc, currentDoc.getDocumentElement());
     856        }
     857       
     858        return finalDoc;
    848859    }
    849860
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSFile.java

    r24248 r24509  
    1919package org.greenstone.gsdl3.util;
    2020
    21 import java.io.File; 
     21import java.io.File;
    2222//import org.apache.soap.encoding.soapenc.Base64;
    2323import org.apache.axis.encoding.Base64;
     
    3636/**
    3737 * GSFile - utility class for Greenstone.
    38  *
    39  * all file paths are created here
    40  * also has file utility methods
    41  *
     38 *
     39 * all file paths are created here also has file utility methods
     40 *
    4241 * @author <a href="mailto:[email protected]">Katherine Don</a>
    4342 * @version $Revision$
     
    4544 */
    4645
    47 public class GSFile {
    48 
    49      static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSFile.class.getName());
    50 
    51     /**  site config file path */
    52     static public String siteConfigFile(String site_home) {
    53     return site_home + File.separatorChar+"siteConfig.xml";
    54 
    55     }
    56     /**  interface config file path */
    57     static public String interfaceConfigFile(String interface_home) {
    58     return interface_home + File.separatorChar+"interfaceConfig.xml";
    59 
    60     }
    61 
    62     /** collection directory path */
    63     static public String collectDir(String site_home) {
    64     return site_home+File.separatorChar+"collect";
    65     }
    66    
    67    
    68     /** collection config file path*/
    69     static public String collectionConfigFile(String site_home,
    70                           String collection_name) {
    71     return collectionConfigFile(collectionBaseDir(site_home, collection_name));
    72     }
    73     static public String collectionConfigFile(String collection_home) {
    74     return collectionEtcDir(collection_home) +
    75             File.separatorChar+"collectionConfig.xml";
    76 
    77     }
    78     /** collection init file path*/
    79     static public String collectionInitFile(String site_home,
    80                         String collection_name) {
    81     return site_home+File.separatorChar+"collect"+
    82             File.separatorChar+collection_name+
    83             File.separatorChar+"etc"+
    84             File.separatorChar+"collectionInit.xml";
    85 
    86     }
    87 
    88     /** collection build config file path*/
    89     static public String collectionBuildConfigFile(String site_home,
    90                            String collection_name ) {
    91     return site_home+File.separatorChar+"collect"+
    92             File.separatorChar+collection_name+
    93             File.separatorChar+"index"+
    94             File.separatorChar+"buildConfig.xml";
    95     }
    96     /** collection build config file path*/
    97     static public String collectionBuildConfigFileBuilding(String site_home,
    98                            String collection_name ) {
    99     return collectionBuildConfigFileBuilding(collectionBaseDir(site_home, collection_name));
    100     }
    101     static public String collectionBuildConfigFileBuilding(String collection_home ) {
    102     return collection_home+
    103         File.separatorChar+"building"+
    104         File.separatorChar+"buildConfig.xml";
    105     }
    106 
    107     /** XML Transform directory path */
    108     static public String xmlTransformDir(String interface_home) {
    109     return interface_home+File.separatorChar+"transform";
    110     }
    111 
    112     /** collection base directory path */   
    113     static public String collectionBaseDir(String site_home,
    114                        String collection_name) {
    115     return site_home+File.separatorChar+"collect"+
    116         File.separatorChar+collection_name;
    117     }
    118     /** collection archive directory path */
    119     static public String collectionArchiveDir(String site_home,
    120                        String collection_name) {
    121     return collectionArchiveDir(collectionBaseDir(site_home, collection_name));
    122     }
    123     static public String collectionArchiveDir(String collection_home) {
    124     return collection_home+File.separatorChar+"archives";
    125     }
    126    
    127     /** collection building directory path */   
    128     static public String collectionBuildDir(String site_home,
    129                        String collection_name) {
    130     return collectionBuildDir(collectionBaseDir(site_home, collection_name));
    131     }
    132     static public String collectionBuildDir(String collection_home) {
    133     return collection_home+File.separator+"building";
    134     }
    135     /** collection building directory path */   
    136     static public String collectionEtcDir(String site_home,
    137                        String collection_name) {
    138     return collectionEtcDir(collectionBaseDir(site_home, collection_name));
    139     }
    140     static public String collectionEtcDir(String collection_home) {
    141     return collection_home+File.separator+"etc";
    142     }
    143 
    144     /** collection building directory path */   
    145     static public String collectionImportDir(String site_home,
    146                        String collection_name) {
    147     return collectionImportDir(collectionBaseDir(site_home, collection_name));
    148        
    149     }
    150     static public String collectionImportDir(String collection_home) {
    151     return collection_home + File.separatorChar+"import";
    152     }
    153     /** collection building directory path */   
    154     static public String collectionIndexDir(String site_home,
    155                        String collection_name) {
    156     return collectionIndexDir(collectionBaseDir(site_home, collection_name));
    157        
    158     }
    159     static public String collectionIndexDir(String collection_home) {
    160     return collection_home + File.separatorChar+"index";
    161     }
    162    
    163     /** text path (for doc retrieval) relative to collectionBaseDir */
    164     static public String collectionTextPath(String index_stem) {
    165     return "index"+File.separatorChar+"text"+File.separatorChar+
    166         index_stem;
    167     }
    168 
    169     /** index path (for querying) relative to collectionBaseDir */
    170     static public String collectionIndexPath(String index_stem,
    171                          String index_name) {
    172     return "index"+File.separatorChar+index_name+File.separatorChar+
    173         index_stem;
    174     }
    175    
    176     /** collection resources directory path */   
    177     static public String collectionResourceDir(String site_home,
    178                            String collection_name) {
    179     return collectionResourceDir(collectionBaseDir(site_home, collection_name));
    180        
    181     }
    182     static public String collectionResourceDir(String collection_home) {
    183     return collection_home + File.separatorChar+"resources";
    184     }
    185 
    186     /** absolute path for an associated file */
    187     static public String assocFileAbsolutePath(String site_home,
    188                            String collection_name,
    189                            String assoc_file_path,
    190                            String filename) {
    191     return collectionBaseDir(site_home, collection_name)+
    192         File.separatorChar+"index"+File.separatorChar+
    193         "assoc"+File.separatorChar+assoc_file_path+
    194         File.separatorChar+filename;
    195     }
    196 
    197     static public String siteHome(String gsdl3_home, String site_name) {
    198     return gsdl3_home + File.separatorChar + "sites" +
    199         File.separatorChar +site_name;
    200     }
    201 
    202     static public String interfaceHome(String gsdl3_home,
    203                        String interface_name) {
    204     return gsdl3_home + File.separatorChar + "interfaces" +
    205         File.separatorChar + interface_name;
    206     }
    207 
    208     static public String interfaceStylesheetFile(String gsdl3_home,
    209                                    String interface_name, String filename) {
    210             return gsdl3_home + File.separatorChar + "interfaces" +
    211                    File.separatorChar + interface_name + File.separatorChar
    212                    + "transform" + File.separatorChar + filename;
    213                 }
    214    
    215     static public String siteStylesheetFile(String site_home,
    216                         String filename) {
    217     return site_home+File.separatorChar + "transform" +
    218         File.separatorChar + filename;
    219     }
    220     static public String collStylesheetFile(String site_home,
    221                         String coll_name,
    222                         String filename) {
    223     return collectionBaseDir(site_home, coll_name) + File.separatorChar+
    224         "transform" + File.separatorChar + filename;
    225     }
     46public class GSFile
     47{
     48
     49    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSFile.class.getName());
     50
     51    /** site config file path */
     52    static public String siteConfigFile(String site_home)
     53    {
     54        return site_home + File.separatorChar + "siteConfig.xml";
     55
     56    }
     57
     58    /** interface config file path */
     59    static public String interfaceConfigFile(String interface_home)
     60    {
     61        return interface_home + File.separatorChar + "interfaceConfig.xml";
     62
     63    }
     64
     65    /** collection directory path */
     66    static public String collectDir(String site_home)
     67    {
     68        return site_home + File.separatorChar + "collect";
     69    }
     70
     71    /** collection config file path */
     72    static public String collectionConfigFile(String site_home, String collection_name)
     73    {
     74        return collectionConfigFile(collectionBaseDir(site_home, collection_name));
     75    }
     76
     77    static public String collectionConfigFile(String collection_home)
     78    {
     79        return collectionEtcDir(collection_home) + File.separatorChar + "collectionConfig.xml";
     80
     81    }
     82
     83    /** collection init file path */
     84    static public String collectionInitFile(String site_home, String collection_name)
     85    {
     86        return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "etc" + File.separatorChar + "collectionInit.xml";
     87
     88    }
     89
     90    /** collection build config file path */
     91    static public String collectionBuildConfigFile(String site_home, String collection_name)
     92    {
     93        return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "buildConfig.xml";
     94    }
     95
     96    /** collection build config file path */
     97    static public String collectionBuildConfigFileBuilding(String site_home, String collection_name)
     98    {
     99        return collectionBuildConfigFileBuilding(collectionBaseDir(site_home, collection_name));
     100    }
     101
     102    static public String collectionBuildConfigFileBuilding(String collection_home)
     103    {
     104        return collection_home + File.separatorChar + "building" + File.separatorChar + "buildConfig.xml";
     105    }
     106
     107    /** XML Transform directory path */
     108    static public String xmlTransformDir(String interface_home)
     109    {
     110        return interface_home + File.separatorChar + "transform";
     111    }
     112
     113    /** collection base directory path */
     114    static public String collectionBaseDir(String site_home, String collection_name)
     115    {
     116        return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name;
     117    }
     118
     119    /** collection archive directory path */
     120    static public String collectionArchiveDir(String site_home, String collection_name)
     121    {
     122        return collectionArchiveDir(collectionBaseDir(site_home, collection_name));
     123    }
     124
     125    static public String collectionArchiveDir(String collection_home)
     126    {
     127        return collection_home + File.separatorChar + "archives";
     128    }
     129
     130    /** collection building directory path */
     131    static public String collectionBuildDir(String site_home, String collection_name)
     132    {
     133        return collectionBuildDir(collectionBaseDir(site_home, collection_name));
     134    }
     135
     136    static public String collectionBuildDir(String collection_home)
     137    {
     138        return collection_home + File.separator + "building";
     139    }
     140
     141    /** collection building directory path */
     142    static public String collectionEtcDir(String site_home, String collection_name)
     143    {
     144        return collectionEtcDir(collectionBaseDir(site_home, collection_name));
     145    }
     146
     147    static public String collectionEtcDir(String collection_home)
     148    {
     149        return collection_home + File.separator + "etc";
     150    }
     151
     152    /** collection building directory path */
     153    static public String collectionImportDir(String site_home, String collection_name)
     154    {
     155        return collectionImportDir(collectionBaseDir(site_home, collection_name));
     156
     157    }
     158
     159    static public String collectionImportDir(String collection_home)
     160    {
     161        return collection_home + File.separatorChar + "import";
     162    }
     163
     164    /** collection building directory path */
     165    static public String collectionIndexDir(String site_home, String collection_name)
     166    {
     167        return collectionIndexDir(collectionBaseDir(site_home, collection_name));
     168
     169    }
     170
     171    static public String collectionIndexDir(String collection_home)
     172    {
     173        return collection_home + File.separatorChar + "index";
     174    }
     175
     176    /** text path (for doc retrieval) relative to collectionBaseDir */
     177    static public String collectionTextPath(String index_stem)
     178    {
     179        return "index" + File.separatorChar + "text" + File.separatorChar + index_stem;
     180    }
     181
     182    /** index path (for querying) relative to collectionBaseDir */
     183    static public String collectionIndexPath(String index_stem, String index_name)
     184    {
     185        return "index" + File.separatorChar + index_name + File.separatorChar + index_stem;
     186    }
     187
     188    /** collection resources directory path */
     189    static public String collectionResourceDir(String site_home, String collection_name)
     190    {
     191        return collectionResourceDir(collectionBaseDir(site_home, collection_name));
     192
     193    }
     194
     195    static public String collectionResourceDir(String collection_home)
     196    {
     197        return collection_home + File.separatorChar + "resources";
     198    }
     199
     200    /** absolute path for an associated file */
     201    static public String assocFileAbsolutePath(String site_home, String collection_name, String assoc_file_path, String filename)
     202    {
     203        return collectionBaseDir(site_home, collection_name) + File.separatorChar + "index" + File.separatorChar + "assoc" + File.separatorChar + assoc_file_path + File.separatorChar + filename;
     204    }
     205
     206    static public String siteHome(String gsdl3_home, String site_name)
     207    {
     208        return gsdl3_home + File.separatorChar + "sites" + File.separatorChar + site_name;
     209    }
     210
     211    static public String interfaceHome(String gsdl3_home, String interface_name)
     212    {
     213        return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name;
     214    }
     215
     216    static public String interfaceStylesheetFile(String gsdl3_home, String interface_name, String filename)
     217    {
     218        return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name + File.separatorChar + "transform" + File.separatorChar + filename;
     219    }
     220
     221    static public String siteStylesheetFile(String site_home, String filename)
     222    {
     223        return site_home + File.separatorChar + "transform" + File.separatorChar + filename;
     224    }
     225
     226    static public String collStylesheetFile(String site_home, String coll_name, String filename)
     227    {
     228        return collectionBaseDir(site_home, coll_name) + File.separatorChar + "transform" + File.separatorChar + filename;
     229    }
     230
     231    /**
     232     * returns the absolute path to a stylesheet stylesheets are looked for in
     233     * the following places, in the following order: current collection, current
     234     * site, current interface, base interfaces returns null if the file cannot
     235     * be found
     236     *
     237     * this is not so good because sites may be on a different computer
     238     */
     239    static public String stylesheetFile(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList base_interfaces, String filename)
     240    {
     241
     242        String site_home = siteHome(gsdl3_home, site_name);
     243        // try collection first
     244        File stylesheet = null;
     245        if (!collection.equals(""))
     246        {
     247
     248            String coll_home = collectionBaseDir(site_home, collection);
     249            stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
     250            if (stylesheet.exists())
     251            {
     252                return stylesheet.getPath();
     253            }
     254        }
     255
     256        // try site one next   
     257        stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
     258        if (stylesheet.exists())
     259        {
     260            return stylesheet.getPath();
     261        }
     262
     263        // try current interface
     264        String interface_home = interfaceHome(gsdl3_home, interface_name);
     265        stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
     266        if (stylesheet.exists())
     267        {
     268            return stylesheet.getPath();
     269        }
     270        // try base interface
     271        if (base_interfaces == null || base_interfaces.size() == 0)
     272        {
     273            return null; // no base interfaces to look for
     274        }
     275        for (int i = 0; i < base_interfaces.size(); i++)
     276        {
     277            interface_home = interfaceHome(gsdl3_home, (String) base_interfaces.get(i));
     278            stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
     279            if (stylesheet.exists())
     280            {
     281                return stylesheet.getPath();
     282            }
     283        }
     284
     285        // still can't find it and we have looked everywhere
     286        return null;
     287    }
    226288   
    227    
    228     /** returns the absolute path to a stylesheet
    229      * stylesheets are looked for in the following places, in the
    230      * following order:
    231      * current collection, current site, current interface, base interfaces
    232      * returns null  if the file cannot be found
    233      *
    234      * this is not so good because sites may be on a different computer */
    235     static public String stylesheetFile(String gsdl3_home,
    236                     String site_name,
    237                     String collection,
    238                     String interface_name,
    239                     ArrayList base_interfaces,
    240                     String filename) {
    241 
    242     String site_home = siteHome(gsdl3_home, site_name);
    243     // try collection first
    244     File stylesheet = null;
    245     if (!collection.equals("")) {
    246        
    247         String coll_home = collectionBaseDir(site_home, collection);
    248         stylesheet = new File(coll_home +File.separatorChar+
    249                   "transform"+File.separatorChar+filename);
    250         if (stylesheet.exists()) {
    251         return stylesheet.getPath();
    252         }
    253     }
    254    
    255     // try site one next   
    256     stylesheet = new File(site_home +File.separatorChar+
    257                   "transform"+File.separatorChar+filename);
    258     if (stylesheet.exists()) {
    259         return stylesheet.getPath();
    260     }
    261    
    262     // try current interface
    263     String interface_home = interfaceHome(gsdl3_home,
    264                           interface_name);
    265     stylesheet = new File(interface_home+File.separatorChar+
    266                   "transform"+File.separatorChar+filename);
    267     if (stylesheet.exists()) {
    268         return stylesheet.getPath();
    269     }
    270     // try base interface
    271     if (base_interfaces==null || base_interfaces.size()==0) {
    272         return null; // no base interfaces to look for
    273     }
    274     for (int i=0; i<base_interfaces.size(); i++) {
    275         interface_home = interfaceHome(gsdl3_home, (String)base_interfaces.get(i));
    276         stylesheet = new File(interface_home+File.separatorChar+
    277                   "transform"+File.separatorChar+filename);
    278         if (stylesheet.exists()) {
    279         return stylesheet.getPath();
    280         }
    281     }
    282        
    283     // still can't find it and we have looked everywhere
    284     return null;
    285     }
    286 
    287 
    288     /** base directory for phind data */
    289     public static String phindBaseDir(String site_home, String coll_name,
    290                       String phind_index) {
    291     return site_home + File.separatorChar +
    292         "collect" + File.separatorChar +
    293         coll_name + File.separatorChar +
    294         "index" + File.separatorChar +
    295         "phind"+phind_index;
    296     }
    297 
    298   /** the collection database file - */
    299     static public String collectionDatabaseFile(String site_home,
    300                         String collection_name,
    301                         String index_stem,
    302                         String database_type) {
    303 
    304         String db_ext = null;
    305     if (database_type.equalsIgnoreCase("jdbm")) {
    306       db_ext = ".jdb";
    307     } else {
    308       // assume gdbm
    309       db_ext = ".gdb";
    310     }
    311        
    312     return site_home + File.separatorChar +
    313         "collect" + File.separatorChar +
    314         collection_name + File.separatorChar +
    315         "index" + File.separatorChar + "text" + File.separatorChar +
    316         index_stem + db_ext;
    317 
    318     }
    319 
    320     // some file utility methods
    321 
    322     /** read in a file and encode it using base64
    323      * encoded data returned as a String */
    324     static public String base64EncodeFromFile(String in_filename) {
    325     byte [] data=null;
    326     try {
    327         data = readFile(in_filename);
    328     } catch (Exception e) {
    329         logger.error("couldn't read the file");
    330     }
    331     String encodedString = Base64.encode(data);
    332     return encodedString;
    333 
    334     }
    335 
    336     /** decode some base64 data, and write it to the specified file */
    337     static public boolean base64DecodeToFile(String data, String out_filename) {
    338     try {
    339         byte[] buffer=Base64.decode(data);
    340         writeFile(buffer, out_filename);
    341        
    342     } catch (Exception e) {
    343         logger.error("file opening/closing errors"+e.getMessage());
    344         return false;
    345     }
    346     return true;
    347    
    348     }
    349 
    350     /** read in a file to a byte array */
    351     public static byte[] readFile(String filename) throws IOException {
    352     File file = new File(filename);
    353     BufferedInputStream bis = new BufferedInputStream(new
    354         FileInputStream(file));
    355     int bytes = (int) file.length();
    356     byte[] buffer = new byte[bytes];
    357     int readBytes = bis.read(buffer);
    358     bis.close();
    359     return buffer;
    360     }
    361 
    362     /** write a byte array to a file */
    363     public static void writeFile(byte [] buffer, String filename) throws IOException {
    364     File file = new File(filename);
    365     BufferedOutputStream bos = new BufferedOutputStream(new
    366         FileOutputStream(file));
    367     bos.write(buffer);
    368     bos.close();
    369     }
    370 
    371     public static boolean deleteFile(File f) {
    372 
    373     if (f.isDirectory()) {
    374         File[] files = f.listFiles();
    375         for (int i=0; files!=null && i<files.length; i++) {
    376         deleteFile(files[i]);
    377         }
    378 
    379     }
    380     // delete the file or directory
    381     return f.delete();
    382     }
    383    
    384     public static boolean copyFile(File source, File destination) {
    385     if (!source.isFile()) {
    386         logger.error(source.getPath()+" is not a file!");
    387         return false;
    388     }
    389     try {
    390         destination.getParentFile().mkdirs();
    391         FileInputStream in = new FileInputStream(source);
    392         FileOutputStream out = new FileOutputStream(destination);
    393         int value = 0;
    394         while((value = in.read()) != -1) {
    395         out.write(value);
    396         }
    397         in.close();
    398         out.close();
    399     } catch (Exception e) {
    400         logger.error("something went wrong copying "+source.getPath()+" to "+destination.getPath());
    401         logger.error("Exception: "+e.getMessage());
    402         return false;
    403     }
    404 
    405     return true;
    406     }
    407     /** Recursively moves the contents of source file to destination,
    408      * maintaining paths.
    409      * @param source A File representing the directory whose contents you
    410      * wish to move.
    411      * @param destination A File representing the directory you wish to move
    412      * files to.
    413      * @return true if successful
    414      */
    415     public static boolean moveDirectory(File source, File destination) {
    416 
    417     // first try rename
    418     if (source.renameTo(destination)) {
    419         return true;
    420     }
    421 
    422     // john T. said that this sometimes doesn't work, so you have to copy files manually
    423 
    424     // copied from gatherer Utility class
    425     File input[] = source.listFiles();
    426     for(int i = 0; i < input.length; i++) {
    427         File output = new File(destination, input[i].getName());
    428         if (input[i].isDirectory()) {
    429         moveDirectory(input[i], output);
    430         } else {
    431         // Copy the file
    432         try {
    433             output.getParentFile().mkdirs();
    434             FileInputStream in = new FileInputStream(input[i]);
    435             FileOutputStream out = new FileOutputStream(output);
    436             int value = 0;
    437             while((value = in.read()) != -1) {
    438             out.write(value);
    439             }
    440             in.close();
    441             out.close();
    442             // Delete file
    443             input[i].delete();
    444         } catch (Exception e) {
    445             logger.error("exception: "+e.getMessage());
    446             return false;
    447         }
    448        
    449         }
    450     }
    451     return true;
    452     }
    453    
     289    static public ArrayList<File> getStylesheetFiles(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList base_interfaces, String filename)
     290    {
     291        ArrayList<File> stylesheets = new ArrayList<File>();
     292        String site_home = siteHome(gsdl3_home, site_name);
     293        // try collection first
     294        File stylesheet = null;
     295        if (!collection.equals(""))
     296        {
     297            String coll_home = collectionBaseDir(site_home, collection);
     298            stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
     299            if (stylesheet.exists())
     300            {
     301                stylesheets.add(stylesheet);
     302            }
     303        }
     304
     305        // try site one next   
     306        stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
     307        if (stylesheet.exists())
     308        {
     309            stylesheets.add(stylesheet);
     310        }
     311
     312        // try current interface
     313        String interface_home = interfaceHome(gsdl3_home, interface_name);
     314        stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
     315        if (stylesheet.exists())
     316        {
     317            stylesheets.add(stylesheet);
     318        }
     319        // try base interface
     320        if (base_interfaces != null && base_interfaces.size() != 0)
     321        {
     322            for (int i = 0; i < base_interfaces.size(); i++)
     323            {
     324                interface_home = interfaceHome(gsdl3_home, (String) base_interfaces.get(i));
     325                stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
     326                if (stylesheet.exists())
     327                {
     328                    stylesheets.add(stylesheet);
     329                }
     330            }
     331        }
     332
     333        return stylesheets;
     334    }
     335
     336    /** base directory for phind data */
     337    public static String phindBaseDir(String site_home, String coll_name, String phind_index)
     338    {
     339        return site_home + File.separatorChar + "collect" + File.separatorChar + coll_name + File.separatorChar + "index" + File.separatorChar + "phind" + phind_index;
     340    }
     341
     342    /** the collection database file - */
     343    static public String collectionDatabaseFile(String site_home, String collection_name, String index_stem, String database_type)
     344    {
     345
     346        String db_ext = null;
     347        if (database_type.equalsIgnoreCase("jdbm"))
     348        {
     349            db_ext = ".jdb";
     350        }
     351        else
     352        {
     353            // assume gdbm
     354            db_ext = ".gdb";
     355        }
     356
     357        return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "text" + File.separatorChar + index_stem + db_ext;
     358
     359    }
     360
     361    // some file utility methods
     362
     363    /**
     364     * read in a file and encode it using base64 encoded data returned as a
     365     * String
     366     */
     367    static public String base64EncodeFromFile(String in_filename)
     368    {
     369        byte[] data = null;
     370        try
     371        {
     372            data = readFile(in_filename);
     373        }
     374        catch (Exception e)
     375        {
     376            logger.error("couldn't read the file");
     377        }
     378        String encodedString = Base64.encode(data);
     379        return encodedString;
     380
     381    }
     382
     383    /** decode some base64 data, and write it to the specified file */
     384    static public boolean base64DecodeToFile(String data, String out_filename)
     385    {
     386        try
     387        {
     388            byte[] buffer = Base64.decode(data);
     389            writeFile(buffer, out_filename);
     390
     391        }
     392        catch (Exception e)
     393        {
     394            logger.error("file opening/closing errors" + e.getMessage());
     395            return false;
     396        }
     397        return true;
     398
     399    }
     400
     401    /** read in a file to a byte array */
     402    public static byte[] readFile(String filename) throws IOException
     403    {
     404        File file = new File(filename);
     405        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
     406        int bytes = (int) file.length();
     407        byte[] buffer = new byte[bytes];
     408        int readBytes = bis.read(buffer);
     409        bis.close();
     410        return buffer;
     411    }
     412
     413    /** write a byte array to a file */
     414    public static void writeFile(byte[] buffer, String filename) throws IOException
     415    {
     416        File file = new File(filename);
     417        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
     418        bos.write(buffer);
     419        bos.close();
     420    }
     421
     422    public static boolean deleteFile(File f)
     423    {
     424
     425        if (f.isDirectory())
     426        {
     427            File[] files = f.listFiles();
     428            for (int i = 0; files != null && i < files.length; i++)
     429            {
     430                deleteFile(files[i]);
     431            }
     432
     433        }
     434        // delete the file or directory
     435        return f.delete();
     436    }
     437
     438    public static boolean copyFile(File source, File destination)
     439    {
     440        if (!source.isFile())
     441        {
     442            logger.error(source.getPath() + " is not a file!");
     443            return false;
     444        }
     445        try
     446        {
     447            destination.getParentFile().mkdirs();
     448            FileInputStream in = new FileInputStream(source);
     449            FileOutputStream out = new FileOutputStream(destination);
     450            int value = 0;
     451            while ((value = in.read()) != -1)
     452            {
     453                out.write(value);
     454            }
     455            in.close();
     456            out.close();
     457        }
     458        catch (Exception e)
     459        {
     460            logger.error("something went wrong copying " + source.getPath() + " to " + destination.getPath());
     461            logger.error("Exception: " + e.getMessage());
     462            return false;
     463        }
     464
     465        return true;
     466    }
     467
     468    /**
     469     * Recursively moves the contents of source file to destination, maintaining
     470     * paths.
     471     *
     472     * @param source
     473     *            A File representing the directory whose contents you wish to
     474     *            move.
     475     * @param destination
     476     *            A File representing the directory you wish to move files to.
     477     * @return true if successful
     478     */
     479    public static boolean moveDirectory(File source, File destination)
     480    {
     481
     482        // first try rename
     483        if (source.renameTo(destination))
     484        {
     485            return true;
     486        }
     487
     488        // john T. said that this sometimes doesn't work, so you have to copy files manually
     489
     490        // copied from gatherer Utility class
     491        File input[] = source.listFiles();
     492        for (int i = 0; i < input.length; i++)
     493        {
     494            File output = new File(destination, input[i].getName());
     495            if (input[i].isDirectory())
     496            {
     497                moveDirectory(input[i], output);
     498            }
     499            else
     500            {
     501                // Copy the file
     502                try
     503                {
     504                    output.getParentFile().mkdirs();
     505                    FileInputStream in = new FileInputStream(input[i]);
     506                    FileOutputStream out = new FileOutputStream(output);
     507                    int value = 0;
     508                    while ((value = in.read()) != -1)
     509                    {
     510                        out.write(value);
     511                    }
     512                    in.close();
     513                    out.close();
     514                    // Delete file
     515                    input[i].delete();
     516                }
     517                catch (Exception e)
     518                {
     519                    logger.error("exception: " + e.getMessage());
     520                    return false;
     521                }
     522
     523            }
     524        }
     525        return true;
     526    }
     527
    454528}
Note: See TracChangeset for help on using the changeset viewer.