KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > JavaDependencyParser


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.nio.*;
25 import java.nio.channels.*;
26 import java.util.*;
27
28 /**
29 Information for this parser comes from
30 http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html
31
32 */

33 class JavaDependencyParser
34         implements DependencyParser, Serializable
35     {
36     private short getShort(FileInputStream fis)
37             throws IOException
38         {
39         ByteBuffer buf = ByteBuffer.allocate(2);
40         buf.order(ByteOrder.BIG_ENDIAN);
41         fis.getChannel().read(buf);
42         buf.rewind();
43         return (buf.getShort());
44         }
45         
46     private byte[] read(FileInputStream fis, int readSz)
47             throws IOException
48         {
49         byte[] buf = new byte[readSz];
50         fis.read(buf);
51         return (buf);
52         }
53         
54     public boolean canRecurse()
55         {
56         return (false);
57         }
58         
59     public String JavaDoc[] parseFile(File file)
60             throws IOException
61         {
62         int magic;
63         short majorVer;
64         short minVer;
65         short tableSz;
66         byte tag;
67         short strSz;
68         String JavaDoc className;
69         Map tableStrs = new HashMap();
70         Stack classEntries = new Stack();
71         Vector dependency = new Vector();
72         ByteBuffer header = ByteBuffer.allocate(10);
73         FileInputStream fis = new FileInputStream(file);
74         //FileChannel fc = fis.getChannel();
75

76         header.order(ByteOrder.BIG_ENDIAN);
77         
78         fis.getChannel().read(header);
79         header.rewind();
80         magic = header.getInt();
81         
82         if (magic != 0xCAFEBABE)
83             return (new String JavaDoc[0]);
84             
85         majorVer = header.getShort();
86         minVer = header.getShort();
87         if ((majorVer != 0) && (minVer < 48))
88             return (new String JavaDoc[0]);
89         
90         //If we got here the file is good and we can start reading in the table
91
tableSz = header.getShort();
92         for (int I = 1; I < tableSz; I++)
93             {
94             //Watch for end of stream -1
95
tag = (byte)fis.read();
96             switch (tag)
97                 {
98                 case 1:
99                     strSz = getShort(fis);
100                     tableStrs.put(new Integer JavaDoc(I), new String JavaDoc(read(fis, strSz)));
101                     break;
102                 case 7:
103                     classEntries.push(new Integer JavaDoc(getShort(fis)));
104                     break;
105                 case 8:
106                     fis.skip(2);
107                     break;
108                 case 3:
109                 case 4:
110                 case 9:
111                 case 10:
112                 case 11:
113                 case 12:
114                     fis.skip(4);
115                     break;
116                 case 5:
117                 case 6:
118                     fis.skip(8);
119                     break;
120                 case 0:
121                     I = tableSz;
122                     break;
123                 default:
124                     System.out.println("Unknown tag "+tag);
125                 
126                 }
127             }
128         
129         fis.close();
130         
131         while (!classEntries.empty())
132             {
133             className = (String JavaDoc)tableStrs.get((Integer JavaDoc)classEntries.pop());
134             if (className != null)
135                 {
136                 //System.out.println(className);
137
dependency.add(className+".java");
138                 }
139             }
140         
141         return ((String JavaDoc[])dependency.toArray(new String JavaDoc[0]));
142         }
143     }
144
Popular Tags