Continuing implementation of XML settings.

This commit is contained in:
StevenLawson 2014-08-19 11:14:36 -04:00
parent 7abb17c9f5
commit 0f00da9dcb
4 changed files with 405 additions and 51 deletions

View file

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.StevenLawson</groupId> <groupId>me.StevenLawson</groupId>
<artifactId>BukkitTelnetClient</artifactId> <artifactId>BukkitTelnetClient</artifactId>
<version>2.0.3</version> <version>2.0.4-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<build> <build>
<plugins> <plugins>
@ -48,6 +48,11 @@
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20140107</version> <version>20140107</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View file

@ -1,28 +1,209 @@
package me.StevenLawson.BukkitTelnetClient; package me.StevenLawson.BukkitTelnetClient;
import java.io.File; import java.io.*;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMException; import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document; import org.w3c.dom.*;
import org.w3c.dom.Element;
public class BTC_ConfigLoader public class BTC_ConfigLoader
{ {
public static class PlayerListCommand private static final String SETTINGS_FILE = "settings.xml";
private final List<PlayerCommandEntry> playerCommands = new ArrayList<>();
private final List<ServerEntry> servers = new ArrayList<>();
public BTC_ConfigLoader()
{
}
public boolean load(boolean verbose)
{
File settings = new File("settings.xml");
if (!settings.exists())
{
if (extractFileFromJar("/" + SETTINGS_FILE, SETTINGS_FILE))
{
if (verbose)
{
System.out.println("Copied default " + SETTINGS_FILE + ".");
}
}
}
if (settings.exists())
{
boolean loadError = loadXML(settings);
final List<ServerEntry> oldServers = importOldConfig();
this.servers.addAll(oldServers);
final HashSet<ServerEntry> uniqueServers = new HashSet<>(this.servers);
this.servers.clear();
this.servers.addAll(uniqueServers);
generateXML(settings);
if (verbose)
{
if (loadError)
{
System.out.println("Settings loaded with errors.");
}
else
{
System.out.println("Settings loaded.");
}
}
return true;
}
else
{
if (verbose)
{
System.out.println("Can't load " + SETTINGS_FILE + ".");
}
}
return false;
}
public boolean save()
{
return generateXML(new File(SETTINGS_FILE));
}
public List<PlayerCommandEntry> getCommands()
{
return this.playerCommands;
}
public List<ServerEntry> getServers()
{
return this.servers;
}
private boolean generateXML(final File file)
{
try
{
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
final Element rootElement = doc.createElement("configuration");
doc.appendChild(rootElement);
rootElement.appendChild(PlayerCommandEntry.listToXML(this.playerCommands, doc));
rootElement.appendChild(ServerEntry.listToXML(this.servers, doc));
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(doc), new StreamResult(file));
return true;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return false;
}
private boolean loadXML(final File file)
{
boolean hadErrors = false;
try
{
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
doc.getDocumentElement().normalize();
if (!PlayerCommandEntry.xmlToList(this.playerCommands, doc))
{
System.out.println("Error loading playerCommands.");
hadErrors = true;
}
if (!ServerEntry.xmlToList(this.servers, doc))
{
System.out.println("Error loading servers.");
hadErrors = true;
}
}
catch (Exception ex)
{
ex.printStackTrace();
hadErrors = true;
}
return hadErrors;
}
private static boolean extractFileFromJar(final String resourceName, final String fileName)
{
final InputStream resource = BTC_ConfigLoader.class.getResourceAsStream(resourceName);
if (resource != null)
{
final File destination = new File(fileName);
try
{
FileUtils.copyInputStreamToFile(resource, destination);
return true;
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return false;
}
private static List<ServerEntry> importOldConfig()
{
final List<ServerEntry> oldServers = new ArrayList<>();
try
{
final File file = new File("btc_servers.cfg");
if (file.exists())
{
try (final BufferedReader in = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = in.readLine()) != null)
{
line = line.trim();
oldServers.add(new ServerEntry("imported", line));
}
}
FileUtils.moveFile(file, new File("btc_servers.cfg.bak"));
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
return oldServers;
}
public static class PlayerCommandEntry
{ {
private final String name; private final String name;
private final String format; private final String format;
public PlayerListCommand(String name, String format) public PlayerCommandEntry(String name, String format)
{ {
this.name = name; this.name = name;
this.format = format; this.format = format;
@ -37,43 +218,14 @@ public class BTC_ConfigLoader
{ {
return name; return name;
} }
}
private static final List<PlayerListCommand> COMMANDS = new ArrayList<>(); public static Element listToXML(final List<PlayerCommandEntry> playerCommands, final Document doc)
static
{ {
COMMANDS.add(new PlayerListCommand("Ban", "glist ban %s")); final Element plcElement = doc.createElement("playerCommands");
COMMANDS.add(new PlayerListCommand("Toggle Mute", "mute %s"));
COMMANDS.add(new PlayerListCommand("Kick", "tempban %s 10s Kicked"));
COMMANDS.add(new PlayerListCommand("Tempban 5m", "tempban %s 5m"));
COMMANDS.add(new PlayerListCommand("Smite", "smite %s"));
COMMANDS.add(new PlayerListCommand("Op", "op %s"));
COMMANDS.add(new PlayerListCommand("Deop", "deop %s"));
COMMANDS.add(new PlayerListCommand("GTFO", "gtfo %s"));
COMMANDS.add(new PlayerListCommand("Toggle Freeze", "fr %s"));
COMMANDS.add(new PlayerListCommand("Cage", "cage %s"));
COMMANDS.add(new PlayerListCommand("Uncage", "cage %s off"));
COMMANDS.add(new PlayerListCommand("Doom", "doom %s"));
COMMANDS.add(new PlayerListCommand("Creative", "creative %s"));
COMMANDS.add(new PlayerListCommand("Survival", "survival %s"));
}
public BTC_ConfigLoader() for (final PlayerCommandEntry command : playerCommands)
{ {
try final Element commandElement = doc.createElement("playerCommand");
{
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
final Element rootElement = doc.createElement("configuration");
doc.appendChild(rootElement);
final Element plcElement = doc.createElement("playerListCommands");
rootElement.appendChild(plcElement);
for (final PlayerListCommand command : COMMANDS)
{
final Element commandElement = doc.createElement("command");
plcElement.appendChild(commandElement); plcElement.appendChild(commandElement);
final Element commandName = doc.createElement("name"); final Element commandName = doc.createElement("name");
@ -85,15 +237,147 @@ public class BTC_ConfigLoader
commandElement.appendChild(commandFormat); commandElement.appendChild(commandFormat);
} }
final Transformer transformer = TransformerFactory.newInstance().newTransformer(); return plcElement;
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(doc), new StreamResult(new File("test.xml")));
} }
catch (ParserConfigurationException | DOMException | IllegalArgumentException | TransformerException ex)
public static boolean xmlToList(final List<PlayerCommandEntry> playerCommands, final Document doc)
{ {
ex.printStackTrace(); NodeList playerCommandNodes = doc.getDocumentElement().getElementsByTagName("playerCommands");
if (playerCommandNodes.getLength() < 1)
{
return false;
}
playerCommandNodes = playerCommandNodes.item(0).getChildNodes();
playerCommands.clear();
for (int i = 0; i < playerCommandNodes.getLength(); i++)
{
final Node node = playerCommandNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
final Element element = (Element) node;
final PlayerCommandEntry command = new PlayerCommandEntry(
element.getElementsByTagName("name").item(0).getTextContent(),
element.getElementsByTagName("format").item(0).getTextContent()
);
playerCommands.add(command);
}
}
return true;
}
}
public static class ServerEntry
{
private final String name;
private final String address;
public ServerEntry(final String name, final String address)
{
this.name = name;
this.address = address;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
@Override
public int hashCode()
{
int hash = 7;
hash = 67 * hash + Objects.hashCode(this.name);
hash = 67 * hash + Objects.hashCode(this.address);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ServerEntry other = (ServerEntry) obj;
if (!Objects.equals(this.name, other.name))
{
return false;
}
if (!Objects.equals(this.address, other.address))
{
return false;
}
return true;
}
public static Element listToXML(final List<ServerEntry> servers, final Document doc)
{
final Element serversElement = doc.createElement("servers");
for (final ServerEntry command : servers)
{
final Element commandElement = doc.createElement("server");
serversElement.appendChild(commandElement);
final Element serverName = doc.createElement("name");
serverName.appendChild(doc.createTextNode(command.getName()));
commandElement.appendChild(serverName);
final Element serverAddress = doc.createElement("address");
serverAddress.appendChild(doc.createTextNode(command.getAddress()));
commandElement.appendChild(serverAddress);
}
return serversElement;
}
public static boolean xmlToList(final List<ServerEntry> servers, final Document doc)
{
NodeList serverNodes = doc.getDocumentElement().getElementsByTagName("servers");
if (serverNodes.getLength() < 1)
{
return false;
}
serverNodes = serverNodes.item(0).getChildNodes();
servers.clear();
for (int i = 0; i < serverNodes.getLength(); i++)
{
final Node node = serverNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
final Element element = (Element) node;
final ServerEntry server = new ServerEntry(
element.getElementsByTagName("name").item(0).getTextContent(),
element.getElementsByTagName("address").item(0).getTextContent()
);
servers.add(server);
}
}
return true;
} }
} }
} }

View file

@ -9,9 +9,12 @@ public class BukkitTelnetClient
public static final String VERSION_STRING = "v2.0.3"; public static final String VERSION_STRING = "v2.0.3";
public static final Logger LOGGER = Logger.getLogger(BukkitTelnetClient.class.getName()); public static final Logger LOGGER = Logger.getLogger(BukkitTelnetClient.class.getName());
public static BTC_MainPanel mainPanel = null; public static BTC_MainPanel mainPanel = null;
public static BTC_ConfigLoader config = new BTC_ConfigLoader();
public static void main(String args[]) public static void main(String args[])
{ {
config.load(true);
findAndSetLookAndFeel("Windows"); findAndSetLookAndFeel("Windows");
java.awt.EventQueue.invokeLater(new Runnable() java.awt.EventQueue.invokeLater(new Runnable()

View file

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configuration>
<playerCommands>
<playerCommand>
<name>Ban</name>
<format>glist ban %s</format>
</playerCommand>
<playerCommand>
<name>Toggle Mute</name>
<format>mute %s</format>
</playerCommand>
<playerCommand>
<name>Kick</name>
<format>tempban %s 10s Kicked</format>
</playerCommand>
<playerCommand>
<name>Tempban 5m</name>
<format>tempban %s 5m</format>
</playerCommand>
<playerCommand>
<name>Smite</name>
<format>smite %s</format>
</playerCommand>
<playerCommand>
<name>Op</name>
<format>op %s</format>
</playerCommand>
<playerCommand>
<name>Deop</name>
<format>deop %s</format>
</playerCommand>
<playerCommand>
<name>GTFO</name>
<format>gtfo %s</format>
</playerCommand>
<playerCommand>
<name>Toggle Freeze</name>
<format>fr %s</format>
</playerCommand>
<playerCommand>
<name>Cage</name>
<format>cage %s</format>
</playerCommand>
<playerCommand>
<name>Uncage</name>
<format>cage %s off</format>
</playerCommand>
<playerCommand>
<name>Doom</name>
<format>doom %s</format>
</playerCommand>
<playerCommand>
<name>Creative</name>
<format>creative %s</format>
</playerCommand>
<playerCommand>
<name>Survival</name>
<format>survival %s</format>
</playerCommand>
</playerCommands>
<servers/>
</configuration>