001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2003 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.protocols;
021
022 import java.util.*;
023 import net.jetrix.*;
024 import net.jetrix.messages.channel.*;
025
026 /**
027 * Protocol to communicate with a console client
028 *
029 * @author Emmanuel Bourg
030 * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
031 */
032 public class ConsoleProtocol extends AbstractProtocol
033 {
034 private static Map<String, String> styles = new HashMap<String, String>();
035
036 static
037 {
038 styles.put("red", "");
039 styles.put("black", "");
040 styles.put("green", "");
041 styles.put("lightGreen", "");
042 styles.put("darkBlue", "");
043 styles.put("blue", "");
044 styles.put("cyan", "");
045 styles.put("aqua", "");
046 styles.put("yellow", "");
047 styles.put("kaki", "");
048 styles.put("brown", "");
049 styles.put("lightGray", "");
050 styles.put("gray", "");
051 styles.put("magenta", "");
052 styles.put("purple", "");
053 styles.put("b", "");
054 styles.put("i", "");
055 styles.put("u", "");
056 styles.put("white", "");
057 }
058
059 /**
060 * Return the name of this protocol
061 */
062 public String getName()
063 {
064 return "console";
065 }
066
067 /**
068 * Parse the specified string and return the corresponding server
069 * message for this protocol.
070 */
071 public Message getMessage(String line)
072 {
073 if (line == null) return null;
074
075 StringTokenizer st = new StringTokenizer(line);
076 Message m = null;
077
078 if (st.hasMoreTokens())
079 {
080 String firstWord = st.nextToken();
081
082 CommandMessage command = new CommandMessage();
083 command.setCommand(firstWord);
084 command.setText(line.substring(line.indexOf(" ") + 1));
085 while (st.hasMoreTokens()) { command.addParameter(st.nextToken()); }
086 m = command;
087 }
088
089 return m;
090 }
091
092 /**
093 * Translate the specified message into a string that will be sent
094 * to a client using this protocol.
095 */
096 public String translate(Message m, Locale locale)
097 {
098 if (m instanceof TextMessage)
099 {
100 return translate((TextMessage) m, locale);
101 }
102 else
103 {
104 return null;
105 }
106 }
107
108 public String translate(TextMessage m, Locale locale)
109 {
110 StringBuilder message = new StringBuilder();
111 message.append(applyStyle(m.getText(locale)));
112 return message.toString();
113 }
114
115 public Map<String, String> getStyles()
116 {
117 return styles;
118 }
119
120 public String applyStyle(String text)
121 {
122 // to be optimized later
123 Map<String, String> styles = getStyles();
124 if (styles == null) return text;
125
126 for (String key : styles.keySet())
127 {
128 String value = styles.get(key);
129 if (value == null) { value = ""; }
130 text = text.replaceAll("<" + key + ">", value);
131 text = text.replaceAll("</" + key + ">", value);
132 }
133 return text;
134 }
135
136 public char getEOL()
137 {
138 return '\n';
139 }
140
141 }