KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > FileState


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.io.*;
14 import org.eclipse.core.internal.localstore.IHistoryStore;
15 import org.eclipse.core.internal.utils.Messages;
16 import org.eclipse.core.internal.utils.UniversalUniqueIdentifier;
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.content.IContentDescription;
20 import org.eclipse.core.runtime.content.IContentTypeManager;
21 import org.eclipse.osgi.util.NLS;
22
23 public class FileState extends PlatformObject implements IFileState {
24     private static final IWorkspace workspace = ResourcesPlugin.getWorkspace();
25     protected long lastModified;
26     protected UniversalUniqueIdentifier uuid;
27     protected IHistoryStore store;
28     protected IPath fullPath;
29
30     public FileState(IHistoryStore store, IPath fullPath, long lastModified, UniversalUniqueIdentifier uuid) {
31         this.store = store;
32         this.lastModified = lastModified;
33         this.uuid = uuid;
34         this.fullPath = fullPath;
35     }
36
37     /* (non-Javadoc)
38      * @see IFileState#exists()
39      */

40     public boolean exists() {
41         return store.exists(this);
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.core.resources.IEncodedStorage#getCharset()
46      */

47     public String JavaDoc getCharset() throws CoreException {
48         // if there is an existing file at this state's path, use the encoding of that file
49
IResource file = workspace.getRoot().findMember(fullPath);
50         if (file != null && file.getType() == IResource.FILE)
51             return ((IFile)file).getCharset();
52         
53         // tries to obtain a description for the file contents
54
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
55         InputStream contents = new BufferedInputStream(getContents());
56         boolean failed = false;
57         try {
58             IContentDescription description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] {IContentDescription.CHARSET});
59             return description == null ? null : description.getCharset();
60         } catch (IOException e) {
61             failed = true;
62             String JavaDoc message = NLS.bind(Messages.history_errorContentDescription, getFullPath());
63             throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
64         } finally {
65             try {
66                 contents.close();
67             } catch (IOException e) {
68                 if (!failed) {
69                     String JavaDoc message = NLS.bind(Messages.history_errorContentDescription, getFullPath());
70                     throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
71                 }
72             }
73         }
74     }
75
76     /* (non-Javadoc)
77      * @see IFileState#getContents()
78      */

79     public InputStream getContents() throws CoreException {
80         return store.getContents(this);
81     }
82
83     /* (non-Javadoc)
84      * @see IFileState#getFullPath()
85      */

86     public IPath getFullPath() {
87         return fullPath;
88     }
89
90     /* (non-Javadoc)
91      * @see IFileState#getModificationTime()
92      */

93     public long getModificationTime() {
94         return lastModified;
95     }
96
97     /* (non-Javadoc)
98      * @see IFileState#getName()
99      */

100     public String JavaDoc getName() {
101         return fullPath.lastSegment();
102     }
103
104     public UniversalUniqueIdentifier getUUID() {
105         return uuid;
106     }
107
108     /* (non-Javadoc)
109      * @see IFileState#isReadOnly()
110      */

111     public boolean isReadOnly() {
112         return true;
113     }
114
115     /**
116      * Returns a string representation of this object. Used for debug only.
117      */

118     public String JavaDoc toString() {
119         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
120         s.append("FileState(uuid: "); //$NON-NLS-1$
121
s.append(uuid.toString());
122         s.append(", lastModified: "); //$NON-NLS-1$
123
s.append(lastModified);
124         s.append(", path: "); //$NON-NLS-1$
125
s.append(fullPath);
126         s.append(')');
127         return s.toString();
128     }
129 }
130
Popular Tags