KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > cfgcmd > CFGOptionMatcher


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 John Jorgensen
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.util.cfgcmd;
21
22 import soot.G;
23 import soot.CompilationDeathException;
24
25 /**
26  * A class used by CFG utilities that need to match different option
27  * strings with classes that implement those options.
28  *
29  * A <code>CFGOptionMatcher</code> maintains a set of named
30  * options, and provides a means for matching abbreviated option
31  * values against those names.
32  */

33
34 public class CFGOptionMatcher {
35
36     /**
37      * The type stored within a <code>CFGOptionMatcher</code>. Options to
38      * be stored in a <code>CFGOptionMatcher</code> must extend this
39      * class.
40      */

41     public static abstract class CFGOption {
42     private final String JavaDoc name;
43     protected CFGOption(String JavaDoc name) {
44         this.name = name;
45     }
46     public String JavaDoc name() {
47         return name;
48     }
49     };
50
51     private CFGOption[] options;
52
53     /**
54      * Creates a CFGOptionMatcher.
55      *
56      * @param options The set of command options to be stored.
57      */

58     public CFGOptionMatcher(CFGOption[] options) {
59     this.options = options;
60     }
61
62     /**
63      * Searches the options in this <code>CFGOptionMatcher</code>
64      * looking for one whose name begins with the passed string
65      * (ignoring the case of letters in the string).
66      *
67      * @param quarry The string to be matched against the stored
68      * option names.
69      *
70      * @return The matching {@link CFGOption}, if exactly one of the
71      * stored option names begins with <code>quarry</code>.
72      *
73      * @throws soot.CompilationDeathException if <code>quarry</code>
74      * matches none of the option names, or if it matches more than
75      * one.
76      */

77     public CFGOption match(String JavaDoc quarry)
78     throws soot.CompilationDeathException {
79     String JavaDoc uncasedQuarry = quarry.toLowerCase();
80     int match = -1;
81     for (int i = 0; i < options.length; i++) {
82         String JavaDoc uncasedName = options[i].name().toLowerCase();
83         if (uncasedName.startsWith(uncasedQuarry)) {
84         if (match == -1) {
85             match = i;
86         } else {
87             G.v().out.println(quarry + " is ambiguous; it matches " +
88                       options[match].name() + " and " +
89                       options[i].name());
90             throw new CompilationDeathException(
91             CompilationDeathException.COMPILATION_ABORTED,
92             "Option parse error");
93         }
94         }
95     }
96     if (match == -1) {
97         G.v().out.println("\"" + quarry + "\"" +
98                   " does not match any value.");
99         throw new CompilationDeathException(
100         CompilationDeathException.COMPILATION_ABORTED,
101         "Option parse error");
102     } else {
103         return options[match];
104     }
105     }
106
107
108     /**
109      * Returns a string containing the names of all the
110      * options in this <code>CFGOptionMatcher</code>, separated by
111      * '|' characters. The string is intended for use in
112      * help messages.
113      *
114      * @param initialIndent The number of blank spaces to insert at the
115      * beginning of the returned string. Ignored if
116      * negative.
117      *
118      * @param rightMargin If positive, newlines will be inserted to try
119      * to keep the length of each line in the
120      * returned string less than or equal to
121      * <code>rightMargin</code>.
122      *
123      * @param hangingIndent If positive, this number of spaces will be
124      * inserted immediately after each newline
125      * inserted to respect the <code>rightMargin</code>.
126      */

127     public String JavaDoc help(int initialIndent, int rightMargin, int hangingIndent) {
128
129     StringBuffer JavaDoc newLineBuf = new StringBuffer JavaDoc(2 + rightMargin);
130     newLineBuf.append('\n');
131     if (hangingIndent < 0) {
132         hangingIndent = 0;
133     }
134     for (int i = 0; i < hangingIndent; i++) {
135         newLineBuf.append(' ');
136     }
137     String JavaDoc newLine = newLineBuf.toString();
138
139     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
140     int lineLength = 0;
141     for (int i = 0; i < initialIndent; i++) {
142         lineLength++;
143         result.append(' ');
144     }
145
146     for (int i = 0; i < options.length; i++) {
147         if (i > 0) {
148         result.append('|');
149         lineLength++;
150         }
151         String JavaDoc name = options[i].name();
152         int nameLength = name.length();
153         if ((lineLength + nameLength) > rightMargin) {
154         result.append(newLine);
155         lineLength = hangingIndent;
156         }
157         result.append(name);
158         lineLength += nameLength;
159     }
160     return result.toString();
161     }
162 }
163
Popular Tags