Skip to content

Commit 397763b

Browse files
Allow platform:/plugin/ URIs in templates extension include element
The 'file' and 'translations' attributes of the <include> element in the org.eclipse.ui.editors.templates extension point previously only accepted plug-in local file paths, resolved relative to the contributing bundle via FileLocator.find(Bundle, IPath, Map). This change extends ContributionTemplateStore.readIncludedTemplates to also accept platform:/ URIs (e.g. platform:/plugin/<bundle-id>/<path>), allowing a contributor to reference template files that live in another plug-in. Bundle-relative paths continue to work unchanged; platform:/ URIs are resolved via FileLocator.find(URL).
1 parent 8ad0c90 commit 397763b

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

bundles/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/templates/ContributionTemplateStore.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import java.io.BufferedInputStream;
1818
import java.io.IOException;
1919
import java.io.InputStream;
20+
import java.net.MalformedURLException;
21+
import java.net.URI;
22+
import java.net.URISyntaxException;
2023
import java.net.URL;
2124
import java.util.ArrayList;
2225
import java.util.Collection;
@@ -128,12 +131,12 @@ private void readIncludedTemplates(Collection<TemplatePersistenceData> templates
128131
String file= element.getAttribute(FILE);
129132
if (file != null) {
130133
Bundle plugin = Platform.getBundle(element.getContributor().getName());
131-
URL url= FileLocator.find(plugin, IPath.fromOSString(file), null);
134+
URL url= resolveResource(plugin, file);
132135
if (url != null) {
133136
ResourceBundle bundle= null;
134137
String translations= element.getAttribute(TRANSLATIONS);
135138
if (translations != null) {
136-
URL bundleURL= FileLocator.find(plugin, IPath.fromOSString(translations), null);
139+
URL bundleURL= resolveResource(plugin, translations);
137140
if (bundleURL != null) {
138141
try (InputStream bundleStream= bundleURL.openStream()) {
139142
bundle= new PropertyResourceBundle(bundleStream);
@@ -160,6 +163,30 @@ private void readIncludedTemplates(Collection<TemplatePersistenceData> templates
160163
}
161164
}
162165

166+
/**
167+
* Resolves a resource location to a URL. Supports plain bundle-relative paths
168+
* as well as <code>platform:/plugin/</code> URIs, which allow referencing files
169+
* contributed by other plug-ins. Other <code>platform:/</code> schemes (e.g.
170+
* <code>platform:/resource/</code>, <code>platform:/fragment/</code>) are not
171+
* handled here and are treated as bundle-relative paths.
172+
*
173+
* @param plugin the contributing bundle, used for bundle-relative lookups
174+
* @param location the resource location (a bundle-relative path or a
175+
* <code>platform:/plugin/</code> URI)
176+
* @return the resolved URL, or <code>null</code> if it could not be found
177+
*/
178+
private static URL resolveResource(Bundle plugin, String location) {
179+
if (location.startsWith("platform:/plugin/")) { //$NON-NLS-1$
180+
try {
181+
return FileLocator.find(new URI(location).toURL());
182+
} catch (URISyntaxException | MalformedURLException e) {
183+
EditorsPlugin.log(e);
184+
return null;
185+
}
186+
}
187+
return FileLocator.find(plugin, IPath.fromOSString(location), null);
188+
}
189+
163190
/**
164191
* Validates a template against the context type registered in the context
165192
* type registry. Returns always <code>true</code> if no registry is

0 commit comments

Comments
 (0)