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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
14 import java.io.IOException;
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;
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;
28 * A basic implementation of <code>ICompilationUnit</code> for use in the
29 * <code>SourceMapper</code>.
31 * @see ICompilationUnit
33 public class BasicCompilationUnit implements ICompilationUnit {
34 protected char[] contents;
36 protected char[] fileName;
38 protected char[][] packageName;
40 protected char[] mainTypeName;
42 protected String encoding;
44 protected IResource resource;
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;
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$
56 // int end = fileName.lastIndexOf("."); //$NON-NLS-1$
58 // end = fileName.length();
60 // this.mainTypeName = fileName.substring(start, end).toCharArray();
61 // this.encoding = encoding;
63 public BasicCompilationUnit(char[] contents, char[][] packageName,
65 this.contents = contents;
66 this.fileName = fileName.toCharArray();
67 this.packageName = packageName;
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$
73 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
75 end = fileName.length();
77 this.mainTypeName = fileName.substring(start, end).toCharArray();
82 public BasicCompilationUnit(char[] contents, char[][] packageName,
83 String fileName, String encoding) {
84 this(contents, packageName, fileName);
85 this.encoding = encoding;
89 public BasicCompilationUnit(char[] contents, char[][] packageName,
90 String fileName, IJavaElement javaElement) {
91 this(contents, packageName, fileName);
92 initEncoding(javaElement);
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
103 private void initEncoding(IJavaElement javaElement) {
104 if (javaElement != null) {
106 IJavaProject javaProject = javaElement.getJavaProject();
107 switch (javaElement.getElementType()) {
108 case IJavaElement.COMPILATION_UNIT:
109 IFile file = (IFile) javaElement.getResource();
113 this.encoding = file.getCharset();
116 // if no file, then get project encoding
118 IProject project = (IProject) javaProject.getResource();
119 if (project != null) {
121 this.encoding = project.getDefaultCharset();
125 } catch (CoreException e1) {
126 this.encoding = null;
129 this.encoding = null;
133 public char[] getContents() {
134 if (this.contents != null)
135 return this.contents; // answer the cached source
137 // otherwise retrieve it
139 return Util.getFileCharContent(new File(new String(fileName)),
141 } catch (IOException e) {
143 return CharOperation.NO_CHAR;
146 public char[] getFileName() {
147 return this.fileName;
150 public char[] getMainTypeName() {
151 return this.mainTypeName;
154 public char[][] getPackageName() {
155 return this.packageName;
158 public String toString() {
159 return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$
165 * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getResource()
167 public IResource getResource() {