KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > DependencyCache


1 /*
2  * Copyright (c) 2005, Brian Hawkins
3  * brianhks@activeclickweb.com
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21 package cpmake;
22  
23 import java.io.*;
24 import java.util.*;
25
26 class DependencyCache
27         implements Serializable
28     {
29     private Map m_parsedFileMap;
30     
31     private static DependencyCache s_depCache = null;
32     private static String JavaDoc s_cacheFile;
33     
34 //===================================================================
35
//==-- ParsedFile inner class --==
36
private class ParsedFile
37             implements Serializable
38         {
39         private long m_lastParsed;
40         private File m_sourceFile;
41         private Set m_dependencyList;
42         private DependencyParser m_parser;
43         
44         private boolean isUpToDate()
45             {
46             return (m_lastParsed > m_sourceFile.lastModified());
47             }
48             
49         private void parseFile(CPMake make)
50                 throws IOException
51             {
52             String JavaDoc[] list;
53             File file;
54             ParsedFile parsedFile;
55             
56             m_dependencyList.clear();
57             
58             if (m_parser == null)
59                 return;
60             
61             CPMake.debugPrint("Parsing file "+m_sourceFile);
62             m_lastParsed = System.currentTimeMillis();
63             list = m_parser.parseFile(m_sourceFile);
64             for (int I = 0; I < list.length; I++)
65                 {
66                 file = make.locateFile(list[I]);
67                 
68                 if (file != null)
69                     {
70                     parsedFile = getParsedFile(file);
71                     
72                     m_dependencyList.add(parsedFile);
73                     }
74                 }
75             }
76         
77         //=====================================================
78
public ParsedFile(File source, DependencyParser parser)
79             {
80             m_sourceFile = source;
81             m_parser = parser;
82             m_dependencyList = new HashSet();
83             m_lastParsed = 0;
84             }
85             
86         public File getSourceFile()
87             {
88             return (m_sourceFile);
89             }
90             
91         public Set getDependencyList(CPMake make)
92                 throws IOException
93             {
94             if (!isUpToDate())
95                 parseFile(make);
96                 
97             return (m_dependencyList);
98             }
99             
100         public boolean canRecurse()
101             {
102             return (m_parser.canRecurse());
103             }
104             
105         public boolean equals(Object JavaDoc obj)
106             {
107             return (m_sourceFile.getAbsolutePath().equals(obj));
108             }
109             
110         public int hashCode()
111             {
112             int ret = 0;
113             
114             //When loading the object form file the m_sourceFile can be null
115
if (m_sourceFile != null)
116                 ret = m_sourceFile.getAbsolutePath().hashCode();
117                 
118             return (ret);
119             }
120             
121         public long getParsedTime()
122             {
123             return (m_lastParsed);
124             }
125         }
126 //==-- End of ParsedFile inner class --==
127
//===================================================================
128

129     private ParsedFile getParsedFile(File source)
130         {
131         ParsedFile parsedFile = (ParsedFile)m_parsedFileMap.get(source.getAbsolutePath());
132         
133         if (parsedFile == null)
134             {
135             parsedFile = new ParsedFile(source, getDependencyParser(source.getName()));
136             m_parsedFileMap.put(parsedFile.getSourceFile().getAbsolutePath(), parsedFile);
137             }
138             
139         return (parsedFile);
140         }
141         
142     private DependencyParser getDependencyParser(String JavaDoc file)
143         {
144         if (file.matches(".*\\.c") || file.matches(".*\\.cpp") ||
145                     file.matches(".*\\.h") || file.matches(".*\\.hpp"))
146             return (new CDependencyParser());
147         else if (file.matches(".*\\.class"))
148             {
149             return (new JavaDependencyParser());
150             }
151         else
152             return (null);
153         }
154         
155     private void getDependencies(Set retSet, CPMake make, ParsedFile parsedFile)
156             throws IOException
157         {
158         Set dependencies = parsedFile.getDependencyList(make);
159         Iterator it = dependencies.iterator();
160         ParsedFile pfile;
161         
162         while (it.hasNext())
163             {
164             pfile = (ParsedFile)it.next();
165             
166             if ((retSet.add(make.getPath(pfile.getSourceFile()))) && (parsedFile.canRecurse()))
167                 getDependencies(retSet, make, pfile);
168             }
169         }
170     
171 //===================================================================
172
private DependencyCache()
173         {
174         m_parsedFileMap = new HashMap();
175         }
176         
177     public boolean canParse(String JavaDoc file)
178         {
179         return (getDependencyParser(file) != null);
180         }
181         
182     public Set getDependencies(CPMake make, String JavaDoc file)
183             throws CPMakeException
184         {
185         File srcFile = make.locateFile(file);
186         ParsedFile parsedFile;
187         DependencyParser parser = null;
188         Set dependencies = new HashSet();
189
190         CPMake.debugPrint("Dependency lookup for "+file);
191         
192         if (srcFile != null)
193             {
194             parsedFile = getParsedFile(srcFile);
195             
196             try
197                 {
198                 getDependencies(dependencies, make, parsedFile);
199                 }
200             catch (IOException ioe)
201                 {
202                 throw (new CPMakeException("Dependency error\n"+ioe.getMessage(), -1));
203                 }
204             }
205         
206         return (dependencies);
207         }
208         
209 //===================================================================
210
public static void writeDependencyCache()
211             throws IOException
212         {
213         FileOutputStream fos = new FileOutputStream(s_cacheFile);
214         ObjectOutputStream oos = new ObjectOutputStream(fos);
215         
216         oos.writeObject(s_depCache);
217         
218         oos.close();
219         }
220         
221     public static DependencyCache getDependencyCache(String JavaDoc cacheFile)
222         {
223         try
224             {
225             if (s_depCache == null)
226                 {
227                 s_cacheFile = cacheFile;
228                 readDependencyCache(cacheFile);
229                 /*Iterator it = s_depCache.m_parsedFileMap.keySet().iterator();
230                 File file;
231                 while (it.hasNext())
232                     {
233                     file = (File)it.next();
234                     System.out.println(file.getAbsolutePath());
235                     
236                     }*/

237                 }
238             }
239         catch (Exception JavaDoc ioe)
240             {
241             CPMake.debugPrint(ioe.getMessage());
242             StringWriter sw = new StringWriter();
243             ioe.printStackTrace(new PrintWriter(sw));
244             CPMake.debugPrint(sw.toString());
245             s_depCache = new DependencyCache();
246             }
247             
248         return (s_depCache);
249         }
250         
251     public static String JavaDoc getCacheFile()
252         {
253         return (s_cacheFile);
254         }
255         
256     private static void readDependencyCache(String JavaDoc cacheFile)
257             throws IOException, ClassNotFoundException JavaDoc
258         {
259         FileInputStream fis = new FileInputStream(cacheFile);
260         ObjectInputStream ois = new ObjectInputStream(fis);
261         
262         s_depCache = (DependencyCache)ois.readObject();
263         
264         ois.close();
265         }
266     }
267
Popular Tags