1:
17: package ;
18:
19: import ;
20: import ;
21: import ;
22: import ;
23: import ;
24: import ;
25: import ;
26: import ;
27:
28: import ;
29: import ;
30: import ;
31: import ;
32: import ;
33:
34:
41: public class WriteableCommandLineImpl extends CommandLineImpl implements WriteableCommandLine
42: {
43: private final Properties m_properties = new Properties();
44: private final List m_options = new ArrayList();
45: private final Map m_nameToOption = new HashMap();
46: private final Map m_values = new HashMap();
47: private final Map m_switches = new HashMap();
48: private final Map m_defaultValues = new HashMap();
49: private final Map m_defaultSwitches = new HashMap();
50: private final List m_normalised;
51: private final Set m_prefixes;
52:
53:
60: public WriteableCommandLineImpl(
61: final Option rootOption, final List arguments )
62: {
63: m_prefixes = rootOption.getPrefixes();
64: m_normalised = arguments;
65: }
66:
67:
71: public void addOption( Option option )
72: {
73: m_options.add( option );
74: m_nameToOption.put( option.getPreferredName(), option );
75: for( Iterator i = option.getTriggers().iterator(); i.hasNext();)
76: {
77: m_nameToOption.put( i.next(), option );
78: }
79: }
80:
81:
86: public void addValue( final Option option, final Object value )
87: {
88: if( option instanceof Argument )
89: {
90: addOption( option );
91: }
92: List valueList = (List) m_values.get( option );
93: if( valueList == null )
94: {
95: valueList = new ArrayList();
96: m_values.put( option, valueList );
97: }
98: valueList.add( value );
99: }
100:
101:
106: public void addSwitch( final Option option, final boolean value )
107: {
108: addOption( option );
109: if( m_switches.containsKey( option ) )
110: {
111: throw new IllegalStateException(
112: ResourceHelper.getResourceHelper().getMessage(
113: ResourceConstants.SWITCH_ALREADY_SET ) );
114: }
115: else
116: {
117: if( value )
118: {
119: m_switches.put( option, Boolean.TRUE );
120: }
121: else
122: {
123: m_switches.put( option, Boolean.FALSE );
124: }
125: }
126: }
127:
128:
134: public boolean hasOption( final Option option )
135: {
136: return m_options.contains( option );
137: }
138:
139:
145: public Option getOption( final String trigger )
146: {
147: return (Option) m_nameToOption.get( trigger );
148: }
149:
150:
157: public List getValues( final Option option, final List defaultValues )
158: {
159:
160: List valueList = (List) m_values.get( option );
161:
162:
163: if( ( valueList == null ) || valueList.isEmpty() )
164: {
165: valueList = defaultValues;
166: }
167:
168:
169: if( ( valueList == null ) || valueList.isEmpty() )
170: {
171: valueList = (List) m_defaultValues.get( option );
172: }
173:
174:
175: if( valueList == null )
176: {
177: valueList = Collections.EMPTY_LIST;
178: }
179:
180: return valueList;
181: }
182:
183:
190: public Boolean getSwitch( final Option option, final Boolean defaultValue )
191: {
192:
193: Boolean bool = (Boolean) m_switches.get( option );
194:
195:
196: if( bool == null )
197: {
198: bool = defaultValue;
199: }
200:
201:
202: if( bool == null )
203: {
204: bool = (Boolean) m_defaultSwitches.get( option );
205: }
206:
207: return bool;
208: }
209:
210:
215: public void addProperty( final String property, final String value )
216: {
217: m_properties.setProperty( property, value );
218: }
219:
220:
227: public String getProperty( final String property, final String defaultValue )
228: {
229: return m_properties.getProperty( property, defaultValue );
230: }
231:
232:
237: public Set getProperties()
238: {
239: return Collections.unmodifiableSet( m_properties.keySet() );
240: }
241:
242:
247: public boolean looksLikeOption( final String trigger )
248: {
249: for( final Iterator i = m_prefixes.iterator(); i.hasNext();)
250: {
251: final String prefix = (String) i.next();
252: if( trigger.startsWith( prefix ) )
253: {
254: return true;
255: }
256: }
257: return false;
258: }
259:
260:
264: public String toString()
265: {
266: final StringBuffer buffer = new StringBuffer();
267:
268: for( final Iterator i = m_normalised.iterator(); i.hasNext();)
269: {
270: final String arg = ( String ) i.next();
271: if( arg.indexOf( ' ' ) >= 0 )
272: {
273: buffer.append( "\"" ).append( arg ).append( "\"" );
274: }
275: else
276: {
277: buffer.append( arg );
278: }
279: if( i.hasNext() )
280: {
281: buffer.append( ' ' );
282: }
283: }
284: return buffer.toString();
285: }
286:
287:
292: public List getOptions()
293: {
294: return Collections.unmodifiableList( m_options );
295: }
296:
297:
302: public Set getOptionTriggers()
303: {
304: return Collections.unmodifiableSet( m_nameToOption.keySet() );
305: }
306:
307:
312: public void setDefaultValues( final Option option, final List defaults )
313: {
314: if( defaults == null )
315: {
316: m_defaultValues.remove( option );
317: }
318: else
319: {
320: m_defaultValues.put( option, defaults );
321: }
322: }
323:
324:
329: public void setDefaultSwitch( final Option option, final Boolean defaultSwitch )
330: {
331: if( defaultSwitch == null )
332: {
333: m_defaultSwitches.remove( defaultSwitch );
334: }
335: else
336: {
337: m_defaultSwitches.put( option, defaultSwitch );
338: }
339: }
340:
341:
345: public List getNormalised()
346: {
347: return Collections.unmodifiableList( m_normalised );
348: }
349: }