Coverage Report - org.apache.commons.cli2.commandline.PreferencesCommandLine
 
Classes in this File Line Coverage Branch Coverage Complexity
PreferencesCommandLine
88%
47/53
100%
18/18
2.667
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.cli2.commandline;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Arrays;
 21  
 import java.util.Collections;
 22  
 import java.util.HashSet;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Set;
 26  
 import java.util.StringTokenizer;
 27  
 import java.util.prefs.BackingStoreException;
 28  
 import java.util.prefs.Preferences;
 29  
 
 30  
 import org.apache.commons.cli2.Option;
 31  
 import org.apache.commons.cli2.option.PropertyOption;
 32  
 
 33  
 /**
 34  
  * A CommandLine implementation using the Preferences API, useful when
 35  
  * constructing a complex DefaultingCommandLine
 36  
  *
 37  
  * This implementation uses the children of a single preference node to populate
 38  
  * the CommandLine.  Options are keyed from their preferred name and presence in
 39  
  * the Preferences object is taken as presence in the CommandLine.  Argument
 40  
  * values are taken from the Preference value and are optionally separated using
 41  
  * the separator char defined, at construction time.  Switch values can be
 42  
  * specified using a simple value of <code>true</code> or <code>false</code>;
 43  
  * obviously this means that Switches with Arguments are not supported by this
 44  
  * implementation.
 45  
  *
 46  
  * @see java.util.prefs.Preferences
 47  
  * @see org.apache.commons.cli2.commandline.DefaultingCommandLine
 48  
  * @see org.apache.commons.cli2.Option#getPreferredName()
 49  
  */
 50  
 public class PreferencesCommandLine extends CommandLineImpl {
 51  
 
 52  
     private static final char NUL = '\0';
 53  
     private final Preferences preferences;
 54  
     private final Option root;
 55  
     private final char separator;
 56  
 
 57  
     /**
 58  
      * Creates a new PreferencesCommandLine using the specified root Option and
 59  
      * Preferences node.  Argument values will be separated using the char 0.
 60  
      *
 61  
      * @param root the CommandLine's root Option
 62  
      * @param preferences the Preferences node to get values from
 63  
      */
 64  
     public PreferencesCommandLine(final Option root, final Preferences preferences){
 65  1
         this(root,preferences,NUL);
 66  1
     }
 67  
 
 68  
     /**
 69  
      * Creates a new PreferencesCommandLine using the specified root Option,
 70  
      * Preferences node and value separator.
 71  
      *
 72  
      * @param root the CommandLine's root Option
 73  
      * @param preferences the Preferences node to get values from
 74  
      * @param separator the character to split argument values
 75  
      */
 76  40
     public PreferencesCommandLine(final Option root, final Preferences preferences, final char separator){
 77  40
         this.root = root;
 78  40
         this.preferences = preferences;
 79  40
         this.separator = separator;
 80  40
     }
 81  
 
 82  
     public boolean hasOption(Option option) {
 83  7
         if(option==null){
 84  2
             return false;
 85  
         }
 86  
         else{
 87  
             try {
 88  5
                 return Arrays.asList(preferences.keys()).contains(option.getPreferredName());
 89  0
             } catch (BackingStoreException e) {
 90  0
                 return false;
 91  
             }
 92  
         }
 93  
     }
 94  
 
 95  
     public Option getOption(String trigger) {
 96  42
         return root.findOption(trigger);
 97  
     }
 98  
 
 99  
     public List getValues(final Option option, final List defaultValues) {
 100  36
         final String value = preferences.get(option.getPreferredName(),null);
 101  
 
 102  36
         if(value==null){
 103  10
             return defaultValues;
 104  
         }
 105  26
         else if(separator>NUL){
 106  22
             final List values = new ArrayList();
 107  22
             final StringTokenizer tokens = new StringTokenizer(value,String.valueOf(separator));
 108  
 
 109  62
             while(tokens.hasMoreTokens()){
 110  40
                 values.add(tokens.nextToken());
 111  
             }
 112  
 
 113  22
             return values;
 114  
         }
 115  
         else{
 116  4
             return Collections.singletonList(value);
 117  
         }
 118  
     }
 119  
 
 120  
     public Boolean getSwitch(final Option option, final Boolean defaultValue) {
 121  11
         final String value = preferences.get(option.getPreferredName(),null);
 122  11
         if("true".equals(value)){
 123  6
             return Boolean.TRUE;
 124  
         }
 125  5
         else if("false".equals(value)){
 126  1
             return Boolean.FALSE;
 127  
         }
 128  
         else{
 129  4
             return defaultValue;
 130  
         }
 131  
     }
 132  
 
 133  
     public String getProperty(final String property) {
 134  2
         return getProperty(new PropertyOption(), property);
 135  
     }
 136  
 
 137  
     public String getProperty(final Option option, final String property, final String defaultValue) {
 138  4
         return preferences.get(property, defaultValue);
 139  
     }
 140  
 
 141  
         public Set getProperties(final Option option) {
 142  
         try {
 143  1
             return new HashSet(Arrays.asList(preferences.keys()));
 144  0
         } catch (BackingStoreException e) {
 145  0
             return Collections.EMPTY_SET;
 146  
         }
 147  
     }
 148  
 
 149  
     public Set getProperties() {
 150  1
         return getProperties(new PropertyOption());
 151  
     }
 152  
 
 153  
     public List getOptions() {
 154  
         try {
 155  7
             final List options = new ArrayList();
 156  7
             final Iterator keys = Arrays.asList(preferences.keys()).iterator();
 157  42
             while (keys.hasNext()) {
 158  35
                 final String trigger = (String) keys.next();
 159  35
                 final Option option = root.findOption(trigger);
 160  35
                 if (option != null) {
 161  28
                     options.add(option);
 162  
                 }
 163  35
             }
 164  7
             return Collections.unmodifiableList(options);
 165  0
         } catch (BackingStoreException e) {
 166  0
             return Collections.EMPTY_LIST;
 167  
         }
 168  
     }
 169  
 
 170  
     public Set getOptionTriggers() {
 171  1
         final Set triggers = new HashSet();
 172  1
         final Iterator options = getOptions().iterator();
 173  5
         while(options.hasNext()){
 174  4
             final Option option = (Option)options.next();
 175  4
             triggers.addAll(option.getTriggers());
 176  4
         }
 177  1
         return Collections.unmodifiableSet(triggers);
 178  
     }
 179  
 }