KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > frontend > TargetFactory


1 package polyglot.frontend;
2
3 import polyglot.main.Options;
4 import polyglot.main.Report;
5 import polyglot.types.*;
6 import polyglot.util.*;
7
8 import java.io.*;
9 import java.util.*;
10
11 /** A <code>TargetFactory</code> is responsible for opening output files. */
12 public class TargetFactory
13 {
14     File outputDirectory;
15     String JavaDoc outputExtension;
16     boolean outputStdout;
17
18     public TargetFactory(File outDir, String JavaDoc outExt, boolean so) {
19     outputDirectory = outDir;
20     outputExtension = outExt;
21     outputStdout = so;
22     }
23
24     /** Open a writer to the output file for the class in the given package. */
25     public Writer outputWriter(String JavaDoc packageName, String JavaDoc className,
26         Source source) throws IOException
27     {
28     return outputWriter(outputFile(packageName, className, source));
29     }
30
31     /** Open a writer to the output file. */
32     public Writer outputWriter(File outputFile) throws IOException {
33     if (Report.should_report(Report.frontend, 2))
34         Report.report(2, "Opening " + outputFile + " for output.");
35
36     if (outputStdout) {
37         return new UnicodeWriter(new PrintWriter(System.out));
38     }
39
40     if (! outputFile.getParentFile().exists()) {
41         File parent = outputFile.getParentFile();
42         parent.mkdirs();
43     }
44
45     return new UnicodeWriter(new FileWriter(outputFile));
46     }
47
48     /** Return a file object for the output of the source file in the given package. */
49     public File outputFile(String JavaDoc packageName, Source source) {
50     String JavaDoc name;
51     name = new File(source.name()).getName();
52     name = name.substring(0, name.lastIndexOf('.'));
53     return outputFile(packageName, name, source);
54     }
55
56     /** Return a file object for the output of the class in the given package. */
57     public File outputFile(String JavaDoc packageName, String JavaDoc className, Source source)
58     {
59     if (outputDirectory == null) {
60           throw new InternalCompilerError("Output directory not set.");
61     }
62
63     if (packageName == null) {
64         packageName = "";
65     }
66
67     File outputFile = new File(outputDirectory,
68                    packageName.replace('.', File.separatorChar)
69                    + File.separatorChar
70                    + className
71                    + "." + outputExtension);
72
73         if (source != null && outputFile.getPath().equals(source.path())) {
74         throw new InternalCompilerError("The output file is the same as the source file");
75     }
76     
77     return outputFile;
78     }
79 }
80
Popular Tags