KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CommitWorkingCopyOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
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 /**
33  * Commits the contents of a working copy compilation
34  * unit to its original element and resource, bringing
35  * the Java Model up-to-date with the current contents of the working
36  * copy.
37  *
38  * <p>It is possible that the contents of the
39  * original resource have changed since the working copy was created,
40  * in which case there is an update conflict. This operation allows
41  * for two settings to resolve conflict set by the <code>fForce</code> flag:<ul>
42  * <li>force flag is <code>false</code> - in this case an <code>JavaModelException</code>
43  * is thrown</li>
44  * <li>force flag is <code>true</code> - in this case the contents of
45  * the working copy are applied to the underlying resource even though
46  * the working copy was created before a subsequent change in the
47  * resource</li>
48  * </ul>
49  *
50  * <p>The default conflict resolution setting is the force flag is <code>false</code>
51  *
52  * A JavaModelOperation exception is thrown either if the commit could not
53  * be performed or if the new content of the compilation unit violates some Java Model
54  * constraint (e.g. if the new package declaration doesn't match the name of the folder
55  * containing the compilation unit).
56  */

57 public class CommitWorkingCopyOperation extends JavaModelOperation {
58     /**
59      * Constructs an operation to commit the contents of a working copy
60      * to its original compilation unit.
61      */

62     public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
63         super(new IJavaElement[] {element}, force);
64     }
65     /**
66      * @exception JavaModelException if setting the source
67      * of the original compilation unit fails
68      */

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                 // case of a working copy without a resource
76
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                 // force opening so that the delta builder can get the old info
91
if (!isPrimary && !primary.isOpen()) {
92                     primary.open(null);
93                 }
94
95                 // creates the delta builder (this remembers the content of the cu) if:
96
// - it is not excluded
97
// - and it is not a primary or it is a non-consistent primary
98
if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
99                     deltaBuilder = new JavaElementDeltaBuilder(primary);
100                 }
101             
102                 // save the cu
103
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                             // restore original buffer contents since something went wrong
118
primaryBuffer.setContents(primaryContents);
119                         }
120                     }
121                 } else {
122                     // for a primary working copy no need to set the content of the buffer again
123
primaryBuffer.save(this.progressMonitor, this.force);
124                     primary.makeConsistent(this);
125                 }
126             } else {
127                 // working copy on cu outside classpath OR resource doesn't exist yet
128
String JavaDoc encoding = null;
129                 try {
130                     encoding = resource.getCharset();
131                 }
132                 catch (CoreException ce) {
133                     // use no encoding
134
}
135                 String JavaDoc contents = workingCopy.getSource();
136                 if (contents == null) return;
137                 try {
138                     byte[] bytes = encoding == null
139                         ? contents.getBytes()
140                         : contents.getBytes(encoding);
141                     ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(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 JavaDoc e) {
156                     throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
157                 }
158                 
159             }
160
161             setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
162             
163             // make sure working copy is in sync
164
workingCopy.updateTimeStamp((CompilationUnit)primary);
165             workingCopy.makeConsistent(this);
166             worked(1);
167         
168             // build the deltas
169
if (deltaBuilder != null) {
170                 deltaBuilder.buildDeltas();
171             
172                 // add the deltas to the list of deltas created during this operation
173
if (deltaBuilder.delta != null) {
174                     addDelta(deltaBuilder.delta);
175                 }
176             }
177             worked(1);
178         } finally {
179             done();
180         }
181     }
182     /**
183      * Returns the compilation unit this operation is working on.
184      */

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     /**
199      * Possible failures: <ul>
200      * <li>INVALID_ELEMENT_TYPES - the compilation unit supplied to this
201      * operation is not a working copy
202      * <li>ELEMENT_NOT_PRESENT - the compilation unit the working copy is
203      * based on no longer exists.
204      * <li>UPDATE_CONFLICT - the original compilation unit has changed since
205      * the working copy was created and the operation specifies no force
206      * <li>READ_ONLY - the original compilation unit is in read-only mode
207      * </ul>
208      */

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         // no read-only check, since some repository adapters can change the flag on save
218
// operation.
219
return JavaModelStatus.VERIFIED_OK;
220     }
221 }
222
Popular Tags