Thursday 15 January 2015

java - GWT platform independent newline character -



java - GWT platform independent newline character -

in 1 of java classes, want write result looks this:

string line_separator = system.lineseparator(); stringbuilder result = new stringbuilder(); result.append("some characters"); result.append(line_separator);

the class using code passed gwt based frontend. gwt compiles java classes used gwt (e.g. classes in fronted) javascript. however, gwt cannot compile system.lineseparator() there no equivalent method in javascript.

string line_separator = system.getproperty(line.separator);

and

string line_separator = string.format("%n");

also cause gwt compiler exceptions. however,

string line_separator = "\r\n";

works, not platform independent.

how can platform independent newline character compatible gwt?

simplest thing perchance work (out of top of head):

class="lang-java prettyprint-override">string line_separator = gwt.isclient() ? "\n" : getlineseparator(); @gwtincompatible private static string getlineseparator() { homecoming system.lineseparator(); }

that require recent version of gwt (2.6 @ least, maybe 2.7).

the alternative utilize super-source simple provider class:

class="lang-java prettyprint-override">public class lineseparatorprovider { public final string line_separator = system.lineseparator(); } // in super-source public class lineseparatorprovider { public final string line_separator = "\n"; }

note in future version of gwt, system.getproperty work (for properties) perchance create work line.separator.

…or, utilize \n everywhere , when need have \r\n replace("\n", "\r\n").

(if inquire me, i'd utilize \n everywhere, on windows)

java javascript gwt

No comments:

Post a Comment