final String chatString,
final String contentType)
{
final String chatFinal = chatString;
SwingWorker worker = new SwingWorker()
{
public Object construct() throws Exception
{
String temp = "", msgStore = chatFinal;
boolean isEnabled
= GuiActivator.getConfigurationService().getBoolean(
ReplacementProperty.REPLACEMENT_ENABLE, true);
Map<String, ReplacementService> listSources
= GuiActivator.getReplacementSources();
Iterator<Entry<String, ReplacementService>> entrySetIter
= listSources.entrySet().iterator();
for (int i = 0; i < listSources.size(); i++)
{
Map.Entry<String, ReplacementService> entry
= entrySetIter.next();
ReplacementService source = entry.getValue();
boolean isSmiley
= source instanceof SmiliesReplacementService;
if (!(GuiActivator.getConfigurationService().getBoolean(
ReplacementProperty.getPropertyName(source
.getSourceName()), true) && (isEnabled || isSmiley)))
continue;
String sourcePattern = source.getPattern();
Pattern p = Pattern.compile(sourcePattern,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(msgStore);
String startPlainTextTag = "";
String endPlainTextTag = "";
if (!HTML_CONTENT_TYPE.equals(contentType))
{
startPlainTextTag = START_PLAINTEXT_TAG;
endPlainTextTag = END_PLAINTEXT_TAG;
}
int count = 0, startPos = 0;
StringBuffer msgBuff = new StringBuffer();
while (m.find())
{
count++;
msgBuff.append(msgStore.substring(startPos, m.start()));
startPos = m.end();
temp = source.getReplacement(m.group());
if(!temp.equals(m.group(0)) || source.getSourceName()
.equals("DIRECTIMAGE"))
{
if(isSmiley)
{
msgBuff.append(endPlainTextTag);
msgBuff.append("<IMG SRC=\"");
}
else
{
msgBuff.append(
"<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\"");
}
msgBuff.append(temp);
msgBuff.append("\" BORDER=\"0\" ALT=\"");
msgBuff.append(m.group(0));
msgBuff.append("\"></IMG>");
if(isSmiley)
msgBuff.append(startPlainTextTag);
}
else
{
msgBuff.append(
msgStore.substring(m.start(), m.end()));
}
}
msgBuff.append(msgStore.substring(startPos));
/*
* replace the msgStore variable with the current replaced
* message before next iteration
*/
if (!msgBuff.toString().equals(msgStore))
{
msgStore = msgBuff.toString();
}
}
if (!msgStore.equals(chatFinal))
{
synchronized (scrollToBottomRunnable)
{
scrollToBottomIsPending = true;
document.setOuterHTML(elem, msgStore.toString()
.substring(msgStore.indexOf("<DIV")));
}
}
return "";
}
};
worker.start();
}