1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.filebuffers.FileBuffers; 16 import org.eclipse.core.filebuffers.ITextFileBuffer; 17 import org.eclipse.core.filebuffers.ITextFileBufferManager; 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.NullProgressMonitor; 23 import org.eclipse.jface.operation.IRunnableWithProgress; 24 import org.eclipse.jface.text.BadLocationException; 25 import org.eclipse.jface.text.FindReplaceDocumentAdapter; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.jface.text.IRegion; 28 import org.eclipse.pde.core.plugin.IPluginModelBase; 29 import org.eclipse.pde.internal.core.ClasspathHelper; 30 import org.eclipse.pde.internal.core.TargetPlatformHelper; 31 import org.eclipse.pde.internal.core.converter.PDEPluginConverter; 32 import org.eclipse.text.edits.DeleteEdit; 33 import org.eclipse.text.edits.MultiTextEdit; 34 import org.eclipse.text.edits.ReplaceEdit; 35 import org.eclipse.text.edits.TextEdit; 36 37 public class CreateManifestOperation implements IRunnableWithProgress{ 38 39 private IPluginModelBase fModel; 40 41 public CreateManifestOperation(IPluginModelBase model) { 42 fModel = model; 43 } 44 45 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 46 try { 47 handleConvert(); 48 trimOldManifest(); 49 } catch (BadLocationException e) { 50 throw new InvocationTargetException (e); 51 } catch (CoreException e) { 52 throw new InvocationTargetException (e); 53 } 54 } 55 56 private void handleConvert() throws CoreException { 57 IProject project = fModel.getUnderlyingResource().getProject(); 58 String target = TargetPlatformHelper.getTargetVersionString(); 59 PDEPluginConverter.convertToOSGIFormat(project, target, ClasspathHelper.getDevDictionary(fModel), new NullProgressMonitor()); 60 } 61 62 private void trimOldManifest() throws BadLocationException, CoreException { 63 ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); 64 String filename = fModel.isFragmentModel() ? "fragment.xml" : "plugin.xml"; IFile file = fModel.getUnderlyingResource().getProject().getFile(filename); 66 try { 67 manager.connect(file.getFullPath(), null); 68 ITextFileBuffer buffer = manager.getTextFileBuffer(file.getFullPath()); 69 IDocument doc = buffer.getDocument(); 70 FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(doc); 71 MultiTextEdit multiEdit = new MultiTextEdit(); 72 TextEdit edit = editRootElement(fModel.isFragmentModel() ? "fragment" : "plugin", adapter, doc, 0); if (edit != null) 74 multiEdit.addChild(edit); 75 edit = removeElement("requires", adapter, doc, 0); if (edit != null) 77 multiEdit.addChild(edit); 78 edit = removeElement("runtime", adapter, doc, 0); if (edit != null) 80 multiEdit.addChild(edit); 81 82 if (multiEdit.hasChildren()) { 83 multiEdit.apply(doc); 84 buffer.commit(null, true); 85 } 86 } finally { 87 manager.disconnect(file.getFullPath(), null); 88 } 89 } 90 91 private TextEdit editRootElement(String elementName, FindReplaceDocumentAdapter adapter, IDocument doc, int offset) throws BadLocationException { 92 IRegion region = adapter.find(0, "<" + elementName + "[^>]*", true, true, false, true); if (region != null) { 94 String replacementString = "<" + elementName; if (doc.getChar(region.getOffset() + region.getLength()) == '/') 96 replacementString += "/"; return new ReplaceEdit(region.getOffset(), region.getLength(), replacementString); 98 } 99 return null; 100 } 101 102 private TextEdit removeElement(String elementName, FindReplaceDocumentAdapter adapter, IDocument doc, int offset) throws BadLocationException { 103 IRegion region = adapter.find(0, "<" + elementName + "[^>]*", true, true, false, true); if (region != null) { 105 if (doc.getChar(region.getOffset() + region.getLength()) == '/') 106 return new DeleteEdit(region.getOffset(), region.getLength() + 1); 107 IRegion endRegion = adapter.find(0, "</" + elementName +">", true, true, false, true); if (endRegion != null) { 109 int lastPos = endRegion.getOffset() + endRegion.getLength() + 1; 110 while (Character.isWhitespace(doc.getChar(lastPos))) { 111 lastPos += 1; 112 } 113 lastPos -= 1; 114 return new DeleteEdit(region.getOffset(), lastPos - region.getOffset()); 115 } 116 } 117 return null; 118 } 119 120 } 121 | Popular Tags |