views:

52

answers:

1

Background:

I have a small problem with Eclipse. I'm working on a workbench plugin which has some classes that validate incoming XML data against a schema. The schema lives inside the plugin project in a "./schemas" folder.

Questions:

  1. When I run the application, how can I read the schema without using a hardcoded path?
  2. When the application is deployed the schema will need to live inside a plugin .jar. Will the solution from (1) be any different in this case?
+1  A: 

You can use the FileLocator class from org.eclipse.core.runtime to get a URL to a resource in your bundle.

For example, something like

URL schema = FileLocator.find(myBundle, new Path("/schemas/data.xsd"), null);

You can get your Bundle object from the BundleContext passed into your Activator (if you have one). Or you could use Platform.getBundle.

You should not assume the url is a file on disk, for exactly the case when the plugin is a jar. You can use URL.openStream to get the contents, or FileLocator.toFileURL() to get a file on disk.

Andrew Niefer
Thank you very much Andrew. Your suggestion was spot on!
Daniel