b22eedee09f087fd62c3196e0ffbd47a1b530e84
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CommitWorkingCopyOperation.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.UnsupportedEncodingException;
15
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
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;
28
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;
33
34
35 /**
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
39  * copy.
40  *
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>
46  *      is thrown</li>
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
50  *      resource</li>
51  * </ul>
52  *
53  * <p>The default conflict resolution setting is the force flag is <code>false</code>
54  *
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).
59  */
60 public class CommitWorkingCopyOperation extends JavaModelOperation {
61         /**
62          * Constructs an operation to commit the contents of a working copy
63          * to its original compilation unit.
64          */
65         public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
66                 super(new IJavaElement[] {element}, force);
67         }
68         /**
69          * @exception JavaModelException if setting the source
70          *      of the original compilation unit fails
71          */
72         protected void executeOperation() throws JavaModelException {
73                 try {
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();
79
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()))) {
85                                         
86                                 // force opening so that the delta builder can get the old info
87                                 if (!isPrimary && !primary.isOpen()) {
88                                         primary.open(null);
89                                 }
90
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);
96                                 }
97                         
98                                 // save the cu
99                                 IBuffer primaryBuffer = primary.getBuffer();
100                                 if (!isPrimary) {
101                                         if (primaryBuffer == null) return;
102                                         char[] primaryContents = primaryBuffer.getCharacters();
103                                         boolean hasSaved = false;
104                                         try {
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);
110                                                 hasSaved = true;
111                                         } finally {
112                                                 if (!hasSaved){
113                                                         // restore original buffer contents since something went wrong
114                                                         primaryBuffer.setContents(primaryContents);
115                                                 }
116                                         }
117                                 } else {
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);
121                                 }
122                         } else {
123                                 // working copy on cu outside classpath OR resource doesn't exist yet
124                                 String encoding = null;
125                                 try {
126                                         encoding = resource.getCharset();
127                                 }
128                                 catch (CoreException ce) {
129                                         // use no encoding
130                                 }
131                                 String contents = workingCopy.getSource();
132                                 if (contents == null) return;
133                                 try {
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(
140                                                         stream, 
141                                                         this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, 
142                                                         null);
143                                         } else {
144                                                 resource.create(
145                                                         stream,
146                                                         this.force,
147                                                         this.progressMonitor);
148                                         }
149                                 } catch (CoreException e) {
150                                         throw new JavaModelException(e);
151                                 } catch (UnsupportedEncodingException e) {
152                                         throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
153                                 }
154                                 
155                         }
156
157                         setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
158                         
159                         // make sure working copy is in sync
160                         workingCopy.updateTimeStamp((CompilationUnit)primary);
161                         workingCopy.makeConsistent(this);
162                         worked(1);
163                 
164                         // build the deltas
165                         if (deltaBuilder != null) {
166                                 deltaBuilder.buildDeltas();
167                         
168                                 // add the deltas to the list of deltas created during this operation
169                                 if (deltaBuilder.delta != null) {
170                                         addDelta(deltaBuilder.delta);
171                                 }
172                         }
173                         worked(1);
174                 } finally {     
175                         done();
176                 }
177         }
178         
179         /**
180          * Returns the compilation unit this operation is working on.
181          */
182         protected CompilationUnit getCompilationUnit() {
183                 return (CompilationUnit)getElementToProcess();
184         }
185         protected ISchedulingRule getSchedulingRule() {
186                 IResource resource = getElementToProcess().getResource();
187                 IWorkspace workspace = resource.getWorkspace();
188                 if (resource.exists()) {
189                         return workspace.getRuleFactory().modifyRule(resource);
190                 } else {
191                         return workspace.getRuleFactory().createRule(resource);
192                 }
193         }
194         /**
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
203          *  </ul>
204          */
205         public IJavaModelStatus verify() {
206                 ICompilationUnit cu = getCompilationUnit();
207                 if (!cu.isWorkingCopy()) {
208                         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
209                 }
210                 ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
211                 IResource resource = original.getResource();
212                 if (!cu.isBasedOn(resource) && !force) {
213                         return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
214                 }
215                 // no read-only check, since some repository adapters can change the flag on save
216                 // operation.   
217                 return JavaModelStatus.VERIFIED_OK;
218         }
219 }