KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > refactoring > BundleManifestChange


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.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.filebuffers.FileBuffers;
16 import org.eclipse.core.filebuffers.ITextFileBufferManager;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IPackageFragment;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.ltk.core.refactoring.Change;
26 import org.eclipse.ltk.core.refactoring.TextFileChange;
27 import org.eclipse.osgi.util.ManifestElement;
28 import org.eclipse.pde.internal.core.ICoreConstants;
29 import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
30 import org.eclipse.pde.internal.core.ibundle.IBundle;
31 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
32 import org.eclipse.pde.internal.core.text.bundle.BasePackageHeader;
33 import org.eclipse.pde.internal.core.text.bundle.Bundle;
34 import org.eclipse.pde.internal.core.text.bundle.BundleModel;
35 import org.eclipse.pde.internal.core.text.bundle.BundleTextChangeListener;
36 import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader;
37 import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
38 import org.eclipse.pde.internal.core.text.bundle.PDEManifestElement;
39 import org.eclipse.pde.internal.core.text.bundle.PackageObject;
40 import org.eclipse.text.edits.MalformedTreeException;
41 import org.eclipse.text.edits.MultiTextEdit;
42 import org.eclipse.text.edits.TextEdit;
43 import org.osgi.framework.Constants;
44
45 public class BundleManifestChange {
46     
47     public static Change createMoveToPackageChange(IFile file, MoveFromChange change, IProgressMonitor monitor) throws CoreException {
48         try {
49             Bundle bundle = getBundle(file, monitor);
50             if (bundle == null)
51                 return null;
52
53             BundleModel model = (BundleModel)bundle.getModel();
54             BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument());
55             bundle.getModel().addModelChangedListener(listener);
56             addPackage(bundle, change);
57             return createChange(listener, file);
58         } catch (CoreException e) {
59         } catch (MalformedTreeException e) {
60         } catch (BadLocationException e) {
61         } finally {
62             FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), monitor);
63         }
64         return null;
65     }
66     
67     public static MoveFromChange createMovePackageChange(IFile file, Object JavaDoc[] elements, IProgressMonitor monitor) throws CoreException {
68         try {
69             Bundle bundle = getBundle(file, monitor);
70             if (bundle == null)
71                 return null;
72             
73             BundleModel model = (BundleModel)bundle.getModel();
74             BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument());
75             bundle.getModel().addModelChangedListener(listener);
76             
77             ArrayList JavaDoc list = new ArrayList JavaDoc();
78             for (int i = 0; i < elements.length; i++) {
79                 if (elements[i] instanceof IJavaElement) {
80                     String JavaDoc packageName = ((IJavaElement)elements[i]).getElementName();
81                     PDEManifestElement export = removePackage(bundle.getManifestHeader(Constants.EXPORT_PACKAGE),
82                             packageName);
83                     if (export != null)
84                         list.add(export);
85                 }
86             }
87
88             TextEdit[] operations = listener.getTextOperations();
89             if (operations.length > 0) {
90                 MoveFromChange change = new MoveFromChange("", file); //$NON-NLS-1$
91
MultiTextEdit edit = new MultiTextEdit();
92                 edit.addChildren(operations);
93                 change.setEdit(edit);
94                 if (list.size() > 0)
95                     change.setMovedElements((PDEManifestElement[])list.toArray(new PDEManifestElement[list.size()]));
96                 return change;
97             }
98         } catch (CoreException e) {
99         } catch (MalformedTreeException e) {
100         } catch (BadLocationException e) {
101         } finally {
102             FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), monitor);
103         }
104         return null;
105     }
106
107     public static Change createRenameChange(IFile file, Object JavaDoc[] elements, String JavaDoc[] newTexts,
108             IProgressMonitor monitor) throws CoreException {
109         try {
110             Bundle bundle = getBundle(file, monitor);
111             if (bundle == null)
112                 return null;
113             
114             BundleModel model = (BundleModel)bundle.getModel();
115             BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument());
116             bundle.getModel().addModelChangedListener(listener);
117             for (int i = 0; i < elements.length; i++) {
118                 Object JavaDoc element = elements[i];
119                 String JavaDoc newText = newTexts[i];
120                 if (element instanceof IType) {
121                     String JavaDoc oldText = ((IType)element).getFullyQualifiedName('$');
122                     resetHeaderValue(bundle.getManifestHeader(Constants.BUNDLE_ACTIVATOR),
123                             false,
124                             oldText,
125                             newText);
126                     resetHeaderValue(bundle.getManifestHeader(ICoreConstants.PLUGIN_CLASS),
127                             false,
128                             oldText,
129                             newText);
130                 } else if (element instanceof IPackageFragment) {
131                     String JavaDoc oldText = ((IPackageFragment)element).getElementName();
132                     resetHeaderValue(bundle.getManifestHeader(Constants.BUNDLE_ACTIVATOR),
133                             true,
134                             oldText,
135                             newText);
136                     resetHeaderValue(bundle.getManifestHeader(ICoreConstants.PLUGIN_CLASS),
137                             true,
138                             oldText,
139                             newText);
140                     renamePackage(bundle.getManifestHeader(Constants.EXPORT_PACKAGE),
141                             oldText,
142                             newText);
143                     renamePackage(bundle.getManifestHeader(ICoreConstants.PROVIDE_PACKAGE),
144                             oldText,
145                             newText);
146                     renamePackage(bundle.getManifestHeader(Constants.IMPORT_PACKAGE),
147                             oldText,
148                             newText);
149                 }
150             }
151             return createChange(listener, file);
152         } catch (CoreException e) {
153         } catch (MalformedTreeException e) {
154         } catch (BadLocationException e) {
155         } finally {
156             FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), monitor);
157         }
158         return null;
159     }
160     
161     private static Change createChange(BundleTextChangeListener listener, IFile file) {
162         TextEdit[] operations = listener.getTextOperations();
163         if (operations.length > 0) {
164             TextFileChange change = new TextFileChange("", file); //$NON-NLS-1$
165
MultiTextEdit edit = new MultiTextEdit();
166             edit.addChildren(operations);
167             change.setEdit(edit);
168             return change;
169         }
170         return null;
171     }
172     
173     private static void resetHeaderValue(IManifestHeader header, boolean isPackage, String JavaDoc oldText, String JavaDoc newText) {
174         if (header != null) {
175             String JavaDoc value = header.getValue();
176             if (oldText.equals(value) || isGoodMatch(value, oldText, isPackage)) {
177                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(newText);
178                 buffer.append(value.substring(oldText.length()));
179                 header.setValue(buffer.toString());
180             }
181         }
182     }
183     
184     private static boolean isGoodMatch(String JavaDoc value, String JavaDoc oldName, boolean isPackage) {
185         if (value == null || value.length() <= oldName.length())
186             return false;
187         boolean goodLengthMatch = isPackage
188                                     ? value.lastIndexOf('.') <= oldName.length()
189                                     : value.charAt(oldName.length()) == '$';
190         return value.startsWith(oldName) && goodLengthMatch;
191     }
192     
193     private static void renamePackage(IManifestHeader header, String JavaDoc oldName, String JavaDoc newName) {
194         if (header instanceof BasePackageHeader) {
195             BasePackageHeader bHeader = (BasePackageHeader)header;
196             bHeader.renamePackage(oldName, newName);
197         }
198     }
199
200     private static PDEManifestElement removePackage(IManifestHeader header, String JavaDoc name) {
201         PDEManifestElement result = null;
202         if (header instanceof BasePackageHeader) {
203             BasePackageHeader bHeader = (BasePackageHeader)header;
204             result = ((PackageObject)bHeader.removePackage(name));
205         }
206         return result;
207     }
208
209     private static void addPackage(Bundle bundle, MoveFromChange change) {
210         String JavaDoc headerName = getExportedPackageHeader(bundle);
211         ExportPackageHeader header = (ExportPackageHeader)bundle.getManifestHeader(headerName);
212         ManifestElement[] elements = change.getMovedElements();
213         for (int i = 0; i < elements.length; i++) {
214             if (header != null) {
215                 if (!header.hasPackage(change.getPackageName(i))) {
216                     header.addPackage(new ExportPackageObject(header, elements[i], getVersionAttribute(header.getBundle())));
217                 }
218             } else {
219                 bundle.setHeader(headerName, change.getMovedText(i));
220                 header = (ExportPackageHeader)bundle.getManifestHeader(headerName);
221             }
222         }
223     }
224     
225     private static String JavaDoc getVersionAttribute(IBundle bundle) {
226         int manifestVersion = BundlePluginBase.getBundleManifestVersion(bundle);
227         return (manifestVersion < 2) ? ICoreConstants.PACKAGE_SPECIFICATION_VERSION : Constants.VERSION_ATTRIBUTE;
228     }
229     
230     private static String JavaDoc getExportedPackageHeader(IBundle bundle) {
231         int manifestVersion = BundlePluginBase.getBundleManifestVersion(bundle);
232         return (manifestVersion < 2) ? ICoreConstants.PROVIDE_PACKAGE : Constants.EXPORT_PACKAGE;
233     }
234
235     public static Bundle getBundle(IFile file, IProgressMonitor monitor) throws CoreException, MalformedTreeException, BadLocationException {
236         ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
237         manager.connect(file.getFullPath(), monitor);
238         
239         IDocument document = manager.getTextFileBuffer(file.getFullPath()).getDocument();
240         BundleModel model = new BundleModel(document, false);
241         model.load();
242         return model.isLoaded() ? (Bundle)model.getBundle() : null;
243     }
244     
245 }
246
Popular Tags