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.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   * A CommandLine implementation using a java Properties instance, useful for
33   * constructing a complex DefaultingCommandLine
34   *
35   * Options are keyed from their property name and presence in the Properties
36   * instance is taken as presence in the CommandLine.  Argument values are taken
37   * from the property value and are optionally separated using the separator
38   * char, defined at construction time.  Switch values can be specified using a
39   * simple value of <code>true</code> or <code>false</code>; obviously this means
40   * that Switches with Arguments are not supported by this implementation.
41   *
42   * @see java.util.Properties
43   * @see org.apache.commons.cli2.commandline.DefaultingCommandLine
44   * @see org.apache.commons.cli2.Option#getPreferredName()
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       * Creates a new PropertiesCommandLine using the specified root Option,
55       * Properties instance.  The character 0 is used as the value separator.
56       *
57       * @param root the CommandLine's root Option
58       * @param properties the Properties instance to get values from
59       */
60      public PropertiesCommandLine(final Option root, final Properties properties){
61          this(root,properties,NUL);
62      }
63  
64      /**
65       * Creates a new PropertiesCommandLine using the specified root Option,
66       * Properties instance and value separator.
67       *
68       * @param root the CommandLine's root Option
69       * @param properties the Properties instance to get values from
70       * @param separator the character to split argument values
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 }