Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5236f80e08b908fd8203e2ed13b5668fdc406d8e (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
/*
 * (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.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) {
				BufferedReader br= new BufferedReader(new InputStreamReader(is));
				String s= fPatcher.patch(fDiff,br, null);
				if (s != null)
					fContent= s.getBytes();
				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