Parser detects wrong include files now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / builder / SourceFile.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.builder;
12
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;
17
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;
22
23 public class SourceFile implements ICompilationUnit {
24
25 IFile resource;
26 ClasspathMultiDirectory sourceLocation;
27 String initialTypeName;
28 String encoding;
29
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;
35 }
36
37 public boolean equals(Object o) {
38         if (this == o) return true;
39         if (!(o instanceof SourceFile)) return false;
40
41         SourceFile f = (SourceFile) o;
42         return sourceLocation == f.sourceLocation && resource.getFullPath().equals(f.resource.getFullPath());
43
44
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();
53
54         char[] result = new char[charCount];
55         int offset = 0;
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);
61                 offset += size;
62                 result[offset++] = '/';
63         }
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);
68 }
69
70 public char[] getContents() {
71
72         try {   
73                 return Util.getResourceContentsAsCharArray(resource, this.encoding);
74         } catch (CoreException e) {
75                 throw new AbortCompilation(true, new MissingSourceFileException(resource.getFullPath().toString()));
76         }
77 }
78
79 public char[] getFileName() {
80         return resource.getFullPath().toString().toCharArray(); // do not know what you want to return here
81 }
82
83 public char[] getMainTypeName() {
84         char[] typeName = initialTypeName.toCharArray();
85         int lastIndex = CharOperation.lastIndexOf('/', typeName);
86         return CharOperation.subarray(typeName, lastIndex + 1, -1);
87 }
88
89 public char[][] getPackageName() {
90         char[] typeName = initialTypeName.toCharArray();
91         int lastIndex = CharOperation.lastIndexOf('/', typeName);
92         return CharOperation.splitOn('/', typeName, 0, lastIndex);
93 }
94
95 String typeLocator() {
96         return resource.getProjectRelativePath().toString();
97 }
98
99 public String toString() {
100         return "SourceFile[" //$NON-NLS-1$
101                 + resource.getFullPath() + "]";  //$NON-NLS-1$
102 }
103
104 public IResource getResource() {
105         return resource; 
106 }
107
108 }