1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.UnsupportedEncodingException ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IWorkspace; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.jobs.ISchedulingRule; 21 import org.eclipse.jdt.core.IBuffer; 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaModelStatus; 25 import org.eclipse.jdt.core.IJavaModelStatusConstants; 26 import org.eclipse.jdt.core.IJavaProject; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.internal.core.util.Messages; 30 import org.eclipse.jdt.internal.core.util.Util; 31 32 57 public class CommitWorkingCopyOperation extends JavaModelOperation { 58 62 public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) { 63 super(new IJavaElement[] {element}, force); 64 } 65 69 protected void executeOperation() throws JavaModelException { 70 try { 71 beginTask(Messages.workingCopy_commit, 2); 72 CompilationUnit workingCopy = getCompilationUnit(); 73 74 if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(workingCopy.getJavaProject().getElementName())) { 75 workingCopy.getBuffer().save(this.progressMonitor, this.force); 77 return; 78 } 79 80 ICompilationUnit primary = workingCopy.getPrimary(); 81 boolean isPrimary = workingCopy.isPrimary(); 82 83 JavaElementDeltaBuilder deltaBuilder = null; 84 PackageFragmentRoot root = (PackageFragmentRoot)workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 85 boolean isIncluded = !Util.isExcluded(workingCopy); 86 IFile resource = (IFile)workingCopy.getResource(); 87 IJavaProject project = root.getJavaProject(); 88 if (isPrimary || (root.validateOnClasspath().isOK() && isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName(), project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)))) { 89 90 if (!isPrimary && !primary.isOpen()) { 92 primary.open(null); 93 } 94 95 if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) { 99 deltaBuilder = new JavaElementDeltaBuilder(primary); 100 } 101 102 IBuffer primaryBuffer = primary.getBuffer(); 104 if (!isPrimary) { 105 if (primaryBuffer == null) return; 106 char[] primaryContents = primaryBuffer.getCharacters(); 107 boolean hasSaved = false; 108 try { 109 IBuffer workingCopyBuffer = workingCopy.getBuffer(); 110 if (workingCopyBuffer == null) return; 111 primaryBuffer.setContents(workingCopyBuffer.getCharacters()); 112 primaryBuffer.save(this.progressMonitor, this.force); 113 primary.makeConsistent(this); 114 hasSaved = true; 115 } finally { 116 if (!hasSaved){ 117 primaryBuffer.setContents(primaryContents); 119 } 120 } 121 } else { 122 primaryBuffer.save(this.progressMonitor, this.force); 124 primary.makeConsistent(this); 125 } 126 } else { 127 String encoding = null; 129 try { 130 encoding = resource.getCharset(); 131 } 132 catch (CoreException ce) { 133 } 135 String contents = workingCopy.getSource(); 136 if (contents == null) return; 137 try { 138 byte[] bytes = encoding == null 139 ? contents.getBytes() 140 : contents.getBytes(encoding); 141 ByteArrayInputStream stream = new ByteArrayInputStream (bytes); 142 if (resource.exists()) { 143 resource.setContents( 144 stream, 145 this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, 146 null); 147 } else { 148 resource.create( 149 stream, 150 this.force, 151 this.progressMonitor); 152 } 153 } catch (CoreException e) { 154 throw new JavaModelException(e); 155 } catch (UnsupportedEncodingException e) { 156 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 157 } 158 159 } 160 161 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 162 163 workingCopy.updateTimeStamp((CompilationUnit)primary); 165 workingCopy.makeConsistent(this); 166 worked(1); 167 168 if (deltaBuilder != null) { 170 deltaBuilder.buildDeltas(); 171 172 if (deltaBuilder.delta != null) { 174 addDelta(deltaBuilder.delta); 175 } 176 } 177 worked(1); 178 } finally { 179 done(); 180 } 181 } 182 185 protected CompilationUnit getCompilationUnit() { 186 return (CompilationUnit)getElementToProcess(); 187 } 188 protected ISchedulingRule getSchedulingRule() { 189 IResource resource = getElementToProcess().getResource(); 190 if (resource == null) return null; 191 IWorkspace workspace = resource.getWorkspace(); 192 if (resource.exists()) { 193 return workspace.getRuleFactory().modifyRule(resource); 194 } else { 195 return workspace.getRuleFactory().createRule(resource); 196 } 197 } 198 209 public IJavaModelStatus verify() { 210 CompilationUnit cu = getCompilationUnit(); 211 if (!cu.isWorkingCopy()) { 212 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu); 213 } 214 if (cu.hasResourceChanged() && !this.force) { 215 return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT); 216 } 217 return JavaModelStatus.VERIFIED_OK; 220 } 221 } 222 | Popular Tags |