improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / batch / CompilationUnit.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.batch;
9
10 import java.io.File;
11 import java.io.IOException;
12
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
15 import net.sourceforge.phpdt.internal.compiler.util.Util;
16
17 import org.eclipse.core.resources.IResource;
18
19 public class CompilationUnit implements ICompilationUnit {
20   public char[] contents;
21
22   public char[] fileName;
23
24   public char[] mainTypeName;
25
26   String encoding;
27
28   public CompilationUnit(char[] contents, String fileName, String encoding) {
29     this.contents = contents;
30     if (File.separator.equals("/")) { //$NON-NLS-1$
31       if (fileName.indexOf("\\") != -1) { //$NON-NLS-1$
32         fileName = fileName.replace('\\', File.separatorChar);
33       }
34     } else {
35       // the file separator is \
36       if (fileName.indexOf('/') != -1) {
37         fileName = fileName.replace('/', File.separatorChar);
38       }
39     }
40     this.fileName = fileName.toCharArray();
41
42     int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
43     if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
44       start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
45
46     int end = fileName.lastIndexOf("."); //$NON-NLS-1$
47     if (end == -1)
48       end = fileName.length();
49
50     this.mainTypeName = fileName.substring(start, end).toCharArray();
51     this.encoding = encoding;
52   }
53
54   public char[] getContents() {
55     if (this.contents != null)
56       return this.contents; // answer the cached source
57
58     // otherwise retrieve it
59     try {
60       return Util.getFileCharContent(new File(new String(this.fileName)), this.encoding);
61     } catch (IOException e) {
62       // assume no content then
63     }
64     return CharOperation.NO_CHAR;
65   }
66
67   public char[] getFileName() {
68     return this.fileName;
69   }
70
71   public char[] getMainTypeName() {
72     return this.mainTypeName;
73   }
74
75   public char[][] getPackageName() {
76     return null;
77   }
78
79   public String toString() {
80     return "CompilationUnit[" + new String(this.fileName) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
81   }
82
83   public IResource getResource() {
84     return null;
85   }
86 }