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 12 public class TargetFactory 13 { 14 File outputDirectory; 15 String outputExtension; 16 boolean outputStdout; 17 18 public TargetFactory(File outDir, String outExt, boolean so) { 19 outputDirectory = outDir; 20 outputExtension = outExt; 21 outputStdout = so; 22 } 23 24 25 public Writer outputWriter(String packageName, String className, 26 Source source) throws IOException 27 { 28 return outputWriter(outputFile(packageName, className, source)); 29 } 30 31 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 49 public File outputFile(String packageName, Source source) { 50 String name; 51 name = new File(source.name()).getName(); 52 name = name.substring(0, name.lastIndexOf('.')); 53 return outputFile(packageName, name, source); 54 } 55 56 57 public File outputFile(String packageName, String 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 |