BBCodeConverter.java (2404B)
1 package de.mtbnews.android.adapter; 2 3 import java.util.regex.Pattern; 4 5 import android.util.Log; 6 import de.mtbnews.android.util.IBC; 7 8 /** 9 * @author Jan Dankert 10 */ 11 public class BBCodeConverter 12 { 13 14 /** 15 * @param string 16 * @return HTML-formated message 17 */ 18 public String process(String string) 19 { 20 string = string.replaceAll("(\r\n|\n\r|\n|\r)", "<br />"); 21 22 string = processTag(string, "\\[color=['\"]?(.*?[^'\"])['\"]?\\](.*?)\\[/color\\]", 23 "<span style='color:$1'>$2</span>"); 24 25 string = processTag(string, "\\[quote[^\\[]*\\](.*?)\\[/quote\\]", "<blockquote>$1</blockquote>"); 26 27 string = processTag(string, "\\[list=['\"]?(.*?[^'\"])['\"]?\\](.*?)\\[/list\\]", "<ul>$2</ul>"); 28 29 // str = str.replaceAll("(\r\n|\n\r|\n|\r)", "<br>"); 30 31 // [color] 32 // [size] 33 string = processTag(string, "\\[size=['\"]?([0-9]|[1-2][0-9])['\"]?\\](.*?)\\[/size\\]", 34 "<span style='font-size:$1px'>$2</span>"); 35 36 string = processTag(string, "\\[mention=['\"]?([0-9]+)['\"]?\\](.*?)\\[/mention\\]", "<a href=\"" 37 + IBC.IBC_FORUM_URL + "member.php?u=$1\">→$2</a>"); 38 39 // [b][u][i] 40 string = processTag(string, "\\[b\\](.*?)\\[/b\\]", "<b>$1</b>"); 41 string = processTag(string, "\\[u\\](.*?)\\[/u\\]", "<u>$1</u>"); 42 string = processTag(string, "\\[i\\](.*?)\\[/i\\]", "<i>$1</i>"); 43 44 // Smileys (very poor implementation) 45 string = processTag(string, ":daumen:", "+1"); 46 string = processTag(string, ":love:", "♥"); 47 string = processTag(string, "[*]", "•"); 48 49 // [img] 50 string = processTag(string, "\\[img\\](.*?)\\[/img\\]", "<a href=\"$1\">Bild anzeigen</a>"); 51 52 // [url] 53 string = processTag(string, "\\[url\\](.*?)\\[/url\\]", "<a href=\"$1\">$1</a>"); 54 string = processTag(string, "\\[url=['\"]?(.*?[^'\"])['\"]?\\](.*?)\\[/url\\]", "<a href=\"$1\">$2</a>"); 55 string = processTag(string, "\\[yt.*\\](.*?)\\[/yt\\]", 56 "<a href=\"http://www.youtube.com/watch?v=$2\">Video bei Youtube anzeigen</a>"); 57 58 // [email] 59 string = processTag(string, "\\[email\\](.*?)\\[/email\\]", "<a href='mailto:$1'>$1</a>"); 60 61 return string; 62 } 63 64 private static String processTag(String text, String pattern, String replaceWith) 65 { 66 try 67 { 68 return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(text).replaceAll(replaceWith); 69 } 70 catch (Exception e) 71 { 72 Log.d(IBC.TAG, "Error while processing '" + pattern + "': " + e.getMessage(), e); 73 return text; 74 } 75 } 76 77 }