KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > editors > parser > JimpleFile


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer 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
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 JavaDoc 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 JavaDoc LEFT_BRACE = "{";
37     public static final String JavaDoc RIGHT_BRACE = "}";
38         
39     public JimpleFile(ArrayList file){
40         setArr(file);
41         Iterator it = file.iterator();
42         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
43         while (it.hasNext()){
44             sb.append((String JavaDoc)it.next());
45         }
46         setFile(sb.toString());
47     }
48     
49     public boolean isJimpleFile(){
50         
51         // handles body section
52

53         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc sb = new StringBuffer JavaDoc(getFile());
93         int leftBracePos = getFile().indexOf(LEFT_BRACE);
94         int rightBracePos = getFile().lastIndexOf(RIGHT_BRACE);
95         
96         // get key - class name
97
StringTokenizer st = new StringTokenizer(sb.substring(0, leftBracePos));
98         String JavaDoc className = null;
99         while (true) {
100             String JavaDoc 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         // gets methods
126
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 JavaDoc getSearch(String JavaDoc val){
161         Iterator it;
162         String JavaDoc 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 JavaDoc val){
185         
186         Iterator it;
187         String JavaDoc search = getSearch(val);
188                 
189         it = getArr().iterator();
190         int count = 0;
191         while (it.hasNext()){
192             String JavaDoc temp = (String JavaDoc)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 JavaDoc val){
203         String JavaDoc search = getSearch(val);
204         search = search.trim();
205         return search.length();
206     }
207     
208     private boolean isFileType(String JavaDoc 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     /**
219      * @return String
220      */

221     public String JavaDoc getFile() {
222         return file;
223     }
224
225     /**
226      * Sets the file.
227      * @param file The file to set
228      */

229     public void setFile(String JavaDoc file) {
230         this.file = file;
231     }
232
233     /**
234      * @return
235      */

236     public ArrayList getArr() {
237         return arr;
238     }
239
240     /**
241      * @param list
242      */

243     public void setArr(ArrayList list) {
244         arr = list;
245     }
246
247     /**
248      * @return
249      */

250     public ArrayList getFields() {
251         return fields;
252     }
253
254     /**
255      * @return
256      */

257     public ArrayList getMethods() {
258         return methods;
259     }
260
261     /**
262      * @param list
263      */

264     public void setFields(ArrayList list) {
265         fields = list;
266     }
267
268     /**
269      * @param list
270      */

271     public void setMethods(ArrayList list) {
272         methods = list;
273     }
274
275     /**
276      * @return
277      */

278     public ArrayList getModifiers() {
279         return modifiers;
280     }
281
282     /**
283      * @param list
284      */

285     public void setModifiers(ArrayList list) {
286         modifiers = list;
287     }
288
289     /**
290      * @return
291      */

292     public int getImageType() {
293         return imageType;
294     }
295
296     /**
297      * @param i
298      */

299     public void setImageType(int i) {
300         imageType = i;
301     }
302
303 }
304
Popular Tags