001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2004 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package ca.mcgill.sable.soot.attributes;
021:
022: import java.io.File;
023: import java.util.*;
024:
025: import org.eclipse.core.resources.*;
026: import org.eclipse.core.runtime.*;
027: import org.eclipse.ui.texteditor.*;
028:
029: import ca.mcgill.sable.soot.SootPlugin;
030:
031: public abstract class AbstractAttributesComputer {
032:
033: private IResource rec;
034: private IProject proj;
035:
036: /**
037: * compute list of xml filenames
038: */
039: protected ArrayList computeFiles(ArrayList names) {
040: ArrayList fileList = new ArrayList();
041: String sep = System.getProperty("file.separator");
042: IContainer con = (IContainer) getProj().getFolder(
043: "sootOutput" + sep + "attributes" + sep);
044: try {
045: IResource[] files = con.members();
046: for (int i = 0; i < files.length; i++) {
047: Iterator it = names.iterator();
048: while (it.hasNext()) {
049: String fileNameToMatch = (String) it.next();
050: if (files[i].getName().matches(
051: fileNameToMatch + "[$].*")
052: || files[i].getName().equals(
053: fileNameToMatch + ".xml")) {
054: fileList.add(files[i]);
055: }
056: }
057: }
058: } catch (CoreException e) {
059: }
060: return fileList;
061: }
062:
063: /**
064: * compute top-level names
065: */
066: protected abstract ArrayList computeNames(AbstractTextEditor editor);
067:
068: protected abstract ArrayList computeNames(IFile file);
069:
070: /**
071: * initialize rec and proj
072: */
073: protected abstract void init(AbstractTextEditor editor);
074:
075: /**
076: * compute attributes
077: */
078: protected SootAttributesHandler computeAttributes(ArrayList files,
079: SootAttributesHandler sah) {
080: SootAttributeFilesReader safr = new SootAttributeFilesReader();
081: Iterator it = files.iterator();
082: while (it.hasNext()) {
083: String fileName = ((IPath) ((IFile) it.next())
084: .getLocation()).toOSString();
085: AttributeDomProcessor adp = safr.readFile(fileName);
086: if (adp != null) {
087: sah.setAttrList(adp.getAttributes());
088: sah.setKeyList(adp.getKeys());
089: }
090: }
091: sah.setValuesSetTime(System.currentTimeMillis());
092: SootPlugin.getDefault().getManager().addToFileWithAttributes(
093: (IFile) getRec(), sah);
094: return sah;
095: }
096:
097: public SootAttributesHandler getAttributesHandler(IFile file) {
098: ArrayList files = computeFiles(computeNames(file));
099: return getHandler(files);
100: }
101:
102: public SootAttributesHandler getAttributesHandler(
103: AbstractTextEditor editor) {
104: // init
105: init(editor);
106: // computeFileNames
107: ArrayList files = computeFiles(computeNames(editor));
108: return getHandler(files);
109: }
110:
111: private SootAttributesHandler getHandler(ArrayList files) {
112: // check if any have changed since creation of attributes in handler
113: if (!(getRec() instanceof IFile))
114: return null;
115:
116: SootAttributesHandler handler = SootPlugin.getDefault()
117: .getManager().getAttributesHandlerForFile(
118: (IFile) getRec());
119: if (handler != null) {
120:
121: long valuesSetTime = handler.getValuesSetTime();
122: boolean update = handler.isUpdate();
123:
124: Iterator it = files.iterator();
125: while (it.hasNext()) {
126: IFile next = (IFile) it.next();
127: File realFile = new File(next.getLocation()
128: .toOSString());
129:
130: if (realFile.lastModified() > valuesSetTime) {
131: update = true;
132: }
133: }
134: handler.setUpdate(update);
135: // if no return handler
136: if (!update) {
137: return handler;
138: } else {
139: return computeAttributes(files, handler);
140: }
141: } else {
142: return computeAttributes(files, new SootAttributesHandler());
143: }
144: }
145:
146: /**
147: * @return
148: */
149: public IProject getProj() {
150: return proj;
151: }
152:
153: /**
154: * @return
155: */
156: public IResource getRec() {
157: return rec;
158: }
159:
160: /**
161: * @param project
162: */
163: public void setProj(IProject project) {
164: proj = project;
165: }
166:
167: /**
168: * @param resource
169: */
170: public void setRec(IResource resource) {
171: rec = resource;
172: }
173:
174: }
|