diff options
author | Sam Davis | 2012-02-15 17:14:38 +0000 |
---|---|---|
committer | Steffen Pingel | 2012-02-15 17:14:38 +0000 |
commit | 3179f495433cb5bcf0a164c52f324daeabbc9e31 (patch) | |
tree | 94a5b222a123c7b3d99489aecb51aaf7d587403e | |
parent | 1f37f45a3c23642fbb8706a682b2eb42cae2ec09 (diff) | |
download | org.eclipse.mylyn.commons-3179f495433cb5bcf0a164c52f324daeabbc9e31.tar.gz org.eclipse.mylyn.commons-3179f495433cb5bcf0a164c52f324daeabbc9e31.tar.xz org.eclipse.mylyn.commons-3179f495433cb5bcf0a164c52f324daeabbc9e31.zip |
NEW - bug 370620: [api] extend ExtensionPointReader to support filtering
and priority
https://bugs.eclipse.org/bugs/show_bug.cgi?id=370620
Change-Id: Ib277af565a58a14844bd06eb7194925137636146
4 files changed, 120 insertions, 5 deletions
diff --git a/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/ExtensionPointReader.java b/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/ExtensionPointReader.java index 578e8605..68d88e48 100644 --- a/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/ExtensionPointReader.java +++ b/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/ExtensionPointReader.java @@ -1,5 +1,3 @@ -package org.eclipse.mylyn.commons.core; - /******************************************************************************* * Copyright (c) 2011 Tasktop Technologies. * All rights reserved. This program and the accompanying materials @@ -10,8 +8,11 @@ package org.eclipse.mylyn.commons.core; * Contributors: * Tasktop Technologies - initial API and implementation *******************************************************************************/ +package org.eclipse.mylyn.commons.core; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import org.eclipse.core.runtime.Assert; @@ -27,10 +28,43 @@ import org.eclipse.osgi.util.NLS; /** * @author Steffen Pingel + * @author Sam Davis * @since 3.7 */ public class ExtensionPointReader<T> { + private static final String ATTRIBUTE_EXTENSION_PRIORITY = "extensionPriority"; //$NON-NLS-1$ + + private static final class PriorityComparator implements Comparator<IConfigurationElement> { + + public int compare(IConfigurationElement arg0, IConfigurationElement arg1) { + double p0 = 0; + double p1 = 0; + try { + String priorityAttribute = arg0.getAttribute(ATTRIBUTE_EXTENSION_PRIORITY); + if (priorityAttribute != null) { + p0 = Double.parseDouble(priorityAttribute); + } + } catch (NumberFormatException e) { + } + try { + String priorityAttribute = arg1.getAttribute(ATTRIBUTE_EXTENSION_PRIORITY); + if (priorityAttribute != null) { + p1 = Double.parseDouble(priorityAttribute); + } + } catch (NumberFormatException e) { + } + if (p1 > p0) { + return 1; + } else if (p1 < -p0) { + return -1; + } + return 0; + } + } + + private static final PriorityComparator PRIORITY_COMPARATOR = new PriorityComparator(); + private final String extensionId; private final String elementId; @@ -43,6 +77,17 @@ public class ExtensionPointReader<T> { private final List<T> items; + private String filterAttributeId; + + private String filterAttributeValue; + + public ExtensionPointReader(String pluginId, String extensionId, String elementId, Class<T> clazz, + String filterAttributeId, String filterAttributeValue) { + this(pluginId, extensionId, elementId, clazz); + this.filterAttributeId = filterAttributeId; + this.filterAttributeValue = filterAttributeValue; + } + public ExtensionPointReader(String pluginId, String extensionId, String elementId, Class<T> clazz) { Assert.isNotNull(pluginId); Assert.isNotNull(extensionId); @@ -88,8 +133,9 @@ public class ExtensionPointReader<T> { IExtension[] extensions = extensionPoint.getExtensions(); for (IExtension extension : extensions) { IConfigurationElement[] elements = extension.getConfigurationElements(); + Arrays.sort(elements, PRIORITY_COMPARATOR); for (IConfigurationElement element : elements) { - if (element.getName().equals(elementId)) { + if (element.getName().equals(elementId) && shouldRead(element)) { T item = readElement(element, result); if (item != null) { items.add(item); @@ -104,6 +150,20 @@ public class ExtensionPointReader<T> { return result; } + /** + * Determines whether the element should be instantiated by this ExtensionPointReader. This implementation checks + * whether the element defines an attribute with id and value matching filterAttributeId and filterAttributeValue. + * If filterAttributeValue is the empty string, an element is also considered to match if it does not define the + * attribute. + * <p> + * Subclasses may override. + */ + protected boolean shouldRead(IConfigurationElement element) { + return filterAttributeId == null || filterAttributeValue == null + || filterAttributeValue.equals(element.getAttribute(filterAttributeId)) + || (filterAttributeValue.length() == 0 && element.getAttribute(filterAttributeId) == null); + } + protected void handleResult(IStatus result) { if (!result.isOK()) { StatusHandler.log(result); diff --git a/org.eclipse.mylyn.commons.tests/plugin.xml b/org.eclipse.mylyn.commons.tests/plugin.xml index 26fc4537..714a8cf5 100644 --- a/org.eclipse.mylyn.commons.tests/plugin.xml +++ b/org.eclipse.mylyn.commons.tests/plugin.xml @@ -26,6 +26,20 @@ point="org.eclipse.mylyn.commons.tests.extensionPointReaderTest"> <extensionElement class="org.eclipse.mylyn.commons.tests.core.ExtensionPointReaderTest$ExtensionPointReaderExtensionImplementation"></extensionElement> + <extensionElement + class="org.eclipse.mylyn.commons.tests.core.ExtensionPointReaderTest$P5ExtensionPointReaderExtensionImplementation" + extensionPriority="5" + testFilterAttribute="value1"> + </extensionElement> + <extensionElement + class="org.eclipse.mylyn.commons.tests.core.ExtensionPointReaderTest$PNegative5ExtensionPointReaderExtensionImplementation" + extensionPriority="-5" + testFilterAttribute="value2"> + </extensionElement> + <extensionElement + class="org.eclipse.mylyn.commons.tests.core.ExtensionPointReaderTest$P10ExtensionPointReaderExtensionImplementation" + extensionPriority="10"> + </extensionElement> </extension> </plugin> diff --git a/org.eclipse.mylyn.commons.tests/schema/extensionPointReaderTest.exsd b/org.eclipse.mylyn.commons.tests/schema/extensionPointReaderTest.exsd index 06a40f79..3929cf58 100644 --- a/org.eclipse.mylyn.commons.tests/schema/extensionPointReaderTest.exsd +++ b/org.eclipse.mylyn.commons.tests/schema/extensionPointReaderTest.exsd @@ -66,6 +66,20 @@ </documentation> </annotation> </attribute> + <attribute name="testFilterAttribute" type="string"> + <annotation> + <documentation> + + </documentation> + </annotation> + </attribute> + <attribute name="extensionPriority" type="string"> + <annotation> + <documentation> + + </documentation> + </annotation> + </attribute> </complexType> </element> @@ -91,7 +105,6 @@ 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 - </documentation> </annotation> diff --git a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/ExtensionPointReaderTest.java b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/ExtensionPointReaderTest.java index 63ef09d0..93585b84 100644 --- a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/ExtensionPointReaderTest.java +++ b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/ExtensionPointReaderTest.java @@ -11,6 +11,7 @@ package org.eclipse.mylyn.commons.tests.core; +import java.util.Arrays; import java.util.Collections; import junit.framework.TestCase; @@ -20,6 +21,7 @@ import org.eclipse.mylyn.commons.core.ExtensionPointReader; /** * @author Steffen Pingel + * @author Sam Davis */ public class ExtensionPointReaderTest extends TestCase { @@ -62,6 +64,18 @@ public class ExtensionPointReaderTest extends TestCase { } } + public static class P5ExtensionPointReaderExtensionImplementation extends + ExtensionPointReaderExtensionImplementation { + } + + public static class PNegative5ExtensionPointReaderExtensionImplementation extends + ExtensionPointReaderExtensionImplementation { + } + + public static class P10ExtensionPointReaderExtensionImplementation extends + ExtensionPointReaderExtensionImplementation { + } + private static final String ID_PLUGIN = "org.eclipse.mylyn.commons.tests"; public void testRead() { @@ -69,7 +83,21 @@ public class ExtensionPointReaderTest extends TestCase { ID_PLUGIN, "extensionPointReaderTest", "extensionElement", ExtensionPointReaderExtension.class); IStatus status = reader.read(); assertEquals(IStatus.OK, status.getSeverity()); - assertEquals(Collections.singletonList(new ExtensionPointReaderExtensionImplementation()), reader.getItems()); + assertEquals( + Arrays.asList(new ExtensionPointReaderExtension[] { + new P10ExtensionPointReaderExtensionImplementation(), + new P5ExtensionPointReaderExtensionImplementation(), + new ExtensionPointReaderExtensionImplementation(), + new PNegative5ExtensionPointReaderExtensionImplementation() }), reader.getItems()); + } + + public void testReadWithFiltering() { + ExtensionPointReader<ExtensionPointReaderExtension> reader = new ExtensionPointReader<ExtensionPointReaderExtension>( + ID_PLUGIN, "extensionPointReaderTest", "extensionElement", ExtensionPointReaderExtension.class, + "testFilterAttribute", "value1"); + IStatus status = reader.read(); + assertEquals(IStatus.OK, status.getSeverity()); + assertEquals(Collections.singletonList(new P5ExtensionPointReaderExtensionImplementation()), reader.getItems()); } } |