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.Util;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
23 public class SourceFile implements ICompilationUnit {
26 ClasspathMultiDirectory sourceLocation;
27 String initialTypeName;
30 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation, String encoding) {
31 this.resource = resource;
32 this.sourceLocation = sourceLocation;
33 this.initialTypeName = extractTypeName();
34 this.encoding = encoding;
37 public boolean equals(Object o) {
38 if (this == o) return true;
39 if (!(o instanceof SourceFile)) return false;
41 SourceFile f = (SourceFile) o;
42 return sourceLocation == f.sourceLocation && resource.getFullPath().equals(f.resource.getFullPath());
45 String extractTypeName() {
46 // answer a String with the qualified type name for the source file in the form: 'p1/p2/A'
47 IPath fullPath = resource.getFullPath();
48 int resourceSegmentCount = fullPath.segmentCount();
49 int sourceFolderSegmentCount = sourceLocation.sourceFolder.getFullPath().segmentCount();
50 int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1) - 5; // length of ".java"
51 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
52 charCount += fullPath.segment(i).length();
54 char[] result = new char[charCount];
56 resourceSegmentCount--; // deal with the last segment separately
57 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) {
58 String segment = fullPath.segment(i);
59 int size = segment.length();
60 segment.getChars(0, size, result, offset);
62 result[offset++] = '/';
64 String segment = fullPath.segment(resourceSegmentCount);
65 int size = segment.length() - 5; // length of ".java"
66 segment.getChars(0, size, result, offset);
67 return new String(result);
70 public char[] getContents() {
73 return Util.getResourceContentsAsCharArray(resource, this.encoding);
74 } catch (CoreException e) {
75 throw new AbortCompilation(true, new MissingSourceFileException(resource.getFullPath().toString()));
79 public char[] getFileName() {
80 return resource.getFullPath().toString().toCharArray(); // do not know what you want to return here
83 public char[] getMainTypeName() {
84 char[] typeName = initialTypeName.toCharArray();
85 int lastIndex = CharOperation.lastIndexOf('/', typeName);
86 return CharOperation.subarray(typeName, lastIndex + 1, -1);
89 public char[][] getPackageName() {
90 char[] typeName = initialTypeName.toCharArray();
91 int lastIndex = CharOperation.lastIndexOf('/', typeName);
92 return CharOperation.splitOn('/', typeName, 0, lastIndex);
95 String typeLocator() {
96 return resource.getProjectRelativePath().toString();
99 public String toString() {
100 return "SourceFile[" //$NON-NLS-1$
101 + resource.getFullPath() + "]"; //$NON-NLS-1$
104 public IResource getResource() {