1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IJavaProject; 21 import org.eclipse.jdt.core.compiler.CharOperation; 22 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 23 import org.eclipse.jdt.internal.compiler.util.Util; 24 25 30 public class BasicCompilationUnit implements ICompilationUnit { 31 protected char[] contents; 32 33 protected char[] fileName; 38 39 protected char[][] packageName; 40 protected char[] mainTypeName; 41 protected String encoding; 42 43 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName) { 44 this.contents = contents; 45 this.fileName = fileName.toCharArray(); 46 this.packageName = packageName; 47 } 48 49 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, String encoding) { 50 this(contents, packageName, fileName); 51 this.encoding = encoding; 52 } 53 54 public BasicCompilationUnit(char[] contents, char[][] packageName, String fileName, IJavaElement javaElement) { 55 this(contents, packageName, fileName); 56 initEncoding(javaElement); 57 } 58 59 67 private void initEncoding(IJavaElement javaElement) { 68 if (javaElement != null) { 69 try { 70 IJavaProject javaProject = javaElement.getJavaProject(); 71 switch (javaElement.getElementType()) { 72 case IJavaElement.COMPILATION_UNIT: 73 IFile file = (IFile) javaElement.getResource(); 74 if (file != null) { 75 this.encoding = file.getCharset(); 76 break; 77 } 78 default: 80 IProject project = (IProject) javaProject.getResource(); 81 if (project != null) { 82 this.encoding = project.getDefaultCharset(); 83 } 84 break; 85 } 86 } catch (CoreException e1) { 87 this.encoding = null; 88 } 89 } else { 90 this.encoding = null; 91 } 92 } 93 94 public char[] getContents() { 95 if (this.contents != null) 96 return this.contents; 98 try { 100 return Util.getFileCharContent(new File (new String (this.fileName)), this.encoding); 101 } catch (IOException e) { 102 } 104 return CharOperation.NO_CHAR; 105 } 106 109 public char[] getFileName() { 110 return this.fileName; 111 } 112 public char[] getMainTypeName() { 113 if (this.mainTypeName == null) { 114 int start = CharOperation.lastIndexOf('/', this.fileName) + 1; 115 if (start == 0 || start < CharOperation.lastIndexOf('\\', this.fileName)) 116 start = CharOperation.lastIndexOf('\\', this.fileName) + 1; 117 int separator = CharOperation.indexOf('|', this.fileName) + 1; 118 if (separator > start) start = separator; 120 121 int end = CharOperation.lastIndexOf('$', this.fileName); 122 if (end == -1 || !Util.isClassFileName(this.fileName)) { 123 end = CharOperation.lastIndexOf('.', this.fileName); 124 if (end == -1) 125 end = this.fileName.length; 126 } 127 128 this.mainTypeName = CharOperation.subarray(this.fileName, start, end); 129 } 130 return this.mainTypeName; 131 } 132 public char[][] getPackageName() { 133 return this.packageName; 134 } 135 public String toString(){ 136 return "CompilationUnit: "+new String (this.fileName); } 138 } 139 | Popular Tags |