KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > BasicCompilationUnit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.compiler.CharOperation;
22 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
23 import org.eclipse.jdt.internal.compiler.util.Util;
24
25 /**
26  * A basic implementation of <code>ICompilationUnit</code>
27  * for use in the <code>SourceMapper</code>.
28  * @see ICompilationUnit
29  */

30 public class BasicCompilationUnit implements ICompilationUnit {
31     protected char[] contents;
32     
33     // Note that if this compiler ICompilationUnit's content is known in advance, the fileName is not used to retrieve this content.
34
// Instead it is used to keep enough information to recreate the IJavaElement corresponding to this compiler ICompilationUnit.
35
// Thus the fileName can be a path to a .class file, or even a path in a .jar to a .class file.
36
// (e.g. /P/lib/mylib.jar|org/eclipse/test/X.class)
37
protected char[] fileName;
38     
39     protected char[][] packageName;
40     protected char[] mainTypeName;
41     protected String JavaDoc encoding;
42
43 public BasicCompilationUnit(char[] contents, char[][] packageName, String JavaDoc fileName) {
44     this.contents = contents;
45     this.fileName = fileName.toCharArray();
46     this.packageName = packageName;
47 }
48
49 public BasicCompilationUnit(char[] contents, char[][] packageName, String JavaDoc fileName, String JavaDoc encoding) {
50     this(contents, packageName, fileName);
51     this.encoding = encoding;
52 }
53
54 public BasicCompilationUnit(char[] contents, char[][] packageName, String JavaDoc fileName, IJavaElement javaElement) {
55     this(contents, packageName, fileName);
56     initEncoding(javaElement);
57 }
58
59 /*
60  * Initialize compilation unit encoding.
61  * If we have a project, then get file name corresponding IFile and retrieve its encoding using
62  * new API for encoding.
63  * In case of a class file, then go through project in order to let the possibility to retrieve
64  * a corresponding source file resource.
65  * If we have a compilation unit, then get encoding from its resource directly...
66  */

67 private void initEncoding(IJavaElement javaElement) {
68     if (javaElement != null) {
69         try {
70             IJavaProject javaProject = javaElement.getJavaProject();
71             switch (javaElement.getElementType()) {
72                 case IJavaElement.COMPILATION_UNIT:
73                     IFile file = (IFile) javaElement.getResource();
74                     if (file != null) {
75                         this.encoding = file.getCharset();
76                         break;
77                     }
78                     // if no file, then get project encoding
79
default:
80                     IProject project = (IProject) javaProject.getResource();
81                     if (project != null) {
82                         this.encoding = project.getDefaultCharset();
83                     }
84                     break;
85             }
86         } catch (CoreException e1) {
87             this.encoding = null;
88         }
89     } else {
90         this.encoding = null;
91     }
92 }
93
94 public char[] getContents() {
95     if (this.contents != null)
96         return this.contents; // answer the cached source
97

98     // otherwise retrieve it
99
try {
100         return Util.getFileCharContent(new File JavaDoc(new String JavaDoc(this.fileName)), this.encoding);
101     } catch (IOException JavaDoc e) {
102         // could not read file: returns an empty array
103
}
104     return CharOperation.NO_CHAR;
105 }
106 /**
107  * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
108  */

109 public char[] getFileName() {
110     return this.fileName;
111 }
112 public char[] getMainTypeName() {
113     if (this.mainTypeName == null) {
114         int start = CharOperation.lastIndexOf('/', this.fileName) + 1;
115         if (start == 0 || start < CharOperation.lastIndexOf('\\', this.fileName))
116             start = CharOperation.lastIndexOf('\\', this.fileName) + 1;
117         int separator = CharOperation.indexOf('|', this.fileName) + 1;
118         if (separator > start) // case of a .class file in a default package in a jar
119
start = separator;
120     
121         int end = CharOperation.lastIndexOf('$', this.fileName);
122         if (end == -1 || !Util.isClassFileName(this.fileName)) {
123             end = CharOperation.lastIndexOf('.', this.fileName);
124             if (end == -1)
125                 end = this.fileName.length;
126         }
127     
128         this.mainTypeName = CharOperation.subarray(this.fileName, start, end);
129     }
130     return this.mainTypeName;
131 }
132 public char[][] getPackageName() {
133     return this.packageName;
134 }
135 public String JavaDoc toString(){
136     return "CompilationUnit: "+new String JavaDoc(this.fileName); //$NON-NLS-1$
137
}
138 }
139
Popular Tags