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;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IWorkspace;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.jobs.ISchedulingRule;
29 import net.sourceforge.phpdt.internal.core.CompilationUnit;
30 import net.sourceforge.phpdt.internal.core.JavaElementDeltaBuilder;
31 import net.sourceforge.phpdt.internal.core.PackageFragmentRoot;
32 import net.sourceforge.phpdt.internal.core.util.Util;
36 * Commits the contents of a working copy compilation
37 * unit to its original element and resource, bringing
38 * the Java Model up-to-date with the current contents of the working
41 * <p>It is possible that the contents of the
42 * original resource have changed since the working copy was created,
43 * in which case there is an update conflict. This operation allows
44 * for two settings to resolve conflict set by the <code>fForce</code> flag:<ul>
45 * <li>force flag is <code>false</code> - in this case an <code>JavaModelException</code>
47 * <li>force flag is <code>true</code> - in this case the contents of
48 * the working copy are applied to the underlying resource even though
49 * the working copy was created before a subsequent change in the
53 * <p>The default conflict resolution setting is the force flag is <code>false</code>
55 * A JavaModelOperation exception is thrown either if the commit could not
56 * be performed or if the new content of the compilation unit violates some Java Model
57 * constraint (e.g. if the new package declaration doesn't match the name of the folder
58 * containing the compilation unit).
60 public class CommitWorkingCopyOperation extends JavaModelOperation {
62 * Constructs an operation to commit the contents of a working copy
63 * to its original compilation unit.
65 public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
66 super(new IJavaElement[] {element}, force);
69 * @exception JavaModelException if setting the source
70 * of the original compilation unit fails
72 protected void executeOperation() throws JavaModelException {
74 beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
75 CompilationUnit workingCopy = getCompilationUnit();
76 IFile resource = (IFile)workingCopy.getResource();
77 ICompilationUnit primary = workingCopy.getPrimary();
78 boolean isPrimary = workingCopy.isPrimary();
80 JavaElementDeltaBuilder deltaBuilder = null;
81 // PackageFragmentRoot root = (PackageFragmentRoot)workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
82 boolean isIncluded = !Util.isExcluded(workingCopy);
83 // if (isPrimary || (root.isOnClasspath() && isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
84 if (isPrimary || (isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
86 // force opening so that the delta builder can get the old info
87 if (!isPrimary && !primary.isOpen()) {
91 // creates the delta builder (this remembers the content of the cu) if:
92 // - it is not excluded
93 // - and it is not a primary or it is a non-consistent primary
94 if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
95 deltaBuilder = new JavaElementDeltaBuilder(primary);
99 IBuffer primaryBuffer = primary.getBuffer();
101 if (primaryBuffer == null) return;
102 char[] primaryContents = primaryBuffer.getCharacters();
103 boolean hasSaved = false;
105 IBuffer workingCopyBuffer = workingCopy.getBuffer();
106 if (workingCopyBuffer == null) return;
107 primaryBuffer.setContents(workingCopyBuffer.getCharacters());
108 primaryBuffer.save(this.progressMonitor, this.force);
109 primary.makeConsistent(this);
113 // restore original buffer contents since something went wrong
114 primaryBuffer.setContents(primaryContents);
118 // for a primary working copy no need to set the content of the buffer again
119 primaryBuffer.save(this.progressMonitor, this.force);
120 primary.makeConsistent(this);
123 // working copy on cu outside classpath OR resource doesn't exist yet
124 String encoding = null;
126 encoding = resource.getCharset();
128 catch (CoreException ce) {
131 String contents = workingCopy.getSource();
132 if (contents == null) return;
134 byte[] bytes = encoding == null
135 ? contents.getBytes()
136 : contents.getBytes(encoding);
137 ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
138 if (resource.exists()) {
139 resource.setContents(
141 this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
147 this.progressMonitor);
149 } catch (CoreException e) {
150 throw new JavaModelException(e);
151 } catch (UnsupportedEncodingException e) {
152 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
157 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
159 // make sure working copy is in sync
160 workingCopy.updateTimeStamp((CompilationUnit)primary);
161 workingCopy.makeConsistent(this);
165 if (deltaBuilder != null) {
166 deltaBuilder.buildDeltas();
168 // add the deltas to the list of deltas created during this operation
169 if (deltaBuilder.delta != null) {
170 addDelta(deltaBuilder.delta);
180 * Returns the compilation unit this operation is working on.
182 protected CompilationUnit getCompilationUnit() {
183 return (CompilationUnit)getElementToProcess();
185 protected ISchedulingRule getSchedulingRule() {
186 IResource resource = getElementToProcess().getResource();
187 IWorkspace workspace = resource.getWorkspace();
188 if (resource.exists()) {
189 return workspace.getRuleFactory().modifyRule(resource);
191 return workspace.getRuleFactory().createRule(resource);
195 * Possible failures: <ul>
196 * <li>INVALID_ELEMENT_TYPES - the compilation unit supplied to this
197 * operation is not a working copy
198 * <li>ELEMENT_NOT_PRESENT - the compilation unit the working copy is
199 * based on no longer exists.
200 * <li>UPDATE_CONFLICT - the original compilation unit has changed since
201 * the working copy was created and the operation specifies no force
202 * <li>READ_ONLY - the original compilation unit is in read-only mode
205 public IJavaModelStatus verify() {
206 ICompilationUnit cu = getCompilationUnit();
207 if (!cu.isWorkingCopy()) {
208 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
210 ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
211 IResource resource = original.getResource();
212 if (!cu.isBasedOn(resource) && !force) {
213 return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
215 // no read-only check, since some repository adapters can change the flag on save
217 return JavaModelStatus.VERIFIED_OK;