KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > options > OptionsBase


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.options;
21 import java.util.*;
22
23 import soot.*;
24
25 /** Soot command-line options parser base class.
26  * @author Ondrej Lhotak
27  */

28
29 abstract class OptionsBase {
30     private String JavaDoc pad( int initial, String JavaDoc opts, int tab, String JavaDoc desc ) {
31         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
32         for( int i = 0; i < initial; i++ ) b.append( " " );
33         b.append(opts);
34         int i;
35         if( tab <= opts.length() ) {
36             b.append( "\n" );
37             i = 0;
38         } else i = opts.length()+initial;
39         for( ; i <= tab; i++ ) {
40             b.append(" ");
41         }
42         for( StringTokenizer t = new StringTokenizer( desc );
43                 t.hasMoreTokens(); ) {
44             String JavaDoc s = t.nextToken();
45             if( i + s.length() > 78 ) {
46                 b.append( "\n" );
47                 i = 0;
48                 for( ; i <= tab; i++ ) {
49                     b.append(" ");
50                 }
51             }
52             b.append( s );
53             b.append( " " );
54             i += s.length() + 1;
55         }
56         b.append( "\n" );
57         return b.toString();
58     }
59
60     protected String JavaDoc padOpt( String JavaDoc opts, String JavaDoc desc ) {
61         return pad( 1, opts, 30, desc );
62     }
63
64     protected String JavaDoc padVal( String JavaDoc vals, String JavaDoc desc ) {
65         return pad( 4, vals, 32, desc );
66     }
67
68     protected String JavaDoc getPhaseUsage() {
69         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
70         b.append( "\nPhases and phase options:\n" );
71         for( Iterator pIt = PackManager.v().allPacks().iterator(); pIt.hasNext(); ) {
72             final Pack p = (Pack) pIt.next();
73             b.append( padOpt( p.getPhaseName(), p.getDeclaredOptions() ) );
74             for( Iterator phIt = p.iterator(); phIt.hasNext(); ) {
75                 final HasPhaseOptions ph = (HasPhaseOptions) phIt.next();
76                 b.append( padVal( ph.getPhaseName(), ph.getDeclaredOptions() ) );
77             }
78         }
79         return b.toString();
80     }
81
82     private LinkedList options = new LinkedList();
83     protected void pushOptions( String JavaDoc s ) {
84         options.addFirst( s );
85     }
86
87     protected boolean hasMoreOptions() { return !options.isEmpty(); }
88     protected String JavaDoc nextOption() { return (String JavaDoc) options.removeFirst(); }
89
90     protected LinkedList classes = new LinkedList();
91     public LinkedList classes() { return classes; }
92
93     public boolean setPhaseOption( String JavaDoc phase, String JavaDoc option ) {
94         return PhaseOptions.v().processPhaseOptions( phase, option );
95     }
96
97 }
98   
99
Popular Tags