KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > SourceFile


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.builder;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.CoreException;
16
17 import org.eclipse.jdt.core.compiler.CharOperation;
18 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
19 import org.eclipse.jdt.internal.compiler.problem.AbortCompilation;
20 import org.eclipse.jdt.internal.core.util.Util;
21
22 public class SourceFile implements ICompilationUnit {
23
24 public IFile resource;
25 ClasspathMultiDirectory sourceLocation;
26 String JavaDoc initialTypeName;
27 boolean updateClassFile;
28
29 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation) {
30     this.resource = resource;
31     this.sourceLocation = sourceLocation;
32     this.initialTypeName = extractTypeName();
33     this.updateClassFile = false;
34 }
35
36 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation, boolean updateClassFile) {
37     this(resource, sourceLocation);
38
39     this.updateClassFile = updateClassFile;
40 }
41
42 public boolean equals(Object JavaDoc o) {
43     if (this == o) return true;
44     if (!(o instanceof SourceFile)) return false;
45
46     SourceFile f = (SourceFile) o;
47     return this.sourceLocation == f.sourceLocation && this.resource.getFullPath().equals(f.resource.getFullPath());
48 }
49
50 String JavaDoc extractTypeName() {
51     // answer a String with the qualified type name for the source file in the form: 'p1/p2/A'
52
IPath fullPath = this.resource.getFullPath();
53     int resourceSegmentCount = fullPath.segmentCount();
54     int sourceFolderSegmentCount = this.sourceLocation.sourceFolder.getFullPath().segmentCount();
55     int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1);
56     resourceSegmentCount--; // deal with the last segment separately
57
for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
58         charCount += fullPath.segment(i).length();
59     String JavaDoc lastSegment = fullPath.segment(resourceSegmentCount);
60     int extensionIndex = Util.indexOfJavaLikeExtension(lastSegment);
61     charCount += extensionIndex;
62
63     char[] result = new char[charCount];
64     int offset = 0;
65     for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) {
66         String JavaDoc segment = fullPath.segment(i);
67         int size = segment.length();
68         segment.getChars(0, size, result, offset);
69         offset += size;
70         result[offset++] = '/';
71     }
72     lastSegment.getChars(0, extensionIndex, result, offset);
73     return new String JavaDoc(result);
74 }
75
76 public char[] getContents() {
77
78     try {
79         return Util.getResourceContentsAsCharArray(this.resource);
80     } catch (CoreException e) {
81         throw new AbortCompilation(true, new MissingSourceFileException(this.resource.getFullPath().toString()));
82     }
83 }
84
85 /**
86  * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
87  */

88 public char[] getFileName() {
89     return this.resource.getFullPath().toString().toCharArray(); // do not know what you want to return here
90
}
91
92 public char[] getMainTypeName() {
93     char[] typeName = this.initialTypeName.toCharArray();
94     int lastIndex = CharOperation.lastIndexOf('/', typeName);
95     return CharOperation.subarray(typeName, lastIndex + 1, -1);
96 }
97
98 public char[][] getPackageName() {
99     char[] typeName = this.initialTypeName.toCharArray();
100     int lastIndex = CharOperation.lastIndexOf('/', typeName);
101     return CharOperation.splitOn('/', typeName, 0, lastIndex);
102 }
103 public int hashCode() {
104     return this.initialTypeName.hashCode();
105 }
106 String JavaDoc typeLocator() {
107     return this.resource.getProjectRelativePath().toString();
108 }
109
110 public String JavaDoc toString() {
111     return "SourceFile[" //$NON-NLS-1$
112
+ this.resource.getFullPath() + "]"; //$NON-NLS-1$
113
}
114 }
115
Popular Tags