Home » Eclipse Projects » Remote Application Platform (RAP) » UTF8 characters not displaying in html served by RAP appserver(HTML created within RAP and served as an rwt-resource displays incorrect UTF8 text)
| |
Re: UTF8 characters not displaying in html served by RAP appserver [message #1859285 is a reply to message #1859265] |
Fri, 26 May 2023 14:16 |
|
Thanks Ivan. Here is a fairly simple self-contained example that demonstrates the issue:
/* Demonstrates character translation issue of resources */
package bug.snippet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.client.service.UrlLauncher;
import org.eclipse.rap.rwt.service.ResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class Bugsy {
private Display display;
private Shell shell;
Button but;
Table tab;
TableColumn col;
TableItem ti;
public void begin() {
System.out.println("BugSnippy Starting...");
// create the Shell
display = new Display();
shell = new Shell(display, SWT.NONE); //, SWT.TITLE|SWT.CLOSE|SWT.RESIZE);
shell.setText("Shell");
shell.setBackground(new Color(null, new RGB(255,192,128)));
FormLayout layout = new FormLayout();
layout.marginBottom = 20;
shell.setLayout(layout);
but = new Button(shell, SWT.PUSH);
but.setText("Test");
FormData fd = new FormData();
fd.left = new FormAttachment(0, 10);
fd.width = 200;
fd.top = new FormAttachment(0, 10);
fd.height = 20;
but.setLayoutData(fd);
but.addSelectionListener(butLnsr);
tab = new Table(shell, SWT.SINGLE);
col = new TableColumn(tab, SWT.NONE);
col.setWidth(500);
col.setText("My Column");
fd = new FormData();
fd.left = new FormAttachment(0, 10);
fd.width = 500;
fd.top = new FormAttachment(0, 50);
fd.bottom = new FormAttachment(100, -10);
tab.setLayoutData(fd);
ti = new TableItem(tab, SWT.NONE);
ti.setText("ABC");
ti = new TableItem(tab, SWT.NONE);
ti.setText("DEF");
ti = new TableItem(tab, SWT.NONE);
ti.setText("GHI");
ti = new TableItem(tab, SWT.NONE);
ti.setText("JKL");
ti = new TableItem(tab, SWT.NONE);
ti.setText("毛茸茸的鸭子");
ti = new TableItem(tab, SWT.NONE);
ti.setText("пушистые уточки");
ti = new TableItem(tab, SWT.NONE);
ti.setText("البط الرقيق");
ti = new TableItem(tab, SWT.NONE);
ti.setText("kabarık ördekler - αφράτες πάπιες");
shell.setFullScreen(true);
shell.open();
System.out.println("BugSnippy Done!");
}
SelectionListener butLnsr = new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent ev) {
System.out.println("but press...");
// write all the table data to a physical file (html page)
String clientDir = "c:/temp/MySessionFiles/";
File directory = new File(clientDir);
directory.mkdirs();
String fileName = clientDir + "listDataAsReport.html";
try {
FileWriter fw = new FileWriter(new File(fileName));
fw.write("<html>\n");
fw.write("<head>\n");
fw.write("<style>\n");
fw.write("body{font-family:Arial, Helvetica, sans-serif;font-size:12px;padding:0;margin:0;}\n");
fw.write("h1{font-family:Arial, Helvetica, sans-serif;font-size:14px;font-weight: bold;padding:0;margin:0;}\n");
fw.write("td{border-bottom: 1px solid #EEEEEE;border-right: 1px solid #EEEEEE;color:#0;font-family:Arial, Helvetica, sans-serif;font-size:10px;padding:0;}\n");
fw.write("td.selected{border-bottom: 1px solid #EEEEEE;border-right: 1px solid #EEEEEE;color:#FFFFFF;background-color: #6E8EFF;font-family:Arial, Helvetica, sans-serif;font-size:10px;padding:0;}\n");
fw.write("</style>\n");
fw.write("</head>\n");
fw.write("<body>\n");
fw.write("<h1>My List Report</h1><br>\n");
fw.write("<table>\n");
int rows = tab.getItemCount();
for (int i = 0; i < rows; i++) {
String txt = tab.getItem(i).getText();
System.out.println("Item " + i + " has data '" + txt + "'");
fw.write("<tr><td>" + txt + "</td></tr>\n");
}
fw.write("</table>\n");
fw.write("</body>\n");
fw.write("</html>\n");
fw.close();
System.out.println("Written to html file: " + fileName);
// register the file as a resource
String rsc = loadAndRegisterResource(fileName);
System.out.println("Resource is: " + rsc);
// open that html file in a new tab
launchURL(rsc);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
};
public String loadAndRegisterResource(String fileName) {
InputStream sourceStream = null;
sourceStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
if (sourceStream == null) {
sourceStream = this.getClass().getClassLoader().getResourceAsStream("rwt-resources/" + fileName);
if (sourceStream == null) {
try {
sourceStream = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
System.out.println("ERROR: loadAndRegisterResource failed to open new inputStream for: " + fileName);
}
}
if (sourceStream == null) {
System.out.println("ERROR: loadAndRegisterResource failed to find requested resource: " + fileName);
return(null);
}
}
ResourceManager resourceManager = RWT.getResourceManager();
try {
System.out.println("A loadAndRegisterResource - Registering resource: " + fileName);
resourceManager.register(fileName, sourceStream);
} catch (Exception e) {
System.out.println("ERROR: loadAndRegisterResource [A:too many attempts] - register error on " + fileName + " - " + e.getMessage());
}
// determine the resource name that it is registered as
String resourceSrc = null;
try {
resourceSrc = getLocation(resourceManager, fileName);
System.out.println("loadAndRegisterResource got " + fileName + " location as a resource: " + resourceSrc);
}
catch (IllegalArgumentException e) {
System.out.println("ERROR: loadAndRegisterResource IllegalArgumentException getting resource location: " + e.getMessage());
}
// close the resource
try {
sourceStream.close();
} catch (IOException e) {
System.out.println("ERROR: loadAndRegisterResource IOException closing resource: " + e.getMessage());
}
System.out.println("DEBUG: loadAndRegisterResource: " + resourceSrc);
return(resourceSrc);
}
public String getLocation(ResourceManager resourceManager, String loadFile) {
String src = null;
try {
src = resourceManager.getLocation(loadFile);
}
catch (Exception e) {
System.out.println("ERROR: getLocation failed to locate resource: " + loadFile + " - " + e.getMessage());
}
return(src);
}
public int launchURL(String url) {
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
if (launcher == null) {
System.out.println("ERROR: RWT.launchURL failed to create RWT launcher for launching: " + url);
return(-1);
}
launcher.openURL(url);
return(0);
}
}
I recreated it in Firefox, Edge and Chrome - haven't tried anything else.
Running in a different container will take a little longer to setup - we normally use OSGi, then the eventual app is deployed using WAR to TOMEE, JBoss, WildFly, WebLogic or WebSphere (though that is customer choice, so not our environment). I probably won't try to recreate the sample in any of these, but I can try my original application.
Hopefully the above is all you need to recreate it though.
Thanks, John
---
Just because you can doesn't mean you should
|
|
| | | | |
Goto Forum:
Current Time: Thu Dec 05 22:42:12 GMT 2024
Powered by FUDForum. Page generated in 0.03734 seconds
|