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: import ;
34: import ;
35:
36:
42: public class Switch extends ParentImpl
43: {
44:
45: public static final ResourceHelper RESOURCES =
46: ResourceHelper.getResourceHelper();
47:
48:
51: public static final String DEFAULT_ENABLED_PREFIX = "+";
52:
53:
56: public static final String DEFAULT_DISABLED_PREFIX = "-";
57:
58: private final String m_enabledPrefix;
59: private final String m_disabledPrefix;
60: private final Set m_triggers;
61: private final String m_preferredName;
62: private final Set m_aliases;
63: private final Set m_prefixes;
64: private final Boolean m_defaultSwitch;
65:
66:
81: public Switch(
82: final String enabledPrefix, final String disabledPrefix, final String preferredName,
83: final Set aliases, final String description, final boolean required,
84: final Argument argument, final Group children, final int id,
85: final Boolean switchDefault )
86: throws IllegalArgumentException
87: {
88: super( argument, children, description, id, required );
89:
90: if( enabledPrefix == null )
91: {
92: throw new IllegalArgumentException(
93: RESOURCES.getMessage(
94: ResourceConstants.SWITCH_NO_ENABLED_PREFIX ) );
95: }
96:
97: if( disabledPrefix == null )
98: {
99: throw new IllegalArgumentException(
100: RESOURCES.getMessage(
101: ResourceConstants.SWITCH_NO_DISABLED_PREFIX ) );
102: }
103:
104: if( enabledPrefix.startsWith( disabledPrefix ) )
105: {
106: throw new IllegalArgumentException(
107: RESOURCES.getMessage(
108: ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED ) );
109: }
110:
111: if( disabledPrefix.startsWith( enabledPrefix ) )
112: {
113: throw new IllegalArgumentException(
114: RESOURCES.getMessage(
115: ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED ) );
116: }
117:
118: m_enabledPrefix = enabledPrefix;
119: m_disabledPrefix = disabledPrefix;
120: m_preferredName = preferredName;
121:
122: if( ( preferredName == null ) || ( preferredName.length() < 1 ) )
123: {
124: throw new IllegalArgumentException(
125: RESOURCES.getMessage(
126: ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT ) );
127: }
128:
129: final Set newTriggers = new HashSet();
130: newTriggers.add( enabledPrefix + preferredName );
131: newTriggers.add( disabledPrefix + preferredName );
132: m_triggers = Collections.unmodifiableSet( newTriggers );
133:
134: if( aliases == null )
135: {
136: m_aliases = Collections.EMPTY_SET;
137: }
138: else
139: {
140: m_aliases = Collections.unmodifiableSet( new HashSet( aliases ) );
141:
142: for( final Iterator i = aliases.iterator(); i.hasNext();)
143: {
144: final String alias = (String) i.next();
145: newTriggers.add( enabledPrefix + alias );
146: newTriggers.add( disabledPrefix + alias );
147: }
148: }
149:
150: final Set newPrefixes = new HashSet( super.getPrefixes() );
151: newPrefixes.add( enabledPrefix );
152: newPrefixes.add( disabledPrefix );
153: m_prefixes = Collections.unmodifiableSet( newPrefixes );
154: m_defaultSwitch = switchDefault;
155: checkPrefixes( newPrefixes );
156: }
157:
158:
168: public void processParent(
169: final WriteableCommandLine commandLine, final ListIterator arguments )
170: throws OptionException
171: {
172: final String arg = (String) arguments.next();
173:
174: if( canProcess( commandLine, arg ) )
175: {
176: if( arg.startsWith( m_enabledPrefix ) )
177: {
178: commandLine.addSwitch( this, true );
179: arguments.set( m_enabledPrefix + m_preferredName );
180: }
181: if( arg.startsWith( m_disabledPrefix ) )
182: {
183: commandLine.addSwitch( this, false );
184: arguments.set( m_disabledPrefix + m_preferredName );
185: }
186: }
187: else
188: {
189: throw new OptionException(
190: this,
191: ResourceConstants.UNEXPECTED_TOKEN,
192: arg );
193: }
194: }
195:
196:
205: public Set getTriggers()
206: {
207: return m_triggers;
208: }
209:
210:
220: public Set getPrefixes()
221: {
222: return m_prefixes;
223: }
224:
225:
232: public void validate( WriteableCommandLine commandLine )
233: throws OptionException
234: {
235: if( isRequired() && !commandLine.hasOption( this ) )
236: {
237: throw new OptionException(
238: this,
239: ResourceConstants.OPTION_MISSING_REQUIRED,
240: getPreferredName() );
241: }
242: super.validate( commandLine );
243: }
244:
245:
252: public void appendUsage(
253: final StringBuffer buffer, final Set helpSettings, final Comparator comp )
254: {
255:
256: final boolean optional =
257: !isRequired()
258: && helpSettings.contains( DisplaySetting.DISPLAY_OPTIONAL );
259:
260: final boolean displayAliases =
261: helpSettings.contains( DisplaySetting.DISPLAY_ALIASES );
262: final boolean disabled =
263: helpSettings.contains( DisplaySetting.DISPLAY_SWITCH_DISABLED );
264: final boolean enabled =
265: !disabled || helpSettings.contains( DisplaySetting.DISPLAY_SWITCH_ENABLED );
266: final boolean both = disabled && enabled;
267:
268: if( optional )
269: {
270: buffer.append( '[' );
271: }
272:
273: if( enabled )
274: {
275: buffer.append( m_enabledPrefix ).append( m_preferredName );
276: }
277:
278: if( both )
279: {
280: buffer.append( '|' );
281: }
282:
283: if( disabled )
284: {
285: buffer.append( m_disabledPrefix ).append( m_preferredName );
286: }
287:
288: if( displayAliases && !m_aliases.isEmpty() )
289: {
290: buffer.append( " (" );
291:
292: final List list = new ArrayList( m_aliases );
293: Collections.sort( list );
294: for( final Iterator i = list.iterator(); i.hasNext();)
295: {
296: final String alias = (String) i.next();
297:
298: if( enabled )
299: {
300: buffer.append( m_enabledPrefix ).append( alias );
301: }
302:
303: if( both )
304: {
305: buffer.append( '|' );
306: }
307:
308: if( disabled )
309: {
310: buffer.append( m_disabledPrefix ).append( alias );
311: }
312:
313: if( i.hasNext() )
314: {
315: buffer.append( ',' );
316: }
317: }
318:
319: buffer.append( ')' );
320: }
321:
322: super.appendUsage( buffer, helpSettings, comp );
323:
324: if( optional )
325: {
326: buffer.append( ']' );
327: }
328: }
329:
330:
336: public String getPreferredName()
337: {
338: return m_enabledPrefix + m_preferredName;
339: }
340:
341:
349: public void defaults( final WriteableCommandLine commandLine )
350: {
351: commandLine.setDefaultSwitch( this, m_defaultSwitch );
352: }
353: }