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 <code>SourceMapper</code>.
30 * @see ICompilationUnit
32 public class BasicCompilationUnit implements ICompilationUnit {
33 protected char[] contents;
35 protected char[] fileName;
37 protected char[][] packageName;
39 protected char[] mainTypeName;
41 protected String encoding;
43 protected IResource resource;
45 // public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
46 // this.contents = contents;
47 // this.fileName = fileName.toCharArray();
48 // this.packageName = packageName;
50 // int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
51 // if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
52 // start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
54 // int end = fileName.lastIndexOf("."); //$NON-NLS-1$
56 // end = fileName.length();
58 // this.mainTypeName = fileName.substring(start, end).toCharArray();
59 // this.encoding = encoding;
61 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName) {
62 this.contents = contents;
63 this.fileName = fileName.toCharArray();
64 this.packageName = packageName;
66 int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
67 if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
68 start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
70 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
72 end = fileName.length();
74 this.mainTypeName = fileName.substring(start, end).toCharArray();
79 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
80 this(contents, packageName, fileName);
81 this.encoding = encoding;
85 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, IJavaElement javaElement) {
86 this(contents, packageName, fileName);
87 initEncoding(javaElement);
91 * Initialize compilation unit encoding. If we have a project, then get file name corresponding IFile and retrieve its encoding
92 * using new API for encoding. In case of a class file, then go through project in order to let the possibility to retrieve a
93 * corresponding source file resource. If we have a compilation unit, then get encoding from its resource directly...
95 private void initEncoding(IJavaElement javaElement) {
96 if (javaElement != null) {
98 IJavaProject javaProject = javaElement.getJavaProject();
99 switch (javaElement.getElementType()) {
100 case IJavaElement.COMPILATION_UNIT:
101 IFile file = (IFile) javaElement.getResource();
105 this.encoding = file.getCharset();
108 // if no file, then get project encoding
110 IProject project = (IProject) javaProject.getResource();
111 if (project != null) {
113 this.encoding = project.getDefaultCharset();
117 } catch (CoreException e1) {
118 this.encoding = null;
121 this.encoding = null;
125 public char[] getContents() {
126 if (this.contents != null)
127 return this.contents; // answer the cached source
129 // otherwise retrieve it
131 return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
132 } catch (IOException e) {
134 return CharOperation.NO_CHAR;
137 public char[] getFileName() {
138 return this.fileName;
141 public char[] getMainTypeName() {
142 return this.mainTypeName;
145 public char[][] getPackageName() {
146 return this.packageName;
149 public String toString() {
150 return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$
154 * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getResource()
156 public IResource getResource() {