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
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.batch;
11 import java.io.IOException;
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;
17 import org.eclipse.core.resources.IResource;
19 public class CompilationUnit implements ICompilationUnit {
20 public char[] contents;
22 public char[] fileName;
24 public char[] mainTypeName;
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);
35 // the file separator is \
36 if (fileName.indexOf('/') != -1) {
37 fileName = fileName.replace('/', File.separatorChar);
40 this.fileName = fileName.toCharArray();
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$
46 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
48 end = fileName.length();
50 this.mainTypeName = fileName.substring(start, end).toCharArray();
51 this.encoding = encoding;
54 public char[] getContents() {
55 if (this.contents != null)
56 return this.contents; // answer the cached source
58 // otherwise retrieve it
60 return Util.getFileCharContent(new File(new String(this.fileName)),
62 } catch (IOException e) {
63 // assume no content then
65 return CharOperation.NO_CHAR;
68 public char[] getFileName() {
72 public char[] getMainTypeName() {
73 return this.mainTypeName;
76 public char[][] getPackageName() {
80 public String toString() {
81 return "CompilationUnit[" + new String(this.fileName) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
84 public IResource getResource() {