001 /* 002 Copyright (C) 2003 Adam Olsen 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU General Public License as published by 006 the Free Software Foundation; either version 1, or (at your option) 007 any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details. 013 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 017 */ 018 019 package com.valhalla.jbother; 020 021 /** 022 * Replaces conversation text like URLS with links 023 * @author Adam Olsen 024 * @version 1.0 025 */ 026 public class ConversationText 027 { 028 /** 029 * Replaces the text so it can better be displayed in the ConversationArea 030 * @param text the text to replace 031 * @param asHTML not used yet 032 */ 033 public static String replaceText( String text, boolean asHTML ) 034 { 035 text = " " + text + " "; 036 037 if( !asHTML ) 038 { 039 text = text.replaceAll( "\\&", "&" ); 040 text = text.replaceAll( "\\<", "<" ); 041 text = text.replaceAll( "\\>", ">" ); 042 } 043 //else {*/ 044 // JEditorPane doesn't support XHTML so well, so get rid of end tags that look like /> 045 //text = text.replaceAll( "\\/\\>", ">" ); 046 /*}*/ 047 048 // replace _word_ with <u>_word_</u> 049 text = text.replaceAll( "(^| )(_[A-Za-z0-9]+_)(\\.|$| )", "$1<u>$2</u>$3" ); 050 051 // replace /word/ with <i>/word/</i> 052 text = text.replaceAll( "(^| )(/[A-Za-z0-9]+/)(\\.|$| )", "$1<i>$2</i>$3" ); 053 054 // replace *word* with <b>*word*</b> 055 text = text.replaceAll( "(^| )(\\*[A-Za-z0-9]+\\*)(\\.|$| )", "$1<b>$2</b>$3" ); 056 057 // replace emoticon symbols 058 text = Emoticons.getInstance().replaceIcons( text ); 059 060 // replace URLs and email addresses 061 text = text.replaceAll( "(\\s|^)((?!(ftp|http|https)://)[^\\s\"']+?@[^\\s\"']+?)(\\s|$)", 062 "$1<a href=\"mailto:$2\">$2</a>$4" ); 063 text = text.replaceAll( "(^|\\s)((ftp|http|https)://[^\\s\"']+?)(\\s|$)", "$1<a href=\"$2\">$2</a>$4" ); 064 065 text = text.replaceAll( " ", " " ); 066 text = text.replaceAll( "\t", " " ); 067 068 return text; 069 } 070 }