View Javadoc

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.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   * A CommandLine implementation using the Preferences API, useful when
35   * constructing a complex DefaultingCommandLine
36   *
37   * This implementation uses the children of a single preference node to populate
38   * the CommandLine.  Options are keyed from their preferred name and presence in
39   * the Preferences object is taken as presence in the CommandLine.  Argument
40   * values are taken from the Preference value and are optionally separated using
41   * the separator char defined, at construction time.  Switch values can be
42   * specified using a simple value of <code>true</code> or <code>false</code>;
43   * obviously this means that Switches with Arguments are not supported by this
44   * implementation.
45   *
46   * @see java.util.prefs.Preferences
47   * @see org.apache.commons.cli2.commandline.DefaultingCommandLine
48   * @see org.apache.commons.cli2.Option#getPreferredName()
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       * Creates a new PreferencesCommandLine using the specified root Option and
59       * Preferences node.  Argument values will be separated using the char 0.
60       *
61       * @param root the CommandLine's root Option
62       * @param preferences the Preferences node to get values from
63       */
64      public PreferencesCommandLine(final Option root, final Preferences preferences){
65          this(root,preferences,NUL);
66      }
67  
68      /**
69       * Creates a new PreferencesCommandLine using the specified root Option,
70       * Preferences node and value separator.
71       *
72       * @param root the CommandLine's root Option
73       * @param preferences the Preferences node to get values from
74       * @param separator the character to split argument values
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 }