1 package net.sourceforge.phpeclipse.builder;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
10 import net.sourceforge.phpdt.internal.ui.util.StreamUtil;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import org.eclipse.core.resources.IStorage;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.PlatformObject;
19 import org.eclipse.core.runtime.Status;
22 * (c) Copyright QNX Software Systems Ltd. 2002.
23 * All Rights Reserved.
30 public class FileStorage extends PlatformObject implements IStorage {
31 private boolean forceReadOnly;
32 private final IPath path;
33 private final File file;
36 * Two FileStorages are equal if their IPaths are equal.
37 * @see java.lang.Object#equals(java.lang.Object)
39 public boolean equals(Object obj) {
42 if (!(obj instanceof FileStorage))
44 FileStorage other = (FileStorage) obj;
45 return path.equals(other.path);
49 * @see org.eclipse.core.resources.IStorage#getContents()
51 public InputStream getContents() throws CoreException {
53 return new FileInputStream(file);
54 } catch (FileNotFoundException e) {
55 throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
60 * @see org.eclipse.core.resources.IStorage#getFullPath()
62 public IPath getFullPath() {
67 * @see org.eclipse.core.resources.IStorage#getName()
69 public String getName() {
70 return this.path.lastSegment();
74 * @see org.eclipse.core.resources.IStorage#isReadOnly()
76 public boolean isReadOnly() {
77 return forceReadOnly || !file.canWrite();
84 public FileStorage(IPath path) {
86 this.file = path.toFile();
90 * @see java.lang.Object#toString()
92 public String toString() {
93 return path.toOSString();
102 public void setContents(InputStream stream, boolean overwrite, boolean b, IProgressMonitor monitor) throws CoreException {
104 StreamUtil.transferStreams(stream, new FileOutputStream(file));
105 } catch (FileNotFoundException e) {
106 throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
107 } catch (IOException e) {
108 throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
113 * Some document providers (notably CompilationUnitDocumentProvider)
114 * can't handle read/write storage.
116 public void setReadOnly() {
117 forceReadOnly = true;