Changeset 29974 for main/trunk/gli


Ignore:
Timestamp:
06/08/15 21:30:46 (5 years ago)
Author:
ak19
Message:

Improving previous commit: 1. now we do a single call to downloadinfo.pl with the describeall flag. Previously, there were two calls, one with the listall flag and then for each download individually. 2. In the previous commit, forgot to update the default value for the mode member var set in the constructor to be dynamic. It had been fixed to Web. I may still need to make sure that the default downloaders are returned in a consistent order, as expected in GLI (Web, MediaWiki, OAI, Z3950, SRW).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/DownloadPane.java

    r29972 r29974  
    101101    list_scroll = getter.getDownloadJobList();     
    102102        list_scroll.setComponentOrientation(Dictionary.getOrientation());
     103
    103104    // TODO should use Vector to store all loaded downloads!!
    104105    String lang = Configuration.getLanguage();
    105106    download_map = new HashMap();
    106107
    107     ArrayList<String> downloaderNamesList = getDownloads(lang);
     108    // run downloadinfo.pl -describeall, load the downloaders into the download_map,
     109    // and get back their list of names, which are of the form "<downloader>Download".
     110    // Store these names in the CONTENTS[] array as "DOWNLOAD.MODE.<downloader>Download",
     111    // with z3950 as a minor exception: DOWNLOAD.MODE.ZDownload.
     112    ArrayList<String> downloaderNamesList = loadDownloadersInfo(lang); 
    108113    int size = downloaderNamesList.size();
    109114    CONTENTS = new String[size];
     
    112117        CONTENTS[i] = "DOWNLOAD.MODE."+downloadName.replace("3950", ""); // A special case is Z3950Download,
    113118                                     // which has to be stored in CONTENTS array as DOWNLOAD.MODE.ZDownload
    114 
    115         String shortName = downloadName.replace("Download", ""); // e.g. "Web"
    116         download_map.put(shortName, loadDownload(downloadName,lang));       
    117119    }
    118120
     
    200202    add(list_scroll);
    201203
    202     mode = "Web";
     204    //set the mode to the first downloader in the list
     205    mode = convertCONTENTStoMode(CONTENTS[0]); // e.g. Web
    203206    generateOptions(options_pane,(Download)download_map.get(mode));
    204207    previous_path = tree.getSelectionPath();
     
    233236    }
    234237
    235     private ArrayList<String> getDownloads(String lang) {
    236     ArrayList downloaders = null;
     238    /** Supporting Functions */
     239    private ArrayList<String> loadDownloadersInfo(String lang) {
    237240    Document document = null;
    238241    Process process = null;
     
    240243    try {
    241244        if (Gatherer.isGsdlRemote) {
    242         String output = Gatherer.remoteGreenstoneServer.getScriptOptions("downloadinfo.pl", "&listall");
     245        String output = Gatherer.remoteGreenstoneServer.getScriptOptions("downloadinfo.pl", "&describeall");
    243246        Reader reader = new StringReader(output);
    244247        document = XMLTools.parseXML(reader);
     
    256259        args_list.add("-S");
    257260        args_list.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadinfo.pl");
    258         args_list.add("-listall");
     261        args_list.add("-describeall");
    259262        args_list.add("-xml");
    260263        args_list.add("-language");
     
    274277    }
    275278    catch (Exception error) {
    276         System.err.println("Failed when trying to parse downloadinfo.pl -listall");
     279        System.err.println("Failed when trying to parse downloadinfo.pl -describeall");
    277280        error.printStackTrace();
    278281    }
     
    282285
    283286    if(document != null) {
    284         //return parseXML(document.getDocumentElement());
    285         Element downloadList = document.getDocumentElement();
    286         int length = -1;
    287         if(downloadList.hasAttribute("length")) {
    288         length = Integer.parseInt(downloadList.getAttribute("length"));
    289         downloaders = new ArrayList<String>(length);       
    290 
    291         for (Node node = downloadList.getFirstChild(); node != null; node = node.getNextSibling()) {
    292             // goes through each <DownloadInfo> of listAll
    293 
    294             String download_name = null;
    295 
    296             for(Node infoNode = node.getFirstChild();
    297             infoNode != null; infoNode = infoNode.getNextSibling()) {
    298 
    299             String node_name = infoNode.getNodeName();
    300             if(node_name.equalsIgnoreCase("Name")) { // <Name>WebDownload</Name>
    301                 download_name = XMLTools.getValue(infoNode);           
    302             }
    303             // skip all the downloaders that are Abstract, as these are pure superclasses
    304             else if(node_name.equalsIgnoreCase("Abstract")) {
    305                 String isAbstract = XMLTools.getValue(infoNode);
     287        return parseXML(document.getDocumentElement());
     288    }
     289
     290    return null;
     291    }
     292
     293    private ArrayList<String> parseXML(Node root) {
     294    ArrayList<String> downloaders = null;
     295    Element downloadList = (Element)root;
     296    int length = -1;
     297    if(downloadList.hasAttribute("length")) {
     298        length = Integer.parseInt(downloadList.getAttribute("length"));
     299        downloaders = new ArrayList<String>(length);       
     300       
     301        for (Node node = downloadList.getFirstChild(); node != null; node = node.getNextSibling()) {
     302        // goes through each <DownloadInfo> of describeAll
     303       
     304        String download_name = null;
     305       
     306        for(Node infoNode = node.getFirstChild();
     307            infoNode != null; infoNode = infoNode.getNextSibling()) {
     308           
     309            String node_name = infoNode.getNodeName();
     310            if(node_name.equalsIgnoreCase("Name")) { // <Name>WebDownload</Name>
     311            download_name = XMLTools.getValue(infoNode); // e.g. WebDownload       
     312            }
     313           
     314            // At this top level of <DownloadInfo> elements,
     315            // skip all the downloaders that are Abstract, as these are pure superclasses       
     316            else if(node_name.equalsIgnoreCase("Abstract")) {
     317            String isAbstract = XMLTools.getValue(infoNode);
     318           
     319            if(isAbstract.equalsIgnoreCase("no") && download_name != null) {
     320                downloaders.add(download_name);
     321                Download downloader = parseDownloadInfoXML(node); // parse the <DownloadInfo> node properly
     322                // now embedded references to abstract superclasses (embedded <DownloadInfo> nodes)
     323                // will be handled
    306324               
    307                 if(isAbstract.equalsIgnoreCase("no") && download_name != null) {
    308                 downloaders.add(download_name);
    309                 }
     325                String shortName = download_name.replace("Download", ""); // e.g. "Web"
     326                download_map.put(shortName, downloader);
    310327            }
    311328            }
     
    317334    }
    318335
    319     /** Supporting Functions */
    320     private Download loadDownload(String download_name, String lang) {
    321     Document document = null;
    322     Process process = null;
    323     try {
    324         if (Gatherer.isGsdlRemote) {
    325         String output = Gatherer.remoteGreenstoneServer.getScriptOptions("downloadinfo.pl", "&downloader="+download_name); //OR: "&listall";
    326         Reader reader = new StringReader(output);
    327         document = XMLTools.parseXML(reader);
    328         }
    329         else {
    330         ArrayList args_list = new ArrayList();
    331         String args[] = null;
    332         if(Configuration.perl_path != null) {
    333             args_list.add(Configuration.perl_path);
    334         } else if(Utility.isWindows()) {
    335             args_list.add("Perl.exe");
    336         } else {
    337             args_list.add("perl");
    338         }
    339         args_list.add("-S");
    340         args_list.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadinfo.pl");
    341         args_list.add("-xml");
    342         args_list.add("-language");
    343         args_list.add(lang);
    344         args_list.add(download_name);
    345        
    346         // Create the process.
    347         args = (String []) args_list.toArray(new String[0]);
    348         Runtime runtime = Runtime.getRuntime();
    349         DebugStream.println("Getting Download Info: "+args_list);
    350         process = runtime.exec(args);
    351        
    352         InputStream input_stream = process.getErrorStream();
    353         document = XMLTools.parseXML(input_stream);
    354         }
    355            
    356     }
    357     catch (Exception error) {
    358         System.err.println("Failed when trying to parse: " + download_name);
    359         error.printStackTrace();
    360     }
    361     finally {
    362         Utility.closeProcess(process);
    363     }
    364 
    365     if(document != null) {
    366         return parseXML(document.getDocumentElement());
    367     }
    368 
    369     return null;
    370     }
    371 
    372     private Download parseXML(Node root) {
     336    private Download parseDownloadInfoXML(Node root) {
     337
    373338    Download download = new Download();
    374339    String node_name = null;
     
    398363        }
    399364            else if(node_name.equalsIgnoreCase("DownloadInfo")) {
    400                 Download super_download = parseXML(node);
     365                Download super_download = parseDownloadInfoXML(node);
    401366        download.setSuper(super_download);
    402367        }
     
    559524        // type has the value DOWNLOAD.MODE.<downloader>Download,
    560525        // mode should then be of the form <downloader>
    561         mode = type.replace("DOWNLOAD.MODE.", "").replace("Download", "");     
     526        mode = convertCONTENTStoMode(type);
    562527        generateOptions(options_pane,(Download)download_map.get(mode));
    563528
     
    569534        }
    570535    }
     536    }
     537
     538    private String convertCONTENTStoMode(String content) {
     539    return content.replace("DOWNLOAD.MODE.", "").replace("ZDownload", "Z3950").replace("Download", "");
    571540    }
    572541
Note: See TracChangeset for help on using the changeset viewer.