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.Set;
25  
26  import org.apache.commons.cli2.CommandLine;
27  import org.apache.commons.cli2.Option;
28  import org.apache.commons.cli2.option.PropertyOption;
29  
30  /**
31   * Manages a queue of default CommandLines. This CommandLine implementation is
32   * backed by a queue of CommandLine instances which are queried in turn until a
33   * suitable result is found.
34   *
35   * CommandLine instances can either be added to the back of the queue or can be
36   * pushed in at a specific position.
37   *
38   * @see #appendCommandLine(CommandLine)
39   * @see #insertCommandLine(int, CommandLine)
40   */
41  public class DefaultingCommandLine extends CommandLineImpl {
42  
43      /**
44       * The list of default CommandLine instances
45       */
46      private final List commandLines = new ArrayList();
47  
48      /**
49       * Adds a CommandLine instance to the back of the queue. The supplied
50       * CommandLine will be used as defaults when all other CommandLines produce
51       * no result
52       *
53       * @param commandLine
54       *            the default values to use if all CommandLines
55       */
56      public void appendCommandLine(final CommandLine commandLine) {
57          commandLines.add(commandLine);
58      }
59  
60      /**
61       * Adds a CommandLine instance to a specified position in the queue.
62       *
63       * @param index ths position at which to insert
64       * @param commandLine the CommandLine to insert
65       */
66      public void insertCommandLine(
67          final int index,
68          final CommandLine commandLine) {
69          commandLines.add(index, commandLine);
70      }
71  
72      /**
73       * Builds an iterator over the build in CommandLines.
74       *
75       * @return an unmodifiable iterator
76       */
77      public Iterator commandLines(){
78          return Collections.unmodifiableList(commandLines).iterator();
79      }
80  
81      public Option getOption(String trigger) {
82          for (final Iterator i = commandLines.iterator(); i.hasNext();) {
83              final CommandLine commandLine = (CommandLine)i.next();
84              final Option actual = commandLine.getOption(trigger);
85              if (actual != null) {
86                  return actual;
87              }
88          }
89          return null;
90      }
91  
92      public List getOptions() {
93          final List options = new ArrayList();
94  
95          final List temp = new ArrayList();
96          for (final Iterator i = commandLines.iterator(); i.hasNext();) {
97              final CommandLine commandLine = (CommandLine)i.next();
98              temp.clear();
99              temp.addAll(commandLine.getOptions());
100             temp.removeAll(options);
101             options.addAll(temp);
102         }
103 
104         return Collections.unmodifiableList(options);
105     }
106 
107     public Set getOptionTriggers() {
108         final Set all = new HashSet();
109         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
110             final CommandLine commandLine = (CommandLine)i.next();
111             all.addAll(commandLine.getOptionTriggers());
112         }
113 
114         return Collections.unmodifiableSet(all);
115     }
116 
117     public boolean hasOption(Option option) {
118         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
119             final CommandLine commandLine = (CommandLine)i.next();
120             if (commandLine.hasOption(option)) {
121                 return true;
122             }
123         }
124         return false;
125     }
126 
127     public List getValues(Option option, List defaultValues) {
128         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
129             final CommandLine commandLine = (CommandLine)i.next();
130             final List actual = commandLine.getValues(option);
131             if (actual != null && !actual.isEmpty()) {
132                 return actual;
133             }
134         }
135         if(defaultValues==null){
136             return Collections.EMPTY_LIST;
137         }
138         else{
139             return defaultValues;
140         }
141     }
142 
143     public Boolean getSwitch(Option option, Boolean defaultValue) {
144         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
145             final CommandLine commandLine = (CommandLine)i.next();
146             final Boolean actual = commandLine.getSwitch(option);
147             if (actual != null) {
148                 return actual;
149             }
150         }
151         return defaultValue;
152     }
153 
154     public String getProperty(final String property) {
155         return getProperty(new PropertyOption(), property);
156     }
157 
158     public String getProperty(final Option option, String property, String defaultValue) {
159         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
160             final CommandLine commandLine = (CommandLine)i.next();
161             final String actual = commandLine.getProperty(option, property);
162             if (actual != null) {
163                 return actual;
164             }
165         }
166         return defaultValue;
167     }
168 
169     public Set getProperties(final Option option) {
170         final Set all = new HashSet();
171         for (final Iterator i = commandLines.iterator(); i.hasNext();) {
172             final CommandLine commandLine = (CommandLine)i.next();
173             all.addAll(commandLine.getProperties(option));
174         }
175         return Collections.unmodifiableSet(all);
176     }
177 
178     public Set getProperties() {
179         return getProperties(new PropertyOption());
180     }
181 }