1 19 23 24 package org.chateverywhere; 25 26 import java.util.*; 27 28 29 public class ChatMessage 30 { 31 private String server_msg; 32 private String emitter; 33 private String command; 34 private Vector args; 35 private int current_arg = 0; 36 private boolean valid_msg; 37 private static String delimiter = "<|>"; 38 private static int del_len = delimiter.length(); 39 40 public ChatMessage(String 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 msg) 50 { 51 int pos; 52 int pos_s; 53 int cur_arg = 0; 54 55 msg = msg.trim(); 56 57 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 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 77 if((pos = msg.indexOf(delimiter)) == -1) { 78 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 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 103 public String toString() 104 { 105 int i; 106 String 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 126 127 public boolean is_valid() { return valid_msg; } 128 public String get_emitter() { return emitter; } 129 public String get_command() { return command; } 130 public int get_args_num() { return args.size(); } 131 public String get_arg(int i) { return (String ) args.elementAt(i-1); } 132 } 133 | Popular Tags |