Added support for single item XML elements.

This commit is contained in:
StevenLawson 2014-08-28 10:45:03 -04:00
parent 2c8785e9dc
commit 88c6b73b0b
8 changed files with 197 additions and 53 deletions

View file

@ -117,9 +117,9 @@ public class BTC_ConfigLoader
final Element rootElement = doc.createElement("configuration"); final Element rootElement = doc.createElement("configuration");
doc.appendChild(rootElement); doc.appendChild(rootElement);
rootElement.appendChild(this.servers.toXML(doc)); rootElement.appendChild(this.servers.listToXML(doc));
rootElement.appendChild(this.playerCommands.toXML(doc)); rootElement.appendChild(this.playerCommands.listToXML(doc));
rootElement.appendChild(this.favoriteButtons.toXML(doc)); rootElement.appendChild(this.favoriteButtons.listToXML(doc));
final Transformer transformer = TransformerFactory.newInstance().newTransformer(); final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 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); final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
if (!this.servers.fromXML(doc)) if (!this.servers.listFromXML(doc))
{ {
System.out.println("Error loading servers."); System.out.println("Error loading servers.");
hadErrors = true; hadErrors = true;
} }
if (!this.playerCommands.fromXML(doc)) if (!this.playerCommands.listFromXML(doc))
{ {
System.out.println("Error loading playerCommands."); System.out.println("Error loading playerCommands.");
hadErrors = true; hadErrors = true;
} }
if (!this.favoriteButtons.fromXML(doc)) if (!this.favoriteButtons.listFromXML(doc))
{ {
System.out.println("Error favorite buttons."); System.out.println("Error favorite buttons.");
hadErrors = true; hadErrors = true;

View file

@ -18,6 +18,103 @@
*/ */
package me.StevenLawson.BukkitTelnetClient; 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;
}
} }

View file

@ -18,7 +18,6 @@
*/ */
package me.StevenLawson.BukkitTelnetClient; package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
@ -45,7 +44,7 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
return entryClass; return entryClass;
} }
public Element toXML(final Document doc) public Element listToXML(final Document doc)
{ {
final Element parent = doc.createElement(getParentElementName()); final Element parent = doc.createElement(getParentElementName());
@ -85,7 +84,7 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
return parent; return parent;
} }
public boolean fromXML(final Document doc) public boolean listFromXML(final Document doc)
{ {
NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName()); NodeList itemNodes = doc.getDocumentElement().getElementsByTagName(getParentElementName());
if (itemNodes.getLength() == 0) if (itemNodes.getLength() == 0)
@ -155,18 +154,4 @@ public abstract class ConfigEntryList<E extends ConfigEntry>
public abstract String getParentElementName(); public abstract String getParentElementName();
public abstract String getItemElementName(); 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();
}
} }

View file

@ -20,7 +20,7 @@ package me.StevenLawson.BukkitTelnetClient;
import java.util.ArrayList; import java.util.ArrayList;
public class FavoriteButtonEntry implements ConfigEntry public class FavoriteButtonEntry extends ConfigEntry
{ {
private String label; private String label;
private String command; private String command;
@ -29,31 +29,25 @@ public class FavoriteButtonEntry implements ConfigEntry
{ {
} }
public FavoriteButtonEntry(final String label, final String command) @ParameterGetter(name = "label")
{
this.label = label;
this.command = command;
}
@ConfigEntryList.ParameterGetter(name = "label")
public String getLabel() public String getLabel()
{ {
return label; return label;
} }
@ConfigEntryList.ParameterSetter(name = "label") @ParameterSetter(name = "label")
public void setLabel(String label) public void setLabel(String label)
{ {
this.label = label; this.label = label;
} }
@ConfigEntryList.ParameterGetter(name = "command") @ParameterGetter(name = "command")
public String getCommand() public String getCommand()
{ {
return command; return command;
} }
@ConfigEntryList.ParameterSetter(name = "command") @ParameterSetter(name = "command")
public void setCommand(String command) public void setCommand(String command)
{ {
this.command = command; this.command = command;
@ -78,4 +72,10 @@ public class FavoriteButtonEntry implements ConfigEntry
return "favoriteButton"; return "favoriteButton";
} }
} }
@Override
public String getElementName()
{
return "favoriteButton";
}
} }

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.*;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface ParameterGetter
{
public String name();
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
package me.StevenLawson.BukkitTelnetClient;
import java.lang.annotation.*;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface ParameterSetter
{
public String name();
}

View file

@ -20,7 +20,7 @@ package me.StevenLawson.BukkitTelnetClient;
import java.util.ArrayList; import java.util.ArrayList;
public class PlayerCommandEntry implements ConfigEntry public class PlayerCommandEntry extends ConfigEntry
{ {
private String name; private String name;
private String format; private String format;
@ -29,31 +29,25 @@ public class PlayerCommandEntry implements ConfigEntry
{ {
} }
public PlayerCommandEntry(String name, String format) @ParameterGetter(name = "format")
{
this.name = name;
this.format = format;
}
@ConfigEntryList.ParameterGetter(name = "format")
public String getFormat() public String getFormat()
{ {
return format; return format;
} }
@ConfigEntryList.ParameterSetter(name = "format") @ParameterSetter(name = "format")
public void setFormat(String format) public void setFormat(String format)
{ {
this.format = format; this.format = format;
} }
@ConfigEntryList.ParameterGetter(name = "name") @ParameterGetter(name = "name")
public String getName() public String getName()
{ {
return name; return name;
} }
@ConfigEntryList.ParameterSetter(name = "name") @ParameterSetter(name = "name")
public void setName(String name) public void setName(String name)
{ {
this.name = name; this.name = name;
@ -78,4 +72,10 @@ public class PlayerCommandEntry implements ConfigEntry
return "playerCommand"; return "playerCommand";
} }
} }
@Override
public String getElementName()
{
return "playerCommand";
}
} }

View file

@ -21,7 +21,7 @@ package me.StevenLawson.BukkitTelnetClient;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
public class ServerEntry implements ConfigEntry public class ServerEntry extends ConfigEntry
{ {
private String name; private String name;
private String address; private String address;
@ -44,37 +44,37 @@ public class ServerEntry implements ConfigEntry
this.lastUsed = lastUsed; this.lastUsed = lastUsed;
} }
@ConfigEntryList.ParameterGetter(name = "name") @ParameterGetter(name = "name")
public String getName() public String getName()
{ {
return name; return name;
} }
@ConfigEntryList.ParameterSetter(name = "name") @ParameterSetter(name = "name")
public void setName(String name) public void setName(String name)
{ {
this.name = name; this.name = name;
} }
@ConfigEntryList.ParameterGetter(name = "address") @ParameterGetter(name = "address")
public String getAddress() public String getAddress()
{ {
return address; return address;
} }
@ConfigEntryList.ParameterSetter(name = "address") @ParameterSetter(name = "address")
public void setAddress(String address) public void setAddress(String address)
{ {
this.address = address; this.address = address;
} }
@ConfigEntryList.ParameterGetter(name = "lastUsed") @ParameterGetter(name = "lastUsed")
public boolean isLastUsed() public boolean isLastUsed()
{ {
return lastUsed; return lastUsed;
} }
@ConfigEntryList.ParameterSetter(name = "lastUsed") @ParameterSetter(name = "lastUsed")
public void setLastUsed(Boolean lastUsed) public void setLastUsed(Boolean lastUsed)
{ {
this.lastUsed = lastUsed; this.lastUsed = lastUsed;
@ -142,4 +142,10 @@ public class ServerEntry implements ConfigEntry
{ {
return String.format("%s (%s)", getName(), getAddress()); return String.format("%s (%s)", getName(), getAddress());
} }
@Override
public String getElementName()
{
return "server";
}
} }