1

我想打开位于资源中的 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;


}

简单HTMLCallBack.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);
  }
}

谢谢...

4

0 回答 0