1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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.core;
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 import org.eclipse.core.resources.IResource;
23 * A basic implementation of <code>ICompilationUnit</code>
24 * for use in the <code>SourceMapper</code>.
25 * @see ICompilationUnit
27 public class BasicCompilationUnit implements ICompilationUnit {
28 protected char[] contents;
29 protected char[] fileName;
30 protected char[][] packageName;
31 protected char[] mainTypeName;
32 protected String encoding;
34 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) {
35 this.contents = contents;
36 this.fileName = fileName.toCharArray();
37 this.packageName = packageName;
39 int start = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$
40 if (start == 0 || start < fileName.lastIndexOf("\\")) //$NON-NLS-1$
41 start = fileName.lastIndexOf("\\") + 1; //$NON-NLS-1$
43 int end = fileName.lastIndexOf("."); //$NON-NLS-1$
45 end = fileName.length();
47 this.mainTypeName = fileName.substring(start, end).toCharArray();
48 this.encoding = encoding;
50 public char[] getContents() {
51 if (this.contents != null)
52 return this.contents; // answer the cached source
54 // otherwise retrieve it
56 return Util.getFileCharContent(new File(new String(fileName)), this.encoding);
57 } catch (IOException e) {
59 return CharOperation.NO_CHAR;
61 public char[] getFileName() {
64 public char[] getMainTypeName() {
65 return this.mainTypeName;
67 public char[][] getPackageName() {
68 return this.packageName;
70 public String toString() {
71 return "CompilationUnit: " + new String(fileName); //$NON-NLS-1$