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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.batch;
14 import java.io.IOException;
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;
20 public class CompilationUnit implements ICompilationUnit {
21 public char[] contents;
22 public char[] fileName;
23 public char[] mainTypeName;
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);
33 // the file separator is \
34 if (fileName.indexOf('/') != -1) {
35 fileName = fileName.replace('/', File.separatorChar);
38 this.fileName = fileName.toCharArray();
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$
44 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
46 end = fileName.length();
48 this.mainTypeName = fileName.substring(start, end).toCharArray();
49 this.encoding = encoding;
51 public char[] getContents() {
52 if (this.contents != null)
53 return this.contents; // answer the cached source
55 // otherwise retrieve it
57 return Util.getFileCharContent(new File(new String(this.fileName)), this.encoding);
58 } catch (IOException e) {
59 // assume no content then
61 return CharOperation.NO_CHAR;
63 public char[] getFileName() {
66 public char[] getMainTypeName() {
67 return this.mainTypeName;
69 public char[][] getPackageName() {
72 public String toString() {
73 return "CompilationUnit[" + new String(this.fileName) + "]"; //$NON-NLS-2$ //$NON-NLS-1$