1 11 package org.eclipse.jdt.internal.corext.refactoring.binary; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.IOException ; 15 import java.io.OutputStream ; 16 import java.net.URI ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.core.filesystem.EFS; 21 import org.eclipse.core.filesystem.IFileStore; 22 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.NullProgressMonitor; 28 import org.eclipse.core.runtime.OperationCanceledException; 29 import org.eclipse.core.runtime.Path; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.core.runtime.SubProgressMonitor; 32 33 import org.eclipse.core.resources.IWorkspaceRunnable; 34 35 import org.eclipse.jdt.core.IClassFile; 36 import org.eclipse.jdt.core.IPackageFragment; 37 38 39 import org.eclipse.jdt.internal.ui.JavaPlugin; 40 41 46 public abstract class AbstractCodeCreationOperation implements IWorkspaceRunnable { 47 48 49 protected final URI fOutputURI; 50 51 52 protected final List fPackages; 53 54 62 protected AbstractCodeCreationOperation(final URI uri, final List packages) { 63 Assert.isNotNull(uri); 64 Assert.isNotNull(packages); 65 fOutputURI= uri; 66 fPackages= packages; 67 } 68 69 83 protected void createCompilationUnit(final IFileStore store, final String name, final String content, final IProgressMonitor monitor) throws CoreException { 84 OutputStream stream= null; 85 try { 86 stream= new BufferedOutputStream (store.getChild(name).openOutputStream(EFS.NONE, new SubProgressMonitor(monitor, 1))); 87 try { 88 stream.write(content.getBytes()); 89 } catch (IOException exception) { 90 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, exception.getLocalizedMessage(), exception)); 91 } 92 } finally { 93 if (stream != null) { 94 try { 95 stream.close(); 96 } catch (IOException exception) { 97 } 99 } 100 } 101 } 102 103 115 protected void createPackageFragment(final IFileStore store, final String name, final IProgressMonitor monitor) throws CoreException { 116 store.mkdir(EFS.NONE, monitor); 117 } 118 119 124 protected abstract String getOperationLabel(); 125 126 138 protected abstract void run(IClassFile file, IFileStore parent, IProgressMonitor monitor) throws CoreException; 139 140 143 public void run(IProgressMonitor monitor) throws CoreException { 144 if (monitor == null) 145 monitor= new NullProgressMonitor(); 146 monitor.beginTask(getOperationLabel(), 100 * fPackages.size()); 147 try { 148 final StringBuffer buffer= new StringBuffer (128); 149 for (final Iterator iterator= fPackages.iterator(); iterator.hasNext();) { 150 final IPackageFragment fragment= (IPackageFragment) iterator.next(); 151 final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 100); 152 final IClassFile[] files= fragment.getClassFiles(); 153 final int size= files.length; 154 subMonitor.beginTask(getOperationLabel(), size * 50); 155 final String name= fragment.getElementName(); 156 IFileStore store= EFS.getStore(fOutputURI); 157 if (!"".equals(name)) { final String pack= name; 159 buffer.setLength(0); 160 buffer.append(name); 161 final int length= buffer.length(); 162 for (int index= 0; index < length; index++) { 163 if (buffer.charAt(index) == '.') 164 buffer.setCharAt(index, '/'); 165 } 166 store= store.getChild(new Path(buffer.toString())); 167 if (!pack.startsWith(".")) createPackageFragment(store, pack, new SubProgressMonitor(subMonitor, 10)); 169 } else 170 createPackageFragment(store, "", new SubProgressMonitor(subMonitor, 10)); final IProgressMonitor subsubMonitor= new SubProgressMonitor(subMonitor, 30); 172 try { 173 subsubMonitor.beginTask(getOperationLabel(), size * 100); 174 for (int index= 0; index < size; index++) { 175 if (subMonitor.isCanceled()) 176 throw new OperationCanceledException(); 177 run(files[index], store, new SubProgressMonitor(subsubMonitor, 100)); 178 } 179 } finally { 180 subsubMonitor.done(); 181 } 182 } 183 } finally { 184 monitor.done(); 185 } 186 } 187 } 188 | Popular Tags |