1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.io.ByteArrayInputStream;
14 import java.io.UnsupportedEncodingException;
16 import net.sourceforge.phpdt.core.IBuffer;
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IJavaModelStatus;
20 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.internal.core.util.Util;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspace;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.jobs.ISchedulingRule;
32 * Commits the contents of a working copy compilation
33 * unit to its original element and resource, bringing
34 * the Java Model up-to-date with the current contents of the working
37 * <p>It is possible that the contents of the
38 * original resource have changed since the working copy was created,
39 * in which case there is an update conflict. This operation allows
40 * for two settings to resolve conflict set by the <code>fForce</code> flag:<ul>
41 * <li>force flag is <code>false</code> - in this case an <code>JavaModelException</code>
43 * <li>force flag is <code>true</code> - in this case the contents of
44 * the working copy are applied to the underlying resource even though
45 * the working copy was created before a subsequent change in the
49 * <p>The default conflict resolution setting is the force flag is <code>false</code>
51 * A JavaModelOperation exception is thrown either if the commit could not
52 * be performed or if the new content of the compilation unit violates some Java Model
53 * constraint (e.g. if the new package declaration doesn't match the name of the folder
54 * containing the compilation unit).
56 public class CommitWorkingCopyOperation extends JavaModelOperation {
58 * Constructs an operation to commit the contents of a working copy
59 * to its original compilation unit.
61 public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
62 super(new IJavaElement[] {element}, force);
65 * @exception JavaModelException if setting the source
66 * of the original compilation unit fails
68 protected void executeOperation() throws JavaModelException {
70 beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
71 CompilationUnit workingCopy = getCompilationUnit();
72 IFile resource = (IFile)workingCopy.getResource();
73 ICompilationUnit primary = workingCopy.getPrimary();
74 boolean isPrimary = workingCopy.isPrimary();
76 JavaElementDeltaBuilder deltaBuilder = null;
77 // PackageFragmentRoot root = (PackageFragmentRoot)workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
78 boolean isIncluded = !Util.isExcluded(workingCopy);
79 // if (isPrimary || (root.isOnClasspath() && isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
80 if (isPrimary || (isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
82 // force opening so that the delta builder can get the old info
83 if (!isPrimary && !primary.isOpen()) {
87 // creates the delta builder (this remembers the content of the cu) if:
88 // - it is not excluded
89 // - and it is not a primary or it is a non-consistent primary
90 if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
91 deltaBuilder = new JavaElementDeltaBuilder(primary);
95 IBuffer primaryBuffer = primary.getBuffer();
97 if (primaryBuffer == null) return;
98 char[] primaryContents = primaryBuffer.getCharacters();
99 boolean hasSaved = false;
101 IBuffer workingCopyBuffer = workingCopy.getBuffer();
102 if (workingCopyBuffer == null) return;
103 primaryBuffer.setContents(workingCopyBuffer.getCharacters());
104 primaryBuffer.save(this.progressMonitor, this.force);
105 primary.makeConsistent(this);
109 // restore original buffer contents since something went wrong
110 primaryBuffer.setContents(primaryContents);
114 // for a primary working copy no need to set the content of the buffer again
115 primaryBuffer.save(this.progressMonitor, this.force);
116 primary.makeConsistent(this);
119 // working copy on cu outside classpath OR resource doesn't exist yet
120 String encoding = null;
122 encoding = resource.getCharset();
124 catch (CoreException ce) {
127 String contents = workingCopy.getSource();
128 if (contents == null) return;
130 byte[] bytes = encoding == null
131 ? contents.getBytes()
132 : contents.getBytes(encoding);
133 ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
134 if (resource.exists()) {
135 resource.setContents(
137 this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
143 this.progressMonitor);
145 } catch (CoreException e) {
146 throw new JavaModelException(e);
147 } catch (UnsupportedEncodingException e) {
148 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
153 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
155 // make sure working copy is in sync
156 workingCopy.updateTimeStamp((CompilationUnit)primary);
157 workingCopy.makeConsistent(this);
161 if (deltaBuilder != null) {
162 deltaBuilder.buildDeltas();
164 // add the deltas to the list of deltas created during this operation
165 if (deltaBuilder.delta != null) {
166 addDelta(deltaBuilder.delta);
176 * Returns the compilation unit this operation is working on.
178 protected CompilationUnit getCompilationUnit() {
179 return (CompilationUnit)getElementToProcess();
181 protected ISchedulingRule getSchedulingRule() {
182 IResource resource = getElementToProcess().getResource();
183 IWorkspace workspace = resource.getWorkspace();
184 if (resource.exists()) {
185 return workspace.getRuleFactory().modifyRule(resource);
187 return workspace.getRuleFactory().createRule(resource);
191 * Possible failures: <ul>
192 * <li>INVALID_ELEMENT_TYPES - the compilation unit supplied to this
193 * operation is not a working copy
194 * <li>ELEMENT_NOT_PRESENT - the compilation unit the working copy is
195 * based on no longer exists.
196 * <li>UPDATE_CONFLICT - the original compilation unit has changed since
197 * the working copy was created and the operation specifies no force
198 * <li>READ_ONLY - the original compilation unit is in read-only mode
201 public IJavaModelStatus verify() {
202 ICompilationUnit cu = getCompilationUnit();
203 if (!cu.isWorkingCopy()) {
204 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
206 ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
207 IResource resource = original.getResource();
208 if (!cu.isBasedOn(resource) && !force) {
209 return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
211 // no read-only check, since some repository adapters can change the flag on save
213 return JavaModelStatus.VERIFIED_OK;