View Javadoc

1   /*
2    * (c) Copyright QNX Software Systems Ltd. 2002.
3    * All Rights Reserved.
4    */
5   package de.matthias_burbach.mosaique4eclipse.util;
6   
7   import java.io.File;
8   import java.io.FileInputStream;
9   import java.io.FileNotFoundException;
10  import java.io.FileOutputStream;
11  import java.io.IOException;
12  import java.io.InputStream;
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   * Wraps a file as an {@link org.eclipse.core.resources.IStorage}.
24   */
25  public class FileStorage extends PlatformObject implements IStorage {
26      /***
27       * The plugin id. Used in error messages.
28       */
29      private static final String PLUGIN_ID = "Mosaique";
30  
31      /***
32       * Whether to force read-only access to the encapsulated file.
33       */
34      private boolean forceReadOnly;
35  
36      /***
37       * The path of the encapsulated file.
38       */
39      private final IPath path;
40  
41      /***
42       * The encapsulated file.
43       */
44      private final File file;
45  
46      /***
47       * @param path The path of the file to encapsulate.
48       *             Must not be <code>null</code>.
49       */
50      public FileStorage(final IPath path) {
51          this.path = path;
52          this.file = path.toFile();
53      }
54  
55      /*
56       * (non-Javadoc)
57       * @see org.eclipse.core.resources.IStorage#getContents()
58       */
59      /***
60       * {@inheritDoc}
61       */
62      public InputStream getContents() throws CoreException {
63          try {
64              return new FileInputStream(file);
65          } catch (FileNotFoundException e) {
66              throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
67                      IStatus.ERROR, e.toString(), e));
68          }
69      }
70  
71      /*
72       * (non-Javadoc)
73       * @see org.eclipse.core.resources.IStorage#getFullPath()
74       */
75      /***
76       * {@inheritDoc}
77       */
78      public IPath getFullPath() {
79          return this.path;
80      }
81  
82      /*
83       * (non-Javadoc)
84       * @see org.eclipse.core.resources.IStorage#getName()
85       */
86      /***
87       * {@inheritDoc}
88       */
89      public String getName() {
90          return this.path.lastSegment();
91      }
92  
93      /*
94       * (non-Javadoc)
95       * @see org.eclipse.core.resources.IStorage#isReadOnly()
96       */
97      /***
98       * {@inheritDoc}
99       */
100     public boolean isReadOnly() {
101         return forceReadOnly || !file.canWrite();
102     }
103 
104     /***
105      * Writes the contents of the given input stream to the encapsulated
106      * file.
107      *
108      * @param stream The input stream to write to the file.
109      * @param overwrite Whether to overwrite the existing file or not.
110      *                  Is ignored.
111      * @param monitor The progress monitor to feed with progress on writing the
112      *                input stream to the file. Is ignored.
113      * @throws CoreException if anything goes wrong
114      */
115     public void setContents(
116             final InputStream stream,
117             final boolean overwrite,
118             final IProgressMonitor monitor)
119             throws CoreException {
120         try {
121             Util.transferStreams(stream, new FileOutputStream(file));
122         } catch (FileNotFoundException e) {
123             throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
124                     IStatus.ERROR, e.toString(), e));
125         } catch (IOException e) {
126             throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
127                     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 
139     /*
140      * (non-Javadoc)
141      * @see java.lang.Object#equals(java.lang.Object)
142      */
143     /***
144      * {@inheritDoc}
145      * Two FileStorages are equal if their IPaths are equal.
146      */
147     public boolean equals(final Object obj) {
148         if (this == obj) {
149             return true;
150         }
151         if (!(obj instanceof FileStorage)) {
152             return false;
153         }
154         FileStorage other = (FileStorage) obj;
155         return path.equals(other.path);
156     }
157 
158     /*
159      * (non-Javadoc)
160      * @see java.lang.Object#hashCode()
161      */
162     /***
163      * {@inheritDoc}
164      */
165     public int hashCode() {
166         int result = path.hashCode();
167         return result;
168     }
169 
170     /*
171      * (non-Javadoc)
172      * @see java.lang.Object#toString()
173      */
174     /***
175      * {@inheritDoc}
176      * The OS specific representation of the file's path.
177      */
178     public String toString() {
179         return path.toOSString();
180     }
181 }