| 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 | 1 | this(root,preferences,NUL); |
| 66 | 1 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 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 | |
} |