LWUIT: How to open XHTML file from resource?

390 Views Asked by At

I want to open XHTML file which resides in resources. I tried with HTMLComponent of LWUIT. But it is opening only HTML files only. What would be correct way to open XHTML file using any LWUIT component.?

I tried with below code. Its working for HTML files with simple tags. Not working for all tags and attributes.

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);
  }
}

Thanks...

0

There are 0 best solutions below