Eclipse插件:html站点的jar目录的路径

时间:2014-08-04 17:00:45

标签: java html image jar eclipse-plugin

我正在用Eclipse插件生成一个临时的html站点。看起来,不可能像Java Swing一样使用常用的方法,如下所示:

URL location = getClass().getResource("/img/notifications/pic.png");
InputStream inputStream = location.openStream();
final Image ideaImage = new Image(display, inputStream);

我尝试使用此功能,但从逻辑上讲,我只获得bundleresource://592.fwk513700442:1/img/html/pic.png

所以目前我正在使用以下方法:

URL configURL = bc.getBundle().getResource("/img/html/pic.png");
File configFile = new File(FileLocator.toFileURL(configURL).getPath());
location = configFile.toString().substring(0, configFile.toString().length()-7);
// 7 = length of "pic.png" to get the root folder

这样可行,但仅适用于我输入上述名称的图像。我想,循环遍历所有图像很麻烦,所以必须有更好的解决方案......

另一个解决方案是将我的所有JAR内容提取到本地文件夹中,但这不是我想要的,因为用户不应该能够轻松访问所有文件。

编辑:澄清

我有一个jar格式的导出插件。从这个插件里面我打开用户的本机浏览器并打开一个临时文件。我用以下类来做这个:

package de.stefansurkamp.handlers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

public class WebsiteGenerator {

    private StringBuilder output = new StringBuilder();
    private ArrayList<Member> achievementList = new ArrayList<Member>();
    private String location;

    public WebsiteGenerator() {
        try {
            // get location information
            BundleContext bc = FrameworkUtil.getBundle(WebsiteGenerator.class).getBundleContext();              
            URL configURL = bc.getBundle().getResource("/img/html/firstCompile.png");
            File configFile = new File(FileLocator.toFileURL(configURL).getPath());
            location = configFile.toString().substring(0, configFile.toString().length()-16);

            // append HTML head
            output.append("<!DOCTYPE html><html><head></head>");

            // apend HTML body
            output.append(generateHtmlBody());
        }
        catch(IOException e1) {
            System.out.println(e1.getMessage());
        }
    }

    public void createPage() throws IOException, PartInitException {
        File temp = File.createTempFile(Long.toString(System.currentTimeMillis()),".html");
        FileWriter writer = new FileWriter(temp);
        writer.write(output.toString());
        writer.close();
        PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(temp.toURI().toURL());
        temp.deleteOnExit();
    }

    private String generateHtmlBody() throws IOException {
        try {
        StringBuilder stringBuilder = new StringBuilder(7250);
        stringBuilder.append("<body>");

        [a lot of other appends]

        stringBuilder.append("<img width=\"162\" height=\"162\" src=\"" + location + getAchievementID() + ".png\" alt=\"");
        // getAchievementID returns an "id" which is equal to the picture name i want to insert here.
        stringBuilder.append("</div>");
        stringBuilder.append("</body>");
        stringBuilder.append("</html>");

        return stringBuilder.toString();
        }
        catch(StorageException e) {
            System.out.println("STORAGE EXCEPTION: " + e.getErrorCode());
            return "";
        }
    }    
}

虽然这不是SSCCE,但我希望我能够清楚地了解如何创建网站。

这里的问题是,我不能使用“本地URL”(如上所述),因为图像路径根不在插件内,而是在浏览器中。有没有解决方案,如何“找到”jar包内朝向图像文件夹的路径?

0 个答案:

没有答案