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.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
59
60
61
62
63
64 public PreferencesCommandLine(final Option root, final Preferences preferences){
65 this(root,preferences,NUL);
66 }
67
68
69
70
71
72
73
74
75
76 public PreferencesCommandLine(final Option root, final Preferences preferences, final char separator){
77 this.root = root;
78 this.preferences = preferences;
79 this.separator = separator;
80 }
81
82 public boolean hasOption(Option option) {
83 if(option==null){
84 return false;
85 }
86 else{
87 try {
88 return Arrays.asList(preferences.keys()).contains(option.getPreferredName());
89 } catch (BackingStoreException e) {
90 return false;
91 }
92 }
93 }
94
95 public Option getOption(String trigger) {
96 return root.findOption(trigger);
97 }
98
99 public List getValues(final Option option, final List defaultValues) {
100 final String value = preferences.get(option.getPreferredName(),null);
101
102 if(value==null){
103 return defaultValues;
104 }
105 else if(separator>NUL){
106 final List values = new ArrayList();
107 final StringTokenizer tokens = new StringTokenizer(value,String.valueOf(separator));
108
109 while(tokens.hasMoreTokens()){
110 values.add(tokens.nextToken());
111 }
112
113 return values;
114 }
115 else{
116 return Collections.singletonList(value);
117 }
118 }
119
120 public Boolean getSwitch(final Option option, final Boolean defaultValue) {
121 final String value = preferences.get(option.getPreferredName(),null);
122 if("true".equals(value)){
123 return Boolean.TRUE;
124 }
125 else if("false".equals(value)){
126 return Boolean.FALSE;
127 }
128 else{
129 return defaultValue;
130 }
131 }
132
133 public String getProperty(final String property) {
134 return getProperty(new PropertyOption(), property);
135 }
136
137 public String getProperty(final Option option, final String property, final String defaultValue) {
138 return preferences.get(property, defaultValue);
139 }
140
141 public Set getProperties(final Option option) {
142 try {
143 return new HashSet(Arrays.asList(preferences.keys()));
144 } catch (BackingStoreException e) {
145 return Collections.EMPTY_SET;
146 }
147 }
148
149 public Set getProperties() {
150 return getProperties(new PropertyOption());
151 }
152
153 public List getOptions() {
154 try {
155 final List options = new ArrayList();
156 final Iterator keys = Arrays.asList(preferences.keys()).iterator();
157 while (keys.hasNext()) {
158 final String trigger = (String) keys.next();
159 final Option option = root.findOption(trigger);
160 if (option != null) {
161 options.add(option);
162 }
163 }
164 return Collections.unmodifiableList(options);
165 } catch (BackingStoreException e) {
166 return Collections.EMPTY_LIST;
167 }
168 }
169
170 public Set getOptionTriggers() {
171 final Set triggers = new HashSet();
172 final Iterator options = getOptions().iterator();
173 while(options.hasNext()){
174 final Option option = (Option)options.next();
175 triggers.addAll(option.getTriggers());
176 }
177 return Collections.unmodifiableSet(triggers);
178 }
179 }