001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.filebuffers.FileBuffers;
015: import org.eclipse.core.filebuffers.ITextFileBuffer;
016: import org.eclipse.core.filebuffers.ITextFileBufferManager;
017: import org.eclipse.core.filebuffers.LocationKind;
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.resources.IProject;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IProgressMonitor;
022: import org.eclipse.core.runtime.NullProgressMonitor;
023: import org.eclipse.jface.operation.IRunnableWithProgress;
024: import org.eclipse.jface.text.BadLocationException;
025: import org.eclipse.jface.text.FindReplaceDocumentAdapter;
026: import org.eclipse.jface.text.IDocument;
027: import org.eclipse.jface.text.IRegion;
028: import org.eclipse.pde.core.plugin.IPluginModelBase;
029: import org.eclipse.pde.internal.core.ClasspathHelper;
030: import org.eclipse.pde.internal.core.TargetPlatformHelper;
031: import org.eclipse.pde.internal.core.converter.PDEPluginConverter;
032: import org.eclipse.text.edits.DeleteEdit;
033: import org.eclipse.text.edits.MultiTextEdit;
034: import org.eclipse.text.edits.ReplaceEdit;
035: import org.eclipse.text.edits.TextEdit;
036:
037: public class CreateManifestOperation implements IRunnableWithProgress {
038:
039: private IPluginModelBase fModel;
040:
041: public CreateManifestOperation(IPluginModelBase model) {
042: fModel = model;
043: }
044:
045: public void run(IProgressMonitor monitor)
046: throws InvocationTargetException, InterruptedException {
047: try {
048: handleConvert();
049: trimOldManifest();
050: } catch (BadLocationException e) {
051: throw new InvocationTargetException(e);
052: } catch (CoreException e) {
053: throw new InvocationTargetException(e);
054: }
055: }
056:
057: private void handleConvert() throws CoreException {
058: IProject project = fModel.getUnderlyingResource().getProject();
059: String target = TargetPlatformHelper.getTargetVersionString();
060: PDEPluginConverter.convertToOSGIFormat(project, target,
061: ClasspathHelper.getDevDictionary(fModel),
062: new NullProgressMonitor());
063: }
064:
065: private void trimOldManifest() throws BadLocationException,
066: CoreException {
067: ITextFileBufferManager manager = FileBuffers
068: .getTextFileBufferManager();
069: String filename = fModel.isFragmentModel() ? "fragment.xml" : "plugin.xml"; //$NON-NLS-1$ //$NON-NLS-2$
070: IFile file = fModel.getUnderlyingResource().getProject()
071: .getFile(filename);
072: try {
073: manager.connect(file.getFullPath(), LocationKind.NORMALIZE,
074: null);
075: ITextFileBuffer buffer = manager.getTextFileBuffer(file
076: .getFullPath(), LocationKind.NORMALIZE);
077: IDocument doc = buffer.getDocument();
078: FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(
079: doc);
080: MultiTextEdit multiEdit = new MultiTextEdit();
081: TextEdit edit = editRootElement(
082: fModel.isFragmentModel() ? "fragment" : "plugin", adapter, doc, 0); //$NON-NLS-1$ //$NON-NLS-2$
083: if (edit != null)
084: multiEdit.addChild(edit);
085: edit = removeElement("requires", adapter, doc, 0); //$NON-NLS-1$
086: if (edit != null)
087: multiEdit.addChild(edit);
088: edit = removeElement("runtime", adapter, doc, 0); //$NON-NLS-1$
089: if (edit != null)
090: multiEdit.addChild(edit);
091:
092: if (multiEdit.hasChildren()) {
093: multiEdit.apply(doc);
094: buffer.commit(null, true);
095: }
096: } finally {
097: manager.disconnect(file.getFullPath(),
098: LocationKind.NORMALIZE, null);
099: }
100: }
101:
102: private TextEdit editRootElement(String elementName,
103: FindReplaceDocumentAdapter adapter, IDocument doc,
104: int offset) throws BadLocationException {
105: IRegion region = adapter.find(0,
106: "<" + elementName + "[^>]*", true, true, false, true); //$NON-NLS-1$ //$NON-NLS-2$
107: if (region != null) {
108: String replacementString = "<" + elementName; //$NON-NLS-1$
109: if (doc.getChar(region.getOffset() + region.getLength()) == '/')
110: replacementString += "/"; //$NON-NLS-1$
111: return new ReplaceEdit(region.getOffset(), region
112: .getLength(), replacementString);
113: }
114: return null;
115: }
116:
117: private TextEdit removeElement(String elementName,
118: FindReplaceDocumentAdapter adapter, IDocument doc,
119: int offset) throws BadLocationException {
120: IRegion region = adapter.find(0,
121: "<" + elementName + "[^>]*", true, true, false, true); //$NON-NLS-1$ //$NON-NLS-2$
122: if (region != null) {
123: if (doc.getChar(region.getOffset() + region.getLength()) == '/')
124: return new DeleteEdit(region.getOffset(), region
125: .getLength() + 1);
126: IRegion endRegion = adapter.find(0,
127: "</" + elementName + ">", true, true, false, true); //$NON-NLS-1$ //$NON-NLS-2$
128: if (endRegion != null) {
129: int lastPos = endRegion.getOffset()
130: + endRegion.getLength() + 1;
131: while (Character.isWhitespace(doc.getChar(lastPos))) {
132: lastPos += 1;
133: }
134: lastPos -= 1;
135: return new DeleteEdit(region.getOffset(), lastPos
136: - region.getOffset());
137: }
138: }
139: return null;
140: }
141:
142: }
|