001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.nls.changes;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.UnsupportedEncodingException;
016: import java.net.URI;
017:
018: import org.eclipse.core.filesystem.EFS;
019: import org.eclipse.core.filesystem.IFileInfo;
020:
021: import org.eclipse.core.runtime.Assert;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.core.runtime.IPath;
024: import org.eclipse.core.runtime.IProgressMonitor;
025: import org.eclipse.core.runtime.OperationCanceledException;
026: import org.eclipse.core.runtime.Platform;
027: import org.eclipse.core.runtime.SubProgressMonitor;
028: import org.eclipse.core.runtime.content.IContentType;
029:
030: import org.eclipse.core.resources.IFile;
031: import org.eclipse.core.resources.IResource;
032: import org.eclipse.core.resources.ResourcesPlugin;
033:
034: import org.eclipse.ltk.core.refactoring.Change;
035: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
036:
037: import org.eclipse.jdt.core.IJavaModelStatusConstants;
038: import org.eclipse.jdt.core.JavaModelException;
039:
040: import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
041: import org.eclipse.jdt.internal.corext.util.Messages;
042:
043: public class CreateFileChange extends JDTChange {
044:
045: private String fChangeName;
046:
047: private IPath fPath;
048: private String fSource;
049: private String fEncoding;
050: private boolean fExplicitEncoding;
051: private long fStampToRestore;
052:
053: public CreateFileChange(IPath path, String source, String encoding) {
054: this (path, source, encoding, IResource.NULL_STAMP);
055: }
056:
057: public CreateFileChange(IPath path, String source, String encoding,
058: long stampToRestore) {
059: Assert.isNotNull(path, "path"); //$NON-NLS-1$
060: Assert.isNotNull(source, "source"); //$NON-NLS-1$
061: fPath = path;
062: fSource = source;
063: fEncoding = encoding;
064: fExplicitEncoding = fEncoding != null;
065: fStampToRestore = stampToRestore;
066: }
067:
068: /*
069: private CreateFileChange(IPath path, String source, String encoding, long stampToRestore, boolean explicit) {
070: Assert.isNotNull(path, "path"); //$NON-NLS-1$
071: Assert.isNotNull(source, "source"); //$NON-NLS-1$
072: Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$
073: fPath= path;
074: fSource= source;
075: fEncoding= encoding;
076: fStampToRestore= stampToRestore;
077: fExplicitEncoding= explicit;
078: }
079: */
080:
081: protected void setEncoding(String encoding, boolean explicit) {
082: Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$
083: fEncoding = encoding;
084: fExplicitEncoding = explicit;
085: }
086:
087: public String getName() {
088: if (fChangeName == null)
089: return Messages.format(
090: NLSChangesMessages.createFile_Create_file, fPath
091: .toOSString());
092: else
093: return fChangeName;
094: }
095:
096: public void setName(String name) {
097: fChangeName = name;
098: }
099:
100: protected void setSource(String source) {
101: fSource = source;
102: }
103:
104: protected String getSource() {
105: return fSource;
106: }
107:
108: protected void setPath(IPath path) {
109: fPath = path;
110: }
111:
112: protected IPath getPath() {
113: return fPath;
114: }
115:
116: public Object getModifiedElement() {
117: return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
118: }
119:
120: public RefactoringStatus isValid(IProgressMonitor pm)
121: throws CoreException {
122: RefactoringStatus result = new RefactoringStatus();
123: IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(
124: fPath);
125:
126: URI location = file.getLocationURI();
127: if (location == null) {
128: result
129: .addFatalError(Messages
130: .format(
131: NLSChangesMessages.CreateFileChange_error_unknownLocation,
132: file.getFullPath().toString()));
133: return result;
134: }
135:
136: IFileInfo jFile = EFS.getStore(location).fetchInfo();
137: if (jFile.exists()) {
138: result.addFatalError(Messages.format(
139: NLSChangesMessages.CreateFileChange_error_exists,
140: file.getFullPath().toString()));
141: return result;
142: }
143: return result;
144: }
145:
146: public Change perform(IProgressMonitor pm) throws CoreException,
147: OperationCanceledException {
148:
149: InputStream is = null;
150: try {
151: pm.beginTask(
152: NLSChangesMessages.createFile_creating_resource, 3);
153:
154: initializeEncoding();
155: IFile file = getOldFile(new SubProgressMonitor(pm, 1));
156: /*
157: if (file.exists()) {
158: CompositeChange composite= new CompositeChange(getName());
159: composite.add(new DeleteFileChange(file));
160: composite.add(new CreateFileChange(fPath, fSource, fEncoding, fStampToRestore, fExplicitEncoding));
161: pm.worked(1);
162: return composite.perform(new SubProgressMonitor(pm, 1));
163: } else { */
164: try {
165: is = new ByteArrayInputStream(fSource
166: .getBytes(fEncoding));
167: file.create(is, false, new SubProgressMonitor(pm, 1));
168: if (fStampToRestore != IResource.NULL_STAMP) {
169: file.revertModificationStamp(fStampToRestore);
170: }
171: if (fExplicitEncoding) {
172: file.setCharset(fEncoding, new SubProgressMonitor(
173: pm, 1));
174: } else {
175: pm.worked(1);
176: }
177: return new DeleteFileChange(file);
178: } catch (UnsupportedEncodingException e) {
179: throw new JavaModelException(e,
180: IJavaModelStatusConstants.IO_EXCEPTION);
181: }
182: } finally {
183: try {
184: if (is != null)
185: is.close();
186: } catch (IOException ioe) {
187: throw new JavaModelException(ioe,
188: IJavaModelStatusConstants.IO_EXCEPTION);
189: } finally {
190: pm.done();
191: }
192: }
193: }
194:
195: protected IFile getOldFile(IProgressMonitor pm)
196: throws OperationCanceledException {
197: pm.beginTask("", 1); //$NON-NLS-1$
198: try {
199: return ResourcesPlugin.getWorkspace().getRoot().getFile(
200: fPath);
201: } finally {
202: pm.done();
203: }
204: }
205:
206: private void initializeEncoding() {
207: if (fEncoding == null) {
208: fExplicitEncoding = false;
209: IFile file = ResourcesPlugin.getWorkspace().getRoot()
210: .getFile(fPath);
211: if (file != null) {
212: try {
213: if (file.exists()) {
214: fEncoding = file.getCharset(false);
215: if (fEncoding == null) {
216: fEncoding = file.getCharset(true);
217: } else {
218: fExplicitEncoding = true;
219: }
220: } else {
221: IContentType contentType = Platform
222: .getContentTypeManager()
223: .findContentTypeFor(file.getName());
224: if (contentType != null)
225: fEncoding = contentType.getDefaultCharset();
226: if (fEncoding == null)
227: fEncoding = file.getCharset(true);
228: }
229: } catch (CoreException e) {
230: fEncoding = ResourcesPlugin.getEncoding();
231: fExplicitEncoding = true;
232: }
233: } else {
234: fEncoding = ResourcesPlugin.getEncoding();
235: fExplicitEncoding = true;
236: }
237: }
238: Assert.isNotNull(fEncoding);
239: }
240: }
|