1 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 40 public boolean exists() { 41 return store.exists(this); 42 } 43 44 47 public String getCharset() throws CoreException { 48 IResource file = workspace.getRoot().findMember(fullPath); 50 if (file != null && file.getType() == IResource.FILE) 51 return ((IFile)file).getCharset(); 52 53 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 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 message = NLS.bind(Messages.history_errorContentDescription, getFullPath()); 70 throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e); 71 } 72 } 73 } 74 } 75 76 79 public InputStream getContents() throws CoreException { 80 return store.getContents(this); 81 } 82 83 86 public IPath getFullPath() { 87 return fullPath; 88 } 89 90 93 public long getModificationTime() { 94 return lastModified; 95 } 96 97 100 public String getName() { 101 return fullPath.lastSegment(); 102 } 103 104 public UniversalUniqueIdentifier getUUID() { 105 return uuid; 106 } 107 108 111 public boolean isReadOnly() { 112 return true; 113 } 114 115 118 public String toString() { 119 StringBuffer s = new StringBuffer (); 120 s.append("FileState(uuid: "); s.append(uuid.toString()); 122 s.append(", lastModified: "); s.append(lastModified); 124 s.append(", path: "); s.append(fullPath); 126 s.append(')'); 127 return s.toString(); 128 } 129 } 130 | Popular Tags |