001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 20010 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.tools;
021
022 import java.io.File;
023 import java.io.FileWriter;
024 import java.io.PrintWriter;
025 import java.util.Arrays;
026 import java.util.Iterator;
027 import java.util.Locale;
028 import java.util.TreeMap;
029
030 import net.jetrix.AccessLevel;
031 import net.jetrix.commands.Command;
032 import net.jetrix.commands.CommandManager;
033 import net.jetrix.config.ChannelConfig;
034 import net.jetrix.config.FilterConfig;
035 import net.jetrix.config.ServerConfig;
036
037 /**
038 * Generates the documentation for the server commands.
039 *
040 * @author Emmanuel Bourg
041 * @version $Revision: 851 $, $Date: 2010-05-04 14:47:25 +0200 (mar., 04 mai 2010) $
042 */
043 public class DocumentationGenerator
044 {
045 public static void main(String[] args) throws Exception
046 {
047 File configFile = new File("src/etc/conf/server.xml");
048 ServerConfig config = new ServerConfig();
049 config.load(configFile);
050
051 File file = new File("src/site/commands.html");
052 file.getParentFile().mkdirs();
053
054 System.out.println("Exporting commands documentation from " + configFile + " to " + file);
055 System.out.println("");
056
057 // collect the general commands
058 TreeMap<String, Command> commands = new TreeMap<String, Command>();
059 Iterator<Command> it = CommandManager.getInstance().getCommands(AccessLevel.ADMINISTRATOR);
060 while (it.hasNext())
061 {
062 Command command = it.next();
063 commands.put(command.getAliases()[0], command);
064 }
065
066 // collect the filter commands
067 for (ChannelConfig channel : config.getChannels())
068 {
069 Iterator<FilterConfig> filters = channel.getFilters();
070 while (filters.hasNext())
071 {
072 FilterConfig filter = filters.next();
073 if (!filter.isGlobal() && filter.getName().equals("command"))
074 {
075 String cls = filter.getProperties().getProperty("class");
076 Command command = (Command) Class.forName(cls).newInstance();
077 commands.put(command.getAliases()[0], command);
078 }
079 }
080 }
081
082 PrintWriter out = new PrintWriter(new FileWriter(file));
083
084 for (Command command : commands.values())
085 {
086 String alias = command.getAliases()[0];
087
088 out.println("<h2 id=\"command-" + alias + "\">" + alias + "</h2>");
089 out.println();
090 out.println("<p>" + command.getDescription(Locale.ENGLISH) + "</p>");
091 out.println();
092 out.println("<div><b>Usage:</b> <tt>" + htmlizeUsage(command.getUsage(Locale.ENGLISH)) + "</tt></div>");
093 if (command.getAliases().length > 1)
094 {
095 out.println("<div><b>Aliases:</b> <tt>" + Arrays.toString(command.getAliases()) + "</tt></div>");
096 }
097 String role = getRoleName(command.getAccessLevel());
098 if (role != null)
099 {
100 out.println("<div><b>Access Level:</b> " + role + "</div>");
101 }
102 out.println();
103 out.println();
104
105 System.out.println(command.getUsage(Locale.ENGLISH));
106 }
107
108
109 out.flush();
110 out.close();
111 }
112
113 private static String getRoleName(int level)
114 {
115 if (level == 0)
116 {
117 return "Player";
118 }
119 else if (level == 1)
120 {
121 return "Channel Operator";
122 }
123 else if (level == 2)
124 {
125 return "Operator";
126 }
127 else if (level == 100)
128 {
129 return "Administrator";
130 }
131 else
132 {
133 return null;
134 }
135 }
136
137 /**
138 * Return a colorized usage string of the specified command.
139 */
140 private static String htmlizeUsage(String usage)
141 {
142 StringBuffer htmlized = new StringBuffer();
143 htmlized.append("<span style=\"color: red\">");
144
145 for (char c : usage.toCharArray())
146 {
147 if (c == '<')
148 {
149 htmlized.append("<span style=\"color: blue\"><");
150 }
151 else if (c == '>')
152 {
153 htmlized.append("></span>");
154 }
155 else
156 {
157 htmlized.append(c);
158 }
159 }
160
161 htmlized.append("</span>");
162
163 return htmlized.toString();
164 }
165 }