KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chat > business > MessageImpl


1 package chat.business;
2
3 import java.util.Vector JavaDoc;
4 import com.lutris.util.HtmlEncoder;
5 import chat.spec.*;
6
7 /**
8  * This class holds all the data about one message (what someone said).
9  * 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 JavaDoc name;
19
20   /**
21    * The HTML encoded version of name.
22    * This string is safe to stick into a page.
23    */

24   public String JavaDoc 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 JavaDoc 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 JavaDoc htmlChunks = new Vector JavaDoc();
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 JavaDoc name, String JavaDoc 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.substring(0,end)));
64         text = text.substring(end+1);
65       } else {
66         htmlChunks.addElement(HtmlEncoder.encode(text));
67         text = "";
68       }
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   public String JavaDoc getHtmlName(){
81     return htmlName;
82   }
83   public Vector JavaDoc getHtmlChunks(){
84     return htmlChunks;
85   }
86
87 }
88
89
Popular Tags