KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jasmin > Main


1 /* --- Copyright Jonathan Meyer 1996. All rights reserved. -----------------
2  > File: jasmin/src/jasmin/Main.java
3  > Purpose: Runs Jasmin, parsing any command line arguments
4  > Author: Jonathan Meyer, 10 July 1996
5  */

6 // Modifications Copyright (C) 2004 Ondrej Lhotak
7

8
9 package jasmin;
10
11 import java.io.*;
12 import jas.jasError;
13
14 /**
15  * Main is the main entry point for Jasmin - it supplies the main()
16  * method, as well as a few other useful odds and ends.
17  */

18 public class Main {
19     public static void assemble(InputStream in, OutputStream out, boolean number_lines) {
20         ClassFile classFile = new ClassFile();
21
22         try {
23             InputStream inp = new BufferedInputStream(in);
24             classFile.readJasmin(inp, "Jasmin", number_lines);
25             inp.close();
26
27             // if we got some errors, don't output a file - just return.
28
if (classFile.errorCount() > 0) {
29                 System.err.println("Jasmin: Found "
30                         + classFile.errorCount() + " errors");
31                 return;
32             }
33
34             classFile.write(out);
35             out.flush();
36         } catch (java.io.FileNotFoundException JavaDoc e) {
37             System.err.println("Jasmin: file not found");
38             System.exit(-1);
39         } catch (jasError e) {
40             classFile.report_error("JAS Error " + e.getMessage());
41             e.printStackTrace();
42         } catch (Exception JavaDoc e) {
43             classFile.report_error("Jasmin: exception - <" +
44                     e.getClass().getName() + "> " + e.getMessage() +
45                     ".");
46             e.printStackTrace();
47         }
48         if (classFile.errorCount() > 0) {
49             System.err.println("Jasmin: Found "
50                     + classFile.errorCount() + " errors");
51         }
52     }
53
54     /**
55      * The Jasmin version
56      */

57     public static final String JavaDoc version = "2.2.3";
58
59     /**
60      * Called to assemble a single file.
61      * @param dest_dir is the directory to place the result in.
62      * @param fname is the name of the file containing the Jasmin source code.
63      * @param number_lines is true if you want Jasmin to generate line numbers
64      * automatically, or false if you are generating line numbers yourself.
65      */

66     public static void assemble(String JavaDoc dest_dir, String JavaDoc fname,
67                 boolean number_lines) {
68         File file = new File(fname);
69         File out_file = null;
70     ClassFile classFile = new ClassFile();
71
72     try {
73         InputStream inp = new BufferedInputStream(new FileInputStream(fname));
74             classFile.readJasmin(inp, file.getName(), number_lines);
75         inp.close();
76
77             // if we got some errors, don't output a file - just return.
78
if (classFile.errorCount() > 0) {
79                 System.err.println(fname + ": Found "
80                 + classFile.errorCount() + " errors");
81         return;
82             }
83
84             String JavaDoc class_path[] = (ScannerUtils.splitClassField(
85                         classFile.getClassName()));
86             String JavaDoc class_name = class_path[1];
87
88             // determine where to place this class file
89
if (class_path[0] != null) {
90                 String JavaDoc class_dir = ScannerUtils.convertChars(
91                        class_path[0], "./",
92                                            File.separatorChar);
93                 if (dest_dir != null) {
94                     dest_dir = dest_dir + File.separator + class_dir;
95                 } else {
96                     dest_dir = class_dir;
97                 }
98             }
99             if (dest_dir == null) {
100                 out_file = new File(class_name + ".class");
101             } else {
102                 out_file = new File(dest_dir, class_name + ".class");
103
104                 // check that dest_dir exists
105

106                 File dest = new File(dest_dir);
107                 if (!dest.exists()) {
108                     dest.mkdirs();
109                 }
110
111                 if (!dest.isDirectory()) {
112                     throw new IOException("Cannot create directory");
113                 }
114             }
115
116             FileOutputStream outp = new FileOutputStream(out_file);
117             classFile.write(outp);
118         outp.close();
119             // System.out.println("Generated: " + out_file.getPath());
120

121         } catch (java.io.FileNotFoundException JavaDoc e) {
122             System.err.println(fname + ": file not found");
123             System.exit(-1);
124         } catch (jasError e) {
125             classFile.report_error("JAS Error " + e.getMessage());
126             e.printStackTrace();
127         } catch (Exception JavaDoc e) {
128         classFile.report_error(fname + ": exception - <" +
129                               e.getClass().getName() + "> " + e.getMessage() +
130                               ".");
131         e.printStackTrace();
132         }
133     if (classFile.errorCount() > 0) {
134             System.err.println(fname + ": Found "
135                 + classFile.errorCount() + " errors");
136     }
137     }
138
139     public static void main(String JavaDoc args[]) {
140         int i;
141         String JavaDoc dest_dir = null;
142         boolean debug = false;
143
144         String JavaDoc files[] = new String JavaDoc[args.length];
145         int num_files = 0;
146
147         if (args.length == 0) {
148             System.err.println("usage: jasmin [-d <directory>] [-version] <file> [<file> ...]");
149             System.exit(-1);
150         }
151
152         for (i = 0; i < args.length; i++) {
153             if (args[i].equals("-d")) {
154                 dest_dir = args[i + 1];
155                 i++;
156             } else if (args[i].equals("-g")) {
157                 debug = true;
158             } else if (args[i].equals("-version")) {
159                 System.out.println("Jasmin version: " + version);
160         System.exit(0);
161             } else {
162                 files[num_files++] = args[i];
163             }
164         }
165
166         for (i = 0; i < num_files; i++) {
167             assemble(dest_dir, files[i], debug);
168         }
169     }
170 };
171
172 /* --- Revision History ---------------------------------------------------
173 --- Jonathan Meyer, Mar 1 1997 tidied error reporting, renamed Jasmin->ClassFile
174 --- Jonathan Meyer, Feb 8 1997 added the assemble() method
175 --- Jonathan Meyer, July 24 1996 added -version flag.
176 */

177
178
179
180
181
Popular Tags