KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > CreateManifestOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.plugin;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
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 JavaDoc, InterruptedException JavaDoc {
46         try {
47             handleConvert();
48             trimOldManifest();
49         } catch (BadLocationException e) {
50             throw new InvocationTargetException JavaDoc(e);
51         } catch (CoreException e) {
52             throw new InvocationTargetException JavaDoc(e);
53         }
54     }
55
56     private void handleConvert() throws CoreException {
57         IProject project = fModel.getUnderlyingResource().getProject();
58         String JavaDoc 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 JavaDoc filename = fModel.isFragmentModel() ? "fragment.xml" : "plugin.xml"; //$NON-NLS-1$ //$NON-NLS-2$
65
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); //$NON-NLS-1$ //$NON-NLS-2$
73
if (edit != null)
74                 multiEdit.addChild(edit);
75             edit = removeElement("requires", adapter, doc, 0); //$NON-NLS-1$
76
if (edit != null)
77                 multiEdit.addChild(edit);
78             edit = removeElement("runtime", adapter, doc, 0); //$NON-NLS-1$
79
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 JavaDoc elementName, FindReplaceDocumentAdapter adapter, IDocument doc, int offset) throws BadLocationException {
92         IRegion region = adapter.find(0, "<" + elementName + "[^>]*", true, true, false, true); //$NON-NLS-1$ //$NON-NLS-2$
93
if (region != null) {
94             String JavaDoc replacementString = "<" + elementName; //$NON-NLS-1$
95
if (doc.getChar(region.getOffset() + region.getLength()) == '/')
96                 replacementString += "/"; //$NON-NLS-1$
97
return new ReplaceEdit(region.getOffset(), region.getLength(), replacementString);
98         }
99         return null;
100     }
101     
102     private TextEdit removeElement(String JavaDoc elementName, FindReplaceDocumentAdapter adapter, IDocument doc, int offset) throws BadLocationException {
103         IRegion region = adapter.find(0, "<" + elementName + "[^>]*", true, true, false, true); //$NON-NLS-1$ //$NON-NLS-2$
104
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); //$NON-NLS-1$ //$NON-NLS-2$
108
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