<s> package org . eclipse . jdt . internal . core ; import java . io . ByteArrayInputStream ; import java . io . IOException ; import java . io . InputStream ; import org . eclipse . core . resources . * ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IStatus ; import org . eclipse . core . runtime . NullProgressMonitor ; import org . eclipse . core . runtime . Path ; import org . eclipse . core . runtime . jobs . ISchedulingRule ; import org . eclipse . jdt . core . IBuffer ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IJavaElementDelta ; import org . eclipse . jdt . core . IJavaModelStatus ; import org . eclipse . jdt . core . IJavaModelStatusConstants ; import org . eclipse . jdt . core . IJavaProject ; import org . eclipse . jdt . core . IPackageFragment ; import org . eclipse . jdt . core . JavaConventions ; import org . eclipse . jdt . core . JavaCore ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . jdt . internal . core . util . Util ; public class CreateCompilationUnitOperation extends JavaModelOperation { protected String name ; protected String source = null ; public CreateCompilationUnitOperation ( IPackageFragment parentElement , String name , String source , boolean force ) { super ( null , new IJavaElement [ ] { parentElement } , force ) ; this . name = name ; this . source = source ; } protected void executeOperation ( ) throws JavaModelException { try { beginTask ( Messages . operation_createUnitProgress , 2 ) ; JavaElementDelta delta = newJavaElementDelta ( ) ; ICompilationUnit unit = getCompilationUnit ( ) ; IPackageFragment pkg = ( IPackageFragment ) getParentElement ( ) ; IContainer folder = ( IContainer ) pkg . getResource ( ) ; worked ( 1 ) ; IFile compilationUnitFile = folder . getFile ( new Path ( this . name ) ) ; if ( compilationUnitFile . exists ( ) ) { if ( this . force ) { IBuffer buffer = unit . getBuffer ( ) ; if ( buffer == null ) return ; buffer . setContents ( this . source ) ; unit . save ( new NullProgressMonitor ( ) , false ) ; this . resultElements = new IJavaElement [ ] { unit } ; if ( ! Util . isExcluded ( unit ) && unit . getParent ( ) . exists ( ) ) { for ( int i = 0 ; i < this . resultElements . length ; i ++ ) { delta . changed ( this . resultElements [ i ] , IJavaElementDelta . F_CONTENT ) ; } addDelta ( delta ) ; } } else { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . NAME_COLLISION , Messages . bind ( Messages . status_nameCollision , compilationUnitFile . getFullPath ( ) . toString ( ) ) ) ) ; } } else { try { String encoding = null ; try { encoding = folder . getDefaultCharset ( ) ; } catch ( CoreException ce ) { } InputStream stream = new ByteArrayInputStream ( encoding == null ? this . source . getBytes ( ) : this . source . getBytes ( encoding ) ) ; createFile ( folder , unit . getElementName ( ) , stream , this . force ) ; this . resultElements = new IJavaElement [ ] { unit } ; if ( ! Util . isExcluded ( unit ) && unit . getParent ( ) . exists ( ) ) { for ( int i = 0 ; i < this . resultElements . length ; i ++ ) { delta . added ( this . resultElements [ i ] ) ; } addDelta ( delta ) ; } } catch ( IOException e ) { throw new JavaModelException ( e , IJavaModelStatusConstants . IO_EXCEPTION ) ; } } worked ( 1 ) ; } finally { done ( ) ; } } protected ICompilationUnit getCompilationUnit ( ) { return ( ( IPackageFragment ) getParentElement ( ) ) . getCompilationUnit ( this . name ) ; } protected ISchedulingRule getSchedulingRule ( ) { IResource resource = getCompilationUnit ( ) . getResource ( ) ; IWorkspace workspace = resource . getWorkspace ( ) ; if ( resource . exists ( ) ) { return workspace . getRuleFactory ( ) . modifyRule ( resource ) ; } else { return workspace . getRuleFactory ( ) . createRule ( resource ) ; } } public IJavaModelStatus verify ( ) { if ( getParentElement ( ) == null ) { return new JavaModelStatus ( IJavaModelStatusConstants . NO_ELEMENTS_TO_PROCESS ) ; } IJavaProject project = getParentElement ( ) . getJavaProject ( ) ; if ( JavaConventions . validateCompilationUnitName ( this . name , project . getOption ( JavaCore . COMPILER_SOURCE , true ) , project . getOption ( JavaCore . COMPILER_COMPLIANCE , true ) ) . getSeverity ( ) == IStatus . ERROR ) { return new JavaModelStatus ( IJavaModelStatusConstants . INVALID_NAME , this . name ) ; } if ( this . source == null ) { return new JavaModelStatus ( IJavaModelStatusConstants . INVALID_CONTENTS ) ; } return JavaModelStatus . VERIFIED_OK ; } } </s>