KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > chateverywhere > ChatMessage


1 /* Chat
2  * Copyright (C) 1998-2004 Alexis de Bernis <alexis@bernis.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */

19 /*
20  * ChatMessage
21  * parses a chat server message for easy use
22  */

23
24 package org.chateverywhere;
25
26 import java.util.*;
27
28
29 public class ChatMessage
30 {
31     private String JavaDoc server_msg;
32     private String JavaDoc emitter;
33     private String JavaDoc command;
34     private Vector args;
35     private int current_arg = 0;
36     private boolean valid_msg;
37     private static String JavaDoc delimiter = "<|>";
38     private static int del_len = delimiter.length();
39
40     public ChatMessage(String JavaDoc server_msg)
41     {
42         this.server_msg = server_msg;
43         args = new Vector();
44         
45         valid_msg = parse_message(server_msg);
46     }
47
48
49     private boolean parse_message(String JavaDoc msg)
50     {
51         int pos;
52         int pos_s;
53         int cur_arg = 0;
54         
55         msg = msg.trim();
56
57         /*** get the emitter of the message ***/
58         if(msg.startsWith("SERVER") && msg.endsWith("SERVER"))
59             emitter = "SERVER";
60         else if(msg.startsWith("CLIENT") && msg.endsWith("CLIENT"))
61             emitter = "CLIENT";
62         else
63             return false;
64
65
66         /*** take off the emitter at the beginning and at the end ***/
67         if((pos = msg.indexOf(delimiter)) == -1)
68             return false;
69         
70         msg = msg.substring(pos + del_len).trim();
71         if((pos = msg.lastIndexOf(delimiter)) == -1)
72             return false;
73         msg = msg.substring(0, pos).trim();
74
75
76         /*** get the command of the message ***/
77         if((pos = msg.indexOf(delimiter)) == -1) {
78             /*** there may be no args ***/
79             if(!msg.equals("")) {
80                 command = msg;
81                 return true;
82             }
83             return false;
84         }
85
86         command = msg.substring(0, pos).trim();
87         msg = msg.substring(pos + del_len).trim();
88
89         while((pos = msg.indexOf(delimiter)) != -1) {
90             /*** get the argument ***/
91             current_arg++;
92             args.add(msg.substring(0, pos).trim());
93             msg = msg.substring(pos + del_len).trim();
94         }
95         current_arg++;
96         args.add(msg);
97
98         return true;
99     }
100
101
102     /* Returns a text-representation of the msg */
103     public String JavaDoc toString()
104     {
105         int i;
106         String JavaDoc res = "msg = %" + server_msg + "%\n"
107                + "valid = " + valid_msg + "\n"
108                + "emitter = %" + emitter + "%\n"
109                + "command = %" + command + "%\n"
110                + "num_args = " + current_arg + "\n";
111        
112         for(i = 0; i < current_arg; i++)
113             res = res + "arg(" + i + ") = %" + args.elementAt(i) + "%\n";
114
115         return res;
116     }
117
118
119
120
121     /*****************************************************
122      * *
123      * Accessors *
124      * *
125      *****************************************************/

126     /************** message characteristics **************/
127     public boolean is_valid() { return valid_msg; }
128     public String JavaDoc get_emitter() { return emitter; }
129     public String JavaDoc get_command() { return command; }
130     public int get_args_num() { return args.size(); }
131     public String JavaDoc get_arg(int i) { return (String JavaDoc) args.elementAt(i-1); }
132 }
133
Popular Tags