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 org.eclipse.core.resources.IResource;
 
  15 import net.sourceforge.phpdt.core.compiler.CharOperation;
 
  16 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
 
  17 import net.sourceforge.phpdt.internal.compiler.util.Util;
 
  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)), this.encoding);
 
  61     } catch (IOException e) {
 
  62       // assume no content then
 
  64     return CharOperation.NO_CHAR;
 
  67   public char[] getFileName() {
 
  71   public char[] getMainTypeName() {
 
  72     return this.mainTypeName;
 
  75   public char[][] getPackageName() {
 
  79   public String toString() {
 
  80     return "CompilationUnit[" + new String(this.fileName) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
 
  86    * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getResource()
 
  88   public IResource getResource() {