Fixed: malfunctioned "Remove trailing spaces on editor save"
[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)),
61                                         this.encoding);
62                 } catch (IOException e) {
63                         // assume no content then
64                 }
65                 return CharOperation.NO_CHAR;
66         }
67
68         public char[] getFileName() {
69                 return this.fileName;
70         }
71
72         public char[] getMainTypeName() {
73                 return this.mainTypeName;
74         }
75
76         public char[][] getPackageName() {
77                 return null;
78         }
79
80         public String toString() {
81                 return "CompilationUnit[" + new String(this.fileName) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
82         }
83
84         public IResource getResource() {
85                 return null;
86         }
87 }