Working on formatted text builder for IRC control chars.

fix-message-formatting
Danny van Heumen 12 years ago
parent 0c28f3cdf8
commit c3c6e23203

@ -0,0 +1,112 @@
package net.java.sip.communicator.impl.protocol.irc;
import java.util.*;
/**
* Builder for constructing a formatted text.
*
* @author Danny van Heumen
*/
public class FormattedTextBuilder
{
/**
* stack with formatting control chars
*/
private final Stack<ControlChar> formatting = new Stack<ControlChar>();
/**
* formatted text container
*/
private final StringBuilder text = new StringBuilder();
/**
* Append a string of text.
*
* @param text
*/
public void append(String text)
{
this.text.append(text);
}
/**
* Apply a control char for formatting.
*
* @param c the control char
*/
public void apply(ControlChar c)
{
if (formatting.contains(c))
{
// cancel active control char
cancel(c);
}
else
{
// start control char formatting
this.text.append(c.getHtmlStart());
}
}
/**
* Cancel the specified control char.
*
* @param c the control char
*/
private void cancel(ControlChar c)
{
final Stack<ControlChar> unwind = new Stack<ControlChar>();
while (!this.formatting.empty())
{
// unwind control chars looking for the cancelled control char
ControlChar current = this.formatting.pop();
this.text.append(current.getHtmlEnd());
if (current == c)
{
break;
}
else
{
unwind.push(current);
}
}
while (!unwind.empty())
{
// rewind remaining control characters
ControlChar current = unwind.pop();
this.text.append(current.getHtmlStart());
this.formatting.push(current);
}
}
/**
* Cancel all remaining control chars.
*/
private void cancelAll()
{
while (!this.formatting.empty())
{
ControlChar c = this.formatting.pop();
this.text.append(c.getHtmlEnd());
}
}
/**
* Finish building the text string. Close outstanding control char
* formatting and returns the result.
*/
public String done()
{
cancelAll();
return this.text.toString();
}
/**
* Return the formatted string. If it is not yet finished (outstanding
* formatting) also finish up remaining control chars.
*/
public String toString()
{
return done();
}
}

@ -100,7 +100,7 @@ public static String parse(String text)
}
catch (IllegalArgumentException e)
{
LOGGER.debug("Invalid color code.", e);
LOGGER.trace("Invalid color code.", e);
}
formatting.push(control);
String htmlTag =

@ -0,0 +1,49 @@
package net.java.sip.communicator.impl.protocol.irc;
import junit.framework.*;
public class FormattedTextBuilderTest
extends TestCase
{
public void testConstructFormattedTextBuilder()
{
new FormattedTextBuilder();
}
public void testFormatNothing()
{
FormattedTextBuilder formatted = new FormattedTextBuilder();
Assert.assertEquals("", formatted.done());
}
public void testPlainText()
{
FormattedTextBuilder formatted = new FormattedTextBuilder();
formatted.append("Hello world!");
Assert.assertEquals("Hello world!", formatted.done());
}
public void testDoneWithoutFormatting()
{
FormattedTextBuilder formatted = new FormattedTextBuilder();
formatted.append("Hello world!");
Assert.assertEquals("Hello world!", formatted.done());
}
public void testDoneRepeatedly()
{
FormattedTextBuilder formatted = new FormattedTextBuilder();
formatted.append("Hello world!");
formatted.done();
formatted.done();
Assert.assertEquals("Hello world!", formatted.done());
}
public void testOnlyFormatting()
{
FormattedTextBuilder formatted = new FormattedTextBuilder();
formatted.append("Hello world!");
Assert.assertEquals("Hello world!", formatted.done());
}
}

@ -153,4 +153,18 @@ public void testParseUnknownBackgroundCOlor()
final String htmlMessage = "<font color=\"White\">,99TEST</font>";
Assert.assertEquals(htmlMessage, Utils.parse(ircMessage));
}
public void testStackIncompatibleFormatToggling()
{
final String ircMessage = "\u0002\u001D\u001FHello\u0002 W\u001Dorld\u001F!";
final String htmlMessage = "<b><i><u>Hello</u></i></b><i><u> W</u></i><u>orld</u>!";
Assert.assertEquals(htmlMessage, Utils.parse(ircMessage));
}
public void testColorSwitch()
{
final String ircMessage = "\u000302,03Hello \u000308,09World\u000F!";
final String htmlMessage = "<font color=\"Navy\" bgcolor=\"Green\">Hello </font><font color=\"Yellow\" bgcolor=\"Lime\">World</font>!";
Assert.assertEquals(htmlMessage, Utils.parse(ircMessage));
}
}

Loading…
Cancel
Save