LWUIT:如何从资源中打开XHTML文件?

时间:2013-05-07 08:39:35

标签: java java-me lwuit lwuit-form lwuit-resource-editor

我想打开驻留在资源中的XHTML文件。我尝试使用LWUIT的HTMLComponent。但它只打开HTML文件。使用任何LWUIT组件打开XHTML文件的正确方法是什么。?

我试过下面的代码。它适用于带有简单标签的HTML文件。不适用于所有标签和属性。

String htmlFileName = "index.html";
HTMLComponent htmlC = new HTMLComponent(new FileRequestHandler(HtmlScreen.this));
htmlC.setHTMLCallback(new SimpleHTMLCallback(this));
htmlC.setPage("jar://"+"/res/"+htmlFileName.trim());

FileRequestHandler.java:

class FileRequestHandler implements com.divaa.app.DocumentRequestHandler {

HtmlScreen htmlDemo;
static final String DEFAULT_RES = "images";

public FileRequestHandler(HtmlScreen htmlDemo) {
    this.htmlDemo=htmlDemo;
}

public InputStream resourceRequested(DocumentInfo docInfo) {
    String url=docInfo.getUrl();


    // If a from was submitted on a local file, just display the parameters
    if ((docInfo.getParams()!=null) && (!docInfo.getParams().equals(""))) {
        String method="GET";
        if (docInfo.isPostRequest()) {
            method="POST";
        }
        String params=docInfo.getParams();
        String newParams="";
        if (params!=null) {
            for(int i=0;i<params.length();i++) {
                char c=params.charAt(i);
                if (c=='&') {
                    newParams+=", ";
                } else {
                    newParams+=c;
                }
            }
        }
        return getStream("<h2>Form submitted locally.</h2><b>Method:</b> "+method+"<br><br><b>Parameters:</b><br>"+newParams+"<hr><a href=\""+docInfo.getUrl()+"\">Continue to local URL</a>","Form Results");
    }

    url=url.substring(6); // Cut the jar://

    byte[] buf;
    try {
        buf = getBuffer(Display.getInstance().getResourceAsStream(getClass(), url));
        if (url.endsWith(".html")) { //only set source to HTML files (not images)
            htmlDemo.setSource(new String(getBuffer(new ByteArrayInputStream(buf))));
        }
        return new ByteArrayInputStream(buf);
    } catch (Exception ex) {
        System.out.println("ex.toString exception........ "+ex.toString());
        ex.printStackTrace();
    }

    return null;


}

SimpleHTMLCallBack.java:

   class SimpleHTMLCallback extends DefaultHTMLCallback {

HtmlScreen htmlDemo;

public SimpleHTMLCallback(HtmlScreen htmlDemo) {
    this.htmlDemo=htmlDemo;
}

public boolean linkClicked(HTMLComponent htmlC, String url) {

    return true; // Signals the HTMLComponent to prcoess this link as usual (i.e. call DocumentRequestHandler.resourceRequested)
}

public void titleUpdated(HTMLComponent htmlC, String title) {
    htmlDemo.setTitle(title);
  }
}

...谢谢

0 个答案:

没有答案