1 19 20 21 package ca.mcgill.sable.soot.editors.parser; 22 23 import java.util.*; 24 25 import ca.mcgill.sable.soot.editors.JimpleOutlineObject; 26 27 public class JimpleFile { 28 29 private String file; 30 private ArrayList arr; 31 private ArrayList fields; 32 private ArrayList methods; 33 private ArrayList modifiers; 34 private int imageType; 35 36 public static final String LEFT_BRACE = "{"; 37 public static final String RIGHT_BRACE = "}"; 38 39 public JimpleFile(ArrayList file){ 40 setArr(file); 41 Iterator it = file.iterator(); 42 StringBuffer sb = new StringBuffer (); 43 while (it.hasNext()){ 44 sb.append((String )it.next()); 45 } 46 setFile(sb.toString()); 47 } 48 49 public boolean isJimpleFile(){ 50 51 53 StringBuffer sb = new StringBuffer (getFile()); 54 int leftBracePos = getFile().indexOf(LEFT_BRACE); 55 int rightBracePos = getFile().lastIndexOf(RIGHT_BRACE); 56 57 JimpleBody jBody = new JimpleBody(sb.subSequence(leftBracePos, rightBracePos).toString(), getArr()); 58 if (!jBody.isJimpleBody()) return false; 59 60 return true; 61 } 62 63 private int findImageType() { 64 if (getModifiers() == null) return JimpleOutlineObject.CLASS; 65 else { 66 if (getModifiers().contains("interface")) return JimpleOutlineObject.INTERFACE; 67 else return JimpleOutlineObject.CLASS; 68 } 69 } 70 71 private BitSet findDecorators() { 72 BitSet bits = new BitSet(); 73 if (getModifiers() == null) return bits; 74 if (getModifiers().contains("abstract")){ 75 bits.set(JimpleOutlineObject.ABSTRACT_DEC); 76 } 77 if (getModifiers().contains("final")){ 78 bits.set(JimpleOutlineObject.FINAL_DEC); 79 } 80 if (getModifiers().contains("static")){ 81 bits.set(JimpleOutlineObject.STATIC_DEC); 82 } 83 if (getModifiers().contains("synchronized")){ 84 bits.set(JimpleOutlineObject.SYNCHRONIZED_DEC); 85 } 86 return bits; 87 } 88 89 public JimpleOutlineObject getOutline(){ 90 91 92 StringBuffer sb = new StringBuffer (getFile()); 93 int leftBracePos = getFile().indexOf(LEFT_BRACE); 94 int rightBracePos = getFile().lastIndexOf(RIGHT_BRACE); 95 96 StringTokenizer st = new StringTokenizer(sb.substring(0, leftBracePos)); 98 String className = null; 99 while (true) { 100 String token = st.nextToken(); 101 if (JimpleModifier.isModifier(token)) { 102 if (getModifiers() == null){ 103 setModifiers(new ArrayList()); 104 } 105 getModifiers().add(token); 106 continue; 107 } 108 if (isFileType(token)) { 109 if (getModifiers() == null){ 110 setModifiers(new ArrayList()); 111 } 112 getModifiers().add(token); 113 continue; 114 } 115 className = token; 116 break; 117 } 118 119 JimpleOutlineObject outline = new JimpleOutlineObject("", JimpleOutlineObject.NONE, null); 120 121 122 JimpleOutlineObject file = new JimpleOutlineObject(className, findImageType(), findDecorators()); 123 outline.addChild(file); 124 125 JimpleBody jBody = new JimpleBody(sb.substring(leftBracePos, rightBracePos), getArr()); 127 128 jBody.parseBody(); 129 ArrayList fieldLabels = jBody.getFields(); 130 131 Iterator itF = fieldLabels.iterator(); 132 while (itF.hasNext()){ 133 JimpleField field = new JimpleField(itF.next().toString()); 134 field.parseField(); 135 field.findImageType(); 136 if (getFields() == null){ 137 setFields(new ArrayList()); 138 } 139 getFields().add(field); 140 file.addChild(new JimpleOutlineObject(field.getLabel(), field.getImageType(), field.findDecorators() )); 141 } 142 143 ArrayList methodLabels =jBody.getMethods(); 144 145 Iterator it = methodLabels.iterator(); 146 while (it.hasNext()){ 147 JimpleMethod method = new JimpleMethod(it.next().toString()); 148 method.parseMethod(); 149 method.findImageType(); 150 if (getMethods() == null){ 151 setMethods(new ArrayList()); 152 } 153 getMethods().add(method); 154 file.addChild(new JimpleOutlineObject(method.getLabel(), method.getImageType(), method.findDecorators())); 155 } 156 157 return outline; 158 } 159 160 public String getSearch(String val){ 161 Iterator it; 162 String search = val; 163 if (getFields() != null) { 164 it = getFields().iterator(); 165 while (it.hasNext()) { 166 JimpleField next = (JimpleField)it.next(); 167 if (val.equals(next.getLabel())){ 168 search = next.getVal(); 169 } 170 } 171 } 172 if (getMethods() != null) { 173 it = getMethods().iterator(); 174 while (it.hasNext()) { 175 JimpleMethod next = (JimpleMethod)it.next(); 176 if (val.equals(next.getLabel())){ 177 search = next.getVal(); 178 } 179 } 180 } 181 return search; 182 } 183 184 public int getStartOfSelected(String val){ 185 186 Iterator it; 187 String search = getSearch(val); 188 189 it = getArr().iterator(); 190 int count = 0; 191 while (it.hasNext()){ 192 String temp = (String )it.next(); 193 if (temp.indexOf(search.trim()) != -1){ 194 count = count + temp.indexOf(search.trim()); 195 return count; 196 } 197 count = count + temp.length() + 1; 198 } 199 return count; 200 } 201 202 public int getLength(String val){ 203 String search = getSearch(val); 204 search = search.trim(); 205 return search.length(); 206 } 207 208 private boolean isFileType(String token) { 209 HashSet filetypes = new HashSet(); 210 filetypes.add("class"); 211 filetypes.add("interface"); 212 213 if (filetypes.contains(token)) return true; 214 else return false; 215 } 216 217 218 221 public String getFile() { 222 return file; 223 } 224 225 229 public void setFile(String file) { 230 this.file = file; 231 } 232 233 236 public ArrayList getArr() { 237 return arr; 238 } 239 240 243 public void setArr(ArrayList list) { 244 arr = list; 245 } 246 247 250 public ArrayList getFields() { 251 return fields; 252 } 253 254 257 public ArrayList getMethods() { 258 return methods; 259 } 260 261 264 public void setFields(ArrayList list) { 265 fields = list; 266 } 267 268 271 public void setMethods(ArrayList list) { 272 methods = list; 273 } 274 275 278 public ArrayList getModifiers() { 279 return modifiers; 280 } 281 282 285 public void setModifiers(ArrayList list) { 286 modifiers = list; 287 } 288 289 292 public int getImageType() { 293 return imageType; 294 } 295 296 299 public void setImageType(int i) { 300 imageType = i; 301 } 302 303 } 304 | Popular Tags |