1 11 package org.eclipse.pde.internal.ui.wizards.tools; 12 13 import java.io.BufferedReader ; 14 import java.io.FileReader ; 15 import java.io.FileWriter ; 16 import java.io.PrintWriter ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.util.ArrayList ; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IProjectDescription; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.jobs.Job; 27 import org.eclipse.jface.dialogs.IDialogSettings; 28 import org.eclipse.jface.operation.IRunnableWithProgress; 29 import org.eclipse.jface.text.BadLocationException; 30 import org.eclipse.jface.text.Document; 31 import org.eclipse.jface.text.FindReplaceDocumentAdapter; 32 import org.eclipse.jface.text.IDocument; 33 import org.eclipse.jface.text.IRegion; 34 import org.eclipse.jface.text.Region; 35 import org.eclipse.jface.wizard.Wizard; 36 import org.eclipse.pde.core.plugin.IPluginExtension; 37 import org.eclipse.pde.core.plugin.IPluginImport; 38 import org.eclipse.pde.core.plugin.IPluginModelBase; 39 import org.eclipse.pde.internal.ui.PDEPlugin; 40 import org.eclipse.pde.internal.ui.PDEPluginImages; 41 import org.eclipse.pde.internal.ui.PDEUIMessages; 42 43 public class MigratePluginWizard extends Wizard { 44 private MigratePluginWizardPage page1; 45 46 private IPluginModelBase[] fSelected; 47 private IPluginModelBase[] fUnmigrated; 48 49 private static final String STORE_SECTION = "MigrationWizard"; 51 public MigratePluginWizard(IPluginModelBase[] models,IPluginModelBase[] selected) { 52 IDialogSettings masterSettings = PDEPlugin.getDefault() 53 .getDialogSettings(); 54 setDialogSettings(getSettingsSection(masterSettings)); 55 setDefaultPageImageDescriptor(PDEPluginImages.DESC_MIGRATE_30_WIZ); 56 setWindowTitle(PDEUIMessages.MigrationWizard_title); 57 setNeedsProgressMonitor(true); 58 this.fSelected = selected; 59 this.fUnmigrated = models; 60 } 61 62 public boolean performFinish() { 63 final IPluginModelBase[] models = page1.getSelected(); 64 page1.storeSettings(); 65 final boolean doUpdateClasspath = page1.isUpdateClasspathRequested(); 66 final boolean doCleanProjects = page1.isCleanProjectsRequested(); 67 68 IRunnableWithProgress operation = new IRunnableWithProgress() { 69 public void run(IProgressMonitor monitor) 70 throws InvocationTargetException , InterruptedException { 71 72 if (PDEPlugin.getWorkspace().validateEdit( 73 getFilesToValidate(models), getContainer().getShell()) 74 .getSeverity() != IStatus.OK) { 75 monitor.done(); 76 return; 77 } 78 79 int numUnits = doUpdateClasspath ? models.length * 2 80 : models.length; 81 monitor 82 .beginTask( 83 PDEUIMessages.MigrationWizard_progress, numUnits); 84 try { 85 for (int i = 0; i < models.length; i++) { 86 monitor.subTask(models[i].getPluginBase().getId()); 87 transform(models[i]); 88 models[i].getUnderlyingResource().refreshLocal( 89 IResource.DEPTH_ZERO, null); 90 monitor.worked(1); 91 if (doCleanProjects) { 92 IProject project = models[i] 93 .getUnderlyingResource().getProject(); 94 IProjectDescription desc = project.getDescription(); 95 desc.setReferencedProjects(new IProject[0]); 96 project.setDescription(desc, null); 97 } 98 monitor.worked(1); 99 } 100 if (doUpdateClasspath) { 101 Job j = new UpdateClasspathJob(models); 102 j.setUser(true); 103 j.schedule(); 104 } 105 } catch (Exception e) { 106 PDEPlugin.logException(e); 107 } finally { 108 monitor.done(); 109 } 110 } 111 }; 112 113 try { 114 getContainer().run(true, false, operation); 115 } catch (InvocationTargetException e) { 116 } catch (InterruptedException e) { 117 } 118 return true; 119 } 120 121 private IFile[] getFilesToValidate(IPluginModelBase[] models) { 122 ArrayList files = new ArrayList (); 123 for (int i = 0; i < models.length; i++) { 124 IProject project = models[i].getUnderlyingResource().getProject(); 125 files.add(models[i].getUnderlyingResource()); 126 files.add(project.getFile(".project")); files.add(project.getFile(".classpath")); } 129 return (IFile[]) files.toArray(new IFile[files.size()]); 130 } 131 132 private IDialogSettings getSettingsSection(IDialogSettings master) { 133 IDialogSettings setting = master.getSection(STORE_SECTION); 134 if (setting == null) { 135 setting = master.addNewSection(STORE_SECTION); 136 } 137 return setting; 138 } 139 140 public void addPages() { 141 page1 = new MigratePluginWizardPage(fUnmigrated, fSelected); 142 addPage(page1); 143 } 144 145 private void transform(IPluginModelBase model) throws Exception { 146 IResource file = model.getUnderlyingResource(); 147 IDocument document = createDocument(file); 148 FindReplaceDocumentAdapter findAdapter = new FindReplaceDocumentAdapter( 149 document); 150 addEclipseProcessingInstruction(document, findAdapter); 151 updateExtensions(document, findAdapter); 152 if (model.getPluginBase().getImports().length > 0) 153 addNewImports(document, findAdapter, getAdditionalImports(model)); 154 writeFile(document, file); 155 } 156 157 private IDocument createDocument(IResource file) throws Exception { 158 BufferedReader reader = new BufferedReader (new FileReader (file 159 .getLocation().toOSString())); 160 StringBuffer buffer = new StringBuffer (); 161 while (reader.ready()) { 162 buffer.append((char) reader.read()); 163 } 164 reader.close(); 165 return new Document(buffer.toString()); 166 } 167 168 private void writeFile(IDocument document, IResource file) throws Exception { 169 PrintWriter writer = new PrintWriter (new FileWriter (file.getLocation() 170 .toOSString())); 171 writer.write(document.get()); 172 writer.close(); 173 } 174 175 private void addEclipseProcessingInstruction(IDocument document, 176 FindReplaceDocumentAdapter adapter) { 177 try { 178 IRegion region = adapter.find(-1, 179 "<\\?xml.*\\?>", true, true, false, true); if (region != null) { 181 String text = document.get(region.getOffset(), region 182 .getLength()); 183 adapter.replace(text + System.getProperty("line.separator") + "<?eclipse version=\"3.0\"?>", false); 186 } 187 } catch (BadLocationException e) { 188 } 189 } 190 191 private void updateExtensions(IDocument document, 192 FindReplaceDocumentAdapter adapter) { 193 int start = 0; 194 for (;;) { 195 try { 196 IRegion region = findNextExtension(adapter, start); 197 if (region == null) 198 break; 199 IRegion idRegion = findPointAttributeRegion(adapter, region); 200 if (idRegion != null) { 201 String point = document.get(idRegion.getOffset(), idRegion 202 .getLength()); 203 if (ExtensionPointMappings.isDeprecated(point.trim())) { 204 adapter.replace(ExtensionPointMappings.getNewId(point 205 .trim()), false); 206 } 207 } 208 start = region.getOffset() + region.getLength(); 209 } catch (BadLocationException e) { 210 } 211 } 212 } 213 214 private IRegion findPointAttributeRegion( 215 FindReplaceDocumentAdapter adapter, IRegion parentRegion) { 216 try { 217 IRegion region = adapter.find(parentRegion.getOffset(), 218 "\\s+point\\s*=\\s*\"", true, true, false, true); 220 if (region != null 221 && region.getOffset() + region.getLength() <= parentRegion 222 .getOffset() 223 + parentRegion.getLength()) { 224 region = adapter.find(region.getOffset() + region.getLength(), 225 "[^\"]*", true, true, false, true); 227 if (region != null 228 && region.getOffset() + region.getLength() < parentRegion 229 .getOffset() 230 + parentRegion.getLength()) { 231 return region; 232 } 233 } 234 } catch (BadLocationException e) { 235 } 236 return null; 237 } 238 239 private IRegion findNextExtension(FindReplaceDocumentAdapter adapter, 240 int start) { 241 int offset = -1; 242 int length = -1; 243 try { 244 IRegion region = adapter.find(start, 245 "<extension\\s+", true, true, false, true); if (region != null) { 247 offset = region.getOffset(); 248 region = adapter.find(offset, ">", true, true, false, false); if (region != null) { 250 length = region.getOffset() - offset + 1; 251 } 252 } 253 } catch (BadLocationException e) { 254 } 255 return (offset != -1 && length != -1) ? new Region(offset, length) 256 : null; 257 } 258 259 private String [] getAdditionalImports(IPluginModelBase model) { 260 ArrayList result = new ArrayList (); 261 result 264 .add("<import plugin=\"org.eclipse.core.runtime.compatibility\"/>"); IPluginImport uiImport = findImport(model, "org.eclipse.ui"); if (uiImport != null) { 267 ArrayList list = new ArrayList (); 268 list.add("org.eclipse.ui.ide"); list.add("org.eclipse.ui.views"); list.add("org.eclipse.jface.text"); list.add("org.eclipse.ui.workbench.texteditor"); list.add("org.eclipse.ui.editors"); IPluginImport[] imports = model.getPluginBase().getImports(); 274 for (int i = 0; i < imports.length; i++) { 275 if (list.contains(imports[i].getId())) { 276 list.remove(imports[i].getId()); 277 } 278 } 279 for (int i = 0; i < list.size(); i++) { 280 StringBuffer buffer = new StringBuffer ("<import plugin=\""); buffer.append(list.get(i) + "\""); if (uiImport.isReexported()) 283 buffer.append(" export=\"true\""); if (uiImport.isOptional()) 285 buffer.append(" optional=\"true\""); buffer.append("/>"); result.add(buffer.toString()); 288 } 289 } else if (needsAdditionalUIImport(model)) { 290 result.add("<import plugin=\"org.eclipse.ui\"/>"); } 292 if (needsHelpBaseImport(model)) 293 result.add("<import plugin=\"org.eclipse.help.base\"/>"); 295 return (String []) result.toArray(new String [result.size()]); 296 } 297 298 private void addNewImports(IDocument document, 299 FindReplaceDocumentAdapter adapter, String [] imports) { 300 try { 301 if (imports.length == 0) 302 return; 303 304 String space = ""; IRegion requiresRegion = adapter.find(0, 306 "<requires>", true, false, false, false); if (requiresRegion != null) { 308 IRegion spacerRegion = adapter.find(requiresRegion.getOffset() 309 + requiresRegion.getLength(), 310 "\\s*", true, true, false, true); if (spacerRegion != null) { 312 space = document.get(spacerRegion.getOffset(), spacerRegion 313 .getLength()); 314 } 315 } 316 StringBuffer buffer = new StringBuffer (space); 317 for (int i = 0; i < imports.length; i++) { 318 buffer.append(imports[i] + space); 319 } 320 adapter.replace(buffer.toString(), false); 321 } catch (BadLocationException e) { 322 } 323 } 324 325 private boolean needsAdditionalUIImport(IPluginModelBase model) { 326 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 327 for (int i = 0; i < extensions.length; i++) { 328 if (ExtensionPointMappings.hasMovedFromHelpToUI(extensions[i] 329 .getPoint()) 330 && findImport(model, "org.eclipse.ui") == null) return true; 332 } 333 return false; 334 } 335 336 private boolean needsHelpBaseImport(IPluginModelBase model) { 337 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 338 for (int i = 0; i < extensions.length; i++) { 339 if (ExtensionPointMappings.hasMovedFromHelpToBase(extensions[i] 340 .getPoint()) 341 && findImport(model, "org.eclipse.help.base") == null) { return true; 343 } 344 } 345 return false; 346 } 347 348 private IPluginImport findImport(IPluginModelBase model, String importID) { 349 IPluginImport[] imports = model.getPluginBase().getImports(); 350 for (int i = 0; i < imports.length; i++) { 351 if (imports[i].getId().equals(importID)) { 352 return imports[i]; 353 } 354 } 355 return null; 356 } 357 358 } 359 | Popular Tags |