KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > batch > CompilationUnit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.batch;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15
16 import org.eclipse.jdt.core.compiler.CharOperation;
17 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
18 import org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit;
19 import org.eclipse.jdt.internal.compiler.util.Util;
20
21 public class CompilationUnit implements ICompilationUnit {
22     public char[] contents;
23     public char[] fileName;
24     public char[] mainTypeName;
25     String JavaDoc encoding;
26     public String JavaDoc destinationPath;
27         // a specific destination path for this compilation unit; coding is
28
// aligned with Main.destinationPath:
29
// == null: unspecified, use whatever value is set by the enclosing
30
// context, id est Main;
31
// == Main.NONE: absorbent element, do not output class files;
32
// else: use as the path of the directory into which class files must
33
// be written.
34

35 public CompilationUnit(char[] contents, String JavaDoc fileName, String JavaDoc encoding) {
36     this(contents, fileName, encoding, null);
37 }
38 public CompilationUnit(char[] contents, String JavaDoc fileName, String JavaDoc encoding,
39         String JavaDoc destinationPath) {
40     this.contents = contents;
41     char[] fileNameCharArray = fileName.toCharArray();
42     switch(File.separatorChar) {
43         case '/' :
44             if (CharOperation.indexOf('\\', fileNameCharArray) != -1) {
45                 CharOperation.replace(fileNameCharArray, '\\', '/');
46             }
47             break;
48         case '\\' :
49             if (CharOperation.indexOf('/', fileNameCharArray) != -1) {
50                 CharOperation.replace(fileNameCharArray, '/', '\\');
51             }
52     }
53     this.fileName = fileNameCharArray;
54     int start = CharOperation.lastIndexOf(File.separatorChar, fileNameCharArray) + 1;
55
56     int end = CharOperation.lastIndexOf('.', fileNameCharArray);
57     if (end == -1) {
58         end = fileNameCharArray.length;
59     }
60
61     this.mainTypeName = CharOperation.subarray(fileNameCharArray, start, end);
62     this.encoding = encoding;
63     this.destinationPath = destinationPath;
64 }
65 public char[] getContents() {
66     if (this.contents != null)
67         return this.contents; // answer the cached source
68

69     // otherwise retrieve it
70
try {
71         return Util.getFileCharContent(new File JavaDoc(new String JavaDoc(this.fileName)), this.encoding);
72     } catch (IOException JavaDoc e) {
73         this.contents = CharOperation.NO_CHAR; // assume no source if asked again
74
throw new AbortCompilationUnit(null, e, this.encoding);
75     }
76 }
77 /**
78  * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
79  */

80 public char[] getFileName() {
81     return this.fileName;
82 }
83 public char[] getMainTypeName() {
84     return this.mainTypeName;
85 }
86 public char[][] getPackageName() {
87     return null;
88 }
89 public String JavaDoc toString() {
90     return "CompilationUnit[" + new String JavaDoc(this.fileName) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
91
}
92 }
93
Popular Tags