3m9 compatible;
[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.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21
22 public class SourceFile implements ICompilationUnit {
23
24 IFile resource;
25 ClasspathMultiDirectory sourceLocation;
26 String initialTypeName;
27 String encoding;
28
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;
34 }
35
36 public boolean equals(Object o) {
37         if (this == o) return true;
38         if (!(o instanceof SourceFile)) return false;
39
40         SourceFile f = (SourceFile) o;
41         return sourceLocation == f.sourceLocation && resource.getFullPath().equals(f.resource.getFullPath());
42
43
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();
52
53         char[] result = new char[charCount];
54         int offset = 0;
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);
60                 offset += size;
61                 result[offset++] = '/';
62         }
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);
67 }
68
69 public char[] getContents() {
70
71         try {   
72                 return Util.getResourceContentsAsCharArray(resource, this.encoding);
73         } catch (CoreException e) {
74                 throw new AbortCompilation(true, new MissingSourceFileException(resource.getFullPath().toString()));
75         }
76 }
77
78 public char[] getFileName() {
79         return resource.getFullPath().toString().toCharArray(); // do not know what you want to return here
80 }
81
82 public char[] getMainTypeName() {
83         char[] typeName = initialTypeName.toCharArray();
84         int lastIndex = CharOperation.lastIndexOf('/', typeName);
85         return CharOperation.subarray(typeName, lastIndex + 1, -1);
86 }
87
88 public char[][] getPackageName() {
89         char[] typeName = initialTypeName.toCharArray();
90         int lastIndex = CharOperation.lastIndexOf('/', typeName);
91         return CharOperation.splitOn('/', typeName, 0, lastIndex);
92 }
93
94 String typeLocator() {
95         return resource.getProjectRelativePath().toString();
96 }
97
98 public String toString() {
99         return "SourceFile[" //$NON-NLS-1$
100                 + resource.getFullPath() + "]";  //$NON-NLS-1$
101 }
102 }