2

My plugin has html file resource which it is displaying on SWT browser widget.

I am able to locate this html file from the resource folder in side my plugin using this code:

public File resolveResource(URL url) {
    File resolvedFile = null;
    try {
        URL resolvedFileURL = FileLocator.toFileURL(url);
        URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null);
        resolvedFile = new File(resolvedURI);
    } catch (Exception e) {
      // exception handling
    }
    return resolvedFile;
}

Now this html file resource is referring some css , images which are present in same resource folder inside plugin.

The problem is when I package and deploy the plugin and try running it - the images and css are not getting displayed/resolved.

This works fine when I run it 'Eclipse Application' from the IDE.

HTML file resource:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; width=device-width" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <link rel="stylesheet" href="css/styles.css" type="text/css" />
    </head>
    <body>
        <div class="content" id="parent">
        </div>
        <div class="footer">
            All contents copyright 2014. 
        </div>
    </body>
</html>
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Amrit
  • 433
  • 3
  • 19

1 Answers1

1

FileLocator.toFileURL expects a URL in the format returned by FileLocator.find. The normal usage is:

Bundle bundle = Platform.getBundle("plugin id");

URL url = FileLocator.find(bundle, new Path("plugin relative path"), null);

URL fileURL = FileLocator.toFileURL(url);

It is important to use FileLocator.find because special URLs are used for resources in a plugin Jar file.

If you need to access other files in a folder then you should use a 'feature' project for your plugins and specify that the plugin is unpacked during plugin install so that a normal folder is created for the files. Or use the 'Eclipse-BundleShape' - see here for more details.

Community
  • 1
  • 1
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • The problem is not in locating the resource as both approach are working - the one I was using and the one you showed above - the actual problem is in locating the resources relative to plugin resource - so here In the above scenario I am able to locate .html file which is a resource located in plugin jar file , but this .html file references css and images as shown which are also in jar file in the same resource folder. Now how would the html file which is located by the above code will know about the css and images from same folder – Amrit Apr 14 '15 at 10:38
  • Added something about unpacking the plugin – greg-449 Apr 14 '15 at 11:15