Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7f9ec6723dd1e029f27b15116b3a783383afcd72 (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
package org.eclipse.jdt.internal.core;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import java.io.*;

/**
 * A basic implementation of <code>ICompilationUnit</code>
 * for use in the <code>SourceMapper</code>./
 * @see ICompilationUnit
 */
 
public class BasicCompilationUnit implements ICompilationUnit {
	protected char[] contents;
	protected char[] fileName;
	protected char[] mainTypeName;
public BasicCompilationUnit(char[] contents, String fileName) {
	this.contents = contents;
	this.fileName = fileName.toCharArray();

	int start = fileName.lastIndexOf("/"/*nonNLS*/) + 1;
	if (start == 0 || start < fileName.lastIndexOf("\\"/*nonNLS*/))
		start = fileName.lastIndexOf("\\"/*nonNLS*/) + 1;

	int end = fileName.lastIndexOf("."/*nonNLS*/);
	if (end == -1)
		end = fileName.length();

	this.mainTypeName = fileName.substring(start, end).toCharArray();
}
public char[] getContents() {
	if (contents != null)
		return contents;   // answer the cached source

	// otherwise retrieve it
	BufferedReader reader = null;
	try {
		File file = new File(new String(fileName));
		reader = new BufferedReader(new FileReader(file));
		int length;
		char[] contents = new char[length = (int) file.length()];
		int len = 0;
		int readSize = 0;
		while ((readSize != -1) && (len != length)) {
			// See PR 1FMS89U
			// We record first the read size. In this case len is the actual read size.
			len += readSize;
			readSize = reader.read(contents, len, length - len);
		}
		reader.close();
		// See PR 1FMS89U
		// Now we need to resize in case the default encoding used more than one byte for each
		// character
		if (len != length)
			System.arraycopy(contents, 0, (contents = new char[len]), 0, len);		
		return contents;
	} catch (FileNotFoundException e) {
	} catch (IOException e) {
		if (reader != null) {
			try {
				reader.close();
			} catch(IOException ioe) {
			}
		}
	};
	return new char[0];
}
public char[] getFileName() {
	return fileName;
}
public char[] getMainTypeName() {
	return mainTypeName;
}
public String toString(){
	return "CompilationUnit: "/*nonNLS*/+new String(fileName);
}
}

Back to the top