Parser detects wrong include files now
[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.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26
27 /**
28  * A basic implementation of <code>ICompilationUnit</code> for use in the <code>SourceMapper</code>.
29  * 
30  * @see ICompilationUnit
31  */
32 public class BasicCompilationUnit implements ICompilationUnit {
33   protected char[] contents;
34
35   protected char[] fileName;
36
37   protected char[][] packageName;
38
39   protected char[] mainTypeName;
40
41   protected String encoding;
42
43   protected IResource resource;
44
45   //  public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
46   //    this.contents = contents;
47   //    this.fileName = fileName.toCharArray();
48   //    this.packageName = packageName;
49   //
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$
53   //
54   //    int end = fileName.lastIndexOf("."); //$NON-NLS-1$
55   //    if (end == -1)
56   //      end = fileName.length();
57   //
58   //    this.mainTypeName = fileName.substring(start, end).toCharArray();
59   //    this.encoding = encoding;
60   //  }
61   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName) {
62     this.contents = contents;
63     this.fileName = fileName.toCharArray();
64     this.packageName = packageName;
65
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$
69
70     int end = fileName.lastIndexOf("."); //$NON-NLS-1$
71     if (end == -1)
72       end = fileName.length();
73
74     this.mainTypeName = fileName.substring(start, end).toCharArray();
75     this.encoding = null;
76     this.resource = null;
77   }
78
79   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
80     this(contents, packageName, fileName);
81     this.encoding = encoding;
82     this.resource = null;
83   }
84
85   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, IJavaElement javaElement) {
86     this(contents, packageName, fileName);
87     initEncoding(javaElement);
88   }
89
90   /*
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...
94    */
95   private void initEncoding(IJavaElement javaElement) {
96     if (javaElement != null) {
97       try {
98         IJavaProject javaProject = javaElement.getJavaProject();
99         switch (javaElement.getElementType()) {
100         case IJavaElement.COMPILATION_UNIT:
101           IFile file = (IFile) javaElement.getResource();
102
103           if (file != null) {
104             resource = file;
105             this.encoding = file.getCharset();
106             break;
107           }
108         // if no file, then get project encoding
109         default:
110           IProject project = (IProject) javaProject.getResource();
111           if (project != null) {
112             resource = project;
113             this.encoding = project.getDefaultCharset();
114           }
115           break;
116         }
117       } catch (CoreException e1) {
118         this.encoding = null;
119       }
120     } else {
121       this.encoding = null;
122     }
123   }
124
125   public char[] getContents() {
126     if (this.contents != null)
127       return this.contents; // answer the cached source
128
129     // otherwise retrieve it
130     try {
131       return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
132     } catch (IOException e) {
133     }
134     return CharOperation.NO_CHAR;
135   }
136
137   public char[] getFileName() {
138     return this.fileName;
139   }
140
141   public char[] getMainTypeName() {
142     return this.mainTypeName;
143   }
144
145   public char[][] getPackageName() {
146     return this.packageName;
147   }
148
149   public String toString() {
150     return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$
151   }
152
153   /* (non-Javadoc)
154    * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getResource()
155    */
156   public IResource getResource() {
157     return resource;
158   }
159 }