3m9 compatible;
[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.runtime.CoreException;
25
26 /**
27  * A basic implementation of <code>ICompilationUnit</code>
28  * for use in the <code>SourceMapper</code>.
29  * @see ICompilationUnit
30  */
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;
37
38 //  public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
39 //    this.contents = contents;
40 //    this.fileName = fileName.toCharArray();
41 //    this.packageName = packageName;
42 //
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$
46 //
47 //    int end = fileName.lastIndexOf("."); //$NON-NLS-1$
48 //    if (end == -1)
49 //      end = fileName.length();
50 //
51 //    this.mainTypeName = fileName.substring(start, end).toCharArray();
52 //    this.encoding = encoding;
53 //  }
54   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName) {
55         this.contents = contents;
56         this.fileName = fileName.toCharArray();
57         this.packageName = packageName;
58
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$
62
63         int end = fileName.lastIndexOf("."); //$NON-NLS-1$
64         if (end == -1)
65                 end = fileName.length();
66
67         this.mainTypeName = fileName.substring(start, end).toCharArray();
68         this.encoding = null;
69 }
70   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
71         this(contents, packageName, fileName);
72         this.encoding = encoding;
73   }
74   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, IJavaElement javaElement) {
75         this(contents, packageName, fileName);
76         initEncoding(javaElement);
77   }
78   /*
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...
85    */
86   private void initEncoding(IJavaElement javaElement) {
87         if (javaElement != null) {
88                 try {
89                         IJavaProject javaProject = javaElement.getJavaProject();
90                         switch (javaElement.getElementType()) {
91                                 case IJavaElement.COMPILATION_UNIT:
92                                         IFile file = (IFile) javaElement.getResource();
93                                         if (file != null) {
94                                                 this.encoding = file.getCharset();
95                                                 break;
96                                         }
97                                         // if no file, then get project encoding
98                                 default:
99                                         IProject project = (IProject) javaProject.getResource();
100                                         if (project != null) {
101                                                 this.encoding = project.getDefaultCharset();
102                                         }
103                                         break;
104                         }
105                 } catch (CoreException e1) {
106                         this.encoding = null;
107                 }
108         } else  {
109                 this.encoding = null;
110         }
111   }
112   public char[] getContents() {
113     if (this.contents != null)
114       return this.contents; // answer the cached source
115
116     // otherwise retrieve it
117     try {
118       return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
119     } catch (IOException e) {
120     }
121     return CharOperation.NO_CHAR;
122   }
123   public char[] getFileName() {
124     return this.fileName;
125   }
126   public char[] getMainTypeName() {
127     return this.mainTypeName;
128   }
129   public char[][] getPackageName() {
130     return this.packageName;
131   }
132   public String toString() {
133     return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$
134   }
135   
136
137 }