KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > enhancer > utils > FileInfoUtil


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.tools.enhancer.utils;
13
14 import java.util.*;
15 import java.io.*;
16
17 import com.versant.lib.bcel.classfile.*;
18 import com.versant.lib.bcel.generic.*;
19 import com.versant.lib.bcel.Constants;
20 import com.versant.lib.bcel.*;
21 import com.versant.lib.bcel.Repository;
22
23 /**
24  * The FileInfoUtil class is a helper class that gets the location of a class file on
25  * the file system
26  *
27  */

28 public class FileInfoUtil {
29
30     private ArrayList list = new ArrayList();
31     private ArrayList allClassesList;
32     private String JavaDoc fileSeparator;
33     private char charfileSeparator;
34
35     public FileInfoUtil() {
36         String JavaDoc paths = System.getProperty("java.class.path");
37         String JavaDoc pathSeparator = System.getProperty("path.separator");
38         fileSeparator = System.getProperty("file.separator");
39         charfileSeparator = fileSeparator.charAt(0);
40         StringTokenizer to = new StringTokenizer(paths,pathSeparator,false);
41         while (to.hasMoreElements()){
42             String JavaDoc path = (String JavaDoc)to.nextElement();
43             if (!path.endsWith(".jar")){
44                 list.add(path);
45             }
46         }
47     }
48
49     public void setPath(String JavaDoc[] paths){
50         for (int i = 0; i < paths.length; i++) {
51             list.add(paths[i]);
52         }
53     }
54
55     public List getAllFiles(){
56         Iterator iter = list.iterator();
57         allClassesList = new ArrayList();
58         while (iter.hasNext()){
59             String JavaDoc path = (String JavaDoc)iter.next();
60             File file = new File(path);
61             addFilesToList(file);
62         }
63         return allClassesList;
64     }
65
66     private void addFilesToList(File file){
67         if (file.isFile()){
68             if (file.getName().endsWith(".class")){
69                 allClassesList.add(file);
70             }
71         } else {
72             File [] files = file.listFiles();
73             for (int i = 0;i < files.length ;i++){
74                 addFilesToList(files[i]);
75             }
76         }
77     }
78
79     /**
80      * Returns a File object representing the location of a class file on the
81      * file system for a given class name.
82      *
83      * @param fullClassName in format ("com.xyz.hr.Employee").
84      * @return File object of where class file is on file system.
85      * @throws FileNotFoundException if the class file could not be found
86      */

87     public File getClassFile(String JavaDoc fullClassName)throws FileNotFoundException{
88         String JavaDoc paths = System.getProperty("java.class.path");
89         String JavaDoc partFileName = fullClassName.replace('.',charfileSeparator);
90         String JavaDoc partFile = fileSeparator+partFileName+".class";
91         Iterator iter = list.iterator();
92         while (iter.hasNext()){
93             String JavaDoc testPath = (String JavaDoc)iter.next();
94             File testFile = new File(testPath+partFile);
95             if (testFile.exists()){
96                 return testFile;
97             }
98         }
99
100         throw new FileNotFoundException("The following class was not found on class path : "+
101             fullClassName+"\nCommon error is that your class path was not set properly");
102     }
103
104     /**
105      * Returns a File object representing the location of a resource file on the
106      * file system for a given resource name.
107      *
108      * @param resourceFile in format ("za.jdo").
109      * @return File object of where resource file is on file system.
110      * @throws FileNotFoundException if the resource file could not be found
111      */

112     public File getResourceFile(String JavaDoc resourceFile)throws FileNotFoundException{
113
114         String JavaDoc partFile = fileSeparator+resourceFile;
115         Iterator iter = list.iterator();
116         while (iter.hasNext()){
117             String JavaDoc testPath = (String JavaDoc)iter.next();
118             File testFile = new File(testPath+partFile);
119             if (testFile.exists()){
120                 return testFile;
121             }
122         }
123
124
125
126         throw new FileNotFoundException("The following resource was not found on class path : "+
127             resourceFile+"\nCommon error is that your class path was not set properly");
128     }
129     public boolean classExists(String JavaDoc clazz) throws ClassNotFoundException JavaDoc {
130         JavaClass javaClass = Repository.lookupClass(clazz);
131         if (javaClass != null)return true;
132         return false;
133
134     }
135
136 }
137
Popular Tags