View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2003  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix.protocols;
21  
22  import java.util.*;
23  import net.jetrix.*;
24  import net.jetrix.messages.channel.*;
25  
26  /***
27   * Protocol to communicate with a console client
28   *
29   * @author Emmanuel Bourg
30   * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
31   */
32  public class ConsoleProtocol extends AbstractProtocol
33  {
34      private static Map<String, String> styles = new HashMap<String, String>();
35  
36      static
37      {
38          styles.put("red", "");
39          styles.put("black", "");
40          styles.put("green", "");
41          styles.put("lightGreen", "");
42          styles.put("darkBlue", "");
43          styles.put("blue", "");
44          styles.put("cyan", "");
45          styles.put("aqua", "");
46          styles.put("yellow", "");
47          styles.put("kaki", "");
48          styles.put("brown", "");
49          styles.put("lightGray", "");
50          styles.put("gray", "");
51          styles.put("magenta", "");
52          styles.put("purple", "");
53          styles.put("b", "");
54          styles.put("i", "");
55          styles.put("u", "");
56          styles.put("white", "");
57      }
58  
59      /***
60       * Return the name of this protocol
61       */
62      public String getName()
63      {
64          return "console";
65      }
66  
67      /***
68       * Parse the specified string and return the corresponding server
69       * message for this protocol.
70       */
71      public Message getMessage(String line)
72      {
73          if (line == null) return null;
74  
75          StringTokenizer st = new StringTokenizer(line);
76          Message m = null;
77  
78          if (st.hasMoreTokens())
79          {
80              String firstWord = st.nextToken();
81  
82              CommandMessage command = new CommandMessage();
83              command.setCommand(firstWord);
84              command.setText(line.substring(line.indexOf(" ") + 1));
85              while (st.hasMoreTokens()) { command.addParameter(st.nextToken()); }
86              m = command;
87          }
88  
89          return m;
90      }
91  
92      /***
93       * Translate the specified message into a string that will be sent
94       * to a client using this protocol.
95       */
96      public String translate(Message m, Locale locale)
97      {
98          if (m instanceof TextMessage)
99          {
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 }