1 19 20 package soot.util.cfgcmd; 21 22 import soot.G; 23 import soot.CompilationDeathException; 24 25 33 34 public class CFGOptionMatcher { 35 36 41 public static abstract class CFGOption { 42 private final String name; 43 protected CFGOption(String name) { 44 this.name = name; 45 } 46 public String name() { 47 return name; 48 } 49 }; 50 51 private CFGOption[] options; 52 53 58 public CFGOptionMatcher(CFGOption[] options) { 59 this.options = options; 60 } 61 62 77 public CFGOption match(String quarry) 78 throws soot.CompilationDeathException { 79 String uncasedQuarry = quarry.toLowerCase(); 80 int match = -1; 81 for (int i = 0; i < options.length; i++) { 82 String 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 127 public String help(int initialIndent, int rightMargin, int hangingIndent) { 128 129 StringBuffer newLineBuf = new StringBuffer (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 newLine = newLineBuf.toString(); 138 139 StringBuffer result = new StringBuffer (); 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 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 |