01: package chat.business;
02:
03: import java.util.Vector;
04: import com.lutris.util.HtmlEncoder;
05: import chat.spec.*;
06:
07: /**
08: * This class holds all the data about one message (what someone said).
09: * The message is broken down into lines, and each line is HTML encoded.
10: * This makes displaying the message faster. The message is created once,
11: * but displayed over and over to all the listeners.
12: */
13: public class MessageImpl implements Message {
14:
15: /**
16: * Who sent in this bit of text?
17: */
18: public String name;
19:
20: /**
21: * The HTML encoded version of name.
22: * This string is safe to stick into a page.
23: */
24: public String htmlName;
25:
26: /**
27: * The text of the message, in one un-encoded string.
28: * The user may have entered invalid HTML, so use the htmlChunks
29: * when emitting as HTML.
30: */
31: public String text;
32:
33: /**
34: * The text of the message, broken down by lines, then HTML encoded.
35: * These strings are safe to stick into a page.
36: */
37: public Vector htmlChunks = new Vector();
38:
39: /**
40: * The time when this message was added to the discussion.
41: * This is used to delete messages that are too old.
42: */
43: private long when;
44:
45: /**
46: * Create a new message. This does not add it to the discussion.
47: * The HTML fields and time are initialized, though.
48: * @see chat.business.Discussion
49: */
50: public MessageImpl(String name, String text) {
51: when = System.currentTimeMillis();
52: this .name = name;
53: this .htmlName = HtmlEncoder.encode(name);
54: this .text = text;
55: /*
56: * Break the text into lines, and HTML encode each line.
57: */
58: if (text.length() == 0)
59: htmlChunks.addElement("");
60: while (text.length() != 0) {
61: int end = text.indexOf("\n");
62: if (end != -1) {
63: htmlChunks.addElement(HtmlEncoder.encode(text
64: .substring(0, end)));
65: text = text.substring(end + 1);
66: } else {
67: htmlChunks.addElement(HtmlEncoder.encode(text));
68: text = "";
69: }
70: }
71: }
72:
73: /**
74: * Get the time when the message was created. The result is in
75: * milliseconds, see System.currentTimeMillis().
76: */
77: public long getWhen() {
78: return when;
79: }
80:
81: public String getHtmlName() {
82: return htmlName;
83: }
84:
85: public Vector getHtmlChunks() {
86: return htmlChunks;
87: }
88:
89: }
|