Skip to main content
summaryrefslogtreecommitdiffstats
blob: b4932a657ad64d0f26b9cc39a9fade8b63184974 (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
/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
package org.eclipse.compare.internal.patch;

import java.io.*;

import org.eclipse.swt.graphics.Image;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.*;

import org.eclipse.compare.*;


/* package */ class PatchedResource implements ITypedElement, IStreamContentAccessor {
	
	private Diff fDiff;
	private IStreamContentAccessor fCurrent;
	private IPath fPath;
	private byte[] fContent;
	private Patcher fPatcher;
	
	/* package */ PatchedResource(IStreamContentAccessor current, Diff diff, IPath path, Patcher patcher) {
		fDiff= diff;
		fCurrent= current;
		fPath= path;
		fPatcher= patcher;
	}
	
	public InputStream getContents() throws CoreException {
		if (fContent == null) {
			InputStream is= null;
			
			try {
				is= fCurrent.getContents();
			} catch (CoreException ex) {
				is= new ByteArrayInputStream(new byte[0]);
			}
			if (is != null) {
				
				try {
					String encoding= ResourcesPlugin.getEncoding();
					BufferedReader br= new BufferedReader(new InputStreamReader(is, encoding));
					String s= fPatcher.patch(fDiff,br, null);
					if (s != null)
						fContent= s.getBytes(encoding);
				} catch (UnsupportedEncodingException e) {
					throw new CoreException(new Status(IStatus.ERROR, CompareUI.PLUGIN_ID, Platform.PLUGIN_ERROR, e.getMessage(), e));
				}
					
				try {
					is.close();
				} catch (IOException ex) {
				}
			}
		}
		return new ByteArrayInputStream(fContent);
	}
	
	public Image getImage() {
		return CompareUI.getImage(getType());
	}
	
	public String getName() {
		return fPath.toOSString();
	}
	
	public String getType() {
		String type= fPath.getFileExtension();
		if (type != null)
			return type;
		return ITypedElement.UNKNOWN_TYPE;
	}
}

Back to the top