<s> package org . eclipse . jdt . core ; import java . io . File ; import java . io . IOException ; import java . io . InputStream ; import java . util . HashMap ; import java . util . Map ; import java . util . zip . ZipEntry ; import java . util . zip . ZipFile ; import org . eclipse . core . resources . IFile ; import org . eclipse . core . runtime . * ; import org . eclipse . jdt . core . compiler . IScanner ; import org . eclipse . jdt . core . compiler . ITerminalSymbols ; import org . eclipse . jdt . core . formatter . CodeFormatter ; import org . eclipse . jdt . core . formatter . DefaultCodeFormatterConstants ; import org . eclipse . jdt . core . util . ClassFileBytesDisassembler ; import org . eclipse . jdt . core . util . ClassFormatException ; import org . eclipse . jdt . core . util . IClassFileReader ; import org . eclipse . jdt . internal . compiler . classfmt . ClassFileConstants ; import org . eclipse . jdt . internal . compiler . impl . CompilerOptions ; import org . eclipse . jdt . internal . compiler . util . SuffixConstants ; import org . eclipse . jdt . internal . compiler . util . Util ; import org . eclipse . jdt . internal . core . JarPackageFragmentRoot ; import org . eclipse . jdt . internal . core . JavaElement ; import org . eclipse . jdt . internal . core . JavaModelManager ; import org . eclipse . jdt . internal . core . PackageFragment ; import org . eclipse . jdt . internal . core . util . ClassFileReader ; import org . eclipse . jdt . internal . core . util . Disassembler ; import org . eclipse . jdt . internal . core . util . PublicScanner ; import org . eclipse . jdt . internal . formatter . DefaultCodeFormatter ; public class ToolFactory { public static final int M_FORMAT_NEW = new Integer ( 0 ) . intValue ( ) ; public static final int M_FORMAT_EXISTING = new Integer ( 1 ) . intValue ( ) ; public static ICodeFormatter createCodeFormatter ( ) { Plugin jdtCorePlugin = JavaCore . getPlugin ( ) ; if ( jdtCorePlugin == null ) return null ; IExtensionPoint extension = jdtCorePlugin . getDescriptor ( ) . getExtensionPoint ( JavaModelManager . FORMATTER_EXTPOINT_ID ) ; if ( extension != null ) { IExtension [ ] extensions = extension . getExtensions ( ) ; for ( int i = 0 ; i < extensions . length ; i ++ ) { IConfigurationElement [ ] configElements = extensions [ i ] . getConfigurationElements ( ) ; for ( int j = 0 ; j < configElements . length ; j ++ ) { try { Object execExt = configElements [ j ] . createExecutableExtension ( "class" ) ; if ( execExt instanceof ICodeFormatter ) { return ( ICodeFormatter ) execExt ; } } catch ( CoreException e ) { } } } } return createDefaultCodeFormatter ( null ) ; } public static CodeFormatter createCodeFormatter ( Map options ) { return createCodeFormatter ( options , M_FORMAT_NEW ) ; } public static CodeFormatter createCodeFormatter ( Map options , int mode ) { if ( options == null ) options = JavaCore . getOptions ( ) ; Map currentOptions = new HashMap ( options ) ; if ( mode == M_FORMAT_NEW ) { currentOptions . put ( DefaultCodeFormatterConstants . FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN , DefaultCodeFormatterConstants . TRUE ) ; currentOptions . put ( DefaultCodeFormatterConstants . FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN , DefaultCodeFormatterConstants . FALSE ) ; currentOptions . put ( DefaultCodeFormatterConstants . FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN , DefaultCodeFormatterConstants . FALSE ) ; } return new DefaultCodeFormatter ( currentOptions ) ; } public static ClassFileBytesDisassembler createDefaultClassFileBytesDisassembler ( ) { return new Disassembler ( ) ; } public static org . eclipse . jdt . core . util . IClassFileDisassembler createDefaultClassFileDisassembler ( ) { class DeprecatedDisassembler extends Disassembler implements org . eclipse . jdt . core . util . IClassFileDisassembler { } return new DeprecatedDisassembler ( ) ; } public static IClassFileReader createDefaultClassFileReader ( IClassFile classfile , int decodingFlag ) { IPackageFragmentRoot root = ( IPackageFragmentRoot ) classfile . getAncestor ( IJavaElement . PACKAGE_FRAGMENT_ROOT ) ; if ( root != null ) { try { if ( root instanceof JarPackageFragmentRoot ) { String archiveName = null ; ZipFile jar = null ; try { jar = ( ( JarPackageFragmentRoot ) root ) . getJar ( ) ; archiveName = jar . getName ( ) ; } finally { JavaModelManager . getJavaModelManager ( ) . closeZipFile ( jar ) ; } PackageFragment packageFragment = ( PackageFragment ) classfile . getParent ( ) ; String classFileName = classfile . getElementName ( ) ; String entryName = org . eclipse . jdt . internal . core . util . Util . concatWith ( packageFragment . names , classFileName , '/' ) ; return createDefaultClassFileReader ( archiveName , entryName , decodingFlag ) ; } else { InputStream in = null ; try { in = ( ( IFile ) ( ( JavaElement ) classfile ) . resource ( ) ) . getContents ( ) ; return createDefaultClassFileReader ( in , decodingFlag ) ; } finally { if ( in != null ) try { in . close ( ) ; } catch ( IOException e ) { } } } } catch ( CoreException e ) { } } return null ; } public static IClassFileReader createDefaultClassFileReader ( InputStream stream , int decodingFlag ) { try { return new ClassFileReader ( Util . getInputStreamAsByteArray ( stream , - 1 ) , decodingFlag ) ; } catch ( ClassFormatException e ) { return null ; } catch ( IOException e ) { return null ; } } public static IClassFileReader createDefaultClassFileReader ( String fileName , int decodingFlag ) { try { return new ClassFileReader ( Util . getFileByteContent ( new File ( fileName ) ) , decodingFlag ) ; } catch ( ClassFormatException e ) { return null ; } catch ( IOException e ) { return null ; } } public static IClassFileReader createDefaultClassFileReader ( String zipFileName , String zipEntryName , int decodingFlag ) { ZipFile zipFile = null ; try { if ( JavaModelManager . ZIP_ACCESS_VERBOSE ) { System . out . println ( "(" + Thread . currentThread ( ) + "" + zipFileName ) ; } zipFile = new ZipFile ( zipFileName ) ; ZipEntry zipEntry = zipFile . getEntry ( zipEntryName ) ; if ( zipEntry == null ) { return null ; } if ( ! zipEntryName . toLowerCase ( ) . endsWith ( SuffixConstants . SUFFIX_STRING_class ) ) { return null ; } byte classFileBytes [ ] = Util . getZipEntryByteContent ( zipEntry , zipFile ) ; return new ClassFileReader ( classFileBytes , decodingFlag ) ; } catch ( ClassFormatException e ) { return null ; } catch ( IOException e ) { return null ; } finally { if ( zipFile != null ) { try { zipFile . close ( ) ; } catch ( IOException e ) { } } } } public static ICodeFormatter createDefaultCodeFormatter ( Map options ) { if ( options == null ) options = JavaCore . getOptions ( ) ; return new org . eclipse . jdt . internal . formatter . old . CodeFormatter ( options ) ; } public static IScanner createScanner ( boolean tokenizeComments , boolean tokenizeWhiteSpace , boolean assertMode , boolean recordLineSeparator ) { long complianceLevelValue = CompilerOptions . versionToJdkLevel ( JavaCore . getOption ( JavaCore . COMPILER_COMPLIANCE ) ) ; if ( complianceLevelValue == 0 ) complianceLevelValue = ClassFileConstants . JDK1_4 ; PublicScanner scanner = new PublicScanner ( tokenizeComments , tokenizeWhiteSpace , false , assertMode ? ClassFileConstants . JDK1_4 : ClassFileConstants . JDK1_3 , complianceLevelValue , null , null , true ) ; scanner . recordLineSeparator = recordLineSeparator ; return scanner ; } public static IScanner createScanner ( boolean tokenizeComments , boolean tokenizeWhiteSpace , boolean recordLineSeparator , String sourceLevel ) { long complianceLevelValue = CompilerOptions . versionToJdkLevel ( JavaCore . getOption ( JavaCore . COMPILER_COMPLIANCE ) ) ; if ( complianceLevelValue == 0 ) complianceLevelValue = ClassFileConstants . JDK1_4 ; long sourceLevelValue = CompilerOptions . versionToJdkLevel ( sourceLevel ) ; if ( sourceLevelValue == 0 ) sourceLevelValue = ClassFileConstants . JDK1_3 ; PublicScanner scanner = new PublicScanner ( tokenizeComments , tokenizeWhiteSpace , false , sourceLevelValue , complianceLevelValue , null , null , true ) ; scanner . recordLineSeparator = recordLineSeparator ; return scanner ; } public static IScanner createScanner ( boolean tokenizeComments , boolean tokenizeWhiteSpace , boolean recordLineSeparator , String sourceLevel , String complianceLevel ) { PublicScanner scanner = null ; long sourceLevelValue = CompilerOptions . versionToJdkLevel ( sourceLevel ) ; if ( sourceLevelValue == 0 ) sourceLevelValue = ClassFileConstants . JDK1_3 ; long complianceLevelValue = CompilerOptions . versionToJdkLevel ( complianceLevel ) ; if ( complianceLevelValue == 0 ) complianceLevelValue = ClassFileConstants . JDK1_4 ; scanner = new PublicScanner ( tokenizeComments , tokenizeWhiteSpace , false , sourceLevelValue , complianceLevelValue , null , null , true ) ; scanner . recordLineSeparator = recordLineSeparator ; return scanner ; } } </s>