| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 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 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public PropertiesCommandLine(final Option root, final Properties properties){ |
| 61 | 1 | this(root,properties,NUL); |
| 62 | 1 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 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 | |
} |