View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  final class Options implements IOptions
27  {
28  
29    enum ManagedOption
30    {
31      PROJECT(false, false), CONFIGURATION(false, false), SDK(false, false), TARGET(false, false), SHOWBUILDSETTINGS(
32            "showBuildSettings", false, true),
33      DRY_RUN("dry-run", false, true), SHOWSDKS(false, true), VERSION(false, true), LIST(false, true), USAGE(false, true), HELP(
34            false, true), LICENSE(false, true), XCCONFIG(false, false);
35  
36      static ManagedOption forName(String name)
37      {
38        for (ManagedOption value : values()) {
39          if (value.name().equals(name)) {
40            return value;
41          }
42        }
43        return null;
44      }
45  
46      private String name;
47      private final boolean required;
48      private final boolean emptyValue;
49  
50      ManagedOption(boolean required, boolean emptyValue)
51      {
52        this(null, required, emptyValue);
53      }
54  
55      ManagedOption(String name, boolean required, boolean emptyValue)
56      {
57        this.name = name;
58        this.required = required;
59        this.emptyValue = emptyValue;
60      }
61  
62      boolean isRequired()
63      {
64        return required;
65      }
66  
67      String getOptionName()
68      {
69        return name == null ? name().toLowerCase() : name;
70      }
71  
72      boolean hasEmptyValue()
73      {
74        return emptyValue;
75      }
76    }
77  
78    private final Map<String, String> userOptions, managedOptions;
79  
80    Options(Map<String, String> userOptions, Map<String, String> managedOptions)
81    {
82  
83      if (userOptions == null)
84        this.userOptions = Collections.emptyMap();
85      else
86        this.userOptions = Collections.unmodifiableMap(new HashMap<String, String>(userOptions));
87  
88      if (managedOptions == null)
89        this.managedOptions = Collections.emptyMap();
90      else
91        this.managedOptions = Collections.unmodifiableMap(new HashMap<String, String>(managedOptions));
92  
93      validateManagedOptions(this.managedOptions);
94  
95      validateUserOptions(this.userOptions);
96  
97      if(null == this.userOptions.get("scheme") && this.managedOptions.get(ManagedOption.PROJECT.getOptionName() )== null){
98  	throw new IllegalOptionException(ManagedOption.PROJECT,"managed option \"project\" or user option \"scheme\" is not available");
99      }
100   }
101 
102   public Map<String, String> getUserOptions()
103   {
104     return userOptions;
105   }
106 
107   public Map<String, String> getManagedOptions()
108   {
109     return managedOptions;
110   }
111 
112   /* (non-Javadoc)
113    * @see com.sap.prd.mobile.ios.mios.IOptions#getAllOptions()
114    */
115   @Override
116   public Map<String, String> getAllOptions()
117   {
118     final Map<String, String> result = new HashMap<String, String>();
119 
120     result.putAll(getUserOptions());
121     result.putAll(getManagedOptions());
122 
123     return result;
124   }
125 
126   /**
127    * @param userOptions
128    *          to be validated.
129    * @return the passed in userOptions if validation passed without exception
130    * @throws IllegalArgumentException
131    *           if the userOptions contain a key of an XCode option that is managed by the plugin.
132    */
133   private final static Map<String, String> validateUserOptions(Map<String, String> userOptions)
134   {
135 
136     for (ManagedOption option : ManagedOption.values()) {
137       if (userOptions.keySet().contains(option.getOptionName()))
138         throw new IllegalOptionException(option, "XCode Option '" + option.getOptionName()
139               + "' is managed by the plugin and cannot be modified by the user.");
140     }
141 
142     return userOptions;
143   }
144 
145   private final static Map<String, String> validateManagedOptions(Map<String, String> managedOptions)
146   {
147 
148     for (ManagedOption option : ManagedOption.values()) {
149 
150       if (option.isRequired() && !managedOptions.containsKey(option.getOptionName()))
151         throw new IllegalOptionException(option, "Required option '" + option.getOptionName()
152               + "' was not available inside the managed options.");
153 
154       if (!managedOptions.containsKey(option.getOptionName()))
155         continue;
156 
157       final String value = managedOptions.get(option.getOptionName());
158 
159       if (!option.hasEmptyValue() && (value == null || value.trim().isEmpty()))
160         throw new IllegalOptionException(option, "Invalid option: " + option.getOptionName()
161               + " must be provided with a value.");
162       if (option.hasEmptyValue() && (value != null && value.trim().isEmpty()))
163         throw new IllegalOptionException(option, "Invalid option: " + option.getOptionName()
164               + " must not be provided with a value.");
165 
166     }
167 
168     for (String key : managedOptions.keySet()) {
169       if (ManagedOption.forName(key.toUpperCase()) == null)
170         throw new IllegalArgumentException("Option '" + key
171               + "' is not managed by the plugin. This option must not be provided as managed option.");
172     }
173 
174     return managedOptions;
175   }
176 
177   @Override
178   public String toString()
179   {
180     final String ls = System.getProperty("line.separator");
181     StringBuffer buffer = new StringBuffer();
182     for (Map.Entry<String, String> entry : getAllOptions().entrySet()) {
183       buffer.append(" -").append(entry.getKey()).append(" ").append(entry.getValue() == null ? "" : entry.getValue())
184         .append(ls);
185     }
186     return buffer.toString();
187   }
188 
189   @Override
190   public int hashCode()
191   {
192     final int prime = 31;
193     int result = 1;
194     result = prime * result + ((managedOptions == null) ? 0 : managedOptions.hashCode());
195     result = prime * result + ((userOptions == null) ? 0 : userOptions.hashCode());
196     return result;
197   }
198 
199   @Override
200   public boolean equals(Object obj)
201   {
202     if (this == obj) return true;
203     if (obj == null) return false;
204     if (getClass() != obj.getClass()) return false;
205     Options other = (Options) obj;
206     if (managedOptions == null) {
207       if (other.managedOptions != null) return false;
208     }
209     else if (!managedOptions.equals(other.managedOptions)) return false;
210     if (userOptions == null) {
211       if (other.userOptions != null) return false;
212     }
213     else if (!userOptions.equals(other.userOptions)) return false;
214     return true;
215   }
216 
217   static class IllegalOptionException extends IllegalArgumentException
218   {
219 
220     private static final long serialVersionUID = -3298815948503432790L;
221 
222     private ManagedOption violated;
223 
224     IllegalOptionException(ManagedOption vialated, String message)
225     {
226       super(message);
227       this.violated = vialated;
228     }
229 
230     ManagedOption getViolated()
231     {
232       return violated;
233     }
234   }
235 }