new version with WorkingCopy Management
[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.compiler.CharOperation;
17 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
18 import net.sourceforge.phpdt.internal.compiler.util.Util;
19
20 import org.eclipse.core.resources.IResource;
21
22 /**
23  * A basic implementation of <code>ICompilationUnit</code>
24  * for use in the <code>SourceMapper</code>.
25  * @see ICompilationUnit
26  */
27 public class BasicCompilationUnit implements ICompilationUnit {
28   protected char[] contents;
29   protected char[] fileName;
30   protected char[][] packageName;
31   protected char[] mainTypeName;
32   protected String encoding;
33
34   public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
35     this.contents = contents;
36     this.fileName = fileName.toCharArray();
37     this.packageName = packageName;
38
39     int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
40     if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
41       start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
42
43     int end = fileName.lastIndexOf("."); //$NON-NLS-1$
44     if (end == -1)
45       end = fileName.length();
46
47     this.mainTypeName = fileName.substring(start, end).toCharArray();
48     this.encoding = encoding;
49   }
50   public char[] getContents() {
51     if (this.contents != null)
52       return this.contents; // answer the cached source
53
54     // otherwise retrieve it
55     try {
56       return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
57     } catch (IOException e) {
58     }
59     return CharOperation.NO_CHAR;
60   }
61   public char[] getFileName() {
62     return this.fileName;
63   }
64   public char[] getMainTypeName() {
65     return this.mainTypeName;
66   }
67   public char[][] getPackageName() {
68     return this.packageName;
69   }
70   public String toString() {
71     return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$
72   }
73   
74
75 }