1 11 package org.eclipse.jdt.internal.corext.refactoring.nls.changes; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.UnsupportedEncodingException ; 17 import java.net.URI ; 18 19 import org.eclipse.core.filesystem.EFS; 20 import org.eclipse.core.filesystem.IFileInfo; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.OperationCanceledException; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 import org.eclipse.core.runtime.content.IContentType; 30 31 import org.eclipse.core.resources.IFile; 32 import org.eclipse.core.resources.IResource; 33 import org.eclipse.core.resources.ResourcesPlugin; 34 35 import org.eclipse.ltk.core.refactoring.Change; 36 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 37 38 import org.eclipse.jdt.core.IJavaModelStatusConstants; 39 import org.eclipse.jdt.core.JavaModelException; 40 41 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange; 42 import org.eclipse.jdt.internal.corext.util.Messages; 43 44 public class CreateFileChange extends JDTChange { 45 46 private String fChangeName; 47 48 private IPath fPath; 49 private String fSource; 50 private String fEncoding; 51 private boolean fExplicitEncoding; 52 private long fStampToRestore; 53 54 public CreateFileChange(IPath path, String source, String encoding) { 55 this(path, source, encoding, IResource.NULL_STAMP); 56 } 57 58 public CreateFileChange(IPath path, String source, String encoding, long stampToRestore) { 59 Assert.isNotNull(path, "path"); Assert.isNotNull(source, "source"); fPath= path; 62 fSource= source; 63 fEncoding= encoding; 64 fExplicitEncoding= fEncoding != null; 65 fStampToRestore= stampToRestore; 66 } 67 68 80 81 protected void setEncoding(String encoding, boolean explicit) { 82 Assert.isNotNull(encoding, "encoding"); fEncoding= encoding; 84 fExplicitEncoding= explicit; 85 } 86 87 public String getName() { 88 if (fChangeName == null) 89 return Messages.format(NLSChangesMessages.createFile_Create_file, fPath.toOSString()); 90 else 91 return fChangeName; 92 } 93 94 public void setName(String name) { 95 fChangeName= name; 96 } 97 98 protected void setSource(String source) { 99 fSource= source; 100 } 101 102 protected String getSource() { 103 return fSource; 104 } 105 106 protected void setPath(IPath path) { 107 fPath= path; 108 } 109 110 protected IPath getPath() { 111 return fPath; 112 } 113 114 public Object getModifiedElement() { 115 return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath); 116 } 117 118 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 119 RefactoringStatus result= new RefactoringStatus(); 120 IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath); 121 122 URI location= file.getLocationURI(); 123 if (location == null) { 124 result.addFatalError(Messages.format( 125 NLSChangesMessages.CreateFileChange_error_unknownLocation, 126 file.getFullPath().toString())); 127 return result; 128 } 129 130 IFileInfo jFile= EFS.getStore(location).fetchInfo(); 131 if (jFile.exists()) { 132 result.addFatalError(Messages.format( 133 NLSChangesMessages.CreateFileChange_error_exists, 134 file.getFullPath().toString())); 135 return result; 136 } 137 return result; 138 } 139 140 public Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException { 141 142 InputStream is= null; 143 try { 144 pm.beginTask(NLSChangesMessages.createFile_creating_resource, 3); 145 146 initializeEncoding(); 147 IFile file= getOldFile(new SubProgressMonitor(pm, 1)); 148 156 try { 157 is= new ByteArrayInputStream (fSource.getBytes(fEncoding)); 158 file.create(is, false, new SubProgressMonitor(pm, 1)); 159 if (fStampToRestore != IResource.NULL_STAMP) { 160 file.revertModificationStamp(fStampToRestore); 161 } 162 if (fExplicitEncoding) { 163 file.setCharset(fEncoding, new SubProgressMonitor(pm, 1)); 164 } else { 165 pm.worked(1); 166 } 167 return new DeleteFileChange(file); 168 } catch (UnsupportedEncodingException e) { 169 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 170 } 171 } finally { 172 try { 173 if (is != null) 174 is.close(); 175 } catch (IOException ioe) { 176 throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION); 177 } finally { 178 pm.done(); 179 } 180 } 181 } 182 183 protected IFile getOldFile(IProgressMonitor pm) throws OperationCanceledException { 184 pm.beginTask("", 1); try { 186 return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath); 187 } finally { 188 pm.done(); 189 } 190 } 191 192 private void initializeEncoding() { 193 if (fEncoding == null) { 194 fExplicitEncoding= false; 195 IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath); 196 if (file != null) { 197 try { 198 if (file.exists()) { 199 fEncoding= file.getCharset(false); 200 if (fEncoding == null) { 201 fEncoding= file.getCharset(true); 202 } else { 203 fExplicitEncoding= true; 204 } 205 } else { 206 IContentType contentType= Platform.getContentTypeManager().findContentTypeFor(file.getName()); 207 if (contentType != null) 208 fEncoding= contentType.getDefaultCharset(); 209 if (fEncoding == null) 210 fEncoding= file.getCharset(true); 211 } 212 } catch (CoreException e) { 213 fEncoding= ResourcesPlugin.getEncoding(); 214 fExplicitEncoding= true; 215 } 216 } else { 217 fEncoding= ResourcesPlugin.getEncoding(); 218 fExplicitEncoding= true; 219 } 220 } 221 Assert.isNotNull(fEncoding); 222 } 223 } 224 | Popular Tags |