Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ae698b339edb1653953efe7782dddee074ecb04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2005, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Benjamin Cabe <benjamin.cabe@anyware-tech.com> - bug 219852, 250334
 *******************************************************************************/
package org.eclipse.pde.internal.ui.refactoring;

import org.eclipse.pde.internal.core.text.bundle.PDEManifestElement;

import java.util.ArrayList;
import org.eclipse.core.filebuffers.*;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.*;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
import org.eclipse.pde.internal.core.ibundle.IBundle;
import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
import org.eclipse.pde.internal.core.text.bundle.*;
import org.eclipse.pde.internal.ui.util.LocaleUtil;
import org.eclipse.pde.internal.ui.util.PDEModelUtility;
import org.eclipse.text.edits.*;
import org.osgi.framework.Constants;

public class BundleManifestChange {

	public static Change createMoveToPackageChange(IFile file, MoveFromChange change, IProgressMonitor monitor) throws CoreException {
		try {
			Bundle bundle = getBundle(file, monitor);
			if (bundle == null)
				return null;

			BundleModel model = (BundleModel) bundle.getModel();
			BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument());
			bundle.getModel().addModelChangedListener(listener);
			addPackage(bundle, change);
			return createChange(listener, file);
		} catch (CoreException e) {
		} catch (MalformedTreeException e) {
		} catch (BadLocationException e) {
		} finally {
			FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor);
		}
		return null;
	}

	public static MoveFromChange createMovePackageChange(IFile file, Object[] elements, IProgressMonitor monitor) throws CoreException {
		try {
			Bundle bundle = getBundle(file, monitor);
			if (bundle == null)
				return null;

			BundleModel model = (BundleModel) bundle.getModel();
			BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument());
			bundle.getModel().addModelChangedListener(listener);

			ArrayList<PDEManifestElement> list = new ArrayList<PDEManifestElement>();
			for (int i = 0; i < elements.length; i++) {
				if (elements[i] instanceof IJavaElement) {
					String packageName = ((IJavaElement) elements[i]).getElementName();
					PDEManifestElement export = removePackage(bundle.getManifestHeader(Constants.EXPORT_PACKAGE), packageName);
					if (export != null)
						list.add(export);
				}
			}

			TextEdit[] operations = listener.getTextOperations();
			if (operations.length > 0) {
				MoveFromChange change = new MoveFromChange("", file); //$NON-NLS-1$
				MultiTextEdit edit = new MultiTextEdit();
				edit.addChildren(operations);
				change.setEdit(edit);
				PDEModelUtility.setChangeTextType(change, file);
				if (list.size() > 0)
					change.setMovedElements(list.toArray(new PDEManifestElement[list.size()]));
				return change;
			}
		} catch (CoreException e) {
		} catch (MalformedTreeException e) {
		} catch (BadLocationException e) {
		} finally {
			FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor);
		}
		return null;
	}

	public static Change createRenameChange(IFile file, Object[] elements, String[] newTexts, IProgressMonitor monitor) throws CoreException {
		try {
			Bundle bundle = getBundle(file, monitor);
			if (bundle == null)
				return null;

			BundleModel model = (BundleModel) bundle.getModel();
			BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument());
			bundle.getModel().addModelChangedListener(listener);
			boolean localizationRenamed = false;
			for (int i = 0; i < elements.length; i++) {
				Object element = elements[i];
				String newText = newTexts[i];
				if (element instanceof IFile) {
					String fileName = ((IFile) element).getProjectRelativePath().toString();
					if (!localizationRenamed && fileName.endsWith(".properties")) { //$NON-NLS-1$
						String oldText = fileName.substring(0, fileName.lastIndexOf(".")); //$NON-NLS-1$
						String oldLocalization = bundle.getLocalization();
						if (LocaleUtil.trimLocalization(oldText).equals(oldLocalization)) {
							renameLocalization(bundle, oldText, newText);
							localizationRenamed = true;
						}
					}
				} else if (element instanceof IType) {
					String oldText = ((IType) element).getFullyQualifiedName('$');
					resetHeaderValue(bundle.getManifestHeader(Constants.BUNDLE_ACTIVATOR), false, oldText, newText);
					resetHeaderValue(bundle.getManifestHeader(ICoreConstants.PLUGIN_CLASS), false, oldText, newText);
				} else if (element instanceof IPackageFragment) {
					String oldText = ((IPackageFragment) element).getElementName();
					resetHeaderValue(bundle.getManifestHeader(Constants.BUNDLE_ACTIVATOR), true, oldText, newText);
					resetHeaderValue(bundle.getManifestHeader(ICoreConstants.PLUGIN_CLASS), true, oldText, newText);
					renamePackage(bundle.getManifestHeader(Constants.EXPORT_PACKAGE), oldText, newText);
					renamePackage(bundle.getManifestHeader(ICoreConstants.PROVIDE_PACKAGE), oldText, newText);
					renamePackage(bundle.getManifestHeader(Constants.IMPORT_PACKAGE), oldText, newText);
				}
			}
			return createChange(listener, file);
		} catch (CoreException e) {
		} catch (MalformedTreeException e) {
		} catch (BadLocationException e) {
		} finally {
			FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor);
		}
		return null;
	}

	private static Change createChange(BundleTextChangeListener listener, IFile file) {
		TextEdit[] operations = listener.getTextOperations();
		if (operations.length > 0) {
			TextFileChange change = new TextFileChange("", file); //$NON-NLS-1$
			MultiTextEdit edit = new MultiTextEdit();
			edit.addChildren(operations);
			change.setEdit(edit);
			PDEModelUtility.setChangeTextType(change, file);
			return change;
		}
		return null;
	}

	private static void renameLocalization(Bundle bundle, String oldText, String newText) {
		if (newText.endsWith(".properties")) //$NON-NLS-1$
			bundle.setHeader(Constants.BUNDLE_LOCALIZATION, LocaleUtil.trimLocalization(newText));
		else
			bundle.setHeader(Constants.BUNDLE_LOCALIZATION, null);
	}

	private static void resetHeaderValue(IManifestHeader header, boolean isPackage, String oldText, String newText) {
		if (header != null) {
			String value = header.getValue();
			if (oldText.equals(value) || isGoodMatch(value, oldText, isPackage)) {
				StringBuffer buffer = new StringBuffer(newText);
				buffer.append(value.substring(oldText.length()));
				header.setValue(buffer.toString());
			}
		}
	}

	private static boolean isGoodMatch(String value, String oldName, boolean isPackage) {
		if (value == null || value.length() <= oldName.length())
			return false;
		boolean goodLengthMatch = isPackage ? value.lastIndexOf('.') <= oldName.length() : value.charAt(oldName.length()) == '$';
		return value.startsWith(oldName) && goodLengthMatch;
	}

	private static void renamePackage(IManifestHeader header, String oldName, String newName) {
		if (header instanceof BasePackageHeader) {
			BasePackageHeader bHeader = (BasePackageHeader) header;
			bHeader.renamePackage(oldName, newName);
		}
	}

	private static PDEManifestElement removePackage(IManifestHeader header, String name) {
		PDEManifestElement result = null;
		if (header instanceof BasePackageHeader) {
			BasePackageHeader bHeader = (BasePackageHeader) header;
			result = ((PackageObject) bHeader.removePackage(name));
		}
		return result;
	}

	private static void addPackage(Bundle bundle, MoveFromChange change) {
		String headerName = getExportedPackageHeader(bundle);
		ExportPackageHeader header = (ExportPackageHeader) bundle.getManifestHeader(headerName);
		ManifestElement[] elements = change.getMovedElements();
		for (int i = 0; i < elements.length; i++) {
			if (header != null) {
				if (!header.hasPackage(change.getPackageName(i))) {
					header.addPackage(new ExportPackageObject(header, elements[i], getVersionAttribute(header.getBundle())));
				}
			} else {
				bundle.setHeader(headerName, change.getMovedText(i));
				header = (ExportPackageHeader) bundle.getManifestHeader(headerName);
			}
		}
	}

	private static String getVersionAttribute(IBundle bundle) {
		int manifestVersion = BundlePluginBase.getBundleManifestVersion(bundle);
		return (manifestVersion < 2) ? ICoreConstants.PACKAGE_SPECIFICATION_VERSION : Constants.VERSION_ATTRIBUTE;
	}

	private static String getExportedPackageHeader(IBundle bundle) {
		int manifestVersion = BundlePluginBase.getBundleManifestVersion(bundle);
		return (manifestVersion < 2) ? ICoreConstants.PROVIDE_PACKAGE : Constants.EXPORT_PACKAGE;
	}

	public static Bundle getBundle(IFile file, IProgressMonitor monitor) throws CoreException, MalformedTreeException, BadLocationException {
		ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
		manager.connect(file.getFullPath(), LocationKind.NORMALIZE, monitor);

		IDocument document = manager.getTextFileBuffer(file.getFullPath(), LocationKind.NORMALIZE).getDocument();
		BundleModel model = new BundleModel(document, false);
		model.load();
		return model.isLoaded() ? (Bundle) model.getBundle() : null;
	}
}

Back to the top