1 11 package org.eclipse.jdt.core; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.zip.ZipEntry ; 19 import java.util.zip.ZipFile ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.jdt.core.compiler.IScanner; 24 import org.eclipse.jdt.core.formatter.CodeFormatter; 25 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 26 import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; 27 import org.eclipse.jdt.core.util.ClassFormatException; 28 import org.eclipse.jdt.core.util.IClassFileReader; 29 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 30 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 31 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 32 import org.eclipse.jdt.internal.compiler.util.Util; 33 import org.eclipse.jdt.internal.core.JarPackageFragmentRoot; 34 import org.eclipse.jdt.internal.core.JavaModelManager; 35 import org.eclipse.jdt.internal.core.PackageFragment; 36 import org.eclipse.jdt.internal.core.util.ClassFileReader; 37 import org.eclipse.jdt.internal.core.util.Disassembler; 38 import org.eclipse.jdt.internal.core.util.PublicScanner; 39 import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; 40 41 49 public class ToolFactory { 50 51 63 public static final int M_FORMAT_NEW = new Integer (0).intValue(); 64 65 76 public static final int M_FORMAT_EXISTING = new Integer (1).intValue(); 77 78 88 public static ICodeFormatter createCodeFormatter(){ 89 90 Plugin jdtCorePlugin = JavaCore.getPlugin(); 91 if (jdtCorePlugin == null) return null; 92 93 IExtensionPoint extension = jdtCorePlugin.getDescriptor().getExtensionPoint(JavaModelManager.FORMATTER_EXTPOINT_ID); 94 if (extension != null) { 95 IExtension[] extensions = extension.getExtensions(); 96 for(int i = 0; i < extensions.length; i++){ 97 IConfigurationElement [] configElements = extensions[i].getConfigurationElements(); 98 for(int j = 0; j < configElements.length; j++){ 99 try { 100 Object execExt = configElements[j].createExecutableExtension("class"); if (execExt instanceof ICodeFormatter){ 102 return (ICodeFormatter)execExt; 104 } 105 } catch(CoreException e){ 106 } 108 } 109 } 110 } 111 return createDefaultCodeFormatter(null); 113 } 114 115 133 public static CodeFormatter createCodeFormatter(Map options){ 134 return createCodeFormatter(options, M_FORMAT_NEW); 135 } 136 137 158 public static CodeFormatter createCodeFormatter(Map options, int mode) { 159 if (options == null) options = JavaCore.getOptions(); 160 Map currentOptions = new HashMap (options); 161 if (mode == M_FORMAT_NEW) { 162 currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); 164 currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); 165 } 166 return new DefaultCodeFormatter(currentOptions); 167 } 168 169 176 public static ClassFileBytesDisassembler createDefaultClassFileBytesDisassembler(){ 177 return new Disassembler(); 178 } 179 180 187 public static org.eclipse.jdt.core.util.IClassFileDisassembler createDefaultClassFileDisassembler(){ 188 class DeprecatedDisassembler extends Disassembler implements org.eclipse.jdt.core.util.IClassFileDisassembler { 189 } 191 return new DeprecatedDisassembler(); 192 } 193 194 208 public static IClassFileReader createDefaultClassFileReader(IClassFile classfile, int decodingFlag){ 209 210 IPackageFragmentRoot root = (IPackageFragmentRoot) classfile.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 211 if (root != null){ 212 try { 213 if (root instanceof JarPackageFragmentRoot) { 214 String archiveName = null; 215 ZipFile jar = null; 216 try { 217 jar = ((JarPackageFragmentRoot)root).getJar(); 218 archiveName = jar.getName(); 219 } finally { 220 JavaModelManager.getJavaModelManager().closeZipFile(jar); 221 } 222 PackageFragment packageFragment = (PackageFragment) classfile.getParent(); 223 String classFileName = classfile.getElementName(); 224 String entryName = org.eclipse.jdt.internal.core.util.Util.concatWith(packageFragment.names, classFileName, '/'); 225 return createDefaultClassFileReader(archiveName, entryName, decodingFlag); 226 } else { 227 InputStream in = null; 228 try { 229 in = ((IFile) classfile.getResource()).getContents(); 230 return createDefaultClassFileReader(in, decodingFlag); 231 } finally { 232 if (in != null) 233 try { 234 in.close(); 235 } catch (IOException e) { 236 } 238 } 239 } 240 } catch(CoreException e){ 241 } 243 } 244 return null; 245 } 246 247 261 public static IClassFileReader createDefaultClassFileReader(InputStream stream, int decodingFlag) { 262 try { 263 return new ClassFileReader(Util.getInputStreamAsByteArray(stream, -1), decodingFlag); 264 } catch(ClassFormatException e) { 265 return null; 266 } catch(IOException e) { 267 return null; 268 } 269 } 270 271 285 public static IClassFileReader createDefaultClassFileReader(String fileName, int decodingFlag){ 286 try { 287 return new ClassFileReader(Util.getFileByteContent(new File (fileName)), decodingFlag); 288 } catch(ClassFormatException e) { 289 return null; 290 } catch(IOException e) { 291 return null; 292 } 293 } 294 295 310 public static IClassFileReader createDefaultClassFileReader(String zipFileName, String zipEntryName, int decodingFlag){ 311 ZipFile zipFile = null; 312 try { 313 if (JavaModelManager.ZIP_ACCESS_VERBOSE) { 314 System.out.println("(" + Thread.currentThread() + ") [ToolFactory.createDefaultClassFileReader()] Creating ZipFile on " + zipFileName); } 316 zipFile = new ZipFile (zipFileName); 317 ZipEntry zipEntry = zipFile.getEntry(zipEntryName); 318 if (zipEntry == null) { 319 return null; 320 } 321 if (!zipEntryName.toLowerCase().endsWith(SuffixConstants.SUFFIX_STRING_class)) { 322 return null; 323 } 324 byte classFileBytes[] = Util.getZipEntryByteContent(zipEntry, zipFile); 325 return new ClassFileReader(classFileBytes, decodingFlag); 326 } catch(ClassFormatException e) { 327 return null; 328 } catch(IOException e) { 329 return null; 330 } finally { 331 if (zipFile != null) { 332 try { 333 zipFile.close(); 334 } catch(IOException e) { 335 } 337 } 338 } 339 } 340 341 355 public static ICodeFormatter createDefaultCodeFormatter(Map options){ 356 if (options == null) options = JavaCore.getOptions(); 357 return new org.eclipse.jdt.internal.formatter.old.CodeFormatter(options); 358 } 359 360 394 public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean assertMode, boolean recordLineSeparator){ 395 396 PublicScanner scanner = new PublicScanner(tokenizeComments, tokenizeWhiteSpace, false, assertMode ? ClassFileConstants.JDK1_4 : ClassFileConstants.JDK1_3, null, null, true); 397 scanner.recordLineSeparator = recordLineSeparator; 398 return scanner; 399 } 400 401 436 public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator, String sourceLevel) { 437 PublicScanner scanner = null; 438 long level = CompilerOptions.versionToJdkLevel(sourceLevel); 439 if (level == 0) level = ClassFileConstants.JDK1_3; scanner = new PublicScanner(tokenizeComments, tokenizeWhiteSpace, false,level , null, null, true); 441 scanner.recordLineSeparator = recordLineSeparator; 442 return scanner; 443 } 444 445 483 public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator, String sourceLevel, String complianceLevel) { 484 PublicScanner scanner = null; 485 long sourceLevelValue = CompilerOptions.versionToJdkLevel(sourceLevel); 486 if (sourceLevelValue == 0) sourceLevelValue = ClassFileConstants.JDK1_3; long complianceLevelValue = CompilerOptions.versionToJdkLevel(complianceLevel); 488 if (complianceLevelValue == 0) complianceLevelValue = ClassFileConstants.JDK1_3; scanner = new PublicScanner(tokenizeComments, tokenizeWhiteSpace, false,sourceLevelValue , complianceLevelValue, null, null, true); 490 scanner.recordLineSeparator = recordLineSeparator; 491 return scanner; 492 } 493 } 494 | Popular Tags |