685ff7969da07a8486f0f93dc58f07a535d3e27b
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / builder / FileStorage.java
1 package net.sourceforge.phpeclipse.builder;
2
3 import java.io.File;
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;
9
10 import net.sourceforge.phpdt.internal.core.util.StreamUtil;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12
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;
20
21 /*
22  * (c) Copyright QNX Software Systems Ltd. 2002.
23  * All Rights Reserved.
24  */
25
26 /**
27  * 
28  * @see IStorage
29  */
30 public class FileStorage extends PlatformObject implements IStorage {
31         private boolean forceReadOnly;
32
33         private final IPath path;
34
35         private final File file;
36
37         /**
38          * Two FileStorages are equal if their IPaths are equal.
39          * 
40          * @see java.lang.Object#equals(java.lang.Object)
41          */
42         public boolean equals(Object obj) {
43                 if (this == obj)
44                         return true;
45                 if (!(obj instanceof FileStorage))
46                         return false;
47                 FileStorage other = (FileStorage) obj;
48                 return path.equals(other.path);
49         }
50
51         /*
52          * (non-Javadoc)
53          * 
54          * @see org.eclipse.core.resources.IStorage#getContents()
55          */
56         public InputStream getContents() throws CoreException {
57                 try {
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));
62                 }
63         }
64
65         /*
66          * (non-Javadoc)
67          * 
68          * @see org.eclipse.core.resources.IStorage#getFullPath()
69          */
70         public IPath getFullPath() {
71                 return this.path;
72         }
73
74         /*
75          * (non-Javadoc)
76          * 
77          * @see org.eclipse.core.resources.IStorage#getName()
78          */
79         public String getName() {
80                 return this.path.lastSegment();
81         }
82
83         /*
84          * (non-Javadoc)
85          * 
86          * @see org.eclipse.core.resources.IStorage#isReadOnly()
87          */
88         public boolean isReadOnly() {
89                 return forceReadOnly || !file.canWrite();
90         }
91
92         /**
93          * Method FileStorage.
94          * 
95          * @param path
96          */
97         public FileStorage(IPath path) {
98                 this.path = path;
99                 this.file = path.toFile();
100         }
101
102         /*
103          * (non-Javadoc)
104          * 
105          * @see java.lang.Object#toString()
106          */
107         public String toString() {
108                 return path.toOSString();
109         }
110
111         /**
112          * @param stream
113          * @param overwrite
114          * @param b
115          * @param monitor
116          */
117         public void setContents(InputStream stream, boolean overwrite, boolean b,
118                         IProgressMonitor monitor) throws CoreException {
119                 try {
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));
127                 }
128         }
129
130         /**
131          * Some document providers (notably CompilationUnitDocumentProvider) can't
132          * handle read/write storage.
133          */
134         public void setReadOnly() {
135                 forceReadOnly = true;
136         }
137 }