1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.builder;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
15 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
16 import net.sourceforge.phpdt.internal.core.Util;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
22 public class SourceFile implements ICompilationUnit {
25 ClasspathMultiDirectory sourceLocation;
26 String initialTypeName;
29 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation, String encoding) {
30 this.resource = resource;
31 this.sourceLocation = sourceLocation;
32 this.initialTypeName = extractTypeName();
33 this.encoding = encoding;
36 public boolean equals(Object o) {
37 if (this == o) return true;
38 if (!(o instanceof SourceFile)) return false;
40 SourceFile f = (SourceFile) o;
41 return sourceLocation == f.sourceLocation && resource.getFullPath().equals(f.resource.getFullPath());
44 String extractTypeName() {
45 // answer a String with the qualified type name for the source file in the form: 'p1/p2/A'
46 IPath fullPath = resource.getFullPath();
47 int resourceSegmentCount = fullPath.segmentCount();
48 int sourceFolderSegmentCount = sourceLocation.sourceFolder.getFullPath().segmentCount();
49 int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1) - 5; // length of ".java"
50 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
51 charCount += fullPath.segment(i).length();
53 char[] result = new char[charCount];
55 resourceSegmentCount--; // deal with the last segment separately
56 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) {
57 String segment = fullPath.segment(i);
58 int size = segment.length();
59 segment.getChars(0, size, result, offset);
61 result[offset++] = '/';
63 String segment = fullPath.segment(resourceSegmentCount);
64 int size = segment.length() - 5; // length of ".java"
65 segment.getChars(0, size, result, offset);
66 return new String(result);
69 public char[] getContents() {
72 return Util.getResourceContentsAsCharArray(resource, this.encoding);
73 } catch (CoreException e) {
74 throw new AbortCompilation(true, new MissingSourceFileException(resource.getFullPath().toString()));
78 public char[] getFileName() {
79 return resource.getFullPath().toString().toCharArray(); // do not know what you want to return here
82 public char[] getMainTypeName() {
83 char[] typeName = initialTypeName.toCharArray();
84 int lastIndex = CharOperation.lastIndexOf('/', typeName);
85 return CharOperation.subarray(typeName, lastIndex + 1, -1);
88 public char[][] getPackageName() {
89 char[] typeName = initialTypeName.toCharArray();
90 int lastIndex = CharOperation.lastIndexOf('/', typeName);
91 return CharOperation.splitOn('/', typeName, 0, lastIndex);
94 String typeLocator() {
95 return resource.getProjectRelativePath().toString();
98 public String toString() {
99 return "SourceFile[" //$NON-NLS-1$
100 + resource.getFullPath() + "]"; //$NON-NLS-1$