Changeset 29972 for main/trunk/gli


Ignore:
Timestamp:
2015-06-08T19:31:20+12:00 (9 years ago)
Author:
ak19
Message:

More dynamic processing of downloaders. GLI used to use a fixed set of downloaders, now downloadinfo.pl is first called with the listall flag to get all the downloaders that are non-abstract, then these are added to the interface.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
2 edited

Legend:

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

    r24287 r29972  
    7373    static final private Dimension LABEL_SIZE = new Dimension(225, 25);
    7474    static final private Dimension TREE_SIZE = new Dimension(150, 500);
    75     static final private String CONTENTS[] = { "DOWNLOAD.MODE.WebDownload", "DOWNLOAD.MODE.MediaWikiDownload", "DOWNLOAD.MODE.OAIDownload", "DOWNLOAD.MODE.ZDownload" , "DOWNLOAD.MODE.SRWDownload"};
     75    //static final private String CONTENTS[] = { "DOWNLOAD.MODE.WebDownload", "DOWNLOAD.MODE.MediaWikiDownload", "DOWNLOAD.MODE.OAIDownload", "DOWNLOAD.MODE.ZDownload" , "DOWNLOAD.MODE.SRWDownload"};
     76    private String CONTENTS[] = null;
    7677
    7778    private boolean download_button_enabled = false;
     
    9697        this.setComponentOrientation(Dictionary.getOrientation());
    9798    // TODO: Download the WDownload and the download panel fixed!!
    98         getter = new DownloadScrollPane();
     99    getter = new DownloadScrollPane();
    99100        getter.start();
    100101    list_scroll = getter.getDownloadJobList();     
     
    103104    String lang = Configuration.getLanguage();
    104105    download_map = new HashMap();
    105     download_map.put("Web", loadDownload("WebDownload",lang));
    106     download_map.put("MediaWiki", loadDownload("MediaWikiDownload",lang));
    107     download_map.put("OAI", loadDownload("OAIDownload",lang));
    108     download_map.put("Z3950", loadDownload("Z3950Download",lang));
    109     download_map.put("SRW", loadDownload("SRWDownload",lang));
     106
     107    ArrayList<String> downloaderNamesList = getDownloads(lang);
     108    int size = downloaderNamesList.size();
     109    CONTENTS = new String[size];
     110    for(int i = 0; i < size; i++) {
     111        String downloadName = downloaderNamesList.get(i); // e.g. "WebDownload"     
     112        CONTENTS[i] = "DOWNLOAD.MODE."+downloadName.replace("3950", ""); // A special case is Z3950Download,
     113                                     // 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));       
     117    }
    110118
    111119    // Creation
     
    225233    }
    226234
     235    private ArrayList<String> getDownloads(String lang) {
     236    ArrayList downloaders = null;
     237    Document document = null;
     238    Process process = null;
     239   
     240    try {
     241        if (Gatherer.isGsdlRemote) {
     242        String output = Gatherer.remoteGreenstoneServer.getScriptOptions("downloadinfo.pl", "&listall");
     243        Reader reader = new StringReader(output);
     244        document = XMLTools.parseXML(reader);
     245        }
     246        else {
     247        ArrayList args_list = new ArrayList();
     248        String args[] = null;
     249        if(Configuration.perl_path != null) {
     250            args_list.add(Configuration.perl_path);
     251        } else if(Utility.isWindows()) {
     252            args_list.add("Perl.exe");
     253        } else {
     254            args_list.add("perl");
     255        }
     256        args_list.add("-S");
     257        args_list.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadinfo.pl");
     258        args_list.add("-listall");
     259        args_list.add("-xml");
     260        args_list.add("-language");
     261        args_list.add(lang);       
     262       
     263        // Create the process.
     264        args = (String []) args_list.toArray(new String[0]);
     265        Runtime runtime = Runtime.getRuntime();
     266        DebugStream.println("Getting Download Info: "+args_list);
     267        process = runtime.exec(args);
     268       
     269        InputStream input_stream = process.getErrorStream();
     270        document = XMLTools.parseXML(input_stream);
     271        }
     272
     273           
     274    }
     275    catch (Exception error) {
     276        System.err.println("Failed when trying to parse downloadinfo.pl -listall");
     277        error.printStackTrace();
     278    }
     279    finally {
     280        Utility.closeProcess(process);
     281    }
     282
     283    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);
     306               
     307                if(isAbstract.equalsIgnoreCase("no") && download_name != null) {
     308                downloaders.add(download_name);
     309                }
     310            }
     311            }
     312        }
     313        }
     314    }
     315
     316    return downloaders;
     317    }
     318
    227319    /** Supporting Functions */
    228320    private Download loadDownload(String download_name, String lang) {
    229321    Document document = null;
    230    
     322    Process process = null;
    231323    try {
    232324        if (Gatherer.isGsdlRemote) {
     
    256348        Runtime runtime = Runtime.getRuntime();
    257349        DebugStream.println("Getting Download Info: "+args_list);
    258         Process process = runtime.exec(args);
     350        process = runtime.exec(args);
    259351       
    260352        InputStream input_stream = process.getErrorStream();
    261353        document = XMLTools.parseXML(input_stream);
    262354        }
    263 
    264355           
    265356    }
     
    267358        System.err.println("Failed when trying to parse: " + download_name);
    268359        error.printStackTrace();
     360    }
     361    finally {
     362        Utility.closeProcess(process);
    269363    }
    270364
     
    462556        String type = (String)node.getUserObject();
    463557        Gatherer.g_man.wait(true);
    464         if(type == CONTENTS[0]) {
    465             mode = "Web";
    466             generateOptions(options_pane,(Download)download_map.get(mode));
    467         }
    468         else if(type == CONTENTS[1]) {
    469             mode = "MediaWiki";
    470             generateOptions(options_pane,(Download)download_map.get(mode));
    471         }
    472         else if(type == CONTENTS[2]) {
    473             mode = "OAI";
    474             generateOptions(options_pane,(Download)download_map.get(mode));
    475         }
    476         else if(type == CONTENTS[3]) {
    477             mode = "Z3950";
    478             generateOptions(options_pane,(Download)download_map.get(mode));
    479         }
    480         else if(type == CONTENTS[4]) {
    481             mode = "SRW";
    482             generateOptions(options_pane,(Download)download_map.get(mode));
    483         }
     558
     559        // type has the value DOWNLOAD.MODE.<downloader>Download,
     560        // mode should then be of the form <downloader>
     561        mode = type.replace("DOWNLOAD.MODE.", "").replace("Download", "");     
     562        generateOptions(options_pane,(Download)download_map.get(mode));
     563
    484564        tree.setSelectionPath(path);
    485565        previous_path = path;
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r29299 r29972  
    584584    }
    585585    }
     586
     587    public static void closeProcess(Process prcs) {
     588    if( prcs != null ) {
     589        closeResource(prcs.getErrorStream());
     590        closeResource(prcs.getOutputStream());
     591        closeResource(prcs.getInputStream());
     592        prcs.destroy();
     593    }
     594    }
    586595}
Note: See TracChangeset for help on using the changeset viewer.