Use JAXB insteaof of Simple-XML for parsing XML files.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jan 2013 11:03:17 +0000 (12:03 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jan 2013 11:03:17 +0000 (12:03 +0100)
pom.xml
src/main/java/net/pterodactylus/reactor/loader/Chain.java
src/main/java/net/pterodactylus/reactor/loader/ChainWatcher.java

diff --git a/pom.xml b/pom.xml
index 7422828..a6ca554 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -51,9 +51,9 @@
                        <version>3.1</version>
                </dependency>
                <dependency>
-                       <groupId>org.simpleframework</groupId>
-                       <artifactId>simple-xml</artifactId>
-                       <version>2.6.9</version>
+                       <groupId>com.sun.xml.bind</groupId>
+                       <artifactId>jaxb</artifactId>
+                       <version>2.1.9</version>
                </dependency>
        </dependencies>
 </project>
index 91892a5..b018859 100644 (file)
 
 package net.pterodactylus.reactor.loader;
 
+import java.util.ArrayList;
 import java.util.List;
 
-import org.simpleframework.xml.Element;
-import org.simpleframework.xml.ElementList;
-import org.simpleframework.xml.Root;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementWrapper;
+import javax.xml.bind.annotation.XmlRootElement;
 
 /**
  * Model for chain definitions.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-@Root
+@XmlRootElement
 public class Chain {
 
        /**
@@ -39,11 +40,11 @@ public class Chain {
        public static class Parameter {
 
                /** The name of the parameter. */
-               @Element
+               @XmlElement(required = true)
                private String name;
 
                /** The value of the parameter. */
-               @Element
+               @XmlElement(required = true)
                private String value;
 
                /**
@@ -103,12 +104,13 @@ public class Chain {
        public static class Part {
 
                /** The class name of the part. */
-               @Element(name = "class")
+               @XmlElement(required = true, name = "class")
                private String name;
 
                /** The parameters of the part. */
-               @ElementList(required = false, empty = false)
-               private List<Parameter> parameters;
+               @XmlElement(name = "parameter")
+               @XmlElementWrapper(name = "parameters")
+               private List<Parameter> parameters = new ArrayList<Parameter>();
 
                /**
                 * Returns the name of the part’s class.
@@ -167,27 +169,28 @@ public class Chain {
        }
 
        /** Whether this chain is enabled. */
-       @Element
+       @XmlElement(required = true)
        private boolean enabled;
 
        /** The query of the chain. */
-       @Element
+       @XmlElement(required = true)
        private Part query;
 
        /** The filters of the chain. */
-       @ElementList(required = false, empty = false)
-       private List<Part> filters;
+       @XmlElement(name = "filter")
+       @XmlElementWrapper(name = "filters")
+       private List<Part> filters = new ArrayList<Part>();
 
        /** The trigger of the chain. */
-       @Element
+       @XmlElement(required = true)
        private Part trigger;
 
        /** The action of the chain. */
-       @Element
+       @XmlElement(required = true)
        private Part action;
 
        /** Interval between updates (in seconds). */
-       @Element
+       @XmlElement(required = true)
        private int updateInterval;
 
        /**
index 10abcc5..a6a5004 100644 (file)
@@ -24,14 +24,16 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.TimeUnit;
 
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+
 import net.pterodactylus.reactor.Reaction;
 import net.pterodactylus.reactor.engine.Engine;
 import net.pterodactylus.reactor.loader.Chain.Parameter;
 import net.pterodactylus.reactor.loader.Chain.Part;
 
 import org.apache.log4j.Logger;
-import org.simpleframework.xml.Serializer;
-import org.simpleframework.xml.core.Persister;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.Maps;
@@ -191,9 +193,15 @@ public class ChainWatcher extends AbstractExecutionThreadService {
         * @return The parsed chain
         */
        private static Chain parseXmlFile(File xmlFile) {
-               Serializer serializer = new Persister();
-               logger.debug(String.format("Reading %s...", xmlFile.getPath()));
-               return serializer.read(Chain.class, xmlFile);
+               try {
+                       JAXBContext context = JAXBContext.newInstance(Chain.class);
+                       Unmarshaller unmarshaller = context.createUnmarshaller();
+                       logger.debug(String.format("Reading %s...", xmlFile.getPath()));
+                       return (Chain) unmarshaller.unmarshal(xmlFile);
+               } catch (JAXBException e) {
+                       e.printStackTrace();
+                       return null;
+               }
        }
 
 }