home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / cmdline / CmdLineApp.class (.txt) next >
Encoding:
Java Class File  |  1999-12-09  |  6.1 KB  |  173 lines

  1. package com.commerceone.util.cmdline;
  2.  
  3. import com.commerceone.util.contract.Contract;
  4. import java.util.Enumeration;
  5. import java.util.Locale;
  6. import java.util.MissingResourceException;
  7. import java.util.ResourceBundle;
  8.  
  9. public abstract class CmdLineApp implements CmdLineAppConstants {
  10.    private ArgumentParser argumentParser = new ArgumentParser(this);
  11.    private String appName;
  12.    protected String remainingMeaning;
  13.    protected String remainingExplainSuccinct;
  14.    protected String remainingExplainVerbose;
  15.    private ResourceBundle displayBundle;
  16.  
  17.    protected void registerOption(String opt, String succinctExplain, String verboseExplain, boolean isBoolean) {
  18.       this.argumentParser.registerOption(opt, succinctExplain, verboseExplain, isBoolean);
  19.    }
  20.  
  21.    protected void registerOption(String opt, String succinctExplain, String verboseExplain, boolean isBoolean, boolean shouldDisplay) {
  22.       this.argumentParser.registerOption(opt, succinctExplain, verboseExplain, isBoolean, shouldDisplay);
  23.    }
  24.  
  25.    public String usageString() {
  26.       String useString = this.displayBundle.getString("usage") + this.getAppName() + " ";
  27.       Enumeration enumKeys = this.argumentParser.getRegisteredValues().elements();
  28.  
  29.       while(enumKeys.hasMoreElements()) {
  30.          Object object = enumKeys.nextElement();
  31.          if (object instanceof RegisteredOption) {
  32.             RegisteredOption option = (RegisteredOption)object;
  33.             if (option.getShouldDisplay()) {
  34.                useString = useString + "[-" + option.getOption() + " " + option.getSuccinctExplanation() + "] ";
  35.             }
  36.          }
  37.       }
  38.  
  39.       if (this.getRemainingMeaning() != null && this.getRemainingSuccinct() != null) {
  40.          useString = useString + "[" + this.getRemainingMeaning() + " " + this.getRemainingSuccinct() + "]\"";
  41.       } else {
  42.          useString = useString + "\"";
  43.       }
  44.  
  45.       return useString;
  46.    }
  47.  
  48.    public CmdLineApp(String name) {
  49.       this.appName = name;
  50.  
  51.       try {
  52.          this.displayBundle = ResourceBundle.getBundle("com.commerceone.util.cmdline.CmdLineAppBundle", Locale.getDefault());
  53.          this.displayBundle.getString("usage");
  54.          this.displayBundle.getString("option");
  55.       } catch (MissingResourceException var3) {
  56.          Contract.require(false, "Should not happen");
  57.          this.displayBundle = new CmdLineAppBundle();
  58.       }
  59.  
  60.    }
  61.  
  62.    public boolean isRegistered(String option) {
  63.       return this.argumentParser.isRegistered(option);
  64.    }
  65.  
  66.    public String helpString() {
  67.       String useString = this.displayBundle.getString("option") + this.getAppName() + "\n";
  68.       int MaxLength = 0;
  69.       Enumeration enumKeys = this.argumentParser.getRegisteredValues().elements();
  70.  
  71.       while(enumKeys.hasMoreElements()) {
  72.          Object object = enumKeys.nextElement();
  73.          if (object instanceof RegisteredOption) {
  74.             RegisteredOption option = (RegisteredOption)object;
  75.             if (option.getShouldDisplay() && MaxLength < option.getOption().length()) {
  76.                MaxLength = option.getOption().length();
  77.             }
  78.          }
  79.       }
  80.  
  81.       Enumeration enumKeys2 = this.argumentParser.getRegisteredValues().elements();
  82.  
  83.       while(enumKeys2.hasMoreElements()) {
  84.          Object object = enumKeys2.nextElement();
  85.          if (object instanceof RegisteredOption) {
  86.             RegisteredOption option = (RegisteredOption)object;
  87.             if (option.getShouldDisplay()) {
  88.                StringBuffer strbuf = new StringBuffer();
  89.                int iPad = MaxLength - option.getOption().length() + 3;
  90.  
  91.                for(int x = 0; x < iPad; ++x) {
  92.                   strbuf.append(' ');
  93.                }
  94.  
  95.                useString = useString + "[-" + option.getOption() + strbuf + option.getSuccinctExplanation() + ": " + option.getVerboseExplanation() + "]\n";
  96.             }
  97.          }
  98.       }
  99.  
  100.       if (this.getRemainingMeaning() != null && this.getRemainingVerbose() != null) {
  101.          useString = useString + "[" + this.getRemainingMeaning() + " " + this.getRemainingVerbose() + "]";
  102.       }
  103.  
  104.       return useString;
  105.    }
  106.  
  107.    protected void registerRemaining(String Meaning, String succinctExplain, String verboseExplain) {
  108.       this.remainingMeaning = Meaning;
  109.       this.remainingExplainSuccinct = succinctExplain;
  110.       this.remainingExplainVerbose = verboseExplain;
  111.    }
  112.  
  113.    public String getAppName() {
  114.       return this.appName;
  115.    }
  116.  
  117.    protected String getRemainingVerbose() {
  118.       return this.remainingExplainVerbose;
  119.    }
  120.  
  121.    protected String getRemainingMeaning() {
  122.       return this.remainingMeaning;
  123.    }
  124.  
  125.    public final void processOptions() throws CmdLineException {
  126.       if (this.argumentParser != null && !this.argumentParser.isEmpty()) {
  127.          Enumeration enumKeys = this.argumentParser.keys();
  128.  
  129.          while(enumKeys.hasMoreElements()) {
  130.             String option = (String)enumKeys.nextElement();
  131.             if (option == null) {
  132.                throw new CmdLineException("BadOptionType", "Options must be of type string");
  133.             }
  134.  
  135.             if (!this.isRegistered(option)) {
  136.                throw new CmdLineInvalidOption(option, "OptionNotFound", "Invalid option specified");
  137.             }
  138.          }
  139.  
  140.          enumKeys = this.argumentParser.keys();
  141.  
  142.          while(enumKeys.hasMoreElements()) {
  143.             String option = (String)enumKeys.nextElement();
  144.             String value = (String)this.argumentParser.get(option);
  145.             if (option == null) {
  146.                throw new CmdLineException("BadOptionType", "Options must be of type string");
  147.             }
  148.  
  149.             if (value == null) {
  150.                throw new CmdLineException("BadValueType", "Values must be of type string");
  151.             }
  152.  
  153.             this.processOption(option, value);
  154.          }
  155.       }
  156.  
  157.    }
  158.  
  159.    public abstract void processOption(String var1, String var2);
  160.  
  161.    public abstract void processApplication(String[] var1);
  162.  
  163.    public final void run(String[] args) throws CmdLineException {
  164.       this.argumentParser.init(args);
  165.       this.processOptions();
  166.       this.processApplication(this.argumentParser.getRemainingArgs());
  167.    }
  168.  
  169.    protected String getRemainingSuccinct() {
  170.       return this.remainingExplainSuccinct;
  171.    }
  172. }
  173.