1 /*
2  * @(#) $Id: JSTKAbstractTool.java,v 1.4 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk;
11
12import java.util.*;
13
14public abstract class JSTKAbstractTool {
15    protected static HashMap cmds = new HashMap();  // Keep it accessible by BenchCommand.
16
17    public String progName(){
18        return "java org.jstk..JSTKAbstractTool";
19    }
20    public String briefDescription(){
21        return "should be replaced by concrete tool description";
22    }
23    public String extendedUsageString(){
24        StringBuffer sb = new StringBuffer();
25        Iterator itr = cmds.entrySet().iterator();
26        while (itr.hasNext()){
27            Map.Entry ent = (Map.Entry)itr.next();
28            String key = (String)ent.getKey();
29            JSTKCommand cmd = (JSTKCommand)ent.getValue();
30
31            sb.append("  " + key);
32            int blanksNeeded = 12 - key.length();
33            for (int i = 0; i < blanksNeeded; i++)
34                sb.append(" ");
35            sb.append(cmd.briefDescription() + "\n");
36        }
37        return sb.toString();
38    }
39
40    public void printUsage(){
41        System.out.print(usageString());
42        System.out.flush();
43    }
44
45    public String usageString(){
46        StringBuffer sb = new StringBuffer();
47        sb.append("Description:: \n  " + briefDescription() + "\n");
48        sb.append("Usage:: \n  " + progName() + " <cmd> [<options>]\n");
49        sb.append("Commands:: \n");
50        sb.append(extendedUsageString() + "\n");
51        sb.append("Notes:: \n");
52        sb.append("  -- Type \"" + progName() + " <cmd> help\" to get command specific help.\n");
53        sb.append("  -- Specify option \"-showtime\" to get command execution time. Example:\n");
54        sb.append("       " + progName() + " <command> -showtime\n");
55        return sb.toString();
56    }
57
58    public void printCmdUsage(JSTKCommand cmd, String cmdString){
59        System.out.print(cmdUsageString(cmd, cmdString));
60        System.out.flush();
61    }
62
63    public String cmdUsageString(JSTKCommand cmd, String cmdString){
64        StringBuffer sb = new StringBuffer();
65        sb.append("Description:: \n  " + cmd.briefDescription() + "\n");
66        sb.append("\nUsage:: \n  " + progName() + " " + cmdString + " [<options>]\n\n");
67
68        String[] forms = cmd.useForms();
69        if (forms != null){
70            for (int i = 0; i < forms.length; i++){
71                sb.append("  " + progName() + " " + cmdString + " " + forms[i] + "\n");
72            }
73        }
74
75        sb.append("\nOptions:: \n");
76        sb.append(cmd.optionsDescription() + "\n");
77        String[] uses = cmd.sampleUses();
78        if (uses != null){
79            sb.append("Sample Uses:: \n");
80            for (int i = 0; i < uses.length; i++){
81                sb.append("  " + progName() + " " + cmdString + " " + uses[i] + "\n");
82            }
83        }
84        return sb.toString();
85    }
86
87    protected int execute(String[] args) throws Exception {
88        JSTKOptions opts = new JSTKOptions();
89        if (args.length < 1){       // No argument. Print help message.
90            printUsage();
91            return 1;
92        }
93        String cmdString = args[0];
94        if (cmdString.equals("-h") || cmdString.equals("help") || cmdString.equals("-?")){
95            printUsage();
96            return 1;
97        }
98
99        JSTKCommand cmd = (JSTKCommand)cmds.get(cmdString);
00        if (cmd == null){   // Unknown command.
01            System.out.println("Unknown Command: " + cmdString);
02            printUsage();
03            return 1;
04        }
05
06        if (args.length > 1 && (args[1].equals("-h") || args[1].equals("help") || args[1].equals("-?"))){
07            printCmdUsage(cmd, cmdString);
08            return 1;
09        }
10
11        opts.parse(args, 1);
12        boolean showtime = Boolean.valueOf(opts.get("showtime")).booleanValue();
13        long ts = 0, tt = 0;
14        if (showtime)
15            ts = System.currentTimeMillis();
16
17        JSTKResult result = (JSTKResult)cmd.execute(opts);
18
19        if (showtime)
20            tt = System.currentTimeMillis() - ts;
21        System.out.println(result.getText());
22        if (showtime)
23            System.out.println("Execution Time: " + (float)tt/1000.0 + " secs.");
24        return (result.isSuccess() ? 0 : 1);
25    }
26}