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.runtime.CoreException;
27 * A basic implementation of <code>ICompilationUnit</code>
28 * for use in the <code>SourceMapper</code>.
29 * @see ICompilationUnit
31 public class BasicCompilationUnit implements ICompilationUnit {
32 protected char[] contents;
33 protected char[] fileName;
34 protected char[][] packageName;
35 protected char[] mainTypeName;
36 protected String encoding;
38 // public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
39 // this.contents = contents;
40 // this.fileName = fileName.toCharArray();
41 // this.packageName = packageName;
43 // int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
44 // if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
45 // start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
47 // int end = fileName.lastIndexOf("."); //$NON-NLS-1$
49 // end = fileName.length();
51 // this.mainTypeName = fileName.substring(start, end).toCharArray();
52 // this.encoding = encoding;
54 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName) {
55 this.contents = contents;
56 this.fileName = fileName.toCharArray();
57 this.packageName = packageName;
59 int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
60 if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
61 start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
63 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
65 end = fileName.length();
67 this.mainTypeName = fileName.substring(start, end).toCharArray();
70 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
71 this(contents, packageName, fileName);
72 this.encoding = encoding;
74 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, IJavaElement javaElement) {
75 this(contents, packageName, fileName);
76 initEncoding(javaElement);
79 * Initialize compilation unit encoding.
80 * If we have a project, then get file name corresponding IFile and retrieve its encoding using
81 * new API for encoding.
82 * In case of a class file, then go through project in order to let the possibility to retrieve
83 * a corresponding source file resource.
84 * If we have a compilation unit, then get encoding from its resource directly...
86 private void initEncoding(IJavaElement javaElement) {
87 if (javaElement != null) {
89 IJavaProject javaProject = javaElement.getJavaProject();
90 switch (javaElement.getElementType()) {
91 case IJavaElement.COMPILATION_UNIT:
92 IFile file = (IFile) javaElement.getResource();
94 this.encoding = file.getCharset();
97 // if no file, then get project encoding
99 IProject project = (IProject) javaProject.getResource();
100 if (project != null) {
101 this.encoding = project.getDefaultCharset();
105 } catch (CoreException e1) {
106 this.encoding = null;
109 this.encoding = null;
112 public char[] getContents() {
113 if (this.contents != null)
114 return this.contents; // answer the cached source
116 // otherwise retrieve it
118 return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
119 } catch (IOException e) {
121 return CharOperation.NO_CHAR;
123 public char[] getFileName() {
124 return this.fileName;
126 public char[] getMainTypeName() {
127 return this.mainTypeName;
129 public char[][] getPackageName() {
130 return this.packageName;
132 public String toString() {
133 return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$