diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java index 1c6895d..0463e72 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java @@ -35,7 +35,7 @@ public class BTC_ConfigLoader private static final String SETTINGS_FILE = "settings.xml"; private final List playerCommands = new ArrayList<>(); - private final Set servers = new HashSet<>(); + private final ServerEntry.ServerEntryList servers = new ServerEntry.ServerEntryList(); public BTC_ConfigLoader() { @@ -60,9 +60,6 @@ public class BTC_ConfigLoader { boolean loadError = loadXML(settings); - final List oldServers = importOldConfig(); - this.servers.addAll(oldServers); - generateXML(settings); if (verbose) @@ -100,9 +97,9 @@ public class BTC_ConfigLoader return this.playerCommands; } - public Set getServers() + public Collection getServers() { - return servers; + return this.servers.getList(); } private boolean generateXML(final File file) @@ -115,7 +112,7 @@ public class BTC_ConfigLoader doc.appendChild(rootElement); rootElement.appendChild(PlayerCommandEntry.listToXML(this.playerCommands, doc)); - rootElement.appendChild(ServerEntry.listToXML(this.servers, doc)); + rootElement.appendChild(this.servers.toXML(doc)); final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); @@ -148,7 +145,7 @@ public class BTC_ConfigLoader hadErrors = true; } - if (!ServerEntry.xmlToList(this.servers, doc)) + if (!this.servers.fromXML(doc)) { System.out.println("Error loading servers."); hadErrors = true; @@ -183,34 +180,4 @@ public class BTC_ConfigLoader return false; } - - private static List importOldConfig() - { - final List 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("legacy", line, false)); - } - } - - FileUtils.moveFile(file, new File("btc_servers.cfg.bak")); - } - } - catch (IOException ex) - { - BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex); - } - - return oldServers; - } } diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_MainPanel.form b/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_MainPanel.form index 3255895..ef21fb3 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_MainPanel.form +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_MainPanel.form @@ -157,7 +157,7 @@ - + @@ -422,4 +422,4 @@ - + \ No newline at end of file diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java new file mode 100644 index 0000000..1471340 --- /dev/null +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2012-2014 Steven Lawson + * + * This file is part of BukkitTelnetClient. + * + * BukkitTelnetClient is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package me.StevenLawson.BukkitTelnetClient; + +public interface ConfigEntry +{ +} diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java new file mode 100644 index 0000000..60f7075 --- /dev/null +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2012-2014 Steven Lawson + * + * This file is part of BukkitTelnetClient. + * + * BukkitTelnetClient is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package me.StevenLawson.BukkitTelnetClient; + +import java.lang.annotation.*; +import java.lang.reflect.*; +import java.util.*; +import java.util.logging.Level; +import org.w3c.dom.*; + +public abstract class ConfigEntryList +{ + private final Collection list; + private final Class entryClass; + + public ConfigEntryList(Collection list, Class entryClass) + { + this.list = list; + this.entryClass = entryClass; + } + + public Collection getList() + { + return list; + } + + public Class getEntryClass() + { + return entryClass; + } + + public Element toXML(final Document doc) + { + final Element parent = doc.createElement(getParentElementName()); + + for (final E entry : getList()) + { + final Element item = doc.createElement(getItemElementName()); + parent.appendChild(item); + + for (final Method method : getEntryClass().getDeclaredMethods()) + { + final ParameterGetter annotation = method.getDeclaredAnnotation(ParameterGetter.class); + if (annotation != null) + { + try + { + final Element parameter = doc.createElement(annotation.name()); + parameter.appendChild(doc.createTextNode(method.invoke(entry).toString())); + item.appendChild(parameter); + } + catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | DOMException ex) + { + BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + return parent; + } + + public boolean fromXML(final Document doc) + { + NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName()); + if (itemNodes.getLength() < 1) + { + return false; + } + itemNodes.item(0).getChildNodes(); + + getList().clear(); + + for (int i = 0; i < itemNodes.getLength(); i++) + { + final Node node = itemNodes.item(0); + if (node.getNodeType() == Node.ELEMENT_NODE) + { + final Element element = (Element) node; + + try + { + final E newEntry = getEntryClass().newInstance(); + + for (final Method method : getEntryClass().getDeclaredMethods()) + { + final ParameterSetter annotation = method.getDeclaredAnnotation(ParameterSetter.class); + if (annotation != null) + { + final String valueStr = element.getElementsByTagName(annotation.name()).item(0).getTextContent(); + final Class _type = method.getParameterTypes()[0]; + if (_type == Boolean.class) + { + method.invoke(newEntry, Boolean.valueOf(valueStr)); + } + else if (_type == String.class) + { + method.invoke(newEntry, valueStr); + } + } + } + + getList().add(newEntry); + } + catch (Exception ex) + { + BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex); + } + } + } + + return true; + } + + public abstract String getParentElementName(); + + public abstract String getItemElementName(); + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public @interface ParameterGetter + { + public String name(); + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public @interface ParameterSetter + { + public String name(); + } +} diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java index 91bfef8..cb484b7 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java @@ -18,15 +18,19 @@ */ package me.StevenLawson.BukkitTelnetClient; -import java.util.*; -import org.w3c.dom.*; +import java.util.HashSet; +import java.util.Objects; -public class ServerEntry +public class ServerEntry implements ConfigEntry { private String name; private String address; private boolean lastUsed = false; + public ServerEntry() + { + } + public ServerEntry(final String name, final String address) { this.name = name; @@ -40,32 +44,38 @@ public class ServerEntry this.lastUsed = lastUsed; } + @ConfigEntryList.ParameterGetter(name = "name") public String getName() { return name; } + @ConfigEntryList.ParameterSetter(name = "name") public void setName(String name) { this.name = name; } + @ConfigEntryList.ParameterGetter(name = "address") public String getAddress() { return address; } + @ConfigEntryList.ParameterSetter(name = "address") public void setAddress(String address) { this.address = address; } + @ConfigEntryList.ParameterGetter(name = "lastUsed") public boolean isLastUsed() { return lastUsed; } - public void setLastUsed(boolean lastUsed) + @ConfigEntryList.ParameterSetter(name = "lastUsed") + public void setLastUsed(Boolean lastUsed) { this.lastUsed = lastUsed; } @@ -74,8 +84,8 @@ public class ServerEntry public int hashCode() { int hash = 7; - hash = 67 * hash + Objects.hashCode(this.name); - hash = 67 * hash + Objects.hashCode(this.address); + hash = 59 * hash + Objects.hashCode(this.name); + hash = 59 * hash + Objects.hashCode(this.address); return hash; } @@ -107,65 +117,29 @@ public class ServerEntry return true; } - public static Element listToXML(final Set servers, final Document doc) + public static class ServerEntryList extends ConfigEntryList { - final Element serversElement = doc.createElement("servers"); - - for (final ServerEntry command : servers) + public ServerEntryList() { - 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); - - final Element serverLastUsed = doc.createElement("lastUsed"); - serverLastUsed.appendChild(doc.createTextNode(Boolean.toString(command.isLastUsed()))); - commandElement.appendChild(serverLastUsed); + super(new HashSet(), ServerEntry.class); } - return serversElement; - } - - public static boolean xmlToList(final Set servers, final Document doc) - { - NodeList serverNodes = doc.getDocumentElement().getElementsByTagName("servers"); - if (serverNodes.getLength() < 1) + @Override + public String getParentElementName() { - 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(), - Boolean.valueOf(element.getElementsByTagName("lastUsed").item(0).getTextContent()) - ); - - servers.add(server); - } + return "servers"; } - return true; + @Override + public String getItemElementName() + { + return "server"; + } } @Override public String toString() { - return String.format("%s (%s)", this.name, this.address); + return String.format("%s (%s)", getName(), getAddress()); } }