1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.tool;
  28 
  29 import java.util.HashMap;
  30 import java.util.Iterator;
  31 import java.util.Map;
  32 import java.util.TreeMap;
  33 
  34 import com.sun.javatest.util.HelpTree;
  35 import com.sun.javatest.util.I18NResourceBundle;
  36 import com.sun.javatest.util.StringArray;
  37 
  38 /**
  39  * A command supporting an extensible set of boolean options.
  40  * Valid options must be registered with the addOption method.
  41  * The current setting of the various options can be obtained from
  42  * the current CommandContext.
  43  */
  44 public class VerboseCommand extends Command
  45 {
  46     /**
  47      * Add a option to the set recognized by this command.
  48      * @param name the name of the option to be accepted.
  49      * @param node the help node for this option
  50      */
  51     public static void addOption(String name, HelpTree.Node node) {
  52         ensureAllOptionsInitialized();
  53         allOptions.put(name, node);
  54     }
  55 
  56     /**
  57      * The name of the default option. If the verbose command is given without
  58      * options, then CommandContext.getVerboseOptionValue(DEFAULT) will be set true.
  59      */
  60     private static final String DEFAULT = "default";
  61     private static final String MAX = "max";
  62     private static final String QUIET = "quiet";
  63     private static final String DATE = "date";
  64     private static final String NO_PREFIX = "no-";
  65 
  66     private static void ensureAllOptionsInitialized() {
  67         if (allOptions == null) {
  68             allOptions = new TreeMap<>();
  69             allOptions.put(MAX,  new HelpTree.Node(i18n, "verb.max"));
  70             allOptions.put(QUIET, new HelpTree.Node(i18n, "verb.quiet"));
  71         }
  72     }
  73     private static Map<String, HelpTree.Node> allOptions;
  74 
  75     //--------------------------------------------------------------------------
  76 
  77 
  78     VerboseCommand(String cmd) throws Fault {
  79         super(cmd);
  80 
  81         int chop = cmd.indexOf(CMD);
  82 
  83         if (chop == -1)
  84             throw new IllegalArgumentException();
  85 
  86         String workstr = cmd.substring(chop + CMD.length());
  87 
  88         if (workstr.length() == 0) {
  89             optionValues.put(DEFAULT, Boolean.TRUE);
  90         }
  91         else if (workstr.charAt(0) == ':') {
  92             // rm colon
  93             workstr = workstr.substring(1);
  94             processOptions(workstr);
  95         }
  96         else
  97             throw new Fault(i18n, "verb.badOpts");
  98     }
  99 
 100     private void processOptions(String ops) throws Fault {
 101         ensureAllOptionsInitialized();
 102         String[] items = StringArray.splitList(ops, ",");
 103 
 104         if (items == null)
 105             throw new Fault(i18n, "verb.noOpts");
 106 
 107         for (int i = 0; i < items.length; i++) {
 108             String item = items[i];
 109             boolean on;
 110             if (item.startsWith(NO_PREFIX)) {
 111                 on = false;
 112                 item = item.substring(NO_PREFIX.length());
 113             }
 114             else
 115                 on = true;
 116 
 117             if (!allOptions.containsKey(item.toLowerCase()))
 118                 throw new Fault(i18n, "verb.badOpt", item);
 119 
 120             optionValues.put(item, new Boolean(on));
 121         }
 122     }
 123 
 124     public void run(CommandContext ctx) throws Fault {
 125         for (Map.Entry<String, Boolean> e : optionValues.entrySet()) {
 126             String name = (e.getKey());
 127             boolean value = e.getValue().booleanValue();
 128             if (name.equalsIgnoreCase(MAX))
 129                 ctx.setVerboseMax(value);
 130             else if (name.equalsIgnoreCase(QUIET))
 131                 ctx.setVerboseQuiet(value);
 132             else if (name.equalsIgnoreCase(DATE))
 133                 ctx.setVerboseTimestampEnabled(value);
 134             else
 135                 ctx.setVerboseOptionValue(name, value);
 136         }
 137     }
 138 
 139     static HelpTree.Node getHelp() {
 140         ensureAllOptionsInitialized();
 141 
 142         HelpTree.Node[] nodes = new HelpTree.Node[allOptions.size()];
 143         int i = 0;
 144         for (Iterator<HelpTree.Node> iter = allOptions.values().iterator(); iter.hasNext(); )
 145             nodes[i++] = iter.next();
 146 
 147         return new HelpTree.Node(i18n, "verb", nodes);
 148     }
 149 
 150     static String getName() {
 151         return CMD;
 152     }
 153 
 154     private Map<String, Boolean> optionValues = new HashMap<>();
 155 
 156     private static final String CMD = "verbose";
 157     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(VerboseCommand.class);
 158 }