Refactory: remove unused classes, imports, fields and methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / BasicCompilationUnit.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;
12
13 import java.io.File;
14 import java.io.IOException;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IJavaProject;
18 import net.sourceforge.phpdt.core.compiler.CharOperation;
19 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
20 import net.sourceforge.phpdt.internal.compiler.util.Util;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26
27 /**
28  * A basic implementation of <code>ICompilationUnit</code> for use in the
29  * <code>SourceMapper</code>.
30  * 
31  * @see ICompilationUnit
32  */
33 public class BasicCompilationUnit implements ICompilationUnit {
34         protected char[] contents;
35
36         protected char[] fileName;
37
38         protected char[][] packageName;
39
40         protected char[] mainTypeName;
41
42         protected String encoding;
43
44         protected IResource resource;
45
46         // public BasicCompilationUnit(char[] contents, char[][] packageName, String
47         // fileName, String encoding) {
48         // this.contents = contents;
49         // this.fileName = fileName.toCharArray();
50         // this.packageName = packageName;
51         //
52         // int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
53         // if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
54         // start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
55         //
56         // int end = fileName.lastIndexOf("."); //$NON-NLS-1$
57         // if (end == -1)
58         // end = fileName.length();
59         //
60         // this.mainTypeName = fileName.substring(start, end).toCharArray();
61         // this.encoding = encoding;
62         // }
63         public BasicCompilationUnit(char[] contents, char[][] packageName,
64                         String fileName) {
65                 this.contents = contents;
66                 this.fileName = fileName.toCharArray();
67                 this.packageName = packageName;
68
69                 int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
70                 if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
71                         start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
72
73                 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
74                 if (end == -1)
75                         end = fileName.length();
76
77                 this.mainTypeName = fileName.substring(start, end).toCharArray();
78                 this.encoding = null;
79                 this.resource = null;
80         }
81
82         public BasicCompilationUnit(char[] contents, char[][] packageName,
83                         String fileName, String encoding) {
84                 this(contents, packageName, fileName);
85                 this.encoding = encoding;
86                 this.resource = null;
87         }
88
89         public BasicCompilationUnit(char[] contents, char[][] packageName,
90                         String fileName, IJavaElement javaElement) {
91                 this(contents, packageName, fileName);
92                 initEncoding(javaElement);
93         }
94
95         /*
96          * Initialize compilation unit encoding. If we have a project, then get file
97          * name corresponding IFile and retrieve its encoding using new API for
98          * encoding. In case of a class file, then go through project in order to
99          * let the possibility to retrieve a corresponding source file resource. If
100          * we have a compilation unit, then get encoding from its resource
101          * directly...
102          */
103         private void initEncoding(IJavaElement javaElement) {
104                 if (javaElement != null) {
105                         try {
106                                 IJavaProject javaProject = javaElement.getJavaProject();
107                                 switch (javaElement.getElementType()) {
108                                 case IJavaElement.COMPILATION_UNIT:
109                                         IFile file = (IFile) javaElement.getResource();
110
111                                         if (file != null) {
112                                                 resource = file;
113                                                 this.encoding = file.getCharset();
114                                                 break;
115                                         }
116                                         // if no file, then get project encoding
117                                 default:
118                                         IProject project = (IProject) javaProject.getResource();
119                                         if (project != null) {
120                                                 resource = project;
121                                                 this.encoding = project.getDefaultCharset();
122                                         }
123                                         break;
124                                 }
125                         } catch (CoreException e1) {
126                                 this.encoding = null;
127                         }
128                 } else {
129                         this.encoding = null;
130                 }
131         }
132
133         public char[] getContents() {
134                 if (this.contents != null)
135                         return this.contents; // answer the cached source
136
137                 // otherwise retrieve it
138                 try {
139                         return Util.getFileCharContent(new File(new String(fileName)),
140                                         this.encoding);
141                 } catch (IOException e) {
142                 }
143                 return CharOperation.NO_CHAR;
144         }
145
146         public char[] getFileName() {
147                 return this.fileName;
148         }
149
150         public char[] getMainTypeName() {
151                 return this.mainTypeName;
152         }
153
154         public char[][] getPackageName() {
155                 return this.packageName;
156         }
157
158         public String toString() {
159                 return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$
160         }
161
162         /*
163          * (non-Javadoc)
164          * 
165          * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getResource()
166          */
167         public IResource getResource() {
168                 return resource;
169         }
170 }