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;
33 private final IPath path;
35 private final File file;
38 * Two FileStorages are equal if their IPaths are equal.
40 * @see java.lang.Object#equals(java.lang.Object)
42 public boolean equals(Object obj) {
45 if (!(obj instanceof FileStorage))
47 FileStorage other = (FileStorage) obj;
48 return path.equals(other.path);
54 * @see org.eclipse.core.resources.IStorage#getContents()
56 public InputStream getContents() throws CoreException {
58 return new FileInputStream(file);
59 } catch (FileNotFoundException e) {
60 throw new CoreException(new Status(IStatus.ERROR,
61 PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
68 * @see org.eclipse.core.resources.IStorage#getFullPath()
70 public IPath getFullPath() {
77 * @see org.eclipse.core.resources.IStorage#getName()
79 public String getName() {
80 return this.path.lastSegment();
86 * @see org.eclipse.core.resources.IStorage#isReadOnly()
88 public boolean isReadOnly() {
89 return forceReadOnly || !file.canWrite();
97 public FileStorage(IPath path) {
99 this.file = path.toFile();
105 * @see java.lang.Object#toString()
107 public String toString() {
108 return path.toOSString();
117 public void setContents(InputStream stream, boolean overwrite, boolean b,
118 IProgressMonitor monitor) throws CoreException {
120 StreamUtil.transferStreams(stream, new FileOutputStream(file));
121 } catch (FileNotFoundException e) {
122 throw new CoreException(new Status(IStatus.ERROR,
123 PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
124 } catch (IOException e) {
125 throw new CoreException(new Status(IStatus.ERROR,
126 PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
131 * Some document providers (notably CompilationUnitDocumentProvider) can't
132 * handle read/write storage.
134 public void setReadOnly() {
135 forceReadOnly = true;