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 {
27 ClasspathMultiDirectory sourceLocation;
29 String initialTypeName;
33 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation, String encoding) {
34 this.resource = resource;
35 this.sourceLocation = sourceLocation;
36 this.initialTypeName = extractTypeName();
37 this.encoding = encoding;
40 public boolean equals(Object o) {
43 if (!(o instanceof SourceFile))
46 SourceFile f = (SourceFile) o;
47 return sourceLocation == f.sourceLocation && resource.getFullPath().equals(f.resource.getFullPath());
50 String extractTypeName() {
51 // answer a String with the qualified type name for the source file in the
53 IPath fullPath = resource.getFullPath();
54 int resourceSegmentCount = fullPath.segmentCount();
55 int sourceFolderSegmentCount = sourceLocation.sourceFolder.getFullPath().segmentCount();
56 String extension = fullPath.getFileExtension();
57 int ext_length = extension == null ? 0 : extension.length() + 1;
58 int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1) - ext_length; // length
61 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
62 charCount += fullPath.segment(i).length();
64 char[] result = new char[charCount];
66 resourceSegmentCount--; // deal with the last segment separately
67 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) {
68 String segment = fullPath.segment(i);
69 int size = segment.length();
70 segment.getChars(0, size, result, offset);
72 result[offset++] = '/';
74 String segment = fullPath.segment(resourceSegmentCount);
75 int size = segment.length() - ext_length; // length of ".php"
76 segment.getChars(0, size, result, offset);
77 return new String(result);
80 public char[] getContents() {
83 return Util.getResourceContentsAsCharArray(resource, this.encoding);
84 } catch (CoreException e) {
85 throw new AbortCompilation(true, new MissingSourceFileException(resource.getFullPath().toString()));
89 public char[] getFileName() {
90 return resource.getFullPath().toString().toCharArray(); // do not know what
95 public char[] getMainTypeName() {
96 char[] typeName = initialTypeName.toCharArray();
97 int lastIndex = CharOperation.lastIndexOf('/', typeName);
98 return CharOperation.subarray(typeName, lastIndex + 1, -1);
101 public char[][] getPackageName() {
102 char[] typeName = initialTypeName.toCharArray();
103 int lastIndex = CharOperation.lastIndexOf('/', typeName);
104 return CharOperation.splitOn('/', typeName, 0, lastIndex);
107 String typeLocator() {
108 return resource.getProjectRelativePath().toString();
111 public String toString() {
112 return "SourceFile[" //$NON-NLS-1$
113 + resource.getFullPath() + "]"; //$NON-NLS-1$
116 public IResource getResource() {