KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > roblisa > classfinder > FileParser


1 /**
2  * ClassFinder - a javadoc webserver.
3  * Copyright (C) 2003 Rob Nielsen
4  * rob@roblisa.com
5  * http://www.roblisa.com/java/classfinder/
6  *
7  * Released under the GNU GPL - http://www.gnu.org/copyleft/gpl.html
8  */

9 package com.roblisa.classfinder;
10
11 import java.io.*;
12 import java.util.*;
13 import java.net.*;
14
15 /**
16  * Provides several assorted methods for getting information out of
17  * specific files such as packages out of java and class files and links
18  * out of .lnk and .url files.
19  */

20 public class FileParser
21 {
22     /**
23      * Gets a list of URLs in a .url file. URLs are taken as lines starting
24      * with either <code>http://</code> or <code>URL=http://</code>. This
25      * provides compatibility with Windows and it's Internet Shortcuts (which
26      * only contain one URL per file) and files on other platforms which just
27      * contain a list of URLs, one per line.
28      * @param file the file to get urls from
29      * @return a list of the urls found
30      */

31     public static List getURLs(File file) throws IOException
32     {
33         List ret=new ArrayList();
34         BufferedReader br=new BufferedReader(new FileReader(file));
35         String line=null;
36         while((line=br.readLine())!=null)
37         {
38             if (line.startsWith("URL="))
39                 line=line.substring(4);
40             if (line.startsWith("http://"))
41             {
42                 if (line.endsWith(".html"))
43                 {
44                     int slash=line.lastIndexOf('/');
45                     line=line.substring(0,slash+1);
46                 }
47                 else
48                 if (line.charAt(line.length()-1)!='/')
49                 {
50                     line=line+"/";
51                 }
52                 ret.add(line);
53             }
54         }
55         br.close();
56         return ret;
57     }
58
59     /**
60      * Gets the package from a java source file. Looks for a line starting with
61      * <code>package</code>. Unpackaged classes will return "", parsing problems
62      * will return null.
63      * @param is the stream to read the file from
64      * @return the package, separated by .'s eg "java.lang"
65      */

66     public static String getJavaPackage(InputStream is)
67     {
68         String ret=null;
69         try
70         {
71             BufferedReader br = new BufferedReader(
72                                         new InputStreamReader(is));
73             String line = null;
74
75             while ((line = br.readLine()) != null)
76             {
77                 line = line.trim();
78                 if (line.startsWith("package "))
79                 {
80                     ret=line.substring(8,
81                                      line.length() - 1);
82                     break;
83                 }
84             }
85             is.close();
86             if (ret==null)
87                 ret="";
88         }
89         catch(IOException e)
90         {
91             System.out.println("Can't read java file");
92         }
93         return ret;
94     }
95
96     /**
97      * Gets the package from a java class file. Parses the binary file for the own
98      * class entry. Unpackaged classes will return "", parsing problems
99      * will return null.
100      * @param is the stream to read the file from
101      * @return the package, separated by .'s eg "java.lang"
102      */

103     public static String getClassPackage(InputStream is)
104     {
105         try
106         {
107             DataInputStream in = new DataInputStream(is);
108
109             // Read the header
110
int magic = in.readInt();
111
112             if(magic != 0xCAFEBABE)
113             {
114                 throw new ClassFormatError("wrong magic!");
115             }
116
117             int minor_version = in.readUnsignedShort();
118             int version = in.readUnsignedShort();
119
120             // Read the constant pool
121
int numClasses = 0;
122             Object[] cpool = new Object[in.readUnsignedShort()];
123
124             for(int i = 1;i < cpool.length;i++)
125             {
126                 int type=in.readByte();
127                 switch(type)
128                 {
129                     case 1: //CONSTANT_UTF8:
130
cpool[i] = in.readUTF().replace('/','.');
131                         break;
132                     case 5: //CONSTANT_LONG:
133
case 6: //CONSTANT_DOUBLE:
134
i++;
135                         in.skipBytes(8);
136
137                         break;
138                     case 7: //CONSTANT_CLASS:
139
numClasses++;
140                         cpool[i] = new Integer(in.readUnsignedShort());
141
142                         break;
143                     case 8: //CONSTANT_STRING:
144
in.skipBytes(2);
145                         break;
146                     case 9: //CONSTANT_FIELD:
147
case 10: //CONSTANT_METHOD:
148
case 11: //CONSTANT_INTERFACEMETHOD:
149
case 12: //CONSTANT_NAMEANDTYPE:
150
case 3: //CONSTANT_INTEGER:
151
case 4: //CONSTANT_FLOAT:
152
in.skipBytes(4);
153                         break;
154                     case 0:
155                     default:
156                         throw new ClassFormatError("invalid constant type: "+
157                                                    type);
158                 }
159             }
160
161             in.skipBytes(2);
162             String className=(String)cpool[((Integer)cpool[in.readUnsignedShort()]).intValue()];
163             is.close();
164             int dot=className.lastIndexOf(".");
165             if (dot!=-1)
166             {
167                 className=className.substring(0,dot);
168             }
169             else
170             {
171                 className="";
172             }
173             return className;
174         }
175         catch(IOException e)
176         {
177             System.out.println("Can't read class");
178         }
179         return null;
180     }
181
182     /**
183      * Parse a windows shortcut file (.lnk). Uses the sun class sun.awt.shell.ShellFolder which is not
184      * guaranteed to be available.
185      * @param fName the file to parse
186      * @return the read file
187      */

188     public static File parseLink(File fName)
189     {
190         try
191         {
192             File f=sun.awt.shell.ShellFolder.getShellFolder(fName).getLinkLocation();
193             return f.getCanonicalFile();
194         }
195         catch(IOException e)
196         {
197             e.printStackTrace();
198             return null;
199         }
200
201     }
202 }
203
Popular Tags