1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
14 import java.io.IOException;
16 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
17 import net.sourceforge.phpdt.internal.compiler.util.Util;
20 * A basic implementation of <code>ICompilationUnit</code>
21 * for use in the <code>SourceMapper</code>.
22 * @see ICompilationUnit
24 public class BasicCompilationUnit implements ICompilationUnit {
25 protected char[] contents;
26 protected char[] fileName;
27 protected char[][] packageName;
28 protected char[] mainTypeName;
29 protected String encoding;
31 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
32 this.contents = contents;
33 this.fileName = fileName.toCharArray();
34 this.packageName = packageName;
36 int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
37 if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
38 start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
40 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
42 end = fileName.length();
44 this.mainTypeName = fileName.substring(start, end).toCharArray();
45 this.encoding = encoding;
47 public char[] getContents() {
48 if (this.contents != null)
49 return this.contents; // answer the cached source
51 // otherwise retrieve it
53 return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
54 } catch (IOException e) {
58 public char[] getFileName() {
61 public char[] getMainTypeName() {
62 return this.mainTypeName;
64 public char[][] getPackageName() {
65 return this.packageName;
67 public String toString(){
68 return "CompilationUnit: "+new String(fileName); //$NON-NLS-1$