A massive organize imports and formatting of the sources using default Eclipse code...
[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,
34                         String encoding) {
35                 this.resource = resource;
36                 this.sourceLocation = sourceLocation;
37                 this.initialTypeName = extractTypeName();
38                 this.encoding = encoding;
39         }
40
41         public boolean equals(Object o) {
42                 if (this == o)
43                         return true;
44                 if (!(o instanceof SourceFile))
45                         return false;
46
47                 SourceFile f = (SourceFile) o;
48                 return sourceLocation == f.sourceLocation
49                                 && resource.getFullPath().equals(f.resource.getFullPath());
50         }
51
52         String extractTypeName() {
53                 // answer a String with the qualified type name for the source file in
54                 // the
55                 // form: 'p1/p2/A'
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
64                 // of
65                 // ".php"
66                 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
67                         charCount += fullPath.segment(i).length();
68
69                 char[] result = new char[charCount];
70                 int offset = 0;
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);
76                         offset += size;
77                         result[offset++] = '/';
78                 }
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);
83         }
84
85         public char[] getContents() {
86
87                 try {
88                         return Util.getResourceContentsAsCharArray(resource, this.encoding);
89                 } catch (CoreException e) {
90                         throw new AbortCompilation(true, new MissingSourceFileException(
91                                         resource.getFullPath().toString()));
92                 }
93         }
94
95         public char[] getFileName() {
96                 return resource.getFullPath().toString().toCharArray(); // do not know
97                                                                                                                                 // what
98                 // you want to
99                 // return here
100         }
101
102         public char[] getMainTypeName() {
103                 char[] typeName = initialTypeName.toCharArray();
104                 int lastIndex = CharOperation.lastIndexOf('/', typeName);
105                 return CharOperation.subarray(typeName, lastIndex + 1, -1);
106         }
107
108         public char[][] getPackageName() {
109                 char[] typeName = initialTypeName.toCharArray();
110                 int lastIndex = CharOperation.lastIndexOf('/', typeName);
111                 return CharOperation.splitOn('/', typeName, 0, lastIndex);
112         }
113
114         String typeLocator() {
115                 return resource.getProjectRelativePath().toString();
116         }
117
118         public String toString() {
119                 return "SourceFile[" //$NON-NLS-1$
120                                 + resource.getFullPath() + "]"; //$NON-NLS-1$
121         }
122
123         public IResource getResource() {
124                 return resource;
125         }
126
127 }