1 11 package org.eclipse.jdt.apt.core.internal.env; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.OutputStream ; 16 import java.io.OutputStreamWriter ; 17 import java.io.PrintWriter ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.jdt.apt.core.env.Phase; 25 import org.eclipse.jdt.apt.core.internal.AptPlugin; 26 import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedSourceFolderManager; 27 28 import com.sun.mirror.apt.Filer; 29 30 34 public class BuildFilerImpl extends FilerImpl { 35 36 private boolean _generatedClassFiles = false; 37 private final BuildEnv _env; 38 39 public BuildFilerImpl(BuildEnv env) { 40 _env = env; 41 } 42 43 51 public OutputStream createClassFile(String typeName) throws IOException 52 { 53 if (typeName == null) 54 throw new IllegalArgumentException ("Type name cannot be null"); if ("".equals(typeName)) throw new IllegalArgumentException ("Type name cannot be empty"); 58 _env.checkValid(); 59 try { 60 _env.validateTypeName(typeName); 61 } catch (CoreException e) { 62 IOException ioe = new IOException (); 63 ioe.initCause(e); 64 throw ioe; 65 } 66 _generatedClassFiles = true; 67 68 if (_env.getPhase() == Phase.RECONCILE) { 70 return new NoOpOutputStream(); 71 } 72 73 GeneratedSourceFolderManager gsfm = _env.getAptProject().getGeneratedSourceFolderManager(); 74 IPath path; 75 try 76 { 77 path = gsfm.getBinaryOutputLocation(); 78 } 79 catch ( Exception e ) 80 { 81 AptPlugin.log(e, "Failure getting the output file"); throw new IOException (); 84 } 85 86 path = path.append(typeName.replace('.', File.separatorChar) + ".class"); 88 IFile file = getEnv().getProject().getFile(path); 89 return new BinaryFileOutputStream(file, _env); 90 } 91 92 public boolean hasGeneratedClassFile(){ return _generatedClassFiles; } 93 94 111 public PrintWriter createTextFile(Filer.Location loc, String pkg, File relPath, String charsetName) 112 throws IOException 113 { 114 if (relPath == null) 115 throw new IllegalArgumentException ("Path cannot be null"); if ("".equals(relPath.getPath())) throw new IllegalArgumentException ("Path cannot be empty"); 119 _env.checkValid(); 120 121 if (_env.getPhase() == Phase.RECONCILE) { 123 return new NoOpPrintWriter(); 124 } 125 126 127 IPath path = getOutputFileForLocation( loc, pkg, relPath ); 128 IFile file = _env.getProject().getFile(path); 129 validateFile(file); 130 OutputStream binaryOut = new EncodedFileOutputStream(file, _env, charsetName); 131 132 if (charsetName == null) { 133 return new PrintWriter (binaryOut); 134 } 135 else { 136 OutputStreamWriter outWriter = new OutputStreamWriter (binaryOut, charsetName); 137 return new PrintWriter (outWriter); 138 } 139 } 140 141 154 public OutputStream createBinaryFile(Filer.Location loc, String pkg, File relPath) 155 throws IOException 156 { 157 if (relPath == null) 158 throw new IllegalArgumentException ("Path cannot be null"); if ("".equals(relPath.getPath())) throw new IllegalArgumentException ("Path cannot be empty"); 162 _env.checkValid(); 163 164 if (_env.getPhase() == Phase.RECONCILE) { 166 return new NoOpOutputStream(); 167 } 168 169 IPath path = getOutputFileForLocation( loc, pkg, relPath ); 170 IFile file = _env.getProject().getFile(path); 171 validateFile(file); 172 return new BinaryFileOutputStream(file, _env); 173 } 174 175 @Override 176 protected AbstractCompilationEnv getEnv() { 177 return _env; 178 } 179 180 private void validateFile(IFile file) throws IOException 181 { 182 IStatus status = file.getWorkspace().validatePath(file.getFullPath().toOSString(), IResource.FILE); 183 if (!status.isOK()) { 184 CoreException ce = new CoreException(status); 185 IOException ioe = new IOException ("Invalid file name"); ioe.initCause(ce); 187 throw ioe; 188 } 189 } 190 191 } 192 | Popular Tags |