android-ibc-forum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit b9a48b595596200229de7104fda8f52b24ba6583
parent 2f758a34c8541e4db401db4d7d94fad591eb6b14
Author: Jan Dankert <devnull@localhost>
Date:   Wed, 10 Oct 2012 22:33:21 +0200

BB-Code-Parser: Exception abfangen, die beim Auswerten der Regexp auftreten kann.

Diffstat:
src/de/mtbnews/android/adapter/BBCodeConverter.java | 88+++++++++++++++----------------------------------------------------------------
1 file changed, 16 insertions(+), 72 deletions(-)

diff --git a/src/de/mtbnews/android/adapter/BBCodeConverter.java b/src/de/mtbnews/android/adapter/BBCodeConverter.java @@ -2,6 +2,7 @@ package de.mtbnews.android.adapter; import java.util.regex.Pattern; +import android.util.Log; import de.mtbnews.android.util.IBC; /** @@ -9,31 +10,6 @@ import de.mtbnews.android.util.IBC; */ public class BBCodeConverter { - private static final String CR_LF = "(?:\r\n|\r|\n)?"; - - /** */ - private boolean acceptHTML = false; - - /** */ - private boolean acceptBBCode = true; - - /** - * @param acceptBBCode - * the new acceptBBCode value - */ - public void setAcceptBBCode(boolean acceptBBCode) - { - this.acceptBBCode = acceptBBCode; - } - - /** - * @param acceptHTML - * the new acceptHTML value - */ - public void setAcceptHTML(boolean acceptHTML) - { - this.acceptHTML = acceptHTML; - } /** * @param string @@ -64,13 +40,14 @@ public class BBCodeConverter string = processTag(string, "\\[mention=['\"]?([0-9]+)['\"]?\\](.*?)\\[/mention\\]", - "<a href=\"" + IBC.IBC_FORUM_URL + "member.php?u=$1\">&rarr;$2</a>"); + "<a href=\"" + IBC.IBC_FORUM_URL + + "member.php?u=$1\">&rarr;$2</a>"); // [b][u][i] string = processTag(string, "\\[b\\](.*?)\\[/b\\]", "<b>$1</b>"); string = processTag(string, "\\[u\\](.*?)\\[/u\\]", "<u>$1</u>"); string = processTag(string, "\\[i\\](.*?)\\[/i\\]", "<i>$1</i>"); - + // Smileys (very poor implementation) string = processTag(string, ":daumen:", "+1"); string = processTag(string, ":love:", "&hearts;"); @@ -86,8 +63,7 @@ public class BBCodeConverter string = processTag(string, "\\[url=['\"]?(.*?[^'\"])['\"]?\\](.*?)\\[/url\\]", "<a href=\"$1\">$2</a>"); - string = processTag(string, - "\\[yt.*\\](.*?)\\[/yt\\]", + string = processTag(string, "\\[yt.*\\](.*?)\\[/yt\\]", "<a href=\"http://www.youtube.com/watch?v=$2\">Video bei Youtube anzeigen</a>"); // [email] @@ -100,49 +76,17 @@ public class BBCodeConverter private static String processTag(String text, String pattern, String replaceWith) { - return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(text) - .replaceAll(replaceWith); - } - - /** - * @param content - * @return - - */ - private static String escapeBBcode(String content) - { - // escaping single characters - content = content.replaceAll("[", "&#91;"); - content = content.replaceAll("]", "&#93"); - content = content.replaceAll("\t", "&nbsp; &nbsp;"); - - // taking off start and end line breaks - content = content.replaceAll("\\A\r\n|\\A\r|\\A\n|\r\n\\z|\r\\z|\n\\z", - ""); - - // replacing spaces for &nbsp; to keep indentation - content = content.replaceAll(" ", "&nbsp; "); - content = content.replaceAll(" ", " &nbsp;"); - - return content; - } - - /** - * @param content - * @return - - */ - private static String escapeHtml(String content) - { - // escaping single characters - content = content.replaceAll("&", "&amp;"); - content = content.replaceAll("<", "&lt;"); - content = content.replaceAll(">", "&gt;"); - - // replacing line breaks for <br> - // content = content.replaceAll("\r\n", "<br>"); - // content = replaceAll(content, "\n\r".toCharArray(), new String[] { - // "<br>", "<br>" }); - - return content; + try + { + return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher( + text).replaceAll(replaceWith); + } + catch (Exception e) + { + Log.d(IBC.TAG, "Error while processing '" + pattern + "': " + + e.getMessage(), e); + return text; + } } }