1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 17 import org.eclipse.core.resources.*; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.jobs.ISchedulingRule; 23 import org.eclipse.jdt.core.IBuffer; 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IJavaElement; 26 import org.eclipse.jdt.core.IJavaElementDelta; 27 import org.eclipse.jdt.core.IJavaModelStatus; 28 import org.eclipse.jdt.core.IJavaModelStatusConstants; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.IPackageFragment; 31 import org.eclipse.jdt.core.JavaConventions; 32 import org.eclipse.jdt.core.JavaCore; 33 import org.eclipse.jdt.core.JavaModelException; 34 import org.eclipse.jdt.internal.core.util.Messages; 35 import org.eclipse.jdt.internal.core.util.Util; 36 37 55 public class CreateCompilationUnitOperation extends JavaModelOperation { 56 57 60 protected String fName; 61 64 protected String fSource= null; 65 69 public CreateCompilationUnitOperation(IPackageFragment parentElement, String name, String source, boolean force) { 70 super(null, new IJavaElement[] {parentElement}, force); 71 fName = name; 72 fSource = source; 73 } 74 79 protected void executeOperation() throws JavaModelException { 80 try { 81 beginTask(Messages.operation_createUnitProgress, 2); 82 JavaElementDelta delta = newJavaElementDelta(); 83 ICompilationUnit unit = getCompilationUnit(); 84 IPackageFragment pkg = (IPackageFragment) getParentElement(); 85 IContainer folder = (IContainer) pkg.getResource(); 86 worked(1); 87 IFile compilationUnitFile = folder.getFile(new Path(fName)); 88 if (compilationUnitFile.exists()) { 89 if (force) { 91 IBuffer buffer = unit.getBuffer(); 92 if (buffer == null) return; 93 buffer.setContents(fSource); 94 unit.save(new NullProgressMonitor(), false); 95 resultElements = new IJavaElement[] {unit}; 96 if (!Util.isExcluded(unit) 97 && unit.getParent().exists()) { 98 for (int i = 0; i < resultElements.length; i++) { 99 delta.changed(resultElements[i], IJavaElementDelta.F_CONTENT); 100 } 101 addDelta(delta); 102 } 103 } else { 104 throw new JavaModelException(new JavaModelStatus( 105 IJavaModelStatusConstants.NAME_COLLISION, 106 Messages.bind(Messages.status_nameCollision, compilationUnitFile.getFullPath().toString()))); 107 } 108 } else { 109 try { 110 String encoding = null; 111 try { 112 encoding = folder.getDefaultCharset(); } 114 catch (CoreException ce) { 115 } 117 InputStream stream = new ByteArrayInputStream (encoding == null ? fSource.getBytes() : fSource.getBytes(encoding)); 118 createFile(folder, unit.getElementName(), stream, force); 119 resultElements = new IJavaElement[] {unit}; 120 if (!Util.isExcluded(unit) 121 && unit.getParent().exists()) { 122 for (int i = 0; i < resultElements.length; i++) { 123 delta.added(resultElements[i]); 124 } 125 addDelta(delta); 126 } 127 } catch (IOException e) { 128 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 129 } 130 } 131 worked(1); 132 } finally { 133 done(); 134 } 135 } 136 139 protected ICompilationUnit getCompilationUnit() { 140 return ((IPackageFragment)getParentElement()).getCompilationUnit(fName); 141 } 142 protected ISchedulingRule getSchedulingRule() { 143 IResource resource = getCompilationUnit().getResource(); 144 IWorkspace workspace = resource.getWorkspace(); 145 if (resource.exists()) { 146 return workspace.getRuleFactory().modifyRule(resource); 147 } else { 148 return workspace.getRuleFactory().createRule(resource); 149 } 150 } 151 160 public IJavaModelStatus verify() { 161 if (getParentElement() == null) { 162 return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS); 163 } 164 IJavaProject project = getParentElement().getJavaProject(); 165 if (JavaConventions.validateCompilationUnitName(fName, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR) { 166 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName); 167 } 168 if (fSource == null) { 169 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS); 170 } 171 return JavaModelStatus.VERIFIED_OK; 172 } 173 } 174 | Popular Tags |