diff options
| author | campbeb <bpcampbell@gmail.com> | 2013-01-19 10:34:39 -1000 |
|---|---|---|
| committer | campbeb <bpcampbell@gmail.com> | 2013-01-19 10:34:39 -1000 |
| commit | ea13018cc48c97125e25668f5bea67135f5df9ee (patch) | |
| tree | 318a84b94b33c2c9774bcc8a64a660ff248b6203 | |
| parent | 9073a540aa3e76f77777c991464f2811dbe780b6 (diff) | |
| download | cgeo-ea13018cc48c97125e25668f5bea67135f5df9ee.zip cgeo-ea13018cc48c97125e25668f5bea67135f5df9ee.tar.gz cgeo-ea13018cc48c97125e25668f5bea67135f5df9ee.tar.bz2 | |
Convert non-Latin characters in uploaded text to HTML equivalents
Add new HtmlUtil function for this as it needs to occur in multiple places
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 27 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/HtmlUtils.java | 25 |
2 files changed, 31 insertions, 21 deletions
diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index b885e5b..e8de993 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -29,6 +29,7 @@ import cgeo.geocaching.network.Parameters; import cgeo.geocaching.ui.DirectionImage; import cgeo.geocaching.utils.BaseUtils; import cgeo.geocaching.utils.CancellableHandler; +import cgeo.geocaching.utils.HtmlUtils; import cgeo.geocaching.utils.LazyInitializedList; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.MatcherWrapper; @@ -933,23 +934,7 @@ public abstract class GCParser { return new ImmutablePair<StatusCode, String>(StatusCode.NO_LOG_TEXT, ""); } - // fix log (non-Latin characters converted to HTML entities) - final int logLen = log.length(); - final StringBuilder logUpdated = new StringBuilder(); - - for (int i = 0; i < logLen; i++) { - char c = log.charAt(i); - - if (c > 300) { - logUpdated.append("&#"); - logUpdated.append(Integer.toString(c)); - logUpdated.append(';'); - } else { - logUpdated.append(c); - } - } - - final String logInfo = logUpdated.toString().replace("\n", "\r\n").trim(); // windows' eol and remove leading and trailing whitespaces + final String logInfo = HtmlUtils.convertNonLatinCharactersToHTML(log).replace("\n", "\r\n").trim(); // windows' eol and remove leading and trailing whitespaces if (trackables != null) { Log.i("Trying to post log for cache #" + cacheid + " - action: " + logType + "; date: " + year + "." + month + "." + day + ", log: " + logInfo + "; trackables: " + trackables.size()); @@ -1071,7 +1056,7 @@ public abstract class GCParser { /** * Upload an image to a log that has already been posted - * + * * @param logId * the ID of the log to upload the image to. Found on page returned when log is uploaded * @param caption @@ -1104,8 +1089,8 @@ public abstract class GCParser { final Parameters uploadParams = new Parameters( "__EVENTTARGET", "", "__EVENTARGUMENT", "", - "ctl00$ContentBody$ImageUploadControl1$uxFileCaption", caption, - "ctl00$ContentBody$ImageUploadControl1$uxFileDesc", description, + "ctl00$ContentBody$ImageUploadControl1$uxFileCaption", HtmlUtils.convertNonLatinCharactersToHTML(caption), + "ctl00$ContentBody$ImageUploadControl1$uxFileDesc", HtmlUtils.convertNonLatinCharactersToHTML(description), "ctl00$ContentBody$ImageUploadControl1$uxUpload", "Upload"); Login.putViewstates(uploadParams, viewstates); @@ -1162,7 +1147,7 @@ public abstract class GCParser { Log.i("Trying to post log for trackable #" + trackingCode + " - action: " + logType + "; date: " + year + "." + month + "." + day + ", log: " + log); - final String logInfo = log.replace("\n", "\r\n"); // windows' eol + final String logInfo = HtmlUtils.convertNonLatinCharactersToHTML(log).replace("\n", "\r\n"); // windows' eol final Calendar currentDate = Calendar.getInstance(); final Parameters params = new Parameters( diff --git a/main/src/cgeo/geocaching/utils/HtmlUtils.java b/main/src/cgeo/geocaching/utils/HtmlUtils.java index a54ba57..9b627ac 100644 --- a/main/src/cgeo/geocaching/utils/HtmlUtils.java +++ b/main/src/cgeo/geocaching/utils/HtmlUtils.java @@ -53,4 +53,29 @@ public class HtmlUtils { return StringUtils.replace(result, "<br />", "\n").trim(); } + /** + * Convert any non-Latin characters into their HTML escaped equivalents + * + * @param input + * String + * @return output String + */ + public static String convertNonLatinCharactersToHTML(final String input) { + final int inputLen = input.length(); + final StringBuilder output = new StringBuilder(); + + for (int i = 0; i < inputLen; i++) { + char c = input.charAt(i); + + if (c > 300) { + output.append("&#"); + output.append(Integer.toString(c)); + output.append(';'); + } else { + output.append(c); + } + } + + return output.toString(); + } } |
