blob: 17c2d88a707eea9656a988dedc2ba68d5d9ffb9b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*******************************************************************************
* Copyright (c) 2008, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.compare.internal.patch;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import org.eclipse.compare.internal.CompareMessages;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.core.patch.DiffProject;
import org.eclipse.compare.patch.ReaderCreator;
import org.eclipse.core.resources.IEncodedStorage;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
public class Utilities {
public static String getCharset(Object resource) {
if (resource instanceof IEncodedStorage) {
try {
return ((IEncodedStorage) resource).getCharset();
} catch (CoreException ex) {
CompareUIPlugin.log(ex);
}
}
return ResourcesPlugin.getEncoding();
}
public static IProject getProject(DiffProject diffProject) {
return ResourcesPlugin.getWorkspace().getRoot().getProject(
diffProject.getName());
}
public static ReaderCreator getReaderCreator(final IStorage storage) {
return new ReaderCreator() {
public Reader createReader() throws CoreException {
return Utilities.createReader(storage);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.compare.patch.ReaderCreator#canCreateReader()
*/
public boolean canCreateReader() {
if (storage == null
|| (storage != null && storage instanceof IFile && !((IFile) storage)
.isAccessible())) {
return false;
}
return true;
}
};
}
public static BufferedReader createReader(IStorage storage)
throws CoreException {
if (storage == null
|| (storage != null && storage instanceof IFile && !((IFile) storage)
.isAccessible())) {
throw new CoreException(new Status(IStatus.WARNING,
CompareUIPlugin.PLUGIN_ID,
CompareMessages.ReaderCreator_fileIsNotAccessible));
}
String charset = null;
if (storage instanceof IEncodedStorage) {
IEncodedStorage es = (IEncodedStorage) storage;
charset = es.getCharset();
}
InputStreamReader in = null;
if (charset != null) {
InputStream contents = storage.getContents();
try {
in = new InputStreamReader(contents, charset);
} catch (UnsupportedEncodingException e) {
CompareUIPlugin.log(e);
try {
contents.close();
} catch (IOException e1) {
// Ignore
}
}
}
if (in == null) {
in = new InputStreamReader(storage.getContents());
}
return new BufferedReader(in);
}
}
|