1 9 package com.roblisa.classfinder; 10 11 import java.io.*; 12 import java.util.*; 13 import java.net.*; 14 15 20 public class FileParser 21 { 22 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 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 103 public static String getClassPackage(InputStream is) 104 { 105 try 106 { 107 DataInputStream in = new DataInputStream(is); 108 109 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 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: cpool[i] = in.readUTF().replace('/','.'); 131 break; 132 case 5: case 6: i++; 135 in.skipBytes(8); 136 137 break; 138 case 7: numClasses++; 140 cpool[i] = new Integer(in.readUnsignedShort()); 141 142 break; 143 case 8: in.skipBytes(2); 145 break; 146 case 9: case 10: case 11: case 12: case 3: case 4: 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 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 |