Added patch for #1434245
[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
27         ClasspathMultiDirectory sourceLocation;
28
29         String initialTypeName;
30
31         String encoding;
32
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;
38         }
39
40         public boolean equals(Object o) {
41                 if (this == o)
42                         return true;
43                 if (!(o instanceof SourceFile))
44                         return false;
45
46                 SourceFile f = (SourceFile) o;
47                 return sourceLocation == f.sourceLocation && resource.getFullPath().equals(f.resource.getFullPath());
48         }
49
50         String extractTypeName() {
51                 // answer a String with the qualified type name for the source file in the
52                 // form: 'p1/p2/A'
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
59                                                                                                                                                                                                                                                                                                                                 // of
60                                                                                                                                                                                                                                                                                                                                 // ".php"
61                 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
62                         charCount += fullPath.segment(i).length();
63
64                 char[] result = new char[charCount];
65                 int offset = 0;
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);
71                         offset += size;
72                         result[offset++] = '/';
73                 }
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);
78         }
79
80         public char[] getContents() {
81
82                 try {
83                         return Util.getResourceContentsAsCharArray(resource, this.encoding);
84                 } catch (CoreException e) {
85                         throw new AbortCompilation(true, new MissingSourceFileException(resource.getFullPath().toString()));
86                 }
87         }
88
89         public char[] getFileName() {
90                 return resource.getFullPath().toString().toCharArray(); // do not know what
91                                                                                                                                                                                                                                                 // you want to
92                                                                                                                                                                                                                                                 // return here
93         }
94
95         public char[] getMainTypeName() {
96                 char[] typeName = initialTypeName.toCharArray();
97                 int lastIndex = CharOperation.lastIndexOf('/', typeName);
98                 return CharOperation.subarray(typeName, lastIndex + 1, -1);
99         }
100
101         public char[][] getPackageName() {
102                 char[] typeName = initialTypeName.toCharArray();
103                 int lastIndex = CharOperation.lastIndexOf('/', typeName);
104                 return CharOperation.splitOn('/', typeName, 0, lastIndex);
105         }
106
107         String typeLocator() {
108                 return resource.getProjectRelativePath().toString();
109         }
110
111         public String toString() {
112                 return "SourceFile[" //$NON-NLS-1$
113                                 + resource.getFullPath() + "]"; //$NON-NLS-1$
114         }
115
116         public IResource getResource() {
117                 return resource;
118         }
119
120 }