KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > attributes > AbstractAttributesComputer


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 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 package ca.mcgill.sable.soot.attributes;
21
22 import java.io.File JavaDoc;
23 import java.util.*;
24
25 import org.eclipse.core.resources.*;
26 import org.eclipse.core.runtime.*;
27 import org.eclipse.ui.texteditor.*;
28
29 import ca.mcgill.sable.soot.SootPlugin;
30
31
32 public abstract class AbstractAttributesComputer {
33
34     private IResource rec;
35     private IProject proj;
36     
37     /**
38      * compute list of xml filenames
39      */

40     protected ArrayList computeFiles(ArrayList names){
41         ArrayList fileList = new ArrayList();
42         String JavaDoc sep = System.getProperty("file.separator");
43         IContainer con = (IContainer)getProj().getFolder("sootOutput"+sep+"attributes"+sep);
44         try {
45             IResource [] files = con.members();
46             for (int i = 0; i < files.length; i++){
47                 Iterator it = names.iterator();
48                 while (it.hasNext()){
49                     String JavaDoc fileNameToMatch = (String JavaDoc)it.next();
50                     if (files[i].getName().matches(fileNameToMatch+"[$].*") || files[i].getName().equals(fileNameToMatch+".xml")){
51                         fileList.add(files[i]);
52                     }
53                 }
54             }
55         }
56         catch(CoreException e){
57         }
58         return fileList;
59     }
60     /**
61      * compute top-level names
62      */

63     protected abstract ArrayList computeNames(AbstractTextEditor editor);
64     
65     protected abstract ArrayList computeNames(IFile file);
66     
67     /**
68      * initialize rec and proj
69      */

70     protected abstract void init(AbstractTextEditor editor);
71     
72     /**
73      * compute attributes
74      */

75     protected SootAttributesHandler computeAttributes(ArrayList files, SootAttributesHandler sah) {
76         SootAttributeFilesReader safr = new SootAttributeFilesReader();
77         Iterator it = files.iterator();
78         while (it.hasNext()){
79             String JavaDoc fileName = ((IPath)((IFile)it.next()).getLocation()).toOSString();
80             AttributeDomProcessor adp = safr.readFile(fileName);
81             if (adp != null) {
82                 sah.setAttrList(adp.getAttributes());
83                 sah.setKeyList(adp.getKeys());
84             }
85         }
86         sah.setValuesSetTime(System.currentTimeMillis());
87         SootPlugin.getDefault().getManager().addToFileWithAttributes((IFile)getRec(), sah);
88         return sah;
89     }
90     
91     public SootAttributesHandler getAttributesHandler(IFile file){
92         ArrayList files = computeFiles(computeNames(file));
93         return getHandler(files);
94     }
95     
96     public SootAttributesHandler getAttributesHandler(AbstractTextEditor editor){
97         // init
98
init(editor);
99         // computeFileNames
100
ArrayList files = computeFiles(computeNames(editor));
101         return getHandler(files);
102     }
103     
104     private SootAttributesHandler getHandler(ArrayList files){
105         // check if any have changed since creation of attributes in handler
106
if (!(getRec() instanceof IFile)) return null;
107         
108         SootAttributesHandler handler = SootPlugin.getDefault().getManager().getAttributesHandlerForFile((IFile)getRec());
109         if (handler != null){
110         
111             long valuesSetTime = handler.getValuesSetTime();
112             boolean update = handler.isUpdate();
113         
114             Iterator it = files.iterator();
115             while (it.hasNext()){
116                 IFile next = (IFile)it.next();
117                 File JavaDoc realFile = new File JavaDoc(next.getLocation().toOSString());
118                 
119                 if (realFile.lastModified() > valuesSetTime){
120                     update = true;
121                 }
122             }
123             handler.setUpdate(update);
124             // if no return handler
125
if (!update){
126                 return handler;
127             }
128             else {
129                 return computeAttributes(files, handler);
130             }
131         }
132         else {
133             return computeAttributes(files, new SootAttributesHandler());
134         }
135     }
136     /**
137      * @return
138      */

139     public IProject getProj() {
140         return proj;
141     }
142
143     /**
144      * @return
145      */

146     public IResource getRec() {
147         return rec;
148     }
149
150     /**
151      * @param project
152      */

153     public void setProj(IProject project) {
154         proj = project;
155     }
156
157     /**
158      * @param resource
159      */

160     public void setRec(IResource resource) {
161         rec = resource;
162     }
163
164 }
165
Popular Tags