1:
17: package ;
18:
19: import ;
20: import ;
21: import ;
22: import ;
23: import ;
24: import ;
25:
26: import ;
27: import ;
28: import ;
29: import ;
30: import ;
31: import ;
32: import ;
33:
34:
40: public abstract class ParentImpl extends OptionImpl implements Parent
41: {
42: private static final char NUL = '\0';
43: private final Group m_children;
44: private final Argument m_argument;
45: private final String m_description;
46:
47:
55: protected ParentImpl(
56: final Argument argument, final Group children, final String description,
57: final int id, final boolean required )
58: {
59: super( id, required );
60:
61: m_children = children;
62: m_argument = argument;
63: m_description = description;
64: }
65:
66:
78: public void process( final WriteableCommandLine commandLine, final ListIterator arguments )
79: throws OptionException
80: {
81: if( m_argument != null )
82: {
83: handleInitialSeparator( arguments, m_argument.getInitialSeparator() );
84: }
85:
86: processParent( commandLine, arguments );
87:
88: if( m_argument != null )
89: {
90: m_argument.processValues( commandLine, arguments, this );
91: }
92:
93: if( ( m_children != null ) && m_children.canProcess( commandLine, arguments ) )
94: {
95: m_children.process( commandLine, arguments );
96: }
97: }
98:
99:
107: public boolean canProcess(
108: final WriteableCommandLine commandLine, final String arg )
109: {
110: final Set triggers = getTriggers();
111: if( m_argument != null )
112: {
113: final char separator = m_argument.getInitialSeparator();
114:
115:
116: if( separator != NUL )
117: {
118: final int initialIndex = arg.indexOf( separator );
119:
120: if( initialIndex > 0 )
121: {
122: return triggers.contains( arg.substring( 0, initialIndex ) );
123: }
124: }
125: }
126:
127: return triggers.contains( arg );
128: }
129:
130:
140: public Set getPrefixes()
141: {
142: if( null == m_children )
143: {
144: return Collections.EMPTY_SET;
145: }
146: else
147: {
148: return m_children.getPrefixes();
149: }
150: }
151:
152:
159: public void validate( WriteableCommandLine commandLine ) throws OptionException
160: {
161: if( commandLine.hasOption( this ) )
162: {
163: if( m_argument != null )
164: {
165: m_argument.validate( commandLine, this );
166: }
167:
168: if( m_children != null )
169: {
170: m_children.validate( commandLine );
171: }
172: }
173: }
174:
175:
182: public void appendUsage(
183: final StringBuffer buffer, final Set helpSettings, final Comparator comp )
184: {
185: final boolean displayArgument =
186: ( m_argument != null )
187: && helpSettings.contains( DisplaySetting.DISPLAY_PARENT_ARGUMENT );
188: final boolean displayChildren =
189: ( m_children != null )
190: && helpSettings.contains( DisplaySetting.DISPLAY_PARENT_CHILDREN );
191:
192: if( displayArgument )
193: {
194: buffer.append( ' ' );
195: m_argument.appendUsage( buffer, helpSettings, comp );
196: }
197:
198: if( displayChildren )
199: {
200: buffer.append( ' ' );
201: m_children.appendUsage( buffer, helpSettings, comp );
202: }
203: }
204:
205:
212: public String getDescription()
213: {
214: return m_description;
215: }
216:
217:
227: public List helpLines(
228: final int depth, final Set helpSettings, final Comparator comp )
229: {
230: final List helpLines = new ArrayList();
231: helpLines.add( new HelpLineImpl( this, depth ) );
232:
233: if( helpSettings.contains( DisplaySetting.DISPLAY_PARENT_ARGUMENT ) && ( m_argument != null ) )
234: {
235: helpLines.addAll( m_argument.helpLines( depth + 1, helpSettings, comp ) );
236: }
237:
238: if( helpSettings.contains( DisplaySetting.DISPLAY_PARENT_CHILDREN ) && ( m_children != null ) )
239: {
240: helpLines.addAll( m_children.helpLines( depth + 1, helpSettings, comp ) );
241: }
242:
243: return helpLines;
244: }
245:
246:
250: public Argument getArgument()
251: {
252: return m_argument;
253: }
254:
255:
259: public Group getChildren()
260: {
261: return m_children;
262: }
263:
264:
269: private void handleInitialSeparator(
270: final ListIterator arguments, final char separator )
271: {
272:
273: final String newArgument = (String) arguments.next();
274:
275:
276: final int initialIndex = newArgument.indexOf( separator );
277:
278: if( initialIndex > 0 )
279: {
280: arguments.remove();
281: arguments.add( newArgument.substring( 0, initialIndex ) );
282: arguments.add( newArgument.substring( initialIndex + 1 ) );
283: arguments.previous();
284: }
285: arguments.previous();
286: }
287:
288:
294: public Option findOption( final String trigger )
295: {
296: final Option found = super.findOption( trigger );
297: if( ( found == null ) && ( m_children != null ) )
298: {
299: return m_children.findOption( trigger );
300: }
301: else
302: {
303: return found;
304: }
305: }
306:
307:
315: public void defaults( final WriteableCommandLine commandLine )
316: {
317: super.defaults( commandLine );
318: if( m_argument != null )
319: {
320: m_argument.defaultValues( commandLine, this );
321: }
322: if( m_children != null )
323: {
324: m_children.defaults( commandLine );
325: }
326: }
327: }