diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java index fa5f7ab..711a3ec 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/BTC_ConfigLoader.java @@ -117,9 +117,9 @@ public class BTC_ConfigLoader final Element rootElement = doc.createElement("configuration"); doc.appendChild(rootElement); - rootElement.appendChild(this.servers.toXML(doc)); - rootElement.appendChild(this.playerCommands.toXML(doc)); - rootElement.appendChild(this.favoriteButtons.toXML(doc)); + rootElement.appendChild(this.servers.listToXML(doc)); + rootElement.appendChild(this.playerCommands.listToXML(doc)); + rootElement.appendChild(this.favoriteButtons.listToXML(doc)); final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); @@ -146,19 +146,19 @@ public class BTC_ConfigLoader final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file); doc.getDocumentElement().normalize(); - if (!this.servers.fromXML(doc)) + if (!this.servers.listFromXML(doc)) { System.out.println("Error loading servers."); hadErrors = true; } - if (!this.playerCommands.fromXML(doc)) + if (!this.playerCommands.listFromXML(doc)) { System.out.println("Error loading playerCommands."); hadErrors = true; } - if (!this.favoriteButtons.fromXML(doc)) + if (!this.favoriteButtons.listFromXML(doc)) { System.out.println("Error favorite buttons."); hadErrors = true; diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java index 1471340..cdb25a1 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntry.java @@ -18,6 +18,103 @@ */ package me.StevenLawson.BukkitTelnetClient; -public interface ConfigEntry +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.logging.Level; +import org.w3c.dom.*; + +public abstract class ConfigEntry { + public abstract String getElementName(); + + public ConfigEntry fromXML(final Document doc) + { + final ConfigEntry newEntry; + try + { + newEntry = getClass().newInstance(); + } + catch (InstantiationException | IllegalAccessException ex) + { + BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex); + return null; + } + + final NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getElementName()); + if (itemNodes.getLength() > 0) + { + final Node itemNode = itemNodes.item(0); + if (itemNode.getNodeType() == Node.ELEMENT_NODE) + { + final Element itemElement = (Element) itemNode; + for (final Method method : getClass().getDeclaredMethods()) + { + final ParameterSetter annotation = method.getDeclaredAnnotation(ParameterSetter.class); + if (annotation == null) + { + continue; + } + + final NodeList tags = itemElement.getElementsByTagName(annotation.name()); + if (tags.getLength() > 0) + { + final String valueStr = itemElement.getElementsByTagName(annotation.name()).item(0).getTextContent(); + final Class _type = method.getParameterTypes()[0]; + + try + { + if (_type == Boolean.class) + { + method.invoke(newEntry, Boolean.valueOf(valueStr)); + } + else if (_type == String.class) + { + method.invoke(newEntry, valueStr); + } + } + catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) + { + BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + } + + return newEntry; + } + + public Element toXML(final Document doc) + { + final Element item = doc.createElement(getElementName()); + + for (final Method method : getClass().getDeclaredMethods()) + { + final ParameterGetter annotation = method.getDeclaredAnnotation(ParameterGetter.class); + if (annotation == null) + { + continue; + } + + final Element parameter = doc.createElement(annotation.name()); + + Object value = null; + try + { + value = method.invoke(this); + } + catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) + { + BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex); + } + if (value != null) + { + parameter.appendChild(doc.createTextNode(value.toString())); + } + + item.appendChild(parameter); + } + + return item; + } } diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java index 63415df..3b6d1bf 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ConfigEntryList.java @@ -18,7 +18,6 @@ */ package me.StevenLawson.BukkitTelnetClient; -import java.lang.annotation.*; import java.lang.reflect.*; import java.util.*; import java.util.logging.Level; @@ -45,7 +44,7 @@ public abstract class ConfigEntryList return entryClass; } - public Element toXML(final Document doc) + public Element listToXML(final Document doc) { final Element parent = doc.createElement(getParentElementName()); @@ -85,7 +84,7 @@ public abstract class ConfigEntryList return parent; } - public boolean fromXML(final Document doc) + public boolean listFromXML(final Document doc) { NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName()); if (itemNodes.getLength() == 0) @@ -155,18 +154,4 @@ public abstract class ConfigEntryList 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/FavoriteButtonEntry.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/FavoriteButtonEntry.java index 77d676d..55a32ac 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/FavoriteButtonEntry.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/FavoriteButtonEntry.java @@ -20,7 +20,7 @@ package me.StevenLawson.BukkitTelnetClient; import java.util.ArrayList; -public class FavoriteButtonEntry implements ConfigEntry +public class FavoriteButtonEntry extends ConfigEntry { private String label; private String command; @@ -29,31 +29,25 @@ public class FavoriteButtonEntry implements ConfigEntry { } - public FavoriteButtonEntry(final String label, final String command) - { - this.label = label; - this.command = command; - } - - @ConfigEntryList.ParameterGetter(name = "label") + @ParameterGetter(name = "label") public String getLabel() { return label; } - @ConfigEntryList.ParameterSetter(name = "label") + @ParameterSetter(name = "label") public void setLabel(String label) { this.label = label; } - @ConfigEntryList.ParameterGetter(name = "command") + @ParameterGetter(name = "command") public String getCommand() { return command; } - @ConfigEntryList.ParameterSetter(name = "command") + @ParameterSetter(name = "command") public void setCommand(String command) { this.command = command; @@ -78,4 +72,10 @@ public class FavoriteButtonEntry implements ConfigEntry return "favoriteButton"; } } + + @Override + public String getElementName() + { + return "favoriteButton"; + } } diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ParameterGetter.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ParameterGetter.java new file mode 100644 index 0000000..9c6a46a --- /dev/null +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ParameterGetter.java @@ -0,0 +1,28 @@ +/* + * 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.*; + +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = ElementType.METHOD) +public @interface ParameterGetter +{ + public String name(); +} diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ParameterSetter.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ParameterSetter.java new file mode 100644 index 0000000..45cdff3 --- /dev/null +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ParameterSetter.java @@ -0,0 +1,28 @@ +/* + * 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.*; + +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = ElementType.METHOD) +public @interface ParameterSetter +{ + public String name(); +} diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/PlayerCommandEntry.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/PlayerCommandEntry.java index 5fcda43..3470a7d 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/PlayerCommandEntry.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/PlayerCommandEntry.java @@ -20,7 +20,7 @@ package me.StevenLawson.BukkitTelnetClient; import java.util.ArrayList; -public class PlayerCommandEntry implements ConfigEntry +public class PlayerCommandEntry extends ConfigEntry { private String name; private String format; @@ -29,31 +29,25 @@ public class PlayerCommandEntry implements ConfigEntry { } - public PlayerCommandEntry(String name, String format) - { - this.name = name; - this.format = format; - } - - @ConfigEntryList.ParameterGetter(name = "format") + @ParameterGetter(name = "format") public String getFormat() { return format; } - @ConfigEntryList.ParameterSetter(name = "format") + @ParameterSetter(name = "format") public void setFormat(String format) { this.format = format; } - @ConfigEntryList.ParameterGetter(name = "name") + @ParameterGetter(name = "name") public String getName() { return name; } - @ConfigEntryList.ParameterSetter(name = "name") + @ParameterSetter(name = "name") public void setName(String name) { this.name = name; @@ -78,4 +72,10 @@ public class PlayerCommandEntry implements ConfigEntry return "playerCommand"; } } + + @Override + public String getElementName() + { + return "playerCommand"; + } } diff --git a/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java b/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java index cb484b7..6981e7a 100644 --- a/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java +++ b/src/main/java/me/StevenLawson/BukkitTelnetClient/ServerEntry.java @@ -21,7 +21,7 @@ package me.StevenLawson.BukkitTelnetClient; import java.util.HashSet; import java.util.Objects; -public class ServerEntry implements ConfigEntry +public class ServerEntry extends ConfigEntry { private String name; private String address; @@ -44,37 +44,37 @@ public class ServerEntry implements ConfigEntry this.lastUsed = lastUsed; } - @ConfigEntryList.ParameterGetter(name = "name") + @ParameterGetter(name = "name") public String getName() { return name; } - @ConfigEntryList.ParameterSetter(name = "name") + @ParameterSetter(name = "name") public void setName(String name) { this.name = name; } - @ConfigEntryList.ParameterGetter(name = "address") + @ParameterGetter(name = "address") public String getAddress() { return address; } - @ConfigEntryList.ParameterSetter(name = "address") + @ParameterSetter(name = "address") public void setAddress(String address) { this.address = address; } - @ConfigEntryList.ParameterGetter(name = "lastUsed") + @ParameterGetter(name = "lastUsed") public boolean isLastUsed() { return lastUsed; } - @ConfigEntryList.ParameterSetter(name = "lastUsed") + @ParameterSetter(name = "lastUsed") public void setLastUsed(Boolean lastUsed) { this.lastUsed = lastUsed; @@ -142,4 +142,10 @@ public class ServerEntry implements ConfigEntry { return String.format("%s (%s)", getName(), getAddress()); } + + @Override + public String getElementName() + { + return "server"; + } }