Re: Unicode Characters in Dynamic Text Content

321 views
Skip to first unread message

drkstr1

unread,
Nov 14, 2012, 4:57:42 PM11/14/12
to google-we...@googlegroups.com
Well I was able to come up with a temporary work around, but it's such an ugly hack I'm almost embarrassed to share it!

I figured what I needed to do was force javascript to evaluate the String somehow. Here are the steps I took to make that happen:

1. Use javascript control codes (as text) in your content (EG: \u201C)

2. Create a JSONObject with the loaded content 

JSONObject json = new JSONObject();
json.put("content", new JSONString(response.getText()));

2. Serialize the object into a json String, and unescape the control codes

//note: StringUtil.unescapeJavaScript is a re-implementation of 
//      org.apache.commons.lang.StringEscapeUtils which uses a 
//      StringBuilder rather than a Writer. I can post if desired.
String data = "[" + StringUtil.unescapeJavaScript(json.toString()) + "]";

3. Evaluate the JSON string back into a JavascriptObject using a JSNI native method

panel1.getElement().setInnerHTML(evalContent(data).get(0).content());

private static native JsArray<ContentMemento> evalContent(String jsonStr) /*-{
return eval(jsonStr);
}-*/;

class ContentMemento extends JavaScriptObject
{
    protected ContentMemento()
    {

    }

    public final native String content() /*-{
return this.content;
    }-*/;


Presto! I now have unicode characters in dynamic text content. However, I would still really really love it if anyone knows of a way I can evaluate the String data without having to go through JSON


Thanks again!

On Monday, November 12, 2012 1:41:23 PM UTC-8, drkstr1 wrote:
Hello All,

I am having an issue with Unicode characters being displayed in dynamic text content.

Please consider the examples UnicodeTest1 and UnicodeTest2, bellow. I can get the desired behavior if I assign the content manually using the javascript escape codes for the unicode character (see UnicodeTest1). However, when I load the text with a RequestBuilder, I can't get any unicode characters to display, regardless of how that content is formatted (see UnicodeTest2).

What is the correct way to display unicode characters in text that was loaded from a RequestBuilder?

Thank you for your time!


public class UnicodeTest1 implements EntryPoint, IsWidget
{
    private static final String TEXT1 = "\u201CHello World!\u201D";

    private static final String TEXT2 = "“Hello World!”";

    public void onModuleLoad()
    {
        RootPanel.get().add(this);
    }

    @Override
    public Widget asWidget()
    {
        VerticalPanel container = new VerticalPanel();

        SimplePanel panel1 = new SimplePanel();
        panel1.getElement().setInnerHTML(TEXT1);
        container.add(panel1);

        SimplePanel panel2 = new SimplePanel();
        panel2.getElement().setInnerHTML(TEXT2);
        container.add(panel2);

        return container;
    }
}

public class UnicodeTest2 implements EntryPoint, IsWidget
{
    public void onModuleLoad()
    {
        RootPanel.get().add(this);
    }

    @Override
    public Widget asWidget()
    {
        VerticalPanel container = new VerticalPanel();

        final SimplePanel panel1 = new SimplePanel();
        container.add(panel1);

        RequestBuilder rb = null;

        // Contains: \u201CHello World!\u201D
        rb = new RequestBuilder(RequestBuilder.GET, "text1.htm");
        try
        {
            rb.sendRequest(null, new RequestCallback()
            {
                public void onError(Request request, Throwable exception)
                {

                }

                public void onResponseReceived(Request request, Response response)
                {
                    GWT.log("Response: " + response.getText());
                    panel1.getElement().setInnerHTML(response.getText());
                }
            });
        }
        catch (RequestException e)
        {
         
        }

        final SimplePanel panel2 = new SimplePanel();
        container.add(panel2);

        // Contains: “Hello World!”
        rb = new RequestBuilder(RequestBuilder.GET, "text2.htm");
        try
        {
            rb.sendRequest(null, new RequestCallback()
            {
                public void onError(Request request, Throwable exception)
                {

                }

                public void onResponseReceived(Request request, Response response)
                {
                    GWT.log("Response: " + response.getText());
                    panel2.getElement().setInnerHTML(response.getText());
                }
            });
        }
        catch (RequestException e)
        {
           
        }

        return container;
    }

 Note: in case my unicode characters got stripped from this post (TEXT2/content2.html contains the same string with the raw unicode characters).
Reply all
Reply to author
Forward
Message has been deleted
0 new messages