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 this(root,properties,NUL);
62 }
63
64
65
66
67
68
69
70
71
72 public PropertiesCommandLine(final Option root, final Properties properties, final char separator){
73 this.root = root;
74 this.properties = properties;
75 this.separator = separator;
76 }
77
78
79 public boolean hasOption(Option option) {
80 if(option==null){
81 return false;
82 }
83 else{
84 return properties.containsKey(option.getPreferredName());
85 }
86 }
87
88 public Option getOption(String trigger) {
89 return root.findOption(trigger);
90 }
91
92 public List getValues(final Option option, final List defaultValues) {
93 final String value = properties.getProperty(option.getPreferredName());
94
95 if(value==null){
96 return defaultValues;
97 }
98 else if(separator>NUL){
99 final List values = new ArrayList();
100 final StringTokenizer tokens = new StringTokenizer(value,String.valueOf(separator));
101
102 while(tokens.hasMoreTokens()){
103 values.add(tokens.nextToken());
104 }
105
106 return values;
107 }
108 else{
109 return Collections.singletonList(value);
110 }
111 }
112
113 public Boolean getSwitch(final Option option, final Boolean defaultValue) {
114 final String value = properties.getProperty(option.getPreferredName());
115 if("true".equals(value)){
116 return Boolean.TRUE;
117 }
118 else if("false".equals(value)){
119 return Boolean.FALSE;
120 }
121 else{
122 return defaultValue;
123 }
124 }
125
126 public String getProperty(final String property) {
127 return getProperty(new PropertyOption(), property);
128 }
129
130 public String getProperty(final Option option, final String property, final String defaultValue) {
131 return properties.getProperty(property,defaultValue);
132 }
133
134 public Set getProperties(final Option option) {
135 return properties.keySet();
136 }
137
138 public Set getProperties() {
139 return getProperties(new PropertyOption());
140 }
141
142 public List getOptions() {
143 final List options = new ArrayList();
144 final Iterator keys = properties.keySet().iterator();
145 while(keys.hasNext()){
146 final String trigger = (String)keys.next();
147 final Option option = root.findOption(trigger);
148 if(option!=null){
149 options.add(option);
150 }
151 }
152 return Collections.unmodifiableList(options);
153 }
154
155 public Set getOptionTriggers() {
156 final Set triggers = new HashSet();
157 final Iterator options = getOptions().iterator();
158 while(options.hasNext()){
159 final Option option = (Option)options.next();
160 triggers.addAll(option.getTriggers());
161 }
162 return Collections.unmodifiableSet(triggers);
163 }
164 }