Coverage Report - org.apache.commons.cli2.commandline.PropertiesCommandLine
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesCommandLine
100%
47/47
100%
18/18
2.167
 
 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.Collections;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Properties;
 25  
 import java.util.Set;
 26  
 import java.util.StringTokenizer;
 27  
 
 28  
 import org.apache.commons.cli2.Option;
 29  
 import org.apache.commons.cli2.option.PropertyOption;
 30  
 
 31  
 /**
 32  
  * A CommandLine implementation using a java Properties instance, useful for
 33  
  * constructing a complex DefaultingCommandLine
 34  
  *
 35  
  * Options are keyed from their property name and presence in the Properties
 36  
  * instance is taken as presence in the CommandLine.  Argument values are taken
 37  
  * from the property value and are optionally separated using the separator
 38  
  * char, defined at construction time.  Switch values can be specified using a
 39  
  * simple value of <code>true</code> or <code>false</code>; obviously this means
 40  
  * that Switches with Arguments are not supported by this implementation.
 41  
  *
 42  
  * @see java.util.Properties
 43  
  * @see org.apache.commons.cli2.commandline.DefaultingCommandLine
 44  
  * @see org.apache.commons.cli2.Option#getPreferredName()
 45  
  */
 46  
 public class PropertiesCommandLine extends CommandLineImpl {
 47  
 
 48  
     private static final char NUL = '\0';
 49  
     private final Properties properties;
 50  
     private final Option root;
 51  
     private final char separator;
 52  
 
 53  
     /**
 54  
      * Creates a new PropertiesCommandLine using the specified root Option,
 55  
      * Properties instance.  The character 0 is used as the value separator.
 56  
      *
 57  
      * @param root the CommandLine's root Option
 58  
      * @param properties the Properties instance to get values from
 59  
      */
 60  
     public PropertiesCommandLine(final Option root, final Properties properties){
 61  1
         this(root,properties,NUL);
 62  1
     }
 63  
 
 64  
     /**
 65  
      * Creates a new PropertiesCommandLine using the specified root Option,
 66  
      * Properties instance and value separator.
 67  
      *
 68  
      * @param root the CommandLine's root Option
 69  
      * @param properties the Properties instance to get values from
 70  
      * @param separator the character to split argument values
 71  
      */
 72  40
     public PropertiesCommandLine(final Option root, final Properties properties, final char separator){
 73  40
         this.root = root;
 74  40
         this.properties = properties;
 75  40
         this.separator = separator;
 76  40
     }
 77  
 
 78  
 
 79  
     public boolean hasOption(Option option) {
 80  7
         if(option==null){
 81  2
             return false;
 82  
         }
 83  
         else{
 84  5
             return properties.containsKey(option.getPreferredName());
 85  
         }
 86  
     }
 87  
 
 88  
     public Option getOption(String trigger) {
 89  42
         return root.findOption(trigger);
 90  
     }
 91  
 
 92  
     public List getValues(final Option option, final List defaultValues) {
 93  36
         final String value = properties.getProperty(option.getPreferredName());
 94  
 
 95  36
         if(value==null){
 96  10
             return defaultValues;
 97  
         }
 98  26
         else if(separator>NUL){
 99  22
             final List values = new ArrayList();
 100  22
             final StringTokenizer tokens = new StringTokenizer(value,String.valueOf(separator));
 101  
 
 102  62
             while(tokens.hasMoreTokens()){
 103  40
                 values.add(tokens.nextToken());
 104  
             }
 105  
 
 106  22
             return values;
 107  
         }
 108  
         else{
 109  4
             return Collections.singletonList(value);
 110  
         }
 111  
     }
 112  
 
 113  
     public Boolean getSwitch(final Option option, final Boolean defaultValue) {
 114  11
         final String value = properties.getProperty(option.getPreferredName());
 115  11
         if("true".equals(value)){
 116  6
             return Boolean.TRUE;
 117  
         }
 118  5
         else if("false".equals(value)){
 119  1
             return Boolean.FALSE;
 120  
         }
 121  
         else{
 122  4
             return defaultValue;
 123  
         }
 124  
     }
 125  
 
 126  
     public String getProperty(final String property) {
 127  2
         return getProperty(new PropertyOption(), property);
 128  
     }
 129  
 
 130  
     public String getProperty(final Option option, final String property, final String defaultValue) {
 131  4
         return properties.getProperty(property,defaultValue);
 132  
     }
 133  
 
 134  
         public Set getProperties(final Option option) {
 135  1
         return properties.keySet();
 136  
     }
 137  
 
 138  
     public Set getProperties() {
 139  1
         return getProperties(new PropertyOption());
 140  
     }
 141  
 
 142  
     public List getOptions() {
 143  7
         final List options = new ArrayList();
 144  7
         final Iterator keys = properties.keySet().iterator();
 145  42
         while(keys.hasNext()){
 146  35
             final String trigger = (String)keys.next();
 147  35
             final Option option = root.findOption(trigger);
 148  35
             if(option!=null){
 149  28
                 options.add(option);
 150  
             }
 151  35
         }
 152  7
         return Collections.unmodifiableList(options);
 153  
     }
 154  
 
 155  
     public Set getOptionTriggers() {
 156  1
         final Set triggers = new HashSet();
 157  1
         final Iterator options = getOptions().iterator();
 158  5
         while(options.hasNext()){
 159  4
             final Option option = (Option)options.next();
 160  4
             triggers.addAll(option.getTriggers());
 161  4
         }
 162  1
         return Collections.unmodifiableSet(triggers);
 163  
     }
 164  
 }