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