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,
35 this.resource = resource;
36 this.sourceLocation = sourceLocation;
37 this.initialTypeName = extractTypeName();
38 this.encoding = encoding;
41 public boolean equals(Object o) {
44 if (!(o instanceof SourceFile))
47 SourceFile f = (SourceFile) o;
48 return sourceLocation == f.sourceLocation
49 && resource.getFullPath().equals(f.resource.getFullPath());
52 String extractTypeName() {
53 // answer a String with the qualified type name for the source file in
56 IPath fullPath = resource.getFullPath();
57 int resourceSegmentCount = fullPath.segmentCount();
58 int sourceFolderSegmentCount = sourceLocation.sourceFolder
59 .getFullPath().segmentCount();
60 String extension = fullPath.getFileExtension();
61 int ext_length = extension == null ? 0 : extension.length() + 1;
62 int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1)
63 - ext_length; // length
66 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
67 charCount += fullPath.segment(i).length();
69 char[] result = new char[charCount];
71 resourceSegmentCount--; // deal with the last segment separately
72 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) {
73 String segment = fullPath.segment(i);
74 int size = segment.length();
75 segment.getChars(0, size, result, offset);
77 result[offset++] = '/';
79 String segment = fullPath.segment(resourceSegmentCount);
80 int size = segment.length() - ext_length; // length of ".php"
81 segment.getChars(0, size, result, offset);
82 return new String(result);
85 public char[] getContents() {
88 return Util.getResourceContentsAsCharArray(resource, this.encoding);
89 } catch (CoreException e) {
90 throw new AbortCompilation(true, new MissingSourceFileException(
91 resource.getFullPath().toString()));
95 public char[] getFileName() {
96 return resource.getFullPath().toString().toCharArray(); // do not know
102 public char[] getMainTypeName() {
103 char[] typeName = initialTypeName.toCharArray();
104 int lastIndex = CharOperation.lastIndexOf('/', typeName);
105 return CharOperation.subarray(typeName, lastIndex + 1, -1);
108 public char[][] getPackageName() {
109 char[] typeName = initialTypeName.toCharArray();
110 int lastIndex = CharOperation.lastIndexOf('/', typeName);
111 return CharOperation.splitOn('/', typeName, 0, lastIndex);
114 String typeLocator() {
115 return resource.getProjectRelativePath().toString();
118 public String toString() {
119 return "SourceFile[" //$NON-NLS-1$
120 + resource.getFullPath() + "]"; //$NON-NLS-1$
123 public IResource getResource() {