Eclipse3M7 version
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / batch / CompilationUnit.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.compiler.batch;
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 public class CompilationUnit implements ICompilationUnit {
21         public char[] contents;
22         public char[] fileName;
23         public char[] mainTypeName;
24         String encoding;
25         
26 public CompilationUnit(char[] contents, String fileName, String encoding) {
27         this.contents = contents;
28         if (File.separator.equals("/")) { //$NON-NLS-1$
29                 if (fileName.indexOf("\\") != -1) { //$NON-NLS-1$
30                         fileName = fileName.replace('\\', File.separatorChar);
31                 }
32         } else {
33                 // the file separator is \
34                 if (fileName.indexOf('/') != -1) {
35                         fileName = fileName.replace('/', File.separatorChar);
36                 }
37         }
38         this.fileName = fileName.toCharArray();
39
40         int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
41         if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
42                 start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
43
44         int end = fileName.lastIndexOf("."); //$NON-NLS-1$
45         if (end == -1)
46                 end = fileName.length();
47
48         this.mainTypeName = fileName.substring(start, end).toCharArray();
49         this.encoding = encoding;
50 }
51 public char[] getContents() {
52         if (this.contents != null)
53                 return this.contents;   // answer the cached source
54
55         // otherwise retrieve it
56         try {
57                 return Util.getFileCharContent(new File(new String(this.fileName)), this.encoding);
58         } catch (IOException e) {
59                 // assume no content then
60         }
61         return CharOperation.NO_CHAR;
62 }
63 public char[] getFileName() {
64         return this.fileName;
65 }
66 public char[] getMainTypeName() {
67         return this.mainTypeName;
68 }
69 public char[][] getPackageName() {
70         return null;
71 }
72 public String toString() {
73         return "CompilationUnit[" + new String(this.fileName) + "]";  //$NON-NLS-2$ //$NON-NLS-1$
74 }
75 }